diff --git a/README.md b/README.md index 924936c..281b6bd 100644 --- a/README.md +++ b/README.md @@ -168,8 +168,6 @@ lstk update # Show resolved config file path lstk config path -# Show version info -lstk version ``` ## Reporting bugs diff --git a/cmd/help_test.go b/cmd/help_test.go index d38fc59..e370a07 100644 --- a/cmd/help_test.go +++ b/cmd/help_test.go @@ -34,6 +34,7 @@ func TestRootHelpOutputTemplate(t *testing.T) { assertContains(t, out, "Options:") assertNotContains(t, out, "Available Commands:") assertNotContains(t, out, `Use "lstk [command] --help" for more information about a command.`) + assertNotContains(t, out, "\n version ") } func TestSubcommandHelpUsesSubcommandUsageLine(t *testing.T) { diff --git a/cmd/non_interactive_test.go b/cmd/non_interactive_test.go index 2ac1e52..a8bf5e7 100644 --- a/cmd/non_interactive_test.go +++ b/cmd/non_interactive_test.go @@ -47,7 +47,7 @@ func TestNonInteractiveFlagAppearsInStopHelp(t *testing.T) { func TestNonInteractiveFlagBindsToCfg(t *testing.T) { cfg := &env.Env{} root := NewRootCmd(cfg, telemetry.New("", true)) - root.SetArgs([]string{"--non-interactive", "version"}) + root.SetArgs([]string{"--non-interactive", "--version"}) _ = root.Execute() if !cfg.NonInteractive { @@ -58,7 +58,7 @@ func TestNonInteractiveFlagBindsToCfg(t *testing.T) { func TestNonInteractiveFlagDefaultIsOff(t *testing.T) { cfg := &env.Env{} root := NewRootCmd(cfg, telemetry.New("", true)) - root.SetArgs([]string{"version"}) + root.SetArgs([]string{"--version"}) _ = root.Execute() if cfg.NonInteractive { diff --git a/cmd/root.go b/cmd/root.go index 0efc875..32263a8 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -33,7 +33,6 @@ func NewRootCmd(cfg *env.Env, tel *telemetry.Client) *cobra.Command { } root.Version = version.Version() - root.SetVersionTemplate(versionLine() + "\n") root.SilenceErrors = true root.SilenceUsage = true @@ -44,6 +43,7 @@ func NewRootCmd(cfg *env.Env, tel *telemetry.Client) *cobra.Command { root.InitDefaultVersionFlag() root.Flags().Lookup("version").Usage = "Show version" + root.SetVersionTemplate(versionTemplate()) root.AddCommand( newStartCmd(cfg, tel), @@ -52,7 +52,6 @@ func NewRootCmd(cfg *env.Env, tel *telemetry.Client) *cobra.Command { newLogoutCmd(cfg), newLogsCmd(), newConfigCmd(), - newVersionCmd(), newUpdateCmd(cfg), ) diff --git a/cmd/version.go b/cmd/version.go index 344fb3f..ffe6b3b 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -4,21 +4,12 @@ import ( "fmt" "github.com/localstack/lstk/internal/version" - "github.com/spf13/cobra" ) -func newVersionCmd() *cobra.Command { - return &cobra.Command{ - Use: "version", - Short: "Show version", - Long: "Print version information for the lstk binary.", - RunE: func(cmd *cobra.Command, args []string) error { - _, err := fmt.Fprintln(cmd.OutOrStdout(), versionLine()) - return err - }, - } +func versionTemplate() string { + return versionLine() + "\n" } func versionLine() string { - return fmt.Sprintf("lstk %s (%s, %s)", version.Version(), version.Commit(), version.BuildDate()) + return fmt.Sprintf("lstk %s", version.Version()) } diff --git a/cmd/version_test.go b/cmd/version_test.go index b02bfe9..dd028d3 100644 --- a/cmd/version_test.go +++ b/cmd/version_test.go @@ -1,17 +1,33 @@ package cmd -import ( - "strings" - "testing" -) +import "testing" func TestVersionLine(t *testing.T) { got := versionLine() - if !strings.HasPrefix(got, "lstk ") { - t.Fatalf("versionLine() = %q, should start with 'lstk '", got) + if got == "" { + t.Fatal("versionLine() should not be empty") } - if !strings.Contains(got, "(") || !strings.Contains(got, ")") { - t.Fatalf("versionLine() = %q, should contain parentheses with commit and date", got) + if got != "lstk dev" { + t.Fatalf("versionLine() = %q, want %q", got, "lstk dev") + } +} + +func TestVersionFlagsPrintSameOutput(t *testing.T) { + longOut, err := executeWithArgs(t, "--version") + if err != nil { + t.Fatalf("expected no error from --version, got %v", err) + } + + shortOut, err := executeWithArgs(t, "-v") + if err != nil { + t.Fatalf("expected no error from -v, got %v", err) + } + + if longOut != versionTemplate() { + t.Fatalf("--version output = %q, want %q", longOut, versionTemplate()) + } + if shortOut != longOut { + t.Fatalf("-v output = %q, want %q", shortOut, longOut) } } diff --git a/internal/version/version.go b/internal/version/version.go index 2f705b7..7d7e58b 100644 --- a/internal/version/version.go +++ b/internal/version/version.go @@ -6,8 +6,7 @@ var ( version = "dev" commit = "none" buildDate = "unknown" + _, _ = commit, buildDate ) -func Version() string { return version } -func Commit() string { return commit } -func BuildDate() string { return buildDate } +func Version() string { return version } diff --git a/test/integration/update_test.go b/test/integration/update_test.go index 25a87be..3d89933 100644 --- a/test/integration/update_test.go +++ b/test/integration/update_test.go @@ -117,7 +117,7 @@ func TestUpdateBinaryInPlace(t *testing.T) { require.NoError(t, err, "go build failed: %s", string(out)) // Verify it reports the fake version - verCmd := exec.CommandContext(ctx, tmpBinary, "version") + verCmd := exec.CommandContext(ctx, tmpBinary, "--version") verOut, err := verCmd.CombinedOutput() require.NoError(t, err) assert.Contains(t, string(verOut), "0.0.1") @@ -132,7 +132,7 @@ func TestUpdateBinaryInPlace(t *testing.T) { assert.Contains(t, updateStr, "Updated to", "should complete the update") // Verify the binary was actually replaced - verCmd2 := exec.CommandContext(ctx, tmpBinary, "version") + verCmd2 := exec.CommandContext(ctx, tmpBinary, "--version") verOut2, err := verCmd2.CombinedOutput() require.NoError(t, err) assert.NotContains(t, string(verOut2), "0.0.1", "binary should no longer be the old version") @@ -183,7 +183,7 @@ func TestUpdateHomebrew(t *testing.T) { require.NoError(t, err, "go build failed: %s", string(out)) // Verify it reports the fake version - verCmd := exec.CommandContext(ctx, caskBinary, "version") + verCmd := exec.CommandContext(ctx, caskBinary, "--version") verOut, err := verCmd.CombinedOutput() require.NoError(t, err) assert.Contains(t, string(verOut), "0.0.1")