Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import (
"fmt"
"io"
"os"
"regexp"
"runtime"
"runtime/debug"
"strings"

"golang.org/x/term"

Expand Down Expand Up @@ -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 == "" {
Expand All @@ -54,7 +67,7 @@ func main() {
flag.Parse()

if *showVersion {
fmt.Println("Version:", Version)
fmt.Printf("yamltecture version %s\n", buildVersionOutput(Version))
return
}

Expand Down
29 changes: 29 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package main

import (
"bytes"
"fmt"
"os"
"os/exec"
"runtime"
"testing"
)

Expand Down Expand Up @@ -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)
}
}
Loading