From 29d57da520afe2565db626f38d47afdaae7becaf Mon Sep 17 00:00:00 2001 From: Julian Zhu Date: Sat, 27 Jun 2026 15:48:30 +0800 Subject: [PATCH] main: print build version on startup --- main.go | 9 ++++++--- version.go | 45 +++++++++++++++++++++++++++++++++++++++++++++ version_test.go | 23 +++++++++++++++++++++++ 3 files changed, 74 insertions(+), 3 deletions(-) create mode 100644 version.go create mode 100644 version_test.go diff --git a/main.go b/main.go index 18b334e..476b15a 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,7 @@ package main import ( + "fmt" "net/http" "os" "time" @@ -22,10 +23,12 @@ Use "go2spec [command] --help" for more information about a command. If there are no commands provided, the tool will default to executing the 'pack' command. ` - println(helpText) + fmt.Print(helpText) } func main() { + printVersion(os.Stdout) + pkgsiteHTTPClient = &http.Client{ Timeout: 30 * time.Second, Transport: httpcache.NewMemoryCacheTransport(), @@ -43,12 +46,12 @@ func main() { printHelp() case "pack": // Placeholder for the pack command implementation - println("Executing 'pack' command...") + fmt.Println("Executing 'pack' command...") // Actual packing logic would go here mainPack(args[1:], nil) default: // Default to 'pack' command if no command is provided - println("No command provided. Defaulting to 'pack' command...") + fmt.Println("No command provided. Defaulting to 'pack' command...") // Actual packing logic would go here mainPack(args, printHelp) } diff --git a/version.go b/version.go new file mode 100644 index 0000000..beca932 --- /dev/null +++ b/version.go @@ -0,0 +1,45 @@ +package main + +import ( + "fmt" + "io" + "runtime/debug" + "time" +) + +const unknownVersion = "unknown" + +func runtimeVersion() string { + info, ok := debug.ReadBuildInfo() + if !ok { + return unknownVersion + } + return versionFromBuildSettings(info.Settings) +} + +func versionFromBuildSettings(settings []debug.BuildSetting) string { + var revision string + var commitTime string + for _, setting := range settings { + switch setting.Key { + case "vcs.revision": + revision = setting.Value + case "vcs.time": + commitTime = setting.Value + } + } + if revision == "" || commitTime == "" { + return unknownVersion + } + if len(revision) > 7 { + revision = revision[:7] + } + if t, err := time.Parse(time.RFC3339, commitTime); err == nil { + commitTime = t.Format("2006-01-02") + } + return fmt.Sprintf("%s %s", commitTime, revision) +} + +func printVersion(w io.Writer) { + fmt.Fprintf(w, "Version: %s\n", runtimeVersion()) +} diff --git a/version_test.go b/version_test.go new file mode 100644 index 0000000..f16b029 --- /dev/null +++ b/version_test.go @@ -0,0 +1,23 @@ +package main + +import ( + "runtime/debug" + "testing" +) + +func TestVersionFromBuildSettings(t *testing.T) { + settings := []debug.BuildSetting{ + {Key: "vcs.revision", Value: "1234567890abcdef"}, + {Key: "vcs.time", Value: "2026-06-27T15:33:29Z"}, + } + + if got, want := versionFromBuildSettings(settings), "2026-06-27 1234567"; got != want { + t.Fatalf("versionFromBuildSettings() = %q, want %q", got, want) + } +} + +func TestVersionFromBuildSettingsUnknown(t *testing.T) { + if got := versionFromBuildSettings(nil); got != unknownVersion { + t.Fatalf("versionFromBuildSettings(nil) = %q, want %q", got, unknownVersion) + } +} -- 2.54.0