Skip to content

Commit f10d6c1

Browse files
author
Jan Sternagel
committed
json/yaml output type safe
1 parent 77c6d96 commit f10d6c1

File tree

14 files changed

+671
-80
lines changed

14 files changed

+671
-80
lines changed

internal/cmd/beta/kms/key/restore/restore.go

Lines changed: 50 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package restore
22

33
import (
44
"context"
5+
"encoding/json"
56
"fmt"
67

78
"github.com/spf13/cobra"
@@ -13,9 +14,9 @@ import (
1314
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
1415
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
1516
kmsUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/kms/utils"
17+
"gopkg.in/yaml.v2"
1618

1719
"github.com/stackitcloud/stackit-cli/internal/pkg/services/kms/client"
18-
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
1920
"github.com/stackitcloud/stackit-sdk-go/services/kms"
2021
)
2122

@@ -33,12 +34,12 @@ type inputModel struct {
3334
func NewCmd(params *params.CmdParams) *cobra.Command {
3435
cmd := &cobra.Command{
3536
Use: "restore",
36-
Short: "Resotre a Key",
37+
Short: "Resotre a key",
3738
Long: "Restores the given key from being deleted.",
3839
Args: args.NoArgs,
3940
Example: examples.Build(
4041
examples.NewExample(
41-
`Restore a KMS Key "my-key-id" inside the Key Ring "my-key-ring-id" that was scheduled for deletion.`,
42+
`Restore a KMS key "my-key-id" inside the key ring "my-key-ring-id" that was scheduled for deletion.`,
4243
`$ stackit beta kms keyring restore --key-ring "my-key-ring-id" --key "my-key-id"`),
4344
),
4445
RunE: func(cmd *cobra.Command, _ []string) error {
@@ -72,11 +73,10 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
7273
req := buildRequest(ctx, model, apiClient)
7374
err = req.Execute()
7475
if err != nil {
75-
return fmt.Errorf("restore KMS Key: %w", err)
76+
return fmt.Errorf("restore KMS key: %w", err)
7677
}
7778

78-
params.Printer.Info("Restored Key %q\n", keyName)
79-
return nil
79+
return outputResult(params.Printer, model.OutputFormat, model.KeyId, keyName)
8080
},
8181
}
8282

@@ -90,22 +90,10 @@ func parseInput(p *print.Printer, cmd *cobra.Command) (*inputModel, error) {
9090
return nil, &errors.ProjectIdError{}
9191
}
9292

93-
keyRingId := flags.FlagToStringValue(p, cmd, keyRingIdFlag)
94-
keyId := flags.FlagToStringValue(p, cmd, keyIdFlag)
95-
96-
// Validate the uuid format of the IDs
97-
errKeyRing := utils.ValidateUUID(keyRingId)
98-
errKey := utils.ValidateUUID(keyId)
99-
if errKeyRing != nil || errKey != nil {
100-
return nil, &errors.DSAInputPlanError{
101-
Cmd: cmd,
102-
}
103-
}
104-
10593
model := inputModel{
10694
GlobalFlagModel: globalFlags,
107-
KeyRingId: keyRingId,
108-
KeyId: keyId,
95+
KeyRingId: flags.FlagToStringValue(p, cmd, keyRingIdFlag),
96+
KeyId: flags.FlagToStringValue(p, cmd, keyIdFlag),
10997
}
11098

11199
if p.IsVerbosityDebug() {
@@ -131,3 +119,45 @@ func configureFlags(cmd *cobra.Command) {
131119
err := flags.MarkFlagsRequired(cmd, keyRingIdFlag, keyIdFlag)
132120
cobra.CheckErr(err)
133121
}
122+
123+
func outputResult(p *print.Printer, outputFormat, keyId, keyName string) error {
124+
switch outputFormat {
125+
case print.JSONOutputFormat:
126+
details := struct {
127+
KeyId string `json:"keyId"`
128+
KeyName string `json:"keyName"`
129+
Status string `json:"status"`
130+
}{
131+
KeyId: keyId,
132+
KeyName: keyName,
133+
Status: "key restored",
134+
}
135+
b, err := json.MarshalIndent(details, "", " ")
136+
if err != nil {
137+
return fmt.Errorf("marshal output to JSON: %w", err)
138+
}
139+
p.Outputln(string(b))
140+
return nil
141+
142+
case print.YAMLOutputFormat:
143+
details := struct {
144+
KeyId string `yaml:"keyId"`
145+
KeyName string `yaml:"keyName"`
146+
Status string `yaml:"status"`
147+
}{
148+
KeyId: keyId,
149+
KeyName: keyName,
150+
Status: "key restored",
151+
}
152+
b, err := yaml.Marshal(details)
153+
if err != nil {
154+
return fmt.Errorf("marshal output to YAML: %w", err)
155+
}
156+
p.Outputln(string(b))
157+
return nil
158+
159+
default:
160+
p.Outputf("Successfully restored KMS key %q\n", keyName)
161+
return nil
162+
}
163+
}

internal/cmd/beta/kms/key/restore/restore_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/google/go-cmp/cmp/cmpopts"
99
"github.com/google/uuid"
1010
"github.com/spf13/cobra"
11+
"github.com/stackitcloud/stackit-cli/internal/cmd/params"
1112
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
1213
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
1314
"github.com/stackitcloud/stackit-sdk-go/services/kms"
@@ -225,3 +226,45 @@ func TestBuildRequest(t *testing.T) {
225226
})
226227
}
227228
}
229+
230+
func TestOutputResult(t *testing.T) {
231+
tests := []struct {
232+
description string
233+
wantErr bool
234+
outputFormat string
235+
keyId string
236+
keyName string
237+
}{
238+
{
239+
description: "default output",
240+
keyId: uuid.NewString(),
241+
keyName: uuid.NewString(),
242+
wantErr: false,
243+
},
244+
{
245+
description: "json output",
246+
outputFormat: print.JSONOutputFormat,
247+
keyId: uuid.NewString(),
248+
keyName: uuid.NewString(),
249+
wantErr: false,
250+
},
251+
{
252+
description: "yaml output",
253+
outputFormat: print.YAMLOutputFormat,
254+
keyId: uuid.NewString(),
255+
keyName: uuid.NewString(),
256+
wantErr: false,
257+
},
258+
}
259+
260+
p := print.NewPrinter()
261+
p.Cmd = NewCmd(&params.CmdParams{Printer: p})
262+
for _, tt := range tests {
263+
t.Run(tt.description, func(t *testing.T) {
264+
err := outputResult(p, tt.outputFormat, tt.keyId, tt.keyName)
265+
if (err != nil) != tt.wantErr {
266+
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
267+
}
268+
})
269+
}
270+
}

internal/cmd/beta/kms/keyring/delete/delete.go

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ package delete
22

33
import (
44
"context"
5+
"encoding/json"
56
"fmt"
67

8+
"github.com/goccy/go-yaml"
79
"github.com/spf13/cobra"
810
"github.com/stackitcloud/stackit-cli/internal/cmd/params"
911
"github.com/stackitcloud/stackit-cli/internal/pkg/args"
@@ -30,12 +32,12 @@ type inputModel struct {
3032
func NewCmd(params *params.CmdParams) *cobra.Command {
3133
cmd := &cobra.Command{
3234
Use: fmt.Sprintf("delete %s", keyRingIdArg),
33-
Short: "Deletes a KMS Keyring",
34-
Long: "Deletes a KMS Keyring.",
35+
Short: "Deletes a KMS key ring",
36+
Long: "Deletes a KMS key ring.",
3537
Args: args.SingleArg(keyRingIdArg, utils.ValidateUUID),
3638
Example: examples.Build(
3739
examples.NewExample(
38-
`Delete a KMS Keyring with ID "xxx"`,
40+
`Delete a KMS key ring with ID "xxx"`,
3941
"$ stackit beta kms keyring delete xxx"),
4042
),
4143
RunE: func(cmd *cobra.Command, args []string) error {
@@ -69,13 +71,12 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
6971
req := buildRequest(ctx, model, apiClient)
7072
err = req.Execute()
7173
if err != nil {
72-
return fmt.Errorf("delete KMS Key Ring: %w", err)
74+
return fmt.Errorf("delete KMS key ring: %w", err)
7375
}
7476

75-
// Wait for async operation not relevant. Keyring deletion is synchronous.
77+
// Wait for async operation not relevant. Key ring deletion is synchronous.
7678

77-
params.Printer.Info("Deleted key ring %q\n", keyRingLabel)
78-
return nil
79+
return outputResult(params.Printer, model.OutputFormat, keyRingLabel)
7980
},
8081
}
8182
return cmd
@@ -110,3 +111,41 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *kms.APIClie
110111
req := apiClient.DeleteKeyRing(ctx, model.ProjectId, model.Region, model.KeyRingId)
111112
return req
112113
}
114+
115+
func outputResult(p *print.Printer, outputFormat, keyRingLabel string) error {
116+
switch outputFormat {
117+
case print.JSONOutputFormat:
118+
details := struct {
119+
KeyRingLabel string `json:"keyRingLabel"`
120+
Status string `json:"status"`
121+
}{
122+
KeyRingLabel: keyRingLabel,
123+
Status: "Key ring deleted.",
124+
}
125+
b, err := json.MarshalIndent(details, "", " ")
126+
if err != nil {
127+
return fmt.Errorf("marshal output to JSON: %w", err)
128+
}
129+
p.Outputln(string(b))
130+
return nil
131+
132+
case print.YAMLOutputFormat:
133+
details := struct {
134+
KeyRingLabel string `yaml:"keyRingLabel"`
135+
Status string `yaml:"status"`
136+
}{
137+
KeyRingLabel: keyRingLabel,
138+
Status: "Key ring deleted.",
139+
}
140+
b, err := yaml.Marshal(details)
141+
if err != nil {
142+
return fmt.Errorf("marshal output to YAML: %w", err)
143+
}
144+
p.Outputln(string(b))
145+
return nil
146+
147+
default:
148+
p.Outputf("Deleted key ring: %s\n", keyRingLabel)
149+
return nil
150+
}
151+
}

internal/cmd/beta/kms/keyring/delete/delete_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,3 +210,41 @@ func TestBuildRequest(t *testing.T) {
210210
})
211211
}
212212
}
213+
214+
func TestOutputResult(t *testing.T) {
215+
tests := []struct {
216+
description string
217+
wantErr bool
218+
outputFormat string
219+
keyRingLabel string
220+
}{
221+
{
222+
description: "default output",
223+
keyRingLabel: "yourKeyRing",
224+
wantErr: false,
225+
},
226+
{
227+
description: "json output",
228+
outputFormat: print.JSONOutputFormat,
229+
keyRingLabel: "yourKeyRing",
230+
wantErr: false,
231+
},
232+
{
233+
description: "yaml output",
234+
outputFormat: print.YAMLOutputFormat,
235+
keyRingLabel: "yourKeyRing",
236+
wantErr: false,
237+
},
238+
}
239+
240+
p := print.NewPrinter()
241+
p.Cmd = NewCmd(&params.CmdParams{Printer: p})
242+
for _, tt := range tests {
243+
t.Run(tt.description, func(t *testing.T) {
244+
err := outputResult(p, tt.outputFormat, tt.keyRingLabel)
245+
if (err != nil) != tt.wantErr {
246+
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
247+
}
248+
})
249+
}
250+
}

0 commit comments

Comments
 (0)