Skip to content

Commit

Permalink
Add version flag to print version and exit (#394)
Browse files Browse the repository at this point in the history
## 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.
  • Loading branch information
pietern committed May 22, 2023
1 parent 055e528 commit d86a1f0
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 9 deletions.
11 changes: 4 additions & 7 deletions cmd/root/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
}
4 changes: 2 additions & 2 deletions cmd/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
}

Expand Down
1 change: 1 addition & 0 deletions internal/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"time"

"github.com/databricks/cli/cmd/root"
_ "github.com/databricks/cli/cmd/version"
"github.com/stretchr/testify/require"
)

Expand Down
29 changes: 29 additions & 0 deletions internal/version_test.go
Original file line number Diff line number Diff line change
@@ -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())
}

0 comments on commit d86a1f0

Please sign in to comment.