Skip to content

Commit

Permalink
feat: plaintext success response (#210)
Browse files Browse the repository at this point in the history
Show success message when updating resource with plaintext output
  • Loading branch information
dbolson authored Apr 25, 2024
1 parent c27e904 commit 82244ed
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 8 deletions.
2 changes: 1 addition & 1 deletion cmd/flags/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion cmd/flags/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
34 changes: 30 additions & 4 deletions internal/output/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down
69 changes: 67 additions & 2 deletions internal/output/output_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 82244ed

Please sign in to comment.