Skip to content

Commit b70ef1d

Browse files
committed
Move version info to internal/version package
1 parent c309066 commit b70ef1d

6 files changed

Lines changed: 31 additions & 26 deletions

File tree

.goreleaser.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ builds:
1818
flags:
1919
- -trimpath
2020
ldflags:
21-
- -s -w -X github.com/localstack/lstk/cmd.version={{ .Version }} -X github.com/localstack/lstk/cmd.commit={{ .Commit }} -X github.com/localstack/lstk/cmd.buildDate={{ .Date }}
21+
- -s -w -X github.com/localstack/lstk/internal/version.version={{ .Version }} -X github.com/localstack/lstk/internal/version.commit={{ .Commit }} -X github.com/localstack/lstk/internal/version.buildDate={{ .Date }}
2222

2323
archives:
2424
- id: lstk

cmd/login.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55

66
"github.com/localstack/lstk/internal/api"
77
"github.com/localstack/lstk/internal/ui"
8+
"github.com/localstack/lstk/internal/version"
89
"github.com/spf13/cobra"
910
)
1011

@@ -18,7 +19,7 @@ var loginCmd = &cobra.Command{
1819
return fmt.Errorf("login requires an interactive terminal")
1920
}
2021
platformClient := api.NewPlatformClient()
21-
return ui.RunLogin(cmd.Context(), version, platformClient)
22+
return ui.RunLogin(cmd.Context(), version.Version(), platformClient)
2223
},
2324
}
2425

cmd/root.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,10 @@ import (
1212
"github.com/localstack/lstk/internal/output"
1313
"github.com/localstack/lstk/internal/runtime"
1414
"github.com/localstack/lstk/internal/ui"
15+
"github.com/localstack/lstk/internal/version"
1516
"github.com/spf13/cobra"
1617
)
1718

18-
var version = "dev"
19-
var commit = "none"
20-
var buildDate = "unknown"
21-
2219
var rootCmd = &cobra.Command{
2320
Use: "lstk",
2421
Short: "LocalStack CLI",
@@ -39,7 +36,7 @@ var rootCmd = &cobra.Command{
3936
}
4037

4138
func init() {
42-
rootCmd.Version = version
39+
rootCmd.Version = version.Version()
4340
rootCmd.SetVersionTemplate(versionLine() + "\n")
4441
rootCmd.AddCommand(startCmd)
4542
}
@@ -51,7 +48,7 @@ func Execute(ctx context.Context) error {
5148
func runStart(ctx context.Context, rt runtime.Runtime) error {
5249
platformClient := api.NewPlatformClient()
5350
if ui.IsInteractive() {
54-
return ui.Run(ctx, rt, version, platformClient)
51+
return ui.Run(ctx, rt, version.Version(), platformClient)
5552
}
5653
return container.Start(ctx, rt, output.NewPlainSink(os.Stdout), platformClient, false)
5754
}

cmd/version.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package cmd
33
import (
44
"fmt"
55

6+
"github.com/localstack/lstk/internal/version"
67
"github.com/spf13/cobra"
78
)
89

@@ -21,5 +22,5 @@ func init() {
2122
}
2223

2324
func versionLine() string {
24-
return fmt.Sprintf("lstk %s (%s, %s)", version, commit, buildDate)
25+
return fmt.Sprintf("lstk %s (%s, %s)", version.Version(), version.Commit(), version.BuildDate())
2526
}

cmd/version_test.go

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,17 @@
11
package cmd
22

3-
import "testing"
3+
import (
4+
"strings"
5+
"testing"
6+
)
47

58
func TestVersionLine(t *testing.T) {
6-
originalVersion := version
7-
originalCommit := commit
8-
originalBuildDate := buildDate
9-
t.Cleanup(func() {
10-
version = originalVersion
11-
commit = originalCommit
12-
buildDate = originalBuildDate
13-
})
14-
15-
version = "2026.2.0"
16-
commit = "abc1234"
17-
buildDate = "2026-02-17T15:04:05Z"
18-
199
got := versionLine()
20-
want := "lstk 2026.2.0 (abc1234, 2026-02-17T15:04:05Z)"
21-
if got != want {
22-
t.Fatalf("versionLine() = %q, want %q", got, want)
10+
11+
if !strings.HasPrefix(got, "lstk ") {
12+
t.Fatalf("versionLine() = %q, should start with 'lstk '", got)
13+
}
14+
if !strings.Contains(got, "(") || !strings.Contains(got, ")") {
15+
t.Fatalf("versionLine() = %q, should contain parentheses with commit and date", got)
2316
}
2417
}

internal/version/version.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package version
2+
3+
// Set via ldflags at build time. Must be variables, not constants,
4+
// because the linker can only modify variables at link time.
5+
var (
6+
version = "dev"
7+
commit = "none"
8+
buildDate = "unknown"
9+
)
10+
11+
func Version() string { return version }
12+
func Commit() string { return commit }
13+
func BuildDate() string { return buildDate }

0 commit comments

Comments
 (0)