diff --git a/cmd/version/version.go b/cmd/version/version.go index 63fd4512..1f772424 100644 --- a/cmd/version/version.go +++ b/cmd/version/version.go @@ -1,34 +1,25 @@ package version import ( - "encoding/json" - "fmt" - "github.com/databricks/cli/cmd/root" "github.com/databricks/cli/internal/build" + "github.com/databricks/cli/libs/cmdio" "github.com/spf13/cobra" ) -var detail = false - var versionCmd = &cobra.Command{ Use: "version", Args: cobra.NoArgs, - RunE: func(cmd *cobra.Command, args []string) error { - info := build.GetInfo() - if detail { - enc := json.NewEncoder(cmd.OutOrStdout()) - enc.SetIndent("", " ") - return enc.Encode(info) - } + Annotations: map[string]string{ + "template": "Databricks CLI v{{.Version}}\n", + }, - _, err := fmt.Fprintf(cmd.OutOrStdout(), "Databricks CLI v%s\n", info.Version) - return err + RunE: func(cmd *cobra.Command, args []string) error { + return cmdio.Render(cmd.Context(), build.GetInfo()) }, } func init() { - versionCmd.Flags().BoolVar(&detail, "detail", false, "output detailed version information as JSON") root.RootCmd.AddCommand(versionCmd) } diff --git a/internal/version_test.go b/internal/version_test.go index 54104002..7dba63cd 100644 --- a/internal/version_test.go +++ b/internal/version_test.go @@ -1,6 +1,7 @@ package internal import ( + "encoding/json" "fmt" "testing" @@ -27,3 +28,15 @@ func TestVersionCommand(t *testing.T) { assert.Equal(t, expectedVersion, stdout.String()) assert.Equal(t, "", stderr.String()) } + +func TestVersionCommandWithJSONOutput(t *testing.T) { + stdout, stderr := RequireSuccessfulRun(t, "version", "--output", "json") + assert.NotEmpty(t, stdout.String()) + assert.Equal(t, "", stderr.String()) + + // Deserialize stdout and confirm we see the right fields. + var output map[string]any + err := json.Unmarshal(stdout.Bytes(), &output) + assert.NoError(t, err) + assert.Equal(t, build.GetInfo().Version, output["Version"]) +}