diff --git a/cmd/flags/create.go b/cmd/flags/create.go index e9180e1d..aaabd9e0 100644 --- a/cmd/flags/create.go +++ b/cmd/flags/create.go @@ -81,7 +81,7 @@ func runCreate(client flags.Client) func(*cobra.Command, []string) error { return errors.NewError(output) } - output, err := output.CmdOutputSingular( + output, err := output.CmdOutputCreate( viper.GetString(cliflags.OutputFlag), response, output.SingularPlaintextOutputFn, diff --git a/cmd/flags/update.go b/cmd/flags/update.go index f2275cfe..5c8bae11 100644 --- a/cmd/flags/update.go +++ b/cmd/flags/update.go @@ -150,7 +150,7 @@ func runUpdate(client flags.Client) func(*cobra.Command, []string) error { return errors.NewError(output) } - output, err := output.CmdOutputSingular( + output, err := output.CmdOutputUpdate( viper.GetString(cliflags.OutputFlag), response, output.SingularPlaintextOutputFn, diff --git a/internal/output/output.go b/internal/output/output.go index 63ace22d..c9bc51c0 100644 --- a/internal/output/output.go +++ b/internal/output/output.go @@ -73,7 +73,33 @@ func CmdOutputSingular(outputKind string, input []byte, fn PlaintextOutputFn) (s return "", err } - return outputFromKind(outputKind, SingularOutputter{ + return outputFromKind(outputKind, "", SingularOutputter{ + outputFn: fn, + resource: r, + resourceJSON: input, + }) +} + +// CmdOutputCreate builds a command response based on the flag the user provided and the shape of +// the input with a successfully created message. The expected shape is a single JSON object. +func CmdOutputCreate(outputKind string, input []byte, fn PlaintextOutputFn) (string, error) { + return cmdOutputWithMessage(outputKind, "Successfully created ", input, fn) +} + +// CmdOutputUpdate builds a command response based on the flag the user provided and the shape of +// the input with a successfully created message. The expected shape is a single JSON object. +func CmdOutputUpdate(outputKind string, input []byte, fn PlaintextOutputFn) (string, error) { + return cmdOutputWithMessage(outputKind, "Successfully updated ", input, fn) +} + +func cmdOutputWithMessage(outputKind string, message string, input []byte, fn PlaintextOutputFn) (string, error) { + var r resource + err := json.Unmarshal(input, &r) + if err != nil { + return "", err + } + + return outputFromKind(outputKind, message, SingularOutputter{ outputFn: fn, resource: r, resourceJSON: input, @@ -95,19 +121,19 @@ func CmdOutputMultiple(outputKind string, input []byte, fn PlaintextOutputFn) (s r.Items = rr } - return outputFromKind(outputKind, MultipleOutputter{ + return outputFromKind(outputKind, "", MultipleOutputter{ outputFn: fn, resources: r, resourceJSON: input, }) } -func outputFromKind(outputKind string, o Outputter) (string, error) { +func outputFromKind(outputKind string, additional string, o Outputter) (string, error) { switch outputKind { case "json": return o.JSON(), nil case "plaintext": - return o.String(), nil + return additional + o.String(), nil } return "", ErrInvalidOutputKind diff --git a/internal/output/output_test.go b/internal/output/output_test.go index 5a16069f..430e491f 100644 --- a/internal/output/output_test.go +++ b/internal/output/output_test.go @@ -8,7 +8,7 @@ import ( "github.com/stretchr/testify/require" ) -func TestCmdOutputResource(t *testing.T) { +func TestCmdOutputSingular(t *testing.T) { tests := map[string]struct { expected string fn output.PlaintextOutputFn @@ -65,7 +65,72 @@ func TestCmdOutputResource(t *testing.T) { } } -func TestCmdOutputResources(t *testing.T) { +func TestCmdOutput_WithSuccessMessage(t *testing.T) { + tests := map[string]struct { + expected string + fn func(string, []byte, output.PlaintextOutputFn) (string, error) + input string + outputKind string + }{ + "when creating with json": { + expected: `{ + "key": "test-key", + "name": "test-name" + }`, + fn: output.CmdOutputCreate, + input: `{ + "key": "test-key", + "name": "test-name" + }`, + outputKind: "json", + }, + "when creating with plaintext": { + expected: "Successfully created test-name (test-key)", + fn: output.CmdOutputCreate, + input: `{ + "key": "test-key", + "name": "test-name" + }`, + outputKind: "plaintext", + }, + "when updating with json": { + expected: `{ + "key": "test-key", + "name": "test-name" + }`, + fn: output.CmdOutputUpdate, + input: `{ + "key": "test-key", + "name": "test-name" + }`, + outputKind: "json", + }, + "when updating with plaintext": { + expected: "Successfully updated test-name (test-key)", + fn: output.CmdOutputUpdate, + input: `{ + "key": "test-key", + "name": "test-name" + }`, + outputKind: "plaintext", + }, + } + for name, tt := range tests { + tt := tt + t.Run(name, func(t *testing.T) { + output, err := tt.fn( + tt.outputKind, + []byte(tt.input), + output.SingularPlaintextOutputFn, + ) + + require.NoError(t, err) + assert.Equal(t, tt.expected, output) + }) + } +} + +func TestCmdOutputMultiple(t *testing.T) { tests := map[string]struct { expected string fn output.PlaintextOutputFn