Skip to content

Commit 33a8a94

Browse files
committed
refactor(cli): pass parameter struct to cmd factories
relates to STACKITCLI-180
1 parent de4712f commit 33a8a94

File tree

790 files changed

+4214
-3506
lines changed

Some content is hidden

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

790 files changed

+4214
-3506
lines changed

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,28 @@ import (
66
"github.com/stackitcloud/stackit-cli/internal/cmd/affinity-groups/delete"
77
"github.com/stackitcloud/stackit-cli/internal/cmd/affinity-groups/describe"
88
"github.com/stackitcloud/stackit-cli/internal/cmd/affinity-groups/list"
9+
"github.com/stackitcloud/stackit-cli/internal/cmd/params"
910
"github.com/stackitcloud/stackit-cli/internal/pkg/args"
10-
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
1111
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
1212
)
1313

14-
func NewCmd(p *print.Printer) *cobra.Command {
14+
func NewCmd(params *params.CmdParams) *cobra.Command {
1515
cmd := &cobra.Command{
1616
Use: "affinity-group",
1717
Short: "Manage server affinity groups",
1818
Long: "Manage the lifecycle of server affinity groups.",
1919
Args: args.NoArgs,
2020
Run: utils.CmdHelp,
2121
}
22-
addSubcommands(cmd, p)
22+
addSubcommands(cmd, params)
2323
return cmd
2424
}
2525

26-
func addSubcommands(cmd *cobra.Command, p *print.Printer) {
26+
func addSubcommands(cmd *cobra.Command, params *params.CmdParams) {
2727
cmd.AddCommand(
28-
create.NewCmd(p),
29-
delete.NewCmd(p),
30-
describe.NewCmd(p),
31-
list.NewCmd(p),
28+
create.NewCmd(params),
29+
delete.NewCmd(params),
30+
describe.NewCmd(params),
31+
list.NewCmd(params),
3232
)
3333
}

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77

88
"github.com/goccy/go-yaml"
99
"github.com/spf13/cobra"
10+
"github.com/stackitcloud/stackit-cli/internal/cmd/params"
1011
"github.com/stackitcloud/stackit-cli/internal/pkg/args"
1112
"github.com/stackitcloud/stackit-cli/internal/pkg/errors"
1213
"github.com/stackitcloud/stackit-cli/internal/pkg/examples"
@@ -29,7 +30,7 @@ type inputModel struct {
2930
Policy string
3031
}
3132

32-
func NewCmd(p *print.Printer) *cobra.Command {
33+
func NewCmd(params *params.CmdParams) *cobra.Command {
3334
cmd := &cobra.Command{
3435
Use: "create",
3536
Short: "Creates an affinity groups",
@@ -43,20 +44,20 @@ func NewCmd(p *print.Printer) *cobra.Command {
4344
),
4445
RunE: func(cmd *cobra.Command, _ []string) error {
4546
ctx := context.Background()
46-
model, err := parseInput(p, cmd)
47+
model, err := parseInput(params.Printer, cmd)
4748
if err != nil {
4849
return err
4950
}
5051

5152
// Configure API client
52-
apiClient, err := client.ConfigureClient(p)
53+
apiClient, err := client.ConfigureClient(params.Printer)
5354
if err != nil {
5455
return err
5556
}
5657

5758
if !model.AssumeYes {
5859
prompt := fmt.Sprintf("Are you sure you want to create the affinity group %q?", model.Name)
59-
err = p.PromptForConfirmation(prompt)
60+
err = params.Printer.PromptForConfirmation(prompt)
6061
if err != nil {
6162
return err
6263
}
@@ -70,7 +71,7 @@ func NewCmd(p *print.Printer) *cobra.Command {
7071
return fmt.Errorf("create affinity group: %w", err)
7172
}
7273
if resp := result; resp != nil {
73-
return outputResult(p, *model, *resp)
74+
return outputResult(params.Printer, *model, *resp)
7475
}
7576
return fmt.Errorf("create affinity group: nil result")
7677
},

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/google/go-cmp/cmp"
88
"github.com/google/go-cmp/cmp/cmpopts"
99
"github.com/google/uuid"
10+
"github.com/stackitcloud/stackit-cli/internal/cmd/params"
1011
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
1112
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
1213
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
@@ -120,7 +121,7 @@ func TestParseInput(t *testing.T) {
120121
for _, tt := range tests {
121122
t.Run(tt.description, func(t *testing.T) {
122123
p := print.NewPrinter()
123-
cmd := NewCmd(p)
124+
cmd := NewCmd(&params.CmdParams{Printer: p})
124125
if err := globalflags.Configure(cmd.Flags()); err != nil {
125126
t.Fatalf("configure global flags: %v", err)
126127
}
@@ -212,7 +213,7 @@ func TestOutputResult(t *testing.T) {
212213
},
213214
}
214215
p := print.NewPrinter()
215-
p.Cmd = NewCmd(p)
216+
p.Cmd = NewCmd(&params.CmdParams{Printer: p})
216217
for _, tt := range tests {
217218
t.Run(tt.description, func(t *testing.T) {
218219
err := outputResult(p, tt.model, tt.response)

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66

77
"github.com/spf13/cobra"
8+
"github.com/stackitcloud/stackit-cli/internal/cmd/params"
89
"github.com/stackitcloud/stackit-cli/internal/pkg/args"
910
"github.com/stackitcloud/stackit-cli/internal/pkg/errors"
1011
"github.com/stackitcloud/stackit-cli/internal/pkg/examples"
@@ -26,7 +27,7 @@ const (
2627
affinityGroupIdArg = "AFFINITY_GROUP"
2728
)
2829

29-
func NewCmd(p *print.Printer) *cobra.Command {
30+
func NewCmd(params *params.CmdParams) *cobra.Command {
3031
cmd := &cobra.Command{
3132
Use: fmt.Sprintf("delete %s", affinityGroupIdArg),
3233
Short: "Deletes an affinity group",
@@ -40,32 +41,32 @@ func NewCmd(p *print.Printer) *cobra.Command {
4041
),
4142
RunE: func(cmd *cobra.Command, args []string) error {
4243
ctx := context.Background()
43-
model, err := parseInput(p, cmd, args)
44+
model, err := parseInput(params.Printer, cmd, args)
4445
if err != nil {
4546
return err
4647
}
4748

4849
// Configure API client
49-
apiClient, err := client.ConfigureClient(p)
50+
apiClient, err := client.ConfigureClient(params.Printer)
5051
if err != nil {
5152
return err
5253
}
5354

54-
projectLabel, err := projectname.GetProjectName(ctx, p, cmd)
55+
projectLabel, err := projectname.GetProjectName(ctx, params.Printer, cmd)
5556
if err != nil {
56-
p.Debug(print.ErrorLevel, "get project name: %v", err)
57+
params.Printer.Debug(print.ErrorLevel, "get project name: %v", err)
5758
projectLabel = model.ProjectId
5859
}
5960

6061
affinityGroupLabel, err := iaasUtils.GetAffinityGroupName(ctx, apiClient, model.ProjectId, model.AffinityGroupId)
6162
if err != nil {
62-
p.Debug(print.ErrorLevel, "get affinity group name: %v", err)
63+
params.Printer.Debug(print.ErrorLevel, "get affinity group name: %v", err)
6364
affinityGroupLabel = model.AffinityGroupId
6465
}
6566

6667
if !model.AssumeYes {
6768
prompt := fmt.Sprintf("Are you sure you want to delete affinity group %q?", affinityGroupLabel)
68-
err = p.PromptForConfirmation(prompt)
69+
err = params.Printer.PromptForConfirmation(prompt)
6970
if err != nil {
7071
return err
7172
}
@@ -77,7 +78,7 @@ func NewCmd(p *print.Printer) *cobra.Command {
7778
if err != nil {
7879
return fmt.Errorf("delete affinity group: %w", err)
7980
}
80-
p.Info("Deleted affinity group %q for %q\n", affinityGroupLabel, projectLabel)
81+
params.Printer.Info("Deleted affinity group %q for %q\n", affinityGroupLabel, projectLabel)
8182

8283
return nil
8384
},

internal/cmd/affinity-groups/delete/delete_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/google/go-cmp/cmp"
88
"github.com/google/go-cmp/cmp/cmpopts"
99
"github.com/google/uuid"
10+
"github.com/stackitcloud/stackit-cli/internal/cmd/params"
1011
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
1112
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
1213
"github.com/stackitcloud/stackit-sdk-go/services/iaas"
@@ -97,7 +98,7 @@ func TestParseInput(t *testing.T) {
9798
for _, tt := range tests {
9899
t.Run(tt.description, func(t *testing.T) {
99100
p := print.NewPrinter()
100-
cmd := NewCmd(p)
101+
cmd := NewCmd(&params.CmdParams{Printer: p})
101102
err := globalflags.Configure(cmd.Flags())
102103
if err != nil {
103104
t.Fatalf("configure global flags: %v", err)

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77

88
"github.com/goccy/go-yaml"
99
"github.com/spf13/cobra"
10+
"github.com/stackitcloud/stackit-cli/internal/cmd/params"
1011
"github.com/stackitcloud/stackit-cli/internal/pkg/args"
1112
"github.com/stackitcloud/stackit-cli/internal/pkg/errors"
1213
"github.com/stackitcloud/stackit-cli/internal/pkg/examples"
@@ -27,7 +28,7 @@ const (
2728
affinityGroupId = "AFFINITY_GROUP_ID"
2829
)
2930

30-
func NewCmd(p *print.Printer) *cobra.Command {
31+
func NewCmd(params *params.CmdParams) *cobra.Command {
3132
cmd := &cobra.Command{
3233
Use: fmt.Sprintf("describe %s", affinityGroupId),
3334
Short: "Show details of an affinity group",
@@ -41,13 +42,13 @@ func NewCmd(p *print.Printer) *cobra.Command {
4142
),
4243
RunE: func(cmd *cobra.Command, args []string) error {
4344
ctx := context.Background()
44-
model, err := parseInput(p, cmd, args)
45+
model, err := parseInput(params.Printer, cmd, args)
4546
if err != nil {
4647
return err
4748
}
4849

4950
// Configure API client
50-
apiClient, err := client.ConfigureClient(p)
51+
apiClient, err := client.ConfigureClient(params.Printer)
5152
if err != nil {
5253
return err
5354
}
@@ -59,7 +60,7 @@ func NewCmd(p *print.Printer) *cobra.Command {
5960
return fmt.Errorf("get affinity group: %w", err)
6061
}
6162

62-
if err := outputResult(p, *model, *result); err != nil {
63+
if err := outputResult(params.Printer, *model, *result); err != nil {
6364
return err
6465
}
6566
return nil

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/google/go-cmp/cmp"
88
"github.com/google/go-cmp/cmp/cmpopts"
99
"github.com/google/uuid"
10+
"github.com/stackitcloud/stackit-cli/internal/cmd/params"
1011
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
1112
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
1213
"github.com/stackitcloud/stackit-sdk-go/services/iaas"
@@ -98,7 +99,7 @@ func TestParseInput(t *testing.T) {
9899
for _, tt := range tests {
99100
t.Run(tt.description, func(t *testing.T) {
100101
p := print.NewPrinter()
101-
cmd := NewCmd(p)
102+
cmd := NewCmd(&params.CmdParams{Printer: p})
102103
err := globalflags.Configure(cmd.Flags())
103104
if err != nil {
104105
t.Fatalf("configure global flags: %v", err)
@@ -191,7 +192,7 @@ func TestOutputResult(t *testing.T) {
191192
},
192193
}
193194
p := print.NewPrinter()
194-
p.Cmd = NewCmd(p)
195+
p.Cmd = NewCmd(&params.CmdParams{Printer: p})
195196

196197
for _, tt := range tests {
197198
t.Run(tt.description, func(t *testing.T) {

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77

88
"github.com/goccy/go-yaml"
9+
"github.com/stackitcloud/stackit-cli/internal/cmd/params"
910
"github.com/stackitcloud/stackit-cli/internal/pkg/tables"
1011
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
1112

@@ -27,7 +28,7 @@ type inputModel struct {
2728

2829
const limitFlag = "limit"
2930

30-
func NewCmd(p *print.Printer) *cobra.Command {
31+
func NewCmd(params *params.CmdParams) *cobra.Command {
3132
cmd := &cobra.Command{
3233
Use: "list",
3334
Short: "Lists affinity groups",
@@ -45,13 +46,13 @@ func NewCmd(p *print.Printer) *cobra.Command {
4546
),
4647
RunE: func(cmd *cobra.Command, _ []string) error {
4748
ctx := context.Background()
48-
model, err := parseInput(p, cmd)
49+
model, err := parseInput(params.Printer, cmd)
4950
if err != nil {
5051
return err
5152
}
5253

5354
// Configure API client
54-
apiClient, err := client.ConfigureClient(p)
55+
apiClient, err := client.ConfigureClient(params.Printer)
5556
if err != nil {
5657
return err
5758
}
@@ -67,10 +68,10 @@ func NewCmd(p *print.Printer) *cobra.Command {
6768
if model.Limit != nil && len(*items) > int(*model.Limit) {
6869
*items = (*items)[:*model.Limit]
6970
}
70-
return outputResult(p, *model, *items)
71+
return outputResult(params.Printer, *model, *items)
7172
}
7273

73-
p.Outputln("No affinity groups found")
74+
params.Printer.Outputln("No affinity groups found")
7475
return nil
7576
},
7677
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/google/go-cmp/cmp"
99
"github.com/google/go-cmp/cmp/cmpopts"
1010
"github.com/google/uuid"
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-cli/internal/pkg/utils"
@@ -105,7 +106,7 @@ func TestParseInput(t *testing.T) {
105106
for _, tt := range tests {
106107
t.Run(tt.description, func(t *testing.T) {
107108
p := print.NewPrinter()
108-
cmd := NewCmd(p)
109+
cmd := NewCmd(&params.CmdParams{Printer: p})
109110
if err := globalflags.Configure(cmd.Flags()); err != nil {
110111
t.Fatalf("configure global flags: %v", err)
111112
}
@@ -186,7 +187,7 @@ func TestOutputResult(t *testing.T) {
186187
},
187188
}
188189
p := print.NewPrinter()
189-
p.Cmd = NewCmd(p)
190+
p.Cmd = NewCmd(&params.CmdParams{Printer: p})
190191
for _, tt := range tests {
191192
t.Run(tt.description, func(t *testing.T) {
192193
err := outputResult(p, tt.model, tt.response)

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66

77
"github.com/spf13/viper"
8+
"github.com/stackitcloud/stackit-cli/internal/cmd/params"
89
"github.com/stackitcloud/stackit-cli/internal/pkg/args"
910
"github.com/stackitcloud/stackit-cli/internal/pkg/auth"
1011
"github.com/stackitcloud/stackit-cli/internal/pkg/config"
@@ -32,7 +33,7 @@ type inputModel struct {
3233
OnlyPrintAccessToken bool
3334
}
3435

35-
func NewCmd(p *print.Printer) *cobra.Command {
36+
func NewCmd(params *params.CmdParams) *cobra.Command {
3637
cmd := &cobra.Command{
3738
Use: "activate-service-account",
3839
Short: "Authenticates using a service account",
@@ -58,7 +59,7 @@ func NewCmd(p *print.Printer) *cobra.Command {
5859
),
5960
),
6061
RunE: func(cmd *cobra.Command, _ []string) error {
61-
model := parseInput(p, cmd)
62+
model := parseInput(params.Printer, cmd)
6263

6364
tokenCustomEndpoint := viper.GetString(config.TokenCustomEndpointKey)
6465
if !model.OnlyPrintAccessToken {
@@ -78,12 +79,12 @@ func NewCmd(p *print.Printer) *cobra.Command {
7879
// Initializes the authentication flow
7980
rt, err := sdkAuth.SetupAuth(cfg)
8081
if err != nil {
81-
p.Debug(print.ErrorLevel, "setup auth: %v", err)
82+
params.Printer.Debug(print.ErrorLevel, "setup auth: %v", err)
8283
return &cliErr.ActivateServiceAccountError{}
8384
}
8485

8586
// Authenticates the service account and stores credentials
86-
email, accessToken, err := auth.AuthenticateServiceAccount(p, rt, model.OnlyPrintAccessToken)
87+
email, accessToken, err := auth.AuthenticateServiceAccount(params.Printer, rt, model.OnlyPrintAccessToken)
8788
if err != nil {
8889
var activateServiceAccountError *cliErr.ActivateServiceAccountError
8990
if !errors.As(err, &activateServiceAccountError) {
@@ -94,9 +95,9 @@ func NewCmd(p *print.Printer) *cobra.Command {
9495

9596
if model.OnlyPrintAccessToken {
9697
// Only output is the access token
97-
p.Outputf("%s\n", accessToken)
98+
params.Printer.Outputf("%s\n", accessToken)
9899
} else {
99-
p.Outputf("You have been successfully authenticated to the STACKIT CLI!\nService account email: %s\n", email)
100+
params.Printer.Outputf("You have been successfully authenticated to the STACKIT CLI!\nService account email: %s\n", email)
100101
}
101102
return nil
102103
},

0 commit comments

Comments
 (0)