Use buildInfo.Main.Version when Version is not set

This commit is contained in:
Filippo Valsorda 2020-03-21 22:04:37 -04:00
parent 243b819761
commit 0603a13b79

15
main.go

@ -20,6 +20,7 @@ import (
"path/filepath" "path/filepath"
"regexp" "regexp"
"runtime" "runtime"
"runtime/debug"
"strings" "strings"
"sync" "sync"
@ -78,8 +79,10 @@ const advancedUsage = `Advanced options:
` `
// Version is set more precisely at build time. // Version can be set at link time to override debug.BuildInfo.Main.Version,
var Version = "v1.4.1-dev" // which is "(devel)" when building from within the module. See
// golang.org/issue/29814 and golang.org/issue/29228.
var Version string
func main() { func main() {
log.SetFlags(0) log.SetFlags(0)
@ -108,7 +111,13 @@ func main() {
return return
} }
if *versionFlag { if *versionFlag {
fmt.Println(Version) if Version != "" {
fmt.Println(Version)
}
if buildInfo, ok := debug.ReadBuildInfo(); ok {
fmt.Println(buildInfo.Main.Version)
}
fmt.Println("(unknown)")
return return
} }
if *carootFlag { if *carootFlag {