Skip to content

Commit ada9a54

Browse files
author
Jan Sternagel
committed
yaml/json format added
1 parent 3ea181c commit ada9a54

File tree

3 files changed

+99
-3
lines changed

3 files changed

+99
-3
lines changed

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

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

33
import (
44
"context"
5+
"encoding/json"
56
"fmt"
7+
"time"
68

9+
"github.com/goccy/go-yaml"
710
"github.com/spf13/cobra"
811
"github.com/stackitcloud/stackit-cli/internal/cmd/params"
912
"github.com/stackitcloud/stackit-cli/internal/pkg/args"
@@ -82,8 +85,7 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
8285
return err
8386
}
8487

85-
params.Printer.Info("Deletion of KMS Key %q scheduled successfully for the deletion date: %q\n", keyName, deletionDate)
86-
return nil
88+
return outputResult(params.Printer, model.OutputFormat, model.KeyId, keyName, deletionDate)
8789
},
8890
}
8991

@@ -138,3 +140,49 @@ func configureFlags(cmd *cobra.Command) {
138140
err := flags.MarkFlagsRequired(cmd, keyRingIdFlag, keyIdFlag)
139141
cobra.CheckErr(err)
140142
}
143+
144+
func outputResult(p *print.Printer, outputFormat, keyId, keyName string, deletionDate time.Time) error {
145+
switch outputFormat {
146+
case print.JSONOutputFormat:
147+
details := struct {
148+
KeyId string `json:"keyId"`
149+
KeyName string `json:"keyName"`
150+
Status string `json:"status"`
151+
DeletionDate time.Time `json:"deletionDate"`
152+
}{
153+
KeyId: keyId,
154+
KeyName: keyName,
155+
Status: "Deletion Scheduled",
156+
DeletionDate: deletionDate,
157+
}
158+
b, err := json.MarshalIndent(details, "", " ")
159+
if err != nil {
160+
return fmt.Errorf("marshal output to JSON: %w", err)
161+
}
162+
p.Outputln(string(b))
163+
return nil
164+
165+
case print.YAMLOutputFormat:
166+
details := struct {
167+
KeyId string `yaml:"keyId"`
168+
KeyName string `yaml:"keyName"`
169+
Status string `yaml:"status"`
170+
DeletionDate time.Time `yaml:"deletionDate"`
171+
}{
172+
KeyId: keyId,
173+
KeyName: keyName,
174+
Status: "Deletion Scheduled",
175+
DeletionDate: deletionDate,
176+
}
177+
b, err := yaml.Marshal(details)
178+
if err != nil {
179+
return fmt.Errorf("marshal output to YAML: %w", err)
180+
}
181+
p.Outputln(string(b))
182+
return nil
183+
184+
default:
185+
p.Outputf("Deletion of KMS Key %q scheduled successfully for the deletion date: %q\n", keyName, deletionDate)
186+
return nil
187+
}
188+
}

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

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ package delete
33
import (
44
"context"
55
"testing"
6+
"time"
67

78
"github.com/google/go-cmp/cmp"
89
"github.com/google/go-cmp/cmp/cmpopts"
910
"github.com/google/uuid"
1011
"github.com/spf13/cobra"
12+
"github.com/stackitcloud/stackit-cli/internal/cmd/params"
1113
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
1214
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
1315
"github.com/stackitcloud/stackit-sdk-go/services/kms"
@@ -225,3 +227,49 @@ func TestBuildRequest(t *testing.T) {
225227
})
226228
}
227229
}
230+
231+
func TestOutputResult(t *testing.T) {
232+
tests := []struct {
233+
description string
234+
wantErr bool
235+
outputFormat string
236+
keyId string
237+
keyName string
238+
deltionDate time.Time
239+
}{
240+
{
241+
description: "default output",
242+
keyId: uuid.NewString(),
243+
keyName: uuid.NewString(),
244+
deltionDate: time.Now(),
245+
wantErr: false,
246+
},
247+
{
248+
description: "json output",
249+
outputFormat: print.JSONOutputFormat,
250+
keyId: uuid.NewString(),
251+
keyName: uuid.NewString(),
252+
deltionDate: time.Now(),
253+
wantErr: false,
254+
},
255+
{
256+
description: "yaml output",
257+
outputFormat: print.YAMLOutputFormat,
258+
keyId: uuid.NewString(),
259+
keyName: uuid.NewString(),
260+
deltionDate: time.Now(),
261+
wantErr: false,
262+
},
263+
}
264+
265+
p := print.NewPrinter()
266+
p.Cmd = NewCmd(&params.CmdParams{Printer: p})
267+
for _, tt := range tests {
268+
t.Run(tt.description, func(t *testing.T) {
269+
err := outputResult(p, tt.outputFormat, tt.keyId, tt.keyName, tt.deltionDate)
270+
if (err != nil) != tt.wantErr {
271+
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
272+
}
273+
})
274+
}
275+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
6969
return fmt.Errorf("restore Key Version: %w", err)
7070
}
7171

72-
params.Printer.Info("Restored version %d of Key %q\n", *model.VersionNumber, keyName)
72+
params.Printer.Outputf("Restored version %d of Key %q\n", *model.VersionNumber, keyName)
7373
return nil
7474
},
7575
}

0 commit comments

Comments
 (0)