diff --git a/main.go b/main.go index 1965a75..0f50b21 100644 --- a/main.go +++ b/main.go @@ -5,7 +5,10 @@ import ( "fmt" "io" "os" + "regexp" + "runtime" "runtime/debug" + "strings" "golang.org/x/term" @@ -39,6 +42,16 @@ var ( var Version = "dev" // This will be set by the build systems to the release version +var semverRe = regexp.MustCompile(`^\d+\.\d+\.\d+`) + +func buildVersionOutput(version string) string { + normalized := version + if semverRe.MatchString(normalized) && !strings.HasPrefix(normalized, "v") { + normalized = "v" + normalized + } + return fmt.Sprintf("%s (%s, %s/%s)", normalized, runtime.Version(), runtime.GOOS, runtime.GOARCH) +} + func main() { // Set the build version from the build info if not set by the build system if Version == "dev" || Version == "" { @@ -54,7 +67,7 @@ func main() { flag.Parse() if *showVersion { - fmt.Println("Version:", Version) + fmt.Printf("yamltecture version %s\n", buildVersionOutput(Version)) return } diff --git a/main_test.go b/main_test.go index 9089ec4..3316bf4 100644 --- a/main_test.go +++ b/main_test.go @@ -3,8 +3,10 @@ package main import ( "bytes" + "fmt" "os" "os/exec" + "runtime" "testing" ) @@ -230,3 +232,30 @@ func TestCLICommands(t *testing.T) { }) } } + +func TestBuildVersionOutputAddsVPrefixAndMetadata(t *testing.T) { + got := buildVersionOutput("1.2.3") + want := fmt.Sprintf("v1.2.3 (%s, %s/%s)", runtime.Version(), runtime.GOOS, runtime.GOARCH) + + if got != want { + t.Fatalf("unexpected version output: got %q, want %q", got, want) + } +} + +func TestBuildVersionOutputPreservesExistingVPrefix(t *testing.T) { + got := buildVersionOutput("v1.2.3") + want := fmt.Sprintf("v1.2.3 (%s, %s/%s)", runtime.Version(), runtime.GOOS, runtime.GOARCH) + + if got != want { + t.Fatalf("unexpected version output: got %q, want %q", got, want) + } +} + +func TestBuildVersionOutputNoVPrefixForDev(t *testing.T) { + got := buildVersionOutput("dev") + want := fmt.Sprintf("dev (%s, %s/%s)", runtime.Version(), runtime.GOOS, runtime.GOARCH) + + if got != want { + t.Fatalf("unexpected version output: got %q, want %q", got, want) + } +}