Skip to content

Commit c52cb3d

Browse files
feat(annotations): improve list k-v visualization (#6878)
1 parent ee0b789 commit c52cb3d

5 files changed

Lines changed: 157 additions & 0 deletions

File tree

‎internal/namespaces/annotations/v1/custom.go‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ func GetCommands() *core.Commands {
1212
human.RegisterMarshalerFunc(annotationsSDK.BindingKey{}, bindingKeyMarshalerFunc)
1313
human.RegisterMarshalerFunc(annotationsSDK.BindingValue{}, bindingValueMarshalerFunc)
1414

15+
cmds.MustFind("annotations", "key-value", "list").Override(keyValueListBuilder)
16+
1517
return cmds
1618
}
1719

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package annotations
2+
3+
import (
4+
"context"
5+
6+
"github.com/scaleway/scaleway-cli/v2/core"
7+
annotationsSDK "github.com/scaleway/scaleway-sdk-go/api/annotations/v1"
8+
)
9+
10+
type keyValueRow struct {
11+
KeyID string `json:"key_id"`
12+
Key string `json:"key"`
13+
ValueID string `json:"value_id"`
14+
Value string `json:"value"`
15+
}
16+
17+
func keyValueListBuilder(c *core.Command) *core.Command {
18+
c.View = &core.View{
19+
Fields: []*core.ViewField{
20+
{
21+
Label: "Key ID",
22+
FieldName: "KeyID",
23+
},
24+
{
25+
Label: "Key Name",
26+
FieldName: "Key",
27+
},
28+
{
29+
Label: "Value ID",
30+
FieldName: "ValueID",
31+
},
32+
{
33+
Label: "Value Name",
34+
FieldName: "Value",
35+
},
36+
},
37+
}
38+
39+
c.AddInterceptors(
40+
func(ctx context.Context, argsI any, runner core.CommandRunner) (any, error) {
41+
originalRes, err := runner(ctx, argsI)
42+
if err != nil {
43+
return nil, err
44+
}
45+
46+
keyValueResponse := originalRes.(*annotationsSDK.ListAllKeysAndValuesResponse)
47+
48+
rows := make([]keyValueRow, 0, len(keyValueResponse.Keys))
49+
for _, key := range keyValueResponse.Keys {
50+
if len(key.Values) == 0 {
51+
rows = append(rows, keyValueRow{KeyID: key.ID, Key: key.Name})
52+
53+
continue
54+
}
55+
for _, value := range key.Values {
56+
rows = append(rows, keyValueRow{
57+
KeyID: key.ID,
58+
Key: key.Name,
59+
ValueID: value.ID,
60+
Value: value.Name,
61+
})
62+
}
63+
}
64+
65+
return rows, nil
66+
},
67+
)
68+
69+
return c
70+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package annotations_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/scaleway/scaleway-cli/v2/core"
7+
annotations "github.com/scaleway/scaleway-cli/v2/internal/namespaces/annotations/v1"
8+
)
9+
10+
func Test_ListKeyValue(t *testing.T) {
11+
t.Run("Simple", core.Test(&core.TestConfig{
12+
Commands: annotations.GetCommands(),
13+
Cmd: "scw annotations key-value list",
14+
Check: core.TestCheckGolden(),
15+
}))
16+
}

‎internal/namespaces/annotations/v1/testdata/test-list-key-value-simple.cassette.yaml‎

Lines changed: 42 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
🎲🎲🎲 EXIT CODE: 0 🎲🎲🎲
2+
🟩🟩🟩 STDOUT️ 🟩🟩🟩️
3+
Key ID Key Name Value ID Value Name
4+
11111111-1111-1111-1111-111111111111 env 22222222-2222-2222-2222-222222222222 prod
5+
11111111-1111-1111-1111-111111111111 env 33333333-3333-3333-3333-333333333333 staging
6+
44444444-4444-4444-4444-444444444444 team - -
7+
🟩🟩🟩 JSON STDOUT 🟩🟩🟩
8+
[
9+
{
10+
"key_id": "11111111-1111-1111-1111-111111111111",
11+
"key": "env",
12+
"value_id": "22222222-2222-2222-2222-222222222222",
13+
"value": "prod"
14+
},
15+
{
16+
"key_id": "11111111-1111-1111-1111-111111111111",
17+
"key": "env",
18+
"value_id": "33333333-3333-3333-3333-333333333333",
19+
"value": "staging"
20+
},
21+
{
22+
"key_id": "44444444-4444-4444-4444-444444444444",
23+
"key": "team",
24+
"value_id": "",
25+
"value": ""
26+
}
27+
]

0 commit comments

Comments
 (0)