diff --git a/cmd/runner/resource_class.go b/cmd/runner/resource_class.go index 749428a74..5d5362e86 100644 --- a/cmd/runner/resource_class.go +++ b/cmd/runner/resource_class.go @@ -1,6 +1,7 @@ package runner import ( + "encoding/json" "io" "github.com/olekukonko/tablewriter" @@ -28,6 +29,7 @@ func newResourceClassCommand(o *runnerOpts, preRunE validator.Validator) *cobra. return nil } + jsonFormat := false genToken := false createCmd := &cobra.Command{ Use: "create ", @@ -41,21 +43,48 @@ func newResourceClassCommand(o *runnerOpts, preRunE validator.Validator) *cobra. if err != nil { return err } - table := newResourceClassTable(cmd.OutOrStdout()) - defer table.Render() - appendResourceClass(table, *rc) - if !genToken { - return nil + var token *runner.Token + if genToken { + token, err = o.r.CreateToken(args[0], "default") + if err != nil { + return err + } } - token, err := o.r.CreateToken(args[0], "default") - if err != nil { - return err + if jsonFormat && !genToken { + // return JSON formatted output for resource-class (without generated token) + jsonRc, err := json.Marshal(rc) + if err != nil { + return err + } + jsonWriter := cmd.OutOrStdout() + jsonWriter.Write(jsonRc) + } else if jsonFormat && genToken { + // return JSON formatted output for token since it contains enough related resource-class info + jsonToken, err := json.Marshal(token) + if err != nil { + return err + } + jsonWriter := cmd.OutOrStdout() + jsonWriter.Write(jsonToken) + } else { + // return default ASCII table format for output + table := newResourceClassTable(cmd.OutOrStdout()) + defer table.Render() + appendResourceClass(table, *rc) + + // check to conditionally return YAML formatted resource-class token + if genToken { + return generateConfig(*token, cmd.OutOrStdout()) + } } - return generateConfig(*token, cmd.OutOrStdout()) + + return nil }, } + createCmd.PersistentFlags().BoolVar(&jsonFormat, "json", false, + "Return stdout output in JSON format.") createCmd.PersistentFlags().BoolVar(&genToken, "generate-token", false, "Generate a default token") cmd.AddCommand(createCmd) @@ -79,7 +108,7 @@ func newResourceClassCommand(o *runnerOpts, preRunE validator.Validator) *cobra. "Delete resource-class and any associated tokens") cmd.AddCommand(deleteCmd) - cmd.AddCommand(&cobra.Command{ + listCmd := &cobra.Command{ Use: "list ", Short: "List resource-classes for a namespace", Aliases: []string{"ls"}, @@ -91,15 +120,29 @@ func newResourceClassCommand(o *runnerOpts, preRunE validator.Validator) *cobra. return err } - table := newResourceClassTable(cmd.OutOrStdout()) - defer table.Render() - for _, rc := range rcs { - appendResourceClass(table, rc) + if jsonFormat { + // return JSON formatted for output + jsonRcs, err := json.Marshal(rcs) + if err != nil { + return err + } + jsonWriter := cmd.OutOrStdout() + jsonWriter.Write(jsonRcs) + } else { + // return default ASCII table format for output + table := newResourceClassTable(cmd.OutOrStdout()) + defer table.Render() + for _, rc := range rcs { + appendResourceClass(table, rc) + } } return nil }, - }) + } + listCmd.PersistentFlags().BoolVar(&jsonFormat, "json", false, + "Return stdout output in JSON format.") + cmd.AddCommand(listCmd) return cmd } diff --git a/cmd/runner/resource_class_test.go b/cmd/runner/resource_class_test.go index df9cbbf27..31280e8fb 100644 --- a/cmd/runner/resource_class_test.go +++ b/cmd/runner/resource_class_test.go @@ -47,6 +47,31 @@ func Test_ResourceClass(t *testing.T) { assert.Check(t, cmp.Contains(stderr.String(), terms)) }) + t.Run("without default token and json", func(t *testing.T) { + defer runner.reset() + defer stdout.Reset() + defer stderr.Reset() + + cmd.SetArgs([]string{ + "create", + "my-namespace/my-resource-class", + "my-description", + "--json", + }) + + err := cmd.Execute() + assert.NilError(t, err) + + assert.Check(t, cmp.Equal(len(runner.resourceClasses), 1)) + assert.Check(t, cmp.Equal(runner.resourceClasses[0].ResourceClass, "my-namespace/my-resource-class")) + assert.Check(t, cmp.Equal(runner.resourceClasses[0].Description, "my-description")) + assert.Check(t, cmp.Contains(stdout.String(), "my-namespace/my-resource-class")) + + assert.Check(t, cmp.Equal(len(runner.tokens), 0)) + + assert.Check(t, cmp.Contains(stderr.String(), terms)) + }) + t.Run("with default token", func(t *testing.T) { defer runner.reset() defer stdout.Reset() @@ -75,6 +100,36 @@ func Test_ResourceClass(t *testing.T) { assert.Check(t, cmp.Contains(stderr.String(), terms)) }) + + t.Run("with default token and json", func(t *testing.T) { + defer runner.reset() + defer stdout.Reset() + defer stderr.Reset() + + cmd.SetArgs([]string{ + "create", + "my-namespace/my-other-resource-class", + "my-description", + "--generate-token", + "--json", + }) + + err := cmd.Execute() + assert.NilError(t, err) + out := stdout.String() + + assert.Check(t, cmp.Equal(len(runner.resourceClasses), 1)) + assert.Check(t, cmp.Equal(runner.resourceClasses[0].ResourceClass, "my-namespace/my-other-resource-class")) + assert.Check(t, cmp.Equal(runner.resourceClasses[0].Description, "my-description")) + assert.Check(t, cmp.Contains(out, "my-namespace/my-other-resource-class")) + + assert.Check(t, cmp.Equal(len(runner.tokens), 1)) + assert.Check(t, cmp.Equal(runner.tokens[0].ResourceClass, "my-namespace/my-other-resource-class")) + assert.Check(t, cmp.Equal(runner.tokens[0].Nickname, "default")) + assert.Check(t, cmp.Contains(out, "fake-token")) + + assert.Check(t, cmp.Contains(stderr.String(), terms)) + }) }) } diff --git a/cmd/runner/testdata/runner/resource-class/create-expected-usage.txt b/cmd/runner/testdata/runner/resource-class/create-expected-usage.txt index c23ce9526..2039aab89 100644 --- a/cmd/runner/testdata/runner/resource-class/create-expected-usage.txt +++ b/cmd/runner/testdata/runner/resource-class/create-expected-usage.txt @@ -3,3 +3,4 @@ Usage: Flags: --generate-token Generate a default token + --json Return output back in JSON format diff --git a/cmd/runner/testdata/runner/resource-class/list-expected-usage.txt b/cmd/runner/testdata/runner/resource-class/list-expected-usage.txt index e161c2eb7..fcb377665 100644 --- a/cmd/runner/testdata/runner/resource-class/list-expected-usage.txt +++ b/cmd/runner/testdata/runner/resource-class/list-expected-usage.txt @@ -3,3 +3,6 @@ Usage: Aliases: list, ls + +Flags: + --json Return output back in JSON format