Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show git commit if version information is missing #1517

Merged
merged 2 commits into from
Dec 26, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
Limero marked this conversation as resolved.
Show resolved Hide resolved
}
}
}

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