Add version flag to print version and exit (#394)

## Changes

With this PR, all of the command below print version and exit:
```
$ databricks -v
Databricks CLI v0.100.1-dev+4d3fa76
$ databricks --version
Databricks CLI v0.100.1-dev+4d3fa76
$ databricks version  
Databricks CLI v0.100.1-dev+4d3fa76
```

## Tests

Added integration test for each flag or command.
This commit is contained in:
Pieter Noordhuis 2023-05-22 18:55:42 +00:00 committed by GitHub
parent 055e528173
commit d86a1f0847
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 36 additions and 9 deletions

View File

@ -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")
}

View File

@ -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
},
}

View File

@ -13,6 +13,7 @@ import (
"time"
"github.com/databricks/cli/cmd/root"
_ "github.com/databricks/cli/cmd/version"
"github.com/stretchr/testify/require"
)

29
internal/version_test.go Normal file
View File

@ -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())
}