From 397f4a089030e7df9ceca5973523ae5a4453c415 Mon Sep 17 00:00:00 2001 From: David Pedersen Date: Sat, 2 Dec 2023 12:17:13 +0800 Subject: [PATCH 1/2] Show git commit if version information is missing --- main.go | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/main.go b/main.go index 013c92d1..c4b1baea 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 = "modified" + } + } + } + + fmt.Printf("Go version: %s\n", buildInfo.GoVersion) + if vcsRevision != "" { + fmt.Printf("Commit: %s %s %s\n", vcsRevision, vcsTime, vcsModified) + } +} + 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) From 548e42f01476d4d01e5bdfe72da948212f38d935 Mon Sep 17 00:00:00 2001 From: David Pedersen Date: Mon, 11 Dec 2023 19:55:08 +0800 Subject: [PATCH 2/2] Update version command format --- main.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/main.go b/main.go index c4b1baea..5861977a 100644 --- a/main.go +++ b/main.go @@ -223,15 +223,15 @@ func printVersion() { vcsTime = setting.Value case "vcs.modified": if setting.Value == "true" { - vcsModified = "modified" + vcsModified = " (dirty)" } } } - fmt.Printf("Go version: %s\n", buildInfo.GoVersion) if vcsRevision != "" { - fmt.Printf("Commit: %s %s %s\n", vcsRevision, vcsTime, vcsModified) + fmt.Printf("Built at commit: %s%s %s\n", vcsRevision, vcsModified, vcsTime) } + fmt.Printf("Go version: %s\n", buildInfo.GoVersion) } func main() {