package main import ( "fmt" "io" "runtime" "runtime/debug" ) // version is overridden at build time: // // go build -ldflags "-X main.version=$(git describe --tags --always --dirty)" // // It stays "dev" for a plain `go build`. See the Makefile. var version = "dev" // commit and date are optional build-time stamps, also set with -X. var ( commit = "" date = "" ) // buildVersion resolves the version string, preferring the ldflags value and // falling back to the VCS revision the Go toolchain embeds for `go install`. func buildVersion() string { v := version c := commit if c == "" { if info, ok := debug.ReadBuildInfo(); ok { for _, s := range info.Settings { if s.Key == "vcs.revision" { c = s.Value } if s.Key == "vcs.time" && date == "" { date = s.Value } } } } if len(c) > 12 { c = c[:12] } if c != "" { v += " (" + c + ")" } return v } func printVersion(w io.Writer) { fmt.Fprintf(w, "rip %s\n", buildVersion()) if date != "" { fmt.Fprintf(w, "built: %s\n", date) } fmt.Fprintf(w, "go: %s\n", runtime.Version()) fmt.Fprintf(w, "target: %s/%s\n", runtime.GOOS, runtime.GOARCH) }