diff options
Diffstat (limited to 'cmd/krino/main.go')
| -rw-r--r-- | cmd/krino/main.go | 22 |
1 files changed, 21 insertions, 1 deletions
diff --git a/cmd/krino/main.go b/cmd/krino/main.go index e204b5f..f5fdfea 100644 --- a/cmd/krino/main.go +++ b/cmd/krino/main.go @@ -9,12 +9,32 @@ import ( "fmt" "io" "os" + "runtime/debug" "strings" ) // version is stamped by the Makefile with -ldflags "-X main.version=...". var version = "dev" +// displayVersion returns the version to print for --version. The Makefile +// stamps a git tag, which carries a leading "v" (required for +// go install ...@v0.0.1, per design.md ยง14); that "v" belongs on the tag, +// not in the output, so it is stripped here. A "go install" build carries no +// ldflags, leaving version at its built-in "dev"; in that case fall back to +// the build info Go embeds automatically. +func displayVersion() string { + v := version + if v == "dev" { + if info, ok := debug.ReadBuildInfo(); ok && info.Main.Version != "" && info.Main.Version != "(devel)" { + v = info.Main.Version + } + } + if len(v) > 1 && v[0] == 'v' && v[1] >= '0' && v[1] <= '9' { + v = v[1:] + } + return v +} + const usage = `usage: krino [-y | -n] [-v] [--json] [-c FILE] [NAME...] krino init krino new NAME PATH @@ -63,7 +83,7 @@ func run(args []string, stdout, stderr io.Writer) int { return code } if *showVersion { - fmt.Fprintf(stdout, "krino %s\n", version) + fmt.Fprintf(stdout, "krino %s\n", displayVersion()) return 0 } rest := fs.Args() |
