Skip to content

Commit 18f214e

Browse files
Merge branch 'stackitcloud:main' into mongodb-flex/stackit-admin
2 parents 75b745a + 99f4d2d commit 18f214e

File tree

572 files changed

+2210
-19116
lines changed

Some content is hidden

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

572 files changed

+2210
-19116
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
}

.github/workflows/renovate.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ jobs:
1313
- name: Checkout
1414
uses: actions/checkout@v5
1515
- name: Self-hosted Renovate
16-
uses: renovatebot/[email protected].17
16+
uses: renovatebot/[email protected].18
1717
with:
1818
configurationFile: .github/renovate.json
1919
token: ${{ secrets.RENOVATE_TOKEN }}

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

Lines changed: 7 additions & 21 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"
@@ -42,9 +40,9 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
4240
"$ stackit affinity-group create --name AFFINITY_GROUP_NAME --policy soft-affinity",
4341
),
4442
),
45-
RunE: func(cmd *cobra.Command, _ []string) error {
43+
RunE: func(cmd *cobra.Command, args []string) error {
4644
ctx := context.Background()
47-
model, err := parseInput(params.Printer, cmd)
45+
model, err := parseInput(params.Printer, cmd, args)
4846
if err != nil {
4947
return err
5048
}
@@ -100,7 +98,7 @@ func buildRequest(ctx context.Context, model inputModel, apiClient *iaas.APIClie
10098
return req
10199
}
102100

103-
func parseInput(p *print.Printer, cmd *cobra.Command) (*inputModel, error) {
101+
func parseInput(p *print.Printer, cmd *cobra.Command, _ []string) (*inputModel, error) {
104102
globalFlags := globalflags.Parse(p, cmd)
105103
if globalFlags.ProjectId == "" {
106104
return nil, &errors.ProjectIdError{}
@@ -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/create/create_test.go

Lines changed: 3 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/stackitcloud/stackit-cli/internal/cmd/params"
1111
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
1212
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
13+
"github.com/stackitcloud/stackit-cli/internal/pkg/testutils"
1314
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
1415
"github.com/stackitcloud/stackit-sdk-go/services/iaas"
1516
)
@@ -80,6 +81,7 @@ func fixturePayload(mods ...func(payload *iaas.CreateAffinityGroupPayload)) iaas
8081
func TestParseInput(t *testing.T) {
8182
tests := []struct {
8283
description string
84+
argValues []string
8385
flagValues map[string]string
8486
isValid bool
8587
expectedModel *inputModel
@@ -120,43 +122,7 @@ func TestParseInput(t *testing.T) {
120122
}
121123
for _, tt := range tests {
122124
t.Run(tt.description, func(t *testing.T) {
123-
p := print.NewPrinter()
124-
cmd := NewCmd(&params.CmdParams{Printer: p})
125-
if err := globalflags.Configure(cmd.Flags()); err != nil {
126-
t.Fatalf("configure global flags: %v", err)
127-
}
128-
129-
for flag, value := range tt.flagValues {
130-
if err := cmd.Flags().Set(flag, value); err != nil {
131-
if !tt.isValid {
132-
return
133-
}
134-
t.Fatalf("setting flag --%s=%s: %v", flag, value, err)
135-
}
136-
}
137-
138-
if err := cmd.ValidateRequiredFlags(); err != nil {
139-
if !tt.isValid {
140-
return
141-
}
142-
t.Fatalf("error validating flags: %v", err)
143-
}
144-
145-
model, err := parseInput(p, cmd)
146-
if err != nil {
147-
if !tt.isValid {
148-
return
149-
}
150-
t.Fatalf("error parsing flags: %v", err)
151-
}
152-
153-
if !tt.isValid {
154-
t.Fatalf("did not fail on invalid input")
155-
}
156-
diff := cmp.Diff(model, tt.expectedModel)
157-
if diff != "" {
158-
t.Fatalf("Data does not match: %s", diff)
159-
}
125+
testutils.TestParseInput(t, NewCmd, parseInput, tt.expectedModel, tt.argValues, tt.flagValues, tt.isValid)
160126
})
161127
}
162128
}

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: 8 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/stackitcloud/stackit-cli/internal/cmd/params"
108
"github.com/stackitcloud/stackit-cli/internal/pkg/tables"
119
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
@@ -44,9 +42,9 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
4442
"$ stackit affinity-group list --limit=10",
4543
),
4644
),
47-
RunE: func(cmd *cobra.Command, _ []string) error {
45+
RunE: func(cmd *cobra.Command, args []string) error {
4846
ctx := context.Background()
49-
model, err := parseInput(params.Printer, cmd)
47+
model, err := parseInput(params.Printer, cmd, args)
5048
if err != nil {
5149
return err
5250
}
@@ -87,7 +85,7 @@ func buildRequest(ctx context.Context, model inputModel, apiClient *iaas.APIClie
8785
return apiClient.ListAffinityGroups(ctx, model.ProjectId)
8886
}
8987

90-
func parseInput(p *print.Printer, cmd *cobra.Command) (*inputModel, error) {
88+
func parseInput(p *print.Printer, cmd *cobra.Command, _ []string) (*inputModel, error) {
9189
globalFlags := globalflags.Parse(p, cmd)
9290
if globalFlags.ProjectId == "" {
9391
return nil, &errors.ProjectIdError{}
@@ -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/affinity-groups/list/list_test.go

Lines changed: 3 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/stackitcloud/stackit-cli/internal/cmd/params"
1212
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
1313
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
14+
"github.com/stackitcloud/stackit-cli/internal/pkg/testutils"
1415
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
1516
"github.com/stackitcloud/stackit-sdk-go/services/iaas"
1617
)
@@ -63,6 +64,7 @@ func fixtureRequest(mods ...func(request *iaas.ApiListAffinityGroupsRequest)) ia
6364
func TestParseInput(t *testing.T) {
6465
tests := []struct {
6566
description string
67+
argValues []string
6668
flagValues map[string]string
6769
isValid bool
6870
expectedModel *inputModel
@@ -105,43 +107,7 @@ func TestParseInput(t *testing.T) {
105107
}
106108
for _, tt := range tests {
107109
t.Run(tt.description, func(t *testing.T) {
108-
p := print.NewPrinter()
109-
cmd := NewCmd(&params.CmdParams{Printer: p})
110-
if err := globalflags.Configure(cmd.Flags()); err != nil {
111-
t.Fatalf("configure global flags: %v", err)
112-
}
113-
114-
for flag, value := range tt.flagValues {
115-
if err := cmd.Flags().Set(flag, value); err != nil {
116-
if !tt.isValid {
117-
return
118-
}
119-
t.Fatalf("setting flag --%s=%s: %v", flag, value, err)
120-
}
121-
}
122-
123-
if err := cmd.ValidateRequiredFlags(); err != nil {
124-
if !tt.isValid {
125-
return
126-
}
127-
t.Fatalf("error validating flags: %v", err)
128-
}
129-
130-
model, err := parseInput(p, cmd)
131-
if err != nil {
132-
if !tt.isValid {
133-
return
134-
}
135-
t.Fatalf("error parsing flags: %v", err)
136-
}
137-
138-
if !tt.isValid {
139-
t.Fatalf("did not fail on invalid input")
140-
}
141-
diff := cmp.Diff(model, tt.expectedModel)
142-
if diff != "" {
143-
t.Fatalf("Data does not match: %s", diff)
144-
}
110+
testutils.TestParseInput(t, NewCmd, parseInput, tt.expectedModel, tt.argValues, tt.flagValues, tt.isValid)
145111
})
146112
}
147113
}

internal/cmd/auth/activate-service-account/activate_service_account.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,11 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
5858
"$ stackit auth activate-service-account --service-account-token my-service-account-token --only-print-access-token",
5959
),
6060
),
61-
RunE: func(cmd *cobra.Command, _ []string) error {
62-
model := parseInput(params.Printer, cmd)
61+
RunE: func(cmd *cobra.Command, args []string) error {
62+
model, err := parseInput(params.Printer, cmd, args)
63+
if err != nil {
64+
return err
65+
}
6366

6467
tokenCustomEndpoint := viper.GetString(config.TokenCustomEndpointKey)
6568
if !model.OnlyPrintAccessToken {
@@ -113,7 +116,7 @@ func configureFlags(cmd *cobra.Command) {
113116
cmd.Flags().Bool(onlyPrintAccessTokenFlag, false, "If this is set to true the credentials are not stored in either the keyring or a file")
114117
}
115118

116-
func parseInput(p *print.Printer, cmd *cobra.Command) *inputModel {
119+
func parseInput(p *print.Printer, cmd *cobra.Command, _ []string) (*inputModel, error) {
117120
model := inputModel{
118121
ServiceAccountToken: flags.FlagToStringValue(p, cmd, serviceAccountTokenFlag),
119122
ServiceAccountKeyPath: flags.FlagToStringValue(p, cmd, serviceAccountKeyPathFlag),
@@ -122,7 +125,7 @@ func parseInput(p *print.Printer, cmd *cobra.Command) *inputModel {
122125
}
123126

124127
p.DebugInputModel(model)
125-
return &model
128+
return &model, nil
126129
}
127130

128131
func storeCustomEndpoint(tokenCustomEndpoint string) error {

0 commit comments

Comments
 (0)