Skip to content

Commit 7b0c48d

Browse files
authored
refactor: implement a new output result func to handle json/yaml output in a central place (#1030)
1 parent cfd42a0 commit 7b0c48d

File tree

229 files changed

+686
-4859
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

229 files changed

+686
-4859
lines changed

.github/docs/contribution-guide/cmd.go

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package bar
22

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

87
"github.com/spf13/cobra"
@@ -17,7 +16,6 @@ import (
1716
"github.com/stackitcloud/stackit-cli/internal/pkg/services/alb/client"
1817
"github.com/stackitcloud/stackit-cli/internal/pkg/tables"
1918
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
20-
"gopkg.in/yaml.v2"
2119
// (...)
2220
)
2321

@@ -118,22 +116,8 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *foo.APIClie
118116

119117
// Output result based on the configured output format
120118
func outputResult(p *print.Printer, cmd *cobra.Command, outputFormat string, resources []foo.Resource) error {
121-
switch outputFormat {
122-
case print.JSONOutputFormat:
123-
details, err := json.MarshalIndent(resources, "", " ")
124-
if err != nil {
125-
return fmt.Errorf("marshal resource list: %w", err)
126-
}
127-
p.Outputln(string(details))
128-
return nil
129-
case print.YAMLOutputFormat:
130-
details, err := yaml.Marshal(resources)
131-
if err != nil {
132-
return fmt.Errorf("marshal resource list: %w", err)
133-
}
134-
p.Outputln(string(details))
135-
return nil
136-
default:
119+
// the output result handles JSON/YAML output, you can pass your own callback func for pretty (default) output format
120+
return p.OutputResult(outputFormat, resources, func() error {
137121
table := tables.NewTable()
138122
table.SetHeader("ID", "NAME", "STATE")
139123
for i := range resources {
@@ -145,5 +129,5 @@ func outputResult(p *print.Printer, cmd *cobra.Command, outputFormat string, res
145129
return fmt.Errorf("render table: %w", err)
146130
}
147131
return nil
148-
}
132+
})
149133
}

internal/cmd/affinity-groups/create/create.go

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@ package create
22

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

8-
"github.com/goccy/go-yaml"
97
"github.com/spf13/cobra"
108
"github.com/stackitcloud/stackit-cli/internal/cmd/params"
119
"github.com/stackitcloud/stackit-cli/internal/pkg/args"
@@ -121,21 +119,9 @@ func outputResult(p *print.Printer, model inputModel, resp iaas.AffinityGroup) e
121119
if model.GlobalFlagModel != nil {
122120
outputFormat = model.GlobalFlagModel.OutputFormat
123121
}
124-
switch outputFormat {
125-
case print.JSONOutputFormat:
126-
details, err := json.MarshalIndent(resp, "", " ")
127-
if err != nil {
128-
return fmt.Errorf("marshal affinity group: %w", err)
129-
}
130-
p.Outputln(string(details))
131-
case print.YAMLOutputFormat:
132-
details, err := yaml.MarshalWithOptions(resp, yaml.IndentSequence(true), yaml.UseJSONMarshaler())
133-
if err != nil {
134-
return fmt.Errorf("marshal affinity group: %w", err)
135-
}
136-
p.Outputln(string(details))
137-
default:
122+
123+
return p.OutputResult(outputFormat, resp, func() error {
138124
p.Outputf("Created affinity group %q with id %s\n", model.Name, utils.PtrString(resp.Id))
139-
}
140-
return nil
125+
return nil
126+
})
141127
}

internal/cmd/affinity-groups/describe/describe.go

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@ package describe
22

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

8-
"github.com/goccy/go-yaml"
97
"github.com/spf13/cobra"
108
"github.com/stackitcloud/stackit-cli/internal/cmd/params"
119
"github.com/stackitcloud/stackit-cli/internal/pkg/args"
@@ -93,20 +91,8 @@ func outputResult(p *print.Printer, model inputModel, resp iaas.AffinityGroup) e
9391
if model.GlobalFlagModel != nil {
9492
outputFormat = model.GlobalFlagModel.OutputFormat
9593
}
96-
switch outputFormat {
97-
case print.JSONOutputFormat:
98-
details, err := json.MarshalIndent(resp, "", " ")
99-
if err != nil {
100-
return fmt.Errorf("marshal affinity group: %w", err)
101-
}
102-
p.Outputln(string(details))
103-
case print.YAMLOutputFormat:
104-
details, err := yaml.MarshalWithOptions(resp, yaml.IndentSequence(true), yaml.UseJSONMarshaler())
105-
if err != nil {
106-
return fmt.Errorf("marshal affinity group: %w", err)
107-
}
108-
p.Outputln(string(details))
109-
default:
94+
95+
return p.OutputResult(outputFormat, resp, func() error {
11096
table := tables.NewTable()
11197

11298
if resp.HasId() {
@@ -129,6 +115,6 @@ func outputResult(p *print.Printer, model inputModel, resp iaas.AffinityGroup) e
129115
if err := table.Display(p); err != nil {
130116
return fmt.Errorf("render table: %w", err)
131117
}
132-
}
133-
return nil
118+
return nil
119+
})
134120
}

internal/cmd/affinity-groups/list/list.go

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@ package list
22

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

8-
"github.com/goccy/go-yaml"
97
"github.com/stackitcloud/stackit-cli/internal/cmd/params"
108
"github.com/stackitcloud/stackit-cli/internal/pkg/tables"
119
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
@@ -115,20 +113,8 @@ func outputResult(p *print.Printer, model inputModel, items []iaas.AffinityGroup
115113
if model.GlobalFlagModel != nil {
116114
outputFormat = model.GlobalFlagModel.OutputFormat
117115
}
118-
switch outputFormat {
119-
case print.JSONOutputFormat:
120-
details, err := json.MarshalIndent(items, "", " ")
121-
if err != nil {
122-
return fmt.Errorf("marshal affinity groups: %w", err)
123-
}
124-
p.Outputln(string(details))
125-
case print.YAMLOutputFormat:
126-
details, err := yaml.MarshalWithOptions(items, yaml.IndentSequence(true), yaml.UseJSONMarshaler())
127-
if err != nil {
128-
return fmt.Errorf("marshal affinity groups: %w", err)
129-
}
130-
p.Outputln(string(details))
131-
default:
116+
117+
return p.OutputResult(outputFormat, items, func() error {
132118
table := tables.NewTable()
133119
table.SetHeader("ID", "NAME", "POLICY")
134120
for _, item := range items {
@@ -143,6 +129,7 @@ func outputResult(p *print.Printer, model inputModel, items []iaas.AffinityGroup
143129
if err := table.Display(p); err != nil {
144130
return fmt.Errorf("render table: %w", err)
145131
}
146-
}
147-
return nil
132+
133+
return nil
134+
})
148135
}

internal/cmd/beta/alb/create/create.go

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -162,29 +162,12 @@ func outputResult(p *print.Printer, model *inputModel, projectLabel string, resp
162162
if resp == nil {
163163
return fmt.Errorf("create loadbalancer response is empty")
164164
}
165-
switch model.OutputFormat {
166-
case print.JSONOutputFormat:
167-
details, err := json.MarshalIndent(resp, "", " ")
168-
if err != nil {
169-
return fmt.Errorf("marshal loadbalancer: %w", err)
170-
}
171-
p.Outputln(string(details))
172-
173-
return nil
174-
case print.YAMLOutputFormat:
175-
details, err := yaml.MarshalWithOptions(resp, yaml.IndentSequence(true), yaml.UseJSONMarshaler())
176-
if err != nil {
177-
return fmt.Errorf("marshal loadbalancer: %w", err)
178-
}
179-
p.Outputln(string(details))
180-
181-
return nil
182-
default:
165+
return p.OutputResult(model.OutputFormat, resp, func() error {
183166
operationState := "Created"
184167
if model.Async {
185168
operationState = "Triggered creation of"
186169
}
187170
p.Outputf("%s application loadbalancer for %q. Name: %s\n", operationState, projectLabel, utils.PtrString(resp.Name))
188171
return nil
189-
}
172+
})
190173
}

internal/cmd/beta/alb/describe/describe.go

Lines changed: 13 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package describe
22

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

@@ -16,7 +15,6 @@ import (
1615
"github.com/stackitcloud/stackit-cli/internal/pkg/services/alb/client"
1716
"github.com/stackitcloud/stackit-cli/internal/pkg/tables"
1817

19-
"github.com/goccy/go-yaml"
2018
"github.com/spf13/cobra"
2119
"github.com/stackitcloud/stackit-sdk-go/services/alb"
2220
)
@@ -89,54 +87,27 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *alb.APIClie
8987
return apiClient.GetLoadBalancer(ctx, model.ProjectId, model.Region, model.Name)
9088
}
9189

92-
func outputResult(p *print.Printer, outputFormat string, response *alb.LoadBalancer) error {
93-
switch outputFormat {
94-
case print.JSONOutputFormat:
95-
details, err := json.MarshalIndent(response, "", " ")
90+
func outputResult(p *print.Printer, outputFormat string, loadbalancer *alb.LoadBalancer) error {
91+
return p.OutputResult(outputFormat, loadbalancer, func() error {
92+
content := []tables.Table{}
9693

97-
if err != nil {
98-
return fmt.Errorf("marshal loadbalancer: %w", err)
94+
content = append(content, buildLoadBalancerTable(loadbalancer))
95+
96+
if loadbalancer.Listeners != nil {
97+
content = append(content, buildListenersTable(*loadbalancer.Listeners))
9998
}
100-
p.Outputln(string(details))
10199

102-
return nil
103-
case print.YAMLOutputFormat:
104-
details, err := yaml.MarshalWithOptions(response, yaml.IndentSequence(true), yaml.UseJSONMarshaler())
100+
if loadbalancer.TargetPools != nil {
101+
content = append(content, buildTargetPoolsTable(*loadbalancer.TargetPools))
102+
}
105103

104+
err := tables.DisplayTables(p, content)
106105
if err != nil {
107-
return fmt.Errorf("marshal loadbalancer: %w", err)
106+
return fmt.Errorf("display output: %w", err)
108107
}
109-
p.Outputln(string(details))
110108

111109
return nil
112-
default:
113-
if err := outputResultAsTable(p, response); err != nil {
114-
return err
115-
}
116-
}
117-
118-
return nil
119-
}
120-
121-
func outputResultAsTable(p *print.Printer, loadbalancer *alb.LoadBalancer) error {
122-
content := []tables.Table{}
123-
124-
content = append(content, buildLoadBalancerTable(loadbalancer))
125-
126-
if loadbalancer.Listeners != nil {
127-
content = append(content, buildListenersTable(*loadbalancer.Listeners))
128-
}
129-
130-
if loadbalancer.TargetPools != nil {
131-
content = append(content, buildTargetPoolsTable(*loadbalancer.TargetPools))
132-
}
133-
134-
err := tables.DisplayTables(p, content)
135-
if err != nil {
136-
return fmt.Errorf("display output: %w", err)
137-
}
138-
139-
return nil
110+
})
140111
}
141112

142113
func buildLoadBalancerTable(loadbalancer *alb.LoadBalancer) tables.Table {

internal/cmd/beta/alb/list/list.go

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

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

8-
"github.com/goccy/go-yaml"
97
"github.com/spf13/cobra"
108
"github.com/stackitcloud/stackit-cli/internal/cmd/params"
119
"github.com/stackitcloud/stackit-cli/internal/pkg/args"
@@ -128,24 +126,7 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *alb.APIClie
128126
return request
129127
}
130128
func outputResult(p *print.Printer, outputFormat string, items []alb.LoadBalancer) error {
131-
switch outputFormat {
132-
case print.JSONOutputFormat:
133-
details, err := json.MarshalIndent(items, "", " ")
134-
if err != nil {
135-
return fmt.Errorf("marshal loadbalancer list: %w", err)
136-
}
137-
p.Outputln(string(details))
138-
139-
return nil
140-
case print.YAMLOutputFormat:
141-
details, err := yaml.MarshalWithOptions(items, yaml.IndentSequence(true), yaml.UseJSONMarshaler())
142-
if err != nil {
143-
return fmt.Errorf("marshal loadbalancer list: %w", err)
144-
}
145-
p.Outputln(string(details))
146-
147-
return nil
148-
default:
129+
return p.OutputResult(outputFormat, items, func() error {
149130
table := tables.NewTable()
150131
table.SetHeader("NAME", "EXTERNAL ADDRESS", "REGION", "STATUS", "VERSION", "ERRORS")
151132
for i := range items {
@@ -169,5 +150,5 @@ func outputResult(p *print.Printer, outputFormat string, items []alb.LoadBalance
169150
}
170151

171152
return nil
172-
}
153+
})
173154
}

internal/cmd/beta/alb/observability-credentials/add/add.go

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package add
22

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

87
"github.com/stackitcloud/stackit-cli/internal/cmd/params"
@@ -13,7 +12,6 @@ import (
1312
"github.com/stackitcloud/stackit-cli/internal/pkg/services/alb/client"
1413
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
1514

16-
"github.com/goccy/go-yaml"
1715
"github.com/spf13/cobra"
1816
"github.com/stackitcloud/stackit-sdk-go/services/alb"
1917
)
@@ -115,25 +113,10 @@ func outputResult(p *print.Printer, outputFormat string, item *alb.CreateCredent
115113
return fmt.Errorf("no credential found")
116114
}
117115

118-
switch outputFormat {
119-
case print.JSONOutputFormat:
120-
details, err := json.MarshalIndent(item, "", " ")
121-
if err != nil {
122-
return fmt.Errorf("marshal credential: %w", err)
123-
}
124-
p.Outputln(string(details))
125-
case print.YAMLOutputFormat:
126-
details, err := yaml.MarshalWithOptions(item, yaml.IndentSequence(true), yaml.UseJSONMarshaler())
127-
if err != nil {
128-
return fmt.Errorf("marshal credential: %w", err)
129-
}
130-
p.Outputln(string(details))
131-
default:
116+
return p.OutputResult(outputFormat, item, func() error {
132117
if item.Credential != nil {
133-
p.Outputf("Created credential %s\n",
134-
utils.PtrString(item.Credential.CredentialsRef),
135-
)
118+
p.Outputf("Created credential %s\n", utils.PtrString(item.Credential.CredentialsRef))
136119
}
137-
}
138-
return nil
120+
return nil
121+
})
139122
}

0 commit comments

Comments
 (0)