Skip to content

Commit e9139c0

Browse files
authored
fix: Fix plaintext output for a resource with only and ID (#263)
Fix plaintext output for a resource with only an ID
1 parent 9a71cfe commit e9139c0

5 files changed

Lines changed: 113 additions & 36 deletions

File tree

cmd/resources/resources.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,11 @@ import (
2525

2626
func getResourcesHelpTemplate() string {
2727
// This template uses `.Parent` to access subcommands on the root command.
28-
return fmt.Sprintf(`Available commands:{{range $index, $cmd := .Parent.Commands}}{{if (or (eq (index $.Parent.Annotations $cmd.Name) "resource"))}}
28+
return `Available commands:{{range $index, $cmd := .Parent.Commands}}{{if (or (eq (index $.Parent.Annotations $cmd.Name) "resource"))}}
2929
{{rpad $cmd.Name $cmd.NamePadding }} {{$cmd.Short}}{{end}}{{end}}
3030
3131
Use "ldcli [command] --help" for more information about a command.
32-
`,
33-
)
32+
`
3433
}
3534

3635
func NewResourcesCmd() *cobra.Command {
@@ -256,7 +255,7 @@ func (op *OperationCmd) initFlags() error {
256255
if err != nil {
257256
return err
258257
}
259-
op.cmd.Flags().SetAnnotation(flagName, "required", []string{"true"})
258+
_ = op.cmd.Flags().SetAnnotation(flagName, "required", []string{"true"})
260259
}
261260

262261
err := viper.BindPFlag(flagName, op.cmd.Flags().Lookup(flagName))
@@ -367,7 +366,7 @@ func NewOperationCmd(parentCmd *cobra.Command, client resources.Client, op Opera
367366
}
368367

369368
func operationUsageTemplate() string {
370-
return fmt.Sprint(`Usage:{{if .Runnable}}
369+
return `Usage:{{if .Runnable}}
371370
{{.UseLine}}{{end}}{{if .HasAvailableSubCommands}}
372371
{{.CommandPath}} [command]{{end}}{{if gt (len .Aliases) 0}}
373372
@@ -400,5 +399,5 @@ Additional help topics:{{range .Commands}}{{if .IsAdditionalHelpTopicCommand}}
400399
{{rpad .CommandPath .CommandPathPadding}} {{.Short}}{{end}}{{end}}{{end}}{{if .HasAvailableSubCommands}}
401400
402401
Use "{{.CommandPath}} [command] --help" for more information about a command.{{end}}
403-
`)
402+
`
404403
}

internal/output/plaintext_fns.go

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -40,29 +40,32 @@ var ErrorPlaintextOutputFn = func(r resource) string {
4040
}
4141
}
4242

43-
// MultipleEmailPlaintextOutputFn converts the resource to plain text specifically for member data.
44-
var MultipleEmailPlaintextOutputFn = func(r resource) string {
45-
return fmt.Sprintf("* %s (%s)", r["email"], r["_id"])
46-
}
47-
48-
// MultipleIDPlaintextOutputFn converts the resource to plain text for data without a key.
49-
var MultipleIDPlaintextOutputFn = func(r resource) string {
50-
return fmt.Sprintf("* %s (%s)", r["name"], r["_id"])
51-
}
52-
53-
// MultiplePlaintextOutputFn converts the resource to plain text based on its name and key in a list.
43+
// MultiplePlaintextOutputFn converts the resource to plain text.
5444
var MultiplePlaintextOutputFn = func(r resource) string {
55-
return fmt.Sprintf("* %s (%s)", r["name"], r["key"])
45+
return fmt.Sprintf("* %s", SingularPlaintextOutputFn(r))
5646
}
5747

5848
// SingularPlaintextOutputFn converts the resource to plain text based on its name and key.
5949
var SingularPlaintextOutputFn = func(r resource) string {
60-
if r["name"] == nil {
61-
return r["key"].(string)
62-
}
63-
if r["key"] == nil {
64-
return r["name"].(string)
65-
}
50+
email := r["email"]
51+
id := r["_id"]
52+
key := r["key"]
53+
name := r["name"]
6654

67-
return fmt.Sprintf("%s (%s)", r["name"], r["key"])
55+
switch {
56+
case name != nil && key != nil:
57+
return fmt.Sprintf("%s (%s)", name.(string), key.(string))
58+
case email != nil && id != nil:
59+
return fmt.Sprintf("%s (%s)", email.(string), id.(string))
60+
case name != nil && id != nil:
61+
return fmt.Sprintf("%s (%s)", name.(string), id.(string))
62+
case key != nil:
63+
return key.(string)
64+
case email != nil:
65+
return email.(string)
66+
case id != nil:
67+
return id.(string)
68+
default:
69+
return "cannot read resource"
70+
}
6871
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package output
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestSingularPlaintextOutputFn(t *testing.T) {
10+
tests := map[string]struct {
11+
resource resource
12+
expected string
13+
}{
14+
"with a name and key": {
15+
resource: resource{
16+
"key": "test-key",
17+
"name": "test-name",
18+
},
19+
expected: "test-name (test-key)",
20+
},
21+
"with only a key": {
22+
resource: resource{
23+
"key": "test-key",
24+
},
25+
expected: "test-key",
26+
},
27+
"with an ID and email": {
28+
resource: resource{
29+
"_id": "test-id",
30+
"email": "test-email",
31+
"name": "test-name",
32+
},
33+
expected: "test-email (test-id)",
34+
},
35+
"with a name and ID": {
36+
resource: resource{
37+
"_id": "test-id",
38+
"name": "test-name",
39+
},
40+
expected: "test-name (test-id)",
41+
},
42+
"with only an ID": {
43+
resource: resource{
44+
"_id": "test-id",
45+
},
46+
expected: "test-id",
47+
},
48+
"without any valid field": {
49+
resource: resource{
50+
"other": "other-value",
51+
},
52+
expected: "cannot read resource",
53+
},
54+
}
55+
56+
for name, tt := range tests {
57+
tt := tt
58+
t.Run(name, func(t *testing.T) {
59+
out := SingularPlaintextOutputFn(tt.resource)
60+
61+
assert.Equal(t, tt.expected, out)
62+
})
63+
}
64+
}

internal/output/resource_output.go

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -56,19 +56,9 @@ func CmdOutput(action string, outputKind string, input []byte) (string, error) {
5656
return "No items found", nil
5757
}
5858

59-
// the response could have various properties we want to show
60-
keyExists := func(key string) bool { _, ok := maybeResources.Items[0][key]; return ok }
61-
outputFn := MultiplePlaintextOutputFn
62-
switch {
63-
case keyExists("email"):
64-
outputFn = MultipleEmailPlaintextOutputFn
65-
case keyExists("_id"):
66-
outputFn = MultipleIDPlaintextOutputFn
67-
}
68-
6959
items := make([]string, 0, len(maybeResources.Items))
7060
for _, i := range maybeResources.Items {
71-
items = append(items, outputFn(i))
61+
items = append(items, MultiplePlaintextOutputFn(i))
7262
}
7363

7464
var (

internal/output/resource_output_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,27 @@ import (
1313
)
1414

1515
func TestCmdOutput(t *testing.T) {
16+
t.Run("with multiple resources with only an ID", func(t *testing.T) {
17+
input := `{
18+
"items": [
19+
{
20+
"_id": "test-id"
21+
}
22+
]
23+
}`
24+
25+
t.Run("with plaintext output", func(t *testing.T) {
26+
t.Run("returns a success message", func(t *testing.T) {
27+
expected := "* test-id"
28+
29+
result, err := output.CmdOutput("list", "plaintext", []byte(input))
30+
31+
require.NoError(t, err)
32+
assert.Equal(t, expected, result)
33+
})
34+
})
35+
})
36+
1637
t.Run("with paginated multiple resources", func(t *testing.T) {
1738
tests := map[string]struct {
1839
limit int

0 commit comments

Comments
 (0)