Skip to content

Commit

Permalink
Support version on go install
Browse files Browse the repository at this point in the history
  • Loading branch information
thbkrkr committed Jul 25, 2024
1 parent b60435f commit cd3d28f
Showing 1 changed file with 45 additions and 2 deletions.
47 changes: 45 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package main
import (
"fmt"
"os"
"runtime/debug"
"strings"
"time"

Expand All @@ -38,7 +39,7 @@ func main() {
Short: "Generate CRD reference documentation",
SilenceUsage: true,
SilenceErrors: true,
Version: version(),
Version: printVersion(),
RunE: doRun,
}

Expand Down Expand Up @@ -150,6 +151,48 @@ var (
buildCommit string
)

func printVersion() string {
return fmt.Sprintf("Version: %s\nGitCommit: %s\nBuildDate: %s\n", version(), commit(), date())
}

func version() string {
return fmt.Sprintf("Version: %s\nGitCommit: %s\nBuildDate: %s\n", buildVersion, buildCommit, buildDate)
if buildVersion == "" {
return buildVersion
}
bi, ok := debug.ReadBuildInfo()
if !ok || bi == nil || bi.Main.Version == "" {
// binary has not been built with module support or doesn't contain a version
return "(unknown)"
}
return bi.Main.Version
}

func date() string {
if buildDate == "" {
return buildDate
}
bi, ok := debug.ReadBuildInfo()
if ok {
for _, setting := range bi.Settings {
if setting.Key == "vcs.time" {
return setting.Value
}
}
}
return "(unknown)"
}

func commit() string {
if buildCommit == "" {
return buildCommit
}
bi, ok := debug.ReadBuildInfo()
if ok {
for _, setting := range bi.Settings {
if setting.Key == "vcs.revision" {
return setting.Value
}
}
}
return "(unknown)"
}

0 comments on commit cd3d28f

Please sign in to comment.