diff --git a/cmd/root/root.go b/cmd/root/root.go index c519d216..3b940a49 100644 --- a/cmd/root/root.go +++ b/cmd/root/root.go @@ -15,8 +15,9 @@ import ( // RootCmd represents the base command when called without any subcommands var RootCmd = &cobra.Command{ - Use: "databricks", - Short: "Databricks CLI", + Use: "databricks", + Short: "Databricks CLI", + Version: build.GetInfo().Version, // Cobra prints the usage string to stderr if a command returns an error. // This usage string should only be displayed if an invalid combination of flags @@ -105,9 +106,5 @@ func Execute() { func init() { RootCmd.SetFlagErrorFunc(flagErrorFunc) - - // The VS Code extension passes `-v` in debug mode and must be changed - // to use the new flags in `./logger.go` prior to removing this flag. - RootCmd.PersistentFlags().BoolP("verbose", "v", false, "") - RootCmd.PersistentFlags().MarkHidden("verbose") + RootCmd.SetVersionTemplate("Databricks CLI v{{.Version}}\n") } diff --git a/cmd/version/version.go b/cmd/version/version.go index c021a980..63fd4512 100644 --- a/cmd/version/version.go +++ b/cmd/version/version.go @@ -23,8 +23,8 @@ var versionCmd = &cobra.Command{ return enc.Encode(info) } - fmt.Fprintln(cmd.OutOrStdout(), info.Version) - return nil + _, err := fmt.Fprintf(cmd.OutOrStdout(), "Databricks CLI v%s\n", info.Version) + return err }, } diff --git a/internal/helpers.go b/internal/helpers.go index b972b91e..42132a54 100644 --- a/internal/helpers.go +++ b/internal/helpers.go @@ -13,6 +13,7 @@ import ( "time" "github.com/databricks/cli/cmd/root" + _ "github.com/databricks/cli/cmd/version" "github.com/stretchr/testify/require" ) diff --git a/internal/version_test.go b/internal/version_test.go new file mode 100644 index 00000000..54104002 --- /dev/null +++ b/internal/version_test.go @@ -0,0 +1,29 @@ +package internal + +import ( + "fmt" + "testing" + + "github.com/databricks/cli/internal/build" + "github.com/stretchr/testify/assert" +) + +var expectedVersion = fmt.Sprintf("Databricks CLI v%s\n", build.GetInfo().Version) + +func TestVersionFlagShort(t *testing.T) { + stdout, stderr := RequireSuccessfulRun(t, "-v") + assert.Equal(t, expectedVersion, stdout.String()) + assert.Equal(t, "", stderr.String()) +} + +func TestVersionFlagLong(t *testing.T) { + stdout, stderr := RequireSuccessfulRun(t, "--version") + assert.Equal(t, expectedVersion, stdout.String()) + assert.Equal(t, "", stderr.String()) +} + +func TestVersionCommand(t *testing.T) { + stdout, stderr := RequireSuccessfulRun(t, "version") + assert.Equal(t, expectedVersion, stdout.String()) + assert.Equal(t, "", stderr.String()) +}