1 Commits

Author SHA1 Message Date
HeliC829 29d57da520 main: print build version on startup 2026-06-27 17:29:45 +08:00
3 changed files with 74 additions and 3 deletions
+6 -3
View File
@@ -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)
}
+45
View File
@@ -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())
}
+23
View File
@@ -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)
}
}