Skip to content

Commit

Permalink
Add --json flag for resource-class cmds
Browse files Browse the repository at this point in the history
  • Loading branch information
jdelnano committed Dec 20, 2024
1 parent 08776cb commit 4b4fb83
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 15 deletions.
73 changes: 58 additions & 15 deletions cmd/runner/resource_class.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package runner

import (
"encoding/json"
"io"

"github.com/olekukonko/tablewriter"
Expand Down Expand Up @@ -28,6 +29,7 @@ func newResourceClassCommand(o *runnerOpts, preRunE validator.Validator) *cobra.
return nil
}

jsonFormat := false
genToken := false
createCmd := &cobra.Command{
Use: "create <resource-class> <description>",
Expand All @@ -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)
Expand All @@ -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 <namespace>",
Short: "List resource-classes for a namespace",
Aliases: []string{"ls"},
Expand All @@ -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
}
Expand Down
55 changes: 55 additions & 0 deletions cmd/runner/resource_class_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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))
})
})
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ Usage:

Flags:
--generate-token Generate a default token
--json Return output back in JSON format

0 comments on commit 4b4fb83

Please sign in to comment.