Use cmdio in version command for `--output` flag (#419)

## Changes

Use cmdio in the version command such that it accepts the `--output` flag.

This removes the existing `--detail` flag which previously made the
command print JSON output.

## Tests

New integration test passes.
This commit is contained in:
Pieter Noordhuis 2023-06-01 12:03:22 +02:00 committed by GitHub
parent 24ebfdf31e
commit 7a4ca786d8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 15 deletions

View File

@ -1,34 +1,25 @@
package version package version
import ( import (
"encoding/json"
"fmt"
"github.com/databricks/cli/cmd/root" "github.com/databricks/cli/cmd/root"
"github.com/databricks/cli/internal/build" "github.com/databricks/cli/internal/build"
"github.com/databricks/cli/libs/cmdio"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
var detail = false
var versionCmd = &cobra.Command{ var versionCmd = &cobra.Command{
Use: "version", Use: "version",
Args: cobra.NoArgs, Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error { Annotations: map[string]string{
info := build.GetInfo() "template": "Databricks CLI v{{.Version}}\n",
if detail { },
enc := json.NewEncoder(cmd.OutOrStdout())
enc.SetIndent("", " ")
return enc.Encode(info)
}
_, err := fmt.Fprintf(cmd.OutOrStdout(), "Databricks CLI v%s\n", info.Version) RunE: func(cmd *cobra.Command, args []string) error {
return err return cmdio.Render(cmd.Context(), build.GetInfo())
}, },
} }
func init() { func init() {
versionCmd.Flags().BoolVar(&detail, "detail", false, "output detailed version information as JSON")
root.RootCmd.AddCommand(versionCmd) root.RootCmd.AddCommand(versionCmd)
} }

View File

@ -1,6 +1,7 @@
package internal package internal
import ( import (
"encoding/json"
"fmt" "fmt"
"testing" "testing"
@ -27,3 +28,15 @@ func TestVersionCommand(t *testing.T) {
assert.Equal(t, expectedVersion, stdout.String()) assert.Equal(t, expectedVersion, stdout.String())
assert.Equal(t, "", stderr.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"])
}