From a51462dcb9121c3d5c41b6e0645c35d946a99ec3 Mon Sep 17 00:00:00 2001 From: David Pedersen Date: Tue, 26 Dec 2023 22:41:15 +0800 Subject: [PATCH] Show git commit if version information is missing (#1517) * Show git commit if version information is missing * Update version command format --- main.go | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/main.go b/main.go index 013c92d1..5861977a 100644 --- a/main.go +++ b/main.go @@ -9,6 +9,7 @@ import ( "path/filepath" "reflect" "runtime" + "runtime/debug" "runtime/pprof" "strconv" "strings" @@ -202,6 +203,37 @@ func checkServer() { } } +func printVersion() { + if gVersion != "" { + fmt.Println(gVersion) + return + } + + buildInfo, ok := debug.ReadBuildInfo() + if !ok { + return + } + + var vcsRevision, vcsTime, vcsModified string + for _, setting := range buildInfo.Settings { + switch setting.Key { + case "vcs.revision": + vcsRevision = setting.Value + case "vcs.time": + vcsTime = setting.Value + case "vcs.modified": + if setting.Value == "true" { + vcsModified = " (dirty)" + } + } + } + + if vcsRevision != "" { + fmt.Printf("Built at commit: %s%s %s\n", vcsRevision, vcsModified, vcsTime) + } + fmt.Printf("Go version: %s\n", buildInfo.GoVersion) +} + func main() { flag.Usage = func() { f := flag.CommandLine.Output() @@ -304,7 +336,7 @@ func main() { case *showDoc: fmt.Print(genDocString) case *showVersion: - fmt.Println(gVersion) + printVersion() case *remoteCmd != "": if err := remote(*remoteCmd); err != nil { log.Fatalf("remote command: %s", err)