Skip to content

Commit 6e7d725

Browse files
committed
refactor(kms): unify output handling for async operations
1 parent 1edb1e2 commit 6e7d725

File tree

15 files changed

+45
-306
lines changed

15 files changed

+45
-306
lines changed

internal/cmd/kms/key/create/create.go

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@ package create
22

33
import (
44
"context"
5-
"encoding/json"
65
"fmt"
76

87
"github.com/stackitcloud/stackit-cli/internal/pkg/types"
98

10-
"github.com/goccy/go-yaml"
119
"github.com/spf13/cobra"
1210
"github.com/stackitcloud/stackit-cli/internal/pkg/args"
1311
cliErr "github.com/stackitcloud/stackit-cli/internal/pkg/errors"
@@ -159,29 +157,14 @@ func outputResult(p *print.Printer, model *inputModel, resp *kms.Key) error {
159157
return fmt.Errorf("response is nil")
160158
}
161159

162-
switch model.OutputFormat {
163-
case print.JSONOutputFormat:
164-
details, err := json.MarshalIndent(resp, "", " ")
165-
if err != nil {
166-
return fmt.Errorf("marshal KMS key: %w", err)
167-
}
168-
p.Outputln(string(details))
169-
170-
case print.YAMLOutputFormat:
171-
details, err := yaml.MarshalWithOptions(resp, yaml.IndentSequence(true), yaml.UseJSONMarshaler())
172-
if err != nil {
173-
return fmt.Errorf("marshal KMS key: %w", err)
174-
}
175-
p.Outputln(string(details))
176-
177-
default:
160+
return p.OutputResult(model.OutputFormat, resp, func() error {
178161
operationState := "Created"
179162
if model.Async {
180163
operationState = "Triggered creation of"
181164
}
182165
p.Outputf("%s the KMS key %q. Key ID: %s\n", operationState, utils.PtrString(resp.DisplayName), utils.PtrString(resp.Id))
183-
}
184-
return nil
166+
return nil
167+
})
185168
}
186169

187170
func configureFlags(cmd *cobra.Command) {

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

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@ package delete
22

33
import (
44
"context"
5-
"encoding/json"
65
"fmt"
76

87
"github.com/stackitcloud/stackit-cli/internal/pkg/types"
98

10-
"github.com/goccy/go-yaml"
119
"github.com/spf13/cobra"
1210
"github.com/stackitcloud/stackit-cli/internal/pkg/args"
1311
"github.com/stackitcloud/stackit-cli/internal/pkg/errors"
@@ -127,22 +125,8 @@ func outputResult(p *print.Printer, outputFormat string, resp *kms.Key) error {
127125
return fmt.Errorf("response is nil")
128126
}
129127

130-
switch outputFormat {
131-
case print.JSONOutputFormat:
132-
details, err := json.MarshalIndent(resp, "", " ")
133-
if err != nil {
134-
return fmt.Errorf("marshal output to JSON: %w", err)
135-
}
136-
p.Outputln(string(details))
137-
case print.YAMLOutputFormat:
138-
details, err := yaml.MarshalWithOptions(resp, yaml.IndentSequence(true), yaml.UseJSONMarshaler())
139-
if err != nil {
140-
return fmt.Errorf("marshal output to YAML: %w", err)
141-
}
142-
p.Outputln(string(details))
143-
144-
default:
128+
return p.OutputResult(outputFormat, resp, func() error {
145129
p.Outputf("Deletion of KMS key %s scheduled successfully for the deletion date: %s\n", utils.PtrString(resp.DisplayName), utils.PtrString(resp.DeletionDate))
146-
}
147-
return nil
130+
return nil
131+
})
148132
}

internal/cmd/kms/key/importKey/importKey.go

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,10 @@ package importKey
33
import (
44
"context"
55
"encoding/base64"
6-
"encoding/json"
76
"fmt"
87

98
"github.com/stackitcloud/stackit-cli/internal/pkg/types"
109

11-
"github.com/goccy/go-yaml"
1210
"github.com/spf13/cobra"
1311
"github.com/stackitcloud/stackit-cli/internal/pkg/args"
1412
cliErr "github.com/stackitcloud/stackit-cli/internal/pkg/errors"
@@ -148,26 +146,10 @@ func outputResult(p *print.Printer, outputFormat, keyRingName, keyName string, r
148146
return fmt.Errorf("response is nil")
149147
}
150148

151-
switch outputFormat {
152-
case print.JSONOutputFormat:
153-
details, err := json.MarshalIndent(resp, "", " ")
154-
if err != nil {
155-
return fmt.Errorf("marshal KMS key: %w", err)
156-
}
157-
p.Outputln(string(details))
158-
159-
case print.YAMLOutputFormat:
160-
details, err := yaml.MarshalWithOptions(resp, yaml.IndentSequence(true), yaml.UseJSONMarshaler())
161-
if err != nil {
162-
return fmt.Errorf("marshal KMS key: %w", err)
163-
}
164-
p.Outputln(string(details))
165-
166-
default:
149+
return p.OutputResult(outputFormat, resp, func() error {
167150
p.Outputf("Imported a new version for the key %q inside the key ring %q\n", keyName, keyRingName)
168-
}
169-
170-
return nil
151+
return nil
152+
})
171153
}
172154

173155
func configureFlags(cmd *cobra.Command) {

internal/cmd/kms/key/list/list.go

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@ package list
22

33
import (
44
"context"
5-
"encoding/json"
65
"fmt"
76

87
"github.com/stackitcloud/stackit-cli/internal/pkg/types"
98

10-
"github.com/goccy/go-yaml"
119
"github.com/spf13/cobra"
1210
"github.com/stackitcloud/stackit-cli/internal/pkg/args"
1311
"github.com/stackitcloud/stackit-cli/internal/pkg/errors"
@@ -106,22 +104,7 @@ func outputResult(p *print.Printer, outputFormat, projectId, keyRingId string, r
106104

107105
keys := *resp.Keys
108106

109-
switch outputFormat {
110-
case print.JSONOutputFormat:
111-
details, err := json.MarshalIndent(keys, "", " ")
112-
if err != nil {
113-
return fmt.Errorf("marshal KMS Keys list: %w", err)
114-
}
115-
p.Outputln(string(details))
116-
117-
case print.YAMLOutputFormat:
118-
details, err := yaml.MarshalWithOptions(keys, yaml.IndentSequence(true), yaml.UseJSONMarshaler())
119-
if err != nil {
120-
return fmt.Errorf("marshal KMS Keys list: %w", err)
121-
}
122-
p.Outputln(string(details))
123-
124-
default:
107+
return p.OutputResult(outputFormat, keys, func() error {
125108
if len(keys) == 0 {
126109
p.Outputf("No keys found for project %q under the key ring %q\n", projectId, keyRingId)
127110
return nil
@@ -144,6 +127,6 @@ func outputResult(p *print.Printer, outputFormat, projectId, keyRingId string, r
144127
if err != nil {
145128
return fmt.Errorf("render table: %w", err)
146129
}
147-
}
148-
return nil
130+
return nil
131+
})
149132
}

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

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@ package restore
22

33
import (
44
"context"
5-
"encoding/json"
65
"fmt"
76

87
"github.com/stackitcloud/stackit-cli/internal/pkg/types"
98

10-
"github.com/goccy/go-yaml"
119
"github.com/spf13/cobra"
1210
"github.com/stackitcloud/stackit-cli/internal/pkg/args"
1311
"github.com/stackitcloud/stackit-cli/internal/pkg/errors"
@@ -126,22 +124,8 @@ func outputResult(p *print.Printer, outputFormat string, resp *kms.Key) error {
126124
return fmt.Errorf("response is nil")
127125
}
128126

129-
switch outputFormat {
130-
case print.JSONOutputFormat:
131-
details, err := json.MarshalIndent(resp, "", " ")
132-
if err != nil {
133-
return fmt.Errorf("marshal output to JSON: %w", err)
134-
}
135-
p.Outputln(string(details))
136-
case print.YAMLOutputFormat:
137-
details, err := yaml.MarshalWithOptions(resp, yaml.IndentSequence(true), yaml.UseJSONMarshaler())
138-
if err != nil {
139-
return fmt.Errorf("marshal output to YAML: %w", err)
140-
}
141-
p.Outputln(string(details))
142-
143-
default:
127+
return p.OutputResult(outputFormat, resp, func() error {
144128
p.Outputf("Successfully restored KMS key %q\n", utils.PtrString(resp.DisplayName))
145-
}
146-
return nil
129+
return nil
130+
})
147131
}

internal/cmd/kms/key/rotate/rotate.go

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@ package rotate
22

33
import (
44
"context"
5-
"encoding/json"
65
"fmt"
76

87
"github.com/stackitcloud/stackit-cli/internal/pkg/types"
98

10-
"github.com/goccy/go-yaml"
119
"github.com/spf13/cobra"
1210
"github.com/stackitcloud/stackit-cli/internal/pkg/args"
1311
"github.com/stackitcloud/stackit-cli/internal/pkg/errors"
@@ -120,24 +118,8 @@ func outputResult(p *print.Printer, outputFormat string, resp *kms.Version) erro
120118
return fmt.Errorf("response is nil")
121119
}
122120

123-
switch outputFormat {
124-
case print.JSONOutputFormat:
125-
details, err := json.MarshalIndent(resp, "", " ")
126-
if err != nil {
127-
return fmt.Errorf("marshal KMS key version: %w", err)
128-
}
129-
p.Outputln(string(details))
130-
131-
case print.YAMLOutputFormat:
132-
details, err := yaml.MarshalWithOptions(resp, yaml.IndentSequence(true), yaml.UseJSONMarshaler())
133-
if err != nil {
134-
return fmt.Errorf("marshal KMS key version: %w", err)
135-
}
136-
p.Outputln(string(details))
137-
138-
default:
121+
return p.OutputResult(outputFormat, resp, func() error {
139122
p.Outputf("Rotated key %s\n", utils.PtrString(resp.KeyId))
140-
}
141-
142-
return nil
123+
return nil
124+
})
143125
}

internal/cmd/kms/keyring/create/create.go

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@ package create
22

33
import (
44
"context"
5-
"encoding/json"
65
"fmt"
76

87
"github.com/stackitcloud/stackit-cli/internal/pkg/types"
98

10-
"github.com/goccy/go-yaml"
119
"github.com/spf13/cobra"
1210
"github.com/stackitcloud/stackit-cli/internal/pkg/args"
1311
cliErr "github.com/stackitcloud/stackit-cli/internal/pkg/errors"
@@ -147,29 +145,14 @@ func outputResult(p *print.Printer, model *inputModel, resp *kms.KeyRing) error
147145
return fmt.Errorf("response is nil")
148146
}
149147

150-
switch model.OutputFormat {
151-
case print.JSONOutputFormat:
152-
details, err := json.MarshalIndent(resp, "", " ")
153-
if err != nil {
154-
return fmt.Errorf("marshal KMS key ring: %w", err)
155-
}
156-
p.Outputln(string(details))
157-
158-
case print.YAMLOutputFormat:
159-
details, err := yaml.MarshalWithOptions(resp, yaml.IndentSequence(true), yaml.UseJSONMarshaler())
160-
if err != nil {
161-
return fmt.Errorf("marshal KMS key ring: %w", err)
162-
}
163-
p.Outputln(string(details))
164-
165-
default:
148+
return p.OutputResult(model.OutputFormat, resp, func() error {
166149
operationState := "Created"
167150
if model.Async {
168151
operationState = "Triggered creation of"
169152
}
170153
p.Outputf("%s key ring. KMS key ring ID: %s\n", operationState, utils.PtrString(resp.Id))
171-
}
172-
return nil
154+
return nil
155+
})
173156
}
174157

175158
func configureFlags(cmd *cobra.Command) {

internal/cmd/kms/keyring/list/list.go

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@ package list
22

33
import (
44
"context"
5-
"encoding/json"
65
"fmt"
76

87
"github.com/stackitcloud/stackit-cli/internal/pkg/types"
98

10-
"github.com/goccy/go-yaml"
119
"github.com/spf13/cobra"
1210
"github.com/stackitcloud/stackit-cli/internal/pkg/args"
1311
"github.com/stackitcloud/stackit-cli/internal/pkg/errors"
@@ -91,22 +89,7 @@ func outputResult(p *print.Printer, outputFormat, projectId string, resp *kms.Ke
9189

9290
keyRings := *resp.KeyRings
9391

94-
switch outputFormat {
95-
case print.JSONOutputFormat:
96-
details, err := json.MarshalIndent(keyRings, "", " ")
97-
if err != nil {
98-
return fmt.Errorf("marshal KMS key rings list: %w", err)
99-
}
100-
p.Outputln(string(details))
101-
102-
case print.YAMLOutputFormat:
103-
details, err := yaml.MarshalWithOptions(keyRings, yaml.IndentSequence(true), yaml.UseJSONMarshaler())
104-
if err != nil {
105-
return fmt.Errorf("marshal KMS key rings list: %w", err)
106-
}
107-
p.Outputln(string(details))
108-
109-
default:
92+
return p.OutputResult(outputFormat, keyRings, func() error {
11093
if len(keyRings) == 0 {
11194
p.Outputf("No key rings found for project %q\n", projectId)
11295
return nil
@@ -128,7 +111,6 @@ func outputResult(p *print.Printer, outputFormat, projectId string, resp *kms.Ke
128111
if err != nil {
129112
return fmt.Errorf("render table: %w", err)
130113
}
131-
}
132-
133-
return nil
114+
return nil
115+
})
134116
}

internal/cmd/kms/version/destroy/destroy.go

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,11 @@ package destroy
22

33
import (
44
"context"
5-
"encoding/json"
65
"fmt"
76
"strconv"
87

98
"github.com/stackitcloud/stackit-cli/internal/pkg/types"
109

11-
"github.com/goccy/go-yaml"
1210
"github.com/spf13/cobra"
1311
"github.com/stackitcloud/stackit-cli/internal/pkg/args"
1412
"github.com/stackitcloud/stackit-cli/internal/pkg/errors"
@@ -125,24 +123,8 @@ func outputResult(p *print.Printer, outputFormat string, resp *kms.Version) erro
125123
return fmt.Errorf("response is nil")
126124
}
127125

128-
switch outputFormat {
129-
case print.JSONOutputFormat:
130-
details, err := json.MarshalIndent(resp, "", " ")
131-
if err != nil {
132-
return fmt.Errorf("marshal KMS key: %w", err)
133-
}
134-
p.Outputln(string(details))
135-
136-
case print.YAMLOutputFormat:
137-
details, err := yaml.MarshalWithOptions(resp, yaml.IndentSequence(true), yaml.UseJSONMarshaler())
138-
if err != nil {
139-
return fmt.Errorf("marshal KMS key: %w", err)
140-
}
141-
p.Outputln(string(details))
142-
143-
default:
126+
return p.OutputResult(outputFormat, resp, func() error {
144127
p.Outputf("Destroyed version %d of the key %q\n", utils.PtrValue(resp.Number), utils.PtrValue(resp.KeyId))
145-
}
146-
147-
return nil
128+
return nil
129+
})
148130
}

0 commit comments

Comments
 (0)