Show git commit if version information is missing (#1517)

* Show git commit if version information is missing

* Update version command format
This commit is contained in:
David Pedersen authored and GitHub committed 2023-12-26 15:41:15 +01:00
1 parent 075caac3a9
commit a51462dcb9
1 file changed
+33 -1
+33 -1
View File
@@ -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)