Files
HackBrowserData/cmd/hack-browser-data/version.go
T
Roger 3e9150249c refactor: rename core types and spell out every name (#677)
Def, Request, DirID, and Source said too little, and short variables and doc comments that skip their name hid intent.
varnamelen and staticcheck ST1020-22 now keep both from coming back.
2026-11-02 11:06:05 +08:00

54 lines
1016 B
Go

package main
import (
"fmt"
"runtime/debug"
"github.com/spf13/cobra"
)
var (
version = "dev"
commit = "none"
buildDate = "unknown"
)
func versionCmd() *cobra.Command {
return &cobra.Command{
Use: "version",
Short: "Print version information",
Run: func(command *cobra.Command, _ []string) {
resolveVersionFromBuildInfo()
fmt.Fprintf(command.OutOrStdout(), "hack-browser-data %s\n commit: %s\n built: %s\n",
version, commit, buildDate)
},
}
}
func resolveVersionFromBuildInfo() {
if version != "dev" {
return
}
info, ok := debug.ReadBuildInfo()
if !ok {
return
}
if info.Main.Version != "" && info.Main.Version != "(devel)" {
version = info.Main.Version
}
for _, setting := range info.Settings {
switch setting.Key {
case "vcs.revision":
if len(setting.Value) > 8 {
commit = setting.Value[:8]
} else if setting.Value != "" {
commit = setting.Value
}
case "vcs.time":
if setting.Value != "" {
buildDate = setting.Value
}
}
}
}