Skip to content

Commit

Permalink
Show git commit if version information is missing
Browse files Browse the repository at this point in the history
  • Loading branch information
Limero committed Dec 2, 2023
1 parent 6abce04 commit 397f4a0
Showing 1 changed file with 33 additions and 1 deletion.
34 changes: 33 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"path/filepath"
"reflect"
"runtime"
"runtime/debug"
"runtime/pprof"
"strconv"
"strings"
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 397f4a0

Please sign in to comment.