diff --git a/main.go b/main.go index caaf38c..d782074 100644 --- a/main.go +++ b/main.go @@ -19,6 +19,7 @@ package main import ( "fmt" "os" + "runtime/debug" "strings" "time" @@ -38,7 +39,7 @@ func main() { Short: "Generate CRD reference documentation", SilenceUsage: true, SilenceErrors: true, - Version: version(), + Version: printVersion(), RunE: doRun, } @@ -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)" }