diff --git a/.github/docs/contribution-guide/client.go b/.github/docs/contribution-guide/client.go new file mode 100644 index 000000000..4b84a6538 --- /dev/null +++ b/.github/docs/contribution-guide/client.go @@ -0,0 +1,35 @@ +package client + +import ( + "github.com/spf13/cobra" + "github.com/spf13/viper" + "github.com/stackitcloud/stackit-cli/internal/pkg/auth" + "github.com/stackitcloud/stackit-cli/internal/pkg/errors" + "github.com/stackitcloud/stackit-sdk-go/services/foo" + // (...) +) + +func ConfigureClient(cmd *cobra.Command) (*foo.APIClient, error) { + var err error + var apiClient foo.APIClient + var cfgOptions []sdkConfig.ConfigurationOption + + authCfgOption, err := auth.AuthenticationConfig(cmd, auth.AuthorizeUser) + if err != nil { + return nil, &errors.AuthError{} + } + cfgOptions = append(cfgOptions, authCfgOption, sdkConfig.WithRegion("eu01")) // Configuring region is needed if "foo" is a regional API + + customEndpoint := viper.GetString(config.fooCustomEndpointKey) + + if customEndpoint != "" { + cfgOptions = append(cfgOptions, sdkConfig.WithEndpoint(customEndpoint)) + } + + apiClient, err = foo.NewAPIClient(cfgOptions...) + if err != nil { + return nil, &errors.AuthError{} + } + + return apiClient, nil +} diff --git a/.github/docs/contribution-guide/cmd.go b/.github/docs/contribution-guide/cmd.go new file mode 100644 index 000000000..5fb05d2f4 --- /dev/null +++ b/.github/docs/contribution-guide/cmd.go @@ -0,0 +1,156 @@ +package bar + +import ( + "context" + "encoding/json" + "fmt" + + "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" + "github.com/stackitcloud/stackit-cli/internal/pkg/args" + "github.com/stackitcloud/stackit-cli/internal/pkg/errors" + "github.com/stackitcloud/stackit-cli/internal/pkg/examples" + "github.com/stackitcloud/stackit-cli/internal/pkg/flags" + "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" + "github.com/stackitcloud/stackit-cli/internal/pkg/projectname" + "github.com/stackitcloud/stackit-cli/internal/pkg/services/alb/client" + "github.com/stackitcloud/stackit-cli/internal/pkg/tables" + "github.com/stackitcloud/stackit-cli/internal/pkg/utils" + "gopkg.in/yaml.v2" + // (...) +) + +// Define consts for command flags +const ( + someArg = "MY_ARG" + someFlag = "my-flag" +) + +// Struct to model user input (arguments and/or flags) +type inputModel struct { + *globalflags.GlobalFlagModel + MyArg string + MyFlag *string +} + +// "bar" command constructor +func NewCmd(params *params.CmdParams) *cobra.Command { + cmd := &cobra.Command{ + Use: "bar", + Short: "Short description of the command (is shown in the help of parent command)", + Long: "Long description of the command. Can contain some more information about the command usage. It is shown in the help of the current command.", + Args: args.SingleArg(someArg, utils.ValidateUUID), // Validate argument, with an optional validation function + Example: examples.Build( + examples.NewExample( + `Do something with command "bar"`, + "$ stackit foo bar arg-value --my-flag flag-value"), + //... + ), + RunE: func(cmd *cobra.Command, args []string) error { + ctx := context.Background() + model, err := parseInput(params.Printer, cmd, args) + if err != nil { + return err + } + + // Configure API client + apiClient, err := client.ConfigureClient(params.Printer, cmd) + if err != nil { + return err + } + + // Call API + req := buildRequest(ctx, model, apiClient) + resp, err := req.Execute() + if err != nil { + return fmt.Errorf("(...): %w", err) + } + + projectLabel, err := projectname.GetProjectName(ctx, params.Printer, cmd) + if err != nil { + projectLabel = model.ProjectId + } + + // Check API response "resp" and output accordingly + if resp.Item == nil { + params.Printer.Info("(...)", projectLabel) + return nil + } + return outputResult(params.Printer, cmd, model.OutputFormat, instances) + }, + } + + configureFlags(cmd) + return cmd +} + +// Configure command flags (type, default value, and description) +func configureFlags(cmd *cobra.Command) { + cmd.Flags().StringP(myFlag, "defaultValue", "My flag description") +} + +// Parse user input (arguments and/or flags) +func parseInput(p *print.Printer, cmd *cobra.Command, inputArgs []string) (*inputModel, error) { + myArg := inputArgs[0] + + globalFlags := globalflags.Parse(cmd) + if globalFlags.ProjectId == "" { + return nil, &errors.ProjectIdError{} + } + + model := inputModel{ + GlobalFlagModel: globalFlags, + MyArg: myArg, + MyFlag: flags.FlagToStringPointer(cmd, myFlag), + } + + // Write the input model to the debug logs + if p.IsVerbosityDebug() { + modelStr, err := print.BuildDebugStrFromInputModel(model) + if err != nil { + p.Debug(print.ErrorLevel, "convert model to string for debugging: %v", err) + } else { + p.Debug(print.DebugLevel, "parsed input values: %s", modelStr) + } + } + + return &model, nil +} + +// Build request to the API +func buildRequest(ctx context.Context, model *inputModel, apiClient *foo.APIClient) foo.ApiListInstancesRequest { + req := apiClient.GetBar(ctx, model.ProjectId, model.MyArg, someParam) + return req +} + +// Output result based on the configured output format +func outputResult(p *print.Printer, cmd *cobra.Command, outputFormat string, resources []foo.Resource) error { + switch outputFormat { + case print.JSONOutputFormat: + details, err := json.MarshalIndent(resources, "", " ") + if err != nil { + return fmt.Errorf("marshal resource list: %w", err) + } + p.Outputln(string(details)) + return nil + case print.YAMLOutputFormat: + details, err := yaml.Marshal(resources) + if err != nil { + return fmt.Errorf("marshal resource list: %w", err) + } + p.Outputln(string(details)) + return nil + default: + table := tables.NewTable() + table.SetHeader("ID", "NAME", "STATE") + for i := range resources { + resource := resources[i] + table.AddRow(*resource.ResourceId, *resource.Name, *resource.State) + } + err := table.Display(cmd) + if err != nil { + return fmt.Errorf("render table: %w", err) + } + return nil + } +} diff --git a/CONTRIBUTION.md b/CONTRIBUTION.md index a73808087..d182c6534 100644 --- a/CONTRIBUTION.md +++ b/CONTRIBUTION.md @@ -53,148 +53,7 @@ Please remember to run `make generate-docs` after your changes to keep the comma Below is a typical structure of a CLI command: -```go -package bar - -import ( - (...) -) - -// Define consts for command flags -const ( - someArg = "MY_ARG" - someFlag = "my-flag" -) - -// Struct to model user input (arguments and/or flags) -type inputModel struct { - *globalflags.GlobalFlagModel - MyArg string - MyFlag *string -} - -// "bar" command constructor -func NewCmd(p *print.Printer) *cobra.Command { - cmd := &cobra.Command{ - Use: "bar", - Short: "Short description of the command (is shown in the help of parent command)", - Long: "Long description of the command. Can contain some more information about the command usage. It is shown in the help of the current command.", - Args: args.SingleArg(someArg, utils.ValidateUUID), // Validate argument, with an optional validation function - Example: examples.Build( - examples.NewExample( - `Do something with command "bar"`, - "$ stackit foo bar arg-value --my-flag flag-value"), - ... - ), - RunE: func(cmd *cobra.Command, args []string) error { - ctx := context.Background() - model, err := parseInput(p, cmd, args) - if err != nil { - return err - } - - // Configure API client - apiClient, err := client.ConfigureClient(p, cmd) - if err != nil { - return err - } - - // Call API - req := buildRequest(ctx, model, apiClient) - resp, err := req.Execute() - if err != nil { - return fmt.Errorf("(...): %w", err) - } - - projectLabel, err := projectname.GetProjectName(ctx, p, cmd) - if err != nil { - projectLabel = model.ProjectId - } - - // Check API response "resp" and output accordingly - if resp.Item == nil { - p.Info("(...)", projectLabel) - return nil - } - return outputResult(p, cmd, model.OutputFormat, instances) - }, - } - - configureFlags(cmd) - return cmd -} - -// Configure command flags (type, default value, and description) -func configureFlags(cmd *cobra.Command) { - cmd.Flags().StringP(myFlag, "defaultValue", "My flag description") -} - -// Parse user input (arguments and/or flags) -func parseInput(cmd *cobra.Command, inputArgs []string) (*inputModel, error) { - myArg := inputArgs[0] - - globalFlags := globalflags.Parse(cmd) - if globalFlags.ProjectId == "" { - return nil, &errors.ProjectIdError{} - } - - model := inputModel{ - GlobalFlagModel: globalFlags, - MyArg myArg, - MyFlag: flags.FlagToStringPointer(cmd, myFlag), - }, nil - - // Write the input model to the debug logs - if p.IsVerbosityDebug() { - modelStr, err := print.BuildDebugStrFromInputModel(model) - if err != nil { - p.Debug(print.ErrorLevel, "convert model to string for debugging: %v", err) - } else { - p.Debug(print.DebugLevel, "parsed input values: %s", modelStr) - } - } - - return &model, nil -} - -// Build request to the API -func buildRequest(ctx context.Context, model *inputModel, apiClient *foo.APIClient) foo.ApiListInstancesRequest { - req := apiClient.GetBar(ctx, model.ProjectId, model.MyArg, someParam) - return req -} - -// Output result based on the configured output format -func outputResult(p *print.Printer, cmd *cobra.Command, outputFormat string, resources []foo.Resource) error { - switch outputFormat { - case print.JSONOutputFormat: - details, err := json.MarshalIndent(resources, "", " ") - if err != nil { - return fmt.Errorf("marshal resource list: %w", err) - } - p.Outputln(string(details)) - return nil - case print.YAMLOutputFormat: - details, err := yaml.Marshal(resources) - if err != nil { - return fmt.Errorf("marshal resource list: %w", err) - } - p.Outputln(string(details)) - return nil - default: - table := tables.NewTable() - table.SetHeader("ID", "NAME", "STATE") - for i := range resources { - resource := resources[i] - table.AddRow(*resource.ResourceId, *resource.Name, *resource.State) - } - err := table.Display(cmd) - if err != nil { - return fmt.Errorf("render table: %w", err) - } - return nil - } -} -``` +https://github.com/stackitcloud/stackit-cli/blob/6f762bd56407ed232080efabc4d2bf87f260b71d/.github/docs/contribution-guide/cmd.go#L23-L156 Please remember to always add unit tests for `parseInput`, `buildRequest` (in `bar_test.go`), and any other util functions used. @@ -224,39 +83,7 @@ If you want to add a command that uses a STACKIT service `foo` that was not yet 1. This is done in `internal/pkg/services/foo/client/client.go` 2. Below is an example of a typical `client.go` file structure: - ```go - package client - - import ( - (...) - "github.com/stackitcloud/stackit-sdk-go/services/foo" - ) - - func ConfigureClient(cmd *cobra.Command) (*foo.APIClient, error) { - var err error - var apiClient foo.APIClient - var cfgOptions []sdkConfig.ConfigurationOption - - authCfgOption, err := auth.AuthenticationConfig(cmd, auth.AuthorizeUser) - if err != nil { - return nil, &errors.AuthError{} - } - cfgOptions = append(cfgOptions, authCfgOption, sdkConfig.WithRegion("eu01")) // Configuring region is needed if "foo" is a regional API - - customEndpoint := viper.GetString(config.fooCustomEndpointKey) - - if customEndpoint != "" { - cfgOptions = append(cfgOptions, sdkConfig.WithEndpoint(customEndpoint)) - } - - apiClient, err = foo.NewAPIClient(cfgOptions...) - if err != nil { - return nil, &errors.AuthError{} - } - - return apiClient, nil - } - ``` +https://github.com/stackitcloud/stackit-cli/blob/6f762bd56407ed232080efabc4d2bf87f260b71d/.github/docs/contribution-guide/client.go#L12-L35 ### Local development diff --git a/internal/cmd/affinity-groups/affinity-groups.go b/internal/cmd/affinity-groups/affinity-groups.go index 57f44f65e..a750b2047 100644 --- a/internal/cmd/affinity-groups/affinity-groups.go +++ b/internal/cmd/affinity-groups/affinity-groups.go @@ -6,12 +6,12 @@ import ( "github.com/stackitcloud/stackit-cli/internal/cmd/affinity-groups/delete" "github.com/stackitcloud/stackit-cli/internal/cmd/affinity-groups/describe" "github.com/stackitcloud/stackit-cli/internal/cmd/affinity-groups/list" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" - "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" ) -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "affinity-group", Short: "Manage server affinity groups", @@ -19,15 +19,15 @@ func NewCmd(p *print.Printer) *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd, p) + addSubcommands(cmd, params) return cmd } -func addSubcommands(cmd *cobra.Command, p *print.Printer) { +func addSubcommands(cmd *cobra.Command, params *params.CmdParams) { cmd.AddCommand( - create.NewCmd(p), - delete.NewCmd(p), - describe.NewCmd(p), - list.NewCmd(p), + create.NewCmd(params), + delete.NewCmd(params), + describe.NewCmd(params), + list.NewCmd(params), ) } diff --git a/internal/cmd/affinity-groups/create/create.go b/internal/cmd/affinity-groups/create/create.go index ab1f48d3a..fc8482ea8 100644 --- a/internal/cmd/affinity-groups/create/create.go +++ b/internal/cmd/affinity-groups/create/create.go @@ -7,6 +7,7 @@ import ( "github.com/goccy/go-yaml" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -29,7 +30,7 @@ type inputModel struct { Policy string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "create", Short: "Creates an affinity groups", @@ -43,20 +44,20 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to create the affinity group %q?", model.Name) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -70,7 +71,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("create affinity group: %w", err) } if resp := result; resp != nil { - return outputResult(p, *model, *resp) + return outputResult(params.Printer, *model, *resp) } return fmt.Errorf("create affinity group: nil result") }, diff --git a/internal/cmd/affinity-groups/create/create_test.go b/internal/cmd/affinity-groups/create/create_test.go index 3ab7db59f..0f71d31db 100644 --- a/internal/cmd/affinity-groups/create/create_test.go +++ b/internal/cmd/affinity-groups/create/create_test.go @@ -7,6 +7,7 @@ import ( "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" "github.com/google/uuid" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -120,7 +121,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) if err := globalflags.Configure(cmd.Flags()); err != nil { t.Fatalf("configure global flags: %v", err) } @@ -212,7 +213,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { err := outputResult(p, tt.model, tt.response) diff --git a/internal/cmd/affinity-groups/delete/delete.go b/internal/cmd/affinity-groups/delete/delete.go index 195b4539d..f36371648 100644 --- a/internal/cmd/affinity-groups/delete/delete.go +++ b/internal/cmd/affinity-groups/delete/delete.go @@ -5,6 +5,7 @@ import ( "fmt" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -26,7 +27,7 @@ const ( affinityGroupIdArg = "AFFINITY_GROUP" ) -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("delete %s", affinityGroupIdArg), Short: "Deletes an affinity group", @@ -40,32 +41,32 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } - projectLabel, err := projectname.GetProjectName(ctx, p, cmd) + projectLabel, err := projectname.GetProjectName(ctx, params.Printer, cmd) if err != nil { - p.Debug(print.ErrorLevel, "get project name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get project name: %v", err) projectLabel = model.ProjectId } affinityGroupLabel, err := iaasUtils.GetAffinityGroupName(ctx, apiClient, model.ProjectId, model.AffinityGroupId) if err != nil { - p.Debug(print.ErrorLevel, "get affinity group name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get affinity group name: %v", err) affinityGroupLabel = model.AffinityGroupId } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to delete affinity group %q?", affinityGroupLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -77,7 +78,7 @@ func NewCmd(p *print.Printer) *cobra.Command { if err != nil { return fmt.Errorf("delete affinity group: %w", err) } - p.Info("Deleted affinity group %q for %q\n", affinityGroupLabel, projectLabel) + params.Printer.Info("Deleted affinity group %q for %q\n", affinityGroupLabel, projectLabel) return nil }, diff --git a/internal/cmd/affinity-groups/delete/delete_test.go b/internal/cmd/affinity-groups/delete/delete_test.go index 71f2b84a7..e3df49cbb 100644 --- a/internal/cmd/affinity-groups/delete/delete_test.go +++ b/internal/cmd/affinity-groups/delete/delete_test.go @@ -7,6 +7,7 @@ import ( "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" "github.com/google/uuid" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-sdk-go/services/iaas" @@ -97,7 +98,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/affinity-groups/describe/describe.go b/internal/cmd/affinity-groups/describe/describe.go index 410937fb1..566e42d5e 100644 --- a/internal/cmd/affinity-groups/describe/describe.go +++ b/internal/cmd/affinity-groups/describe/describe.go @@ -7,6 +7,7 @@ import ( "github.com/goccy/go-yaml" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -27,7 +28,7 @@ const ( affinityGroupId = "AFFINITY_GROUP_ID" ) -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("describe %s", affinityGroupId), Short: "Show details of an affinity group", @@ -41,13 +42,13 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -59,7 +60,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("get affinity group: %w", err) } - if err := outputResult(p, *model, *result); err != nil { + if err := outputResult(params.Printer, *model, *result); err != nil { return err } return nil diff --git a/internal/cmd/affinity-groups/describe/describe_test.go b/internal/cmd/affinity-groups/describe/describe_test.go index 1d8a1f23b..530319c96 100644 --- a/internal/cmd/affinity-groups/describe/describe_test.go +++ b/internal/cmd/affinity-groups/describe/describe_test.go @@ -7,6 +7,7 @@ import ( "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" "github.com/google/uuid" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-sdk-go/services/iaas" @@ -98,7 +99,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -191,7 +192,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { diff --git a/internal/cmd/affinity-groups/list/list.go b/internal/cmd/affinity-groups/list/list.go index 3c270bcdb..cb06a6612 100644 --- a/internal/cmd/affinity-groups/list/list.go +++ b/internal/cmd/affinity-groups/list/list.go @@ -6,6 +6,7 @@ import ( "fmt" "github.com/goccy/go-yaml" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/tables" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -27,7 +28,7 @@ type inputModel struct { const limitFlag = "limit" -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "list", Short: "Lists affinity groups", @@ -45,13 +46,13 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -67,10 +68,10 @@ func NewCmd(p *print.Printer) *cobra.Command { if model.Limit != nil && len(*items) > int(*model.Limit) { *items = (*items)[:*model.Limit] } - return outputResult(p, *model, *items) + return outputResult(params.Printer, *model, *items) } - p.Outputln("No affinity groups found") + params.Printer.Outputln("No affinity groups found") return nil }, } diff --git a/internal/cmd/affinity-groups/list/list_test.go b/internal/cmd/affinity-groups/list/list_test.go index da35b5778..b44af626c 100644 --- a/internal/cmd/affinity-groups/list/list_test.go +++ b/internal/cmd/affinity-groups/list/list_test.go @@ -8,6 +8,7 @@ import ( "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" "github.com/google/uuid" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -105,7 +106,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) if err := globalflags.Configure(cmd.Flags()); err != nil { t.Fatalf("configure global flags: %v", err) } @@ -186,7 +187,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { err := outputResult(p, tt.model, tt.response) diff --git a/internal/cmd/auth/activate-service-account/activate_service_account.go b/internal/cmd/auth/activate-service-account/activate_service_account.go index 46922720a..5ddc73f29 100644 --- a/internal/cmd/auth/activate-service-account/activate_service_account.go +++ b/internal/cmd/auth/activate-service-account/activate_service_account.go @@ -5,6 +5,7 @@ import ( "fmt" "github.com/spf13/viper" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/auth" "github.com/stackitcloud/stackit-cli/internal/pkg/config" @@ -32,7 +33,7 @@ type inputModel struct { OnlyPrintAccessToken bool } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "activate-service-account", Short: "Authenticates using a service account", @@ -58,7 +59,7 @@ func NewCmd(p *print.Printer) *cobra.Command { ), ), RunE: func(cmd *cobra.Command, _ []string) error { - model := parseInput(p, cmd) + model := parseInput(params.Printer, cmd) tokenCustomEndpoint := viper.GetString(config.TokenCustomEndpointKey) if !model.OnlyPrintAccessToken { @@ -78,12 +79,12 @@ func NewCmd(p *print.Printer) *cobra.Command { // Initializes the authentication flow rt, err := sdkAuth.SetupAuth(cfg) if err != nil { - p.Debug(print.ErrorLevel, "setup auth: %v", err) + params.Printer.Debug(print.ErrorLevel, "setup auth: %v", err) return &cliErr.ActivateServiceAccountError{} } // Authenticates the service account and stores credentials - email, accessToken, err := auth.AuthenticateServiceAccount(p, rt, model.OnlyPrintAccessToken) + email, accessToken, err := auth.AuthenticateServiceAccount(params.Printer, rt, model.OnlyPrintAccessToken) if err != nil { var activateServiceAccountError *cliErr.ActivateServiceAccountError if !errors.As(err, &activateServiceAccountError) { @@ -94,9 +95,9 @@ func NewCmd(p *print.Printer) *cobra.Command { if model.OnlyPrintAccessToken { // Only output is the access token - p.Outputf("%s\n", accessToken) + params.Printer.Outputf("%s\n", accessToken) } else { - p.Outputf("You have been successfully authenticated to the STACKIT CLI!\nService account email: %s\n", email) + params.Printer.Outputf("You have been successfully authenticated to the STACKIT CLI!\nService account email: %s\n", email) } return nil }, diff --git a/internal/cmd/auth/activate-service-account/activate_service_account_test.go b/internal/cmd/auth/activate-service-account/activate_service_account_test.go index 9dcbb22b7..84532195c 100644 --- a/internal/cmd/auth/activate-service-account/activate_service_account_test.go +++ b/internal/cmd/auth/activate-service-account/activate_service_account_test.go @@ -4,6 +4,7 @@ import ( "testing" "github.com/spf13/viper" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/auth" "github.com/stackitcloud/stackit-cli/internal/pkg/config" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" @@ -106,7 +107,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/auth/auth.go b/internal/cmd/auth/auth.go index 2a8b3a7f2..7e1c020cf 100644 --- a/internal/cmd/auth/auth.go +++ b/internal/cmd/auth/auth.go @@ -5,14 +5,14 @@ import ( getaccesstoken "github.com/stackitcloud/stackit-cli/internal/cmd/auth/get-access-token" "github.com/stackitcloud/stackit-cli/internal/cmd/auth/login" "github.com/stackitcloud/stackit-cli/internal/cmd/auth/logout" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" - "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" "github.com/spf13/cobra" ) -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "auth", Short: "Authenticates the STACKIT CLI", @@ -20,13 +20,13 @@ func NewCmd(p *print.Printer) *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd, p) + addSubcommands(cmd, params) return cmd } -func addSubcommands(cmd *cobra.Command, p *print.Printer) { - cmd.AddCommand(login.NewCmd(p)) - cmd.AddCommand(logout.NewCmd(p)) - cmd.AddCommand(activateserviceaccount.NewCmd(p)) - cmd.AddCommand(getaccesstoken.NewCmd(p)) +func addSubcommands(cmd *cobra.Command, params *params.CmdParams) { + cmd.AddCommand(login.NewCmd(params)) + cmd.AddCommand(logout.NewCmd(params)) + cmd.AddCommand(activateserviceaccount.NewCmd(params)) + cmd.AddCommand(getaccesstoken.NewCmd(params)) } diff --git a/internal/cmd/auth/get-access-token/get_access_token.go b/internal/cmd/auth/get-access-token/get_access_token.go index 85fc9247c..0b13fd2fa 100644 --- a/internal/cmd/auth/get-access-token/get_access_token.go +++ b/internal/cmd/auth/get-access-token/get_access_token.go @@ -2,14 +2,14 @@ package getaccesstoken import ( "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/auth" cliErr "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" - "github.com/stackitcloud/stackit-cli/internal/pkg/print" ) -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "get-access-token", Short: "Prints a short-lived access token.", @@ -42,7 +42,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return &cliErr.AccessTokenExpiredError{} } - p.Outputf("%s\n", accessToken) + params.Printer.Outputf("%s\n", accessToken) return nil }, } diff --git a/internal/cmd/auth/login/login.go b/internal/cmd/auth/login/login.go index 06531f706..8740fead7 100644 --- a/internal/cmd/auth/login/login.go +++ b/internal/cmd/auth/login/login.go @@ -3,15 +3,15 @@ package login import ( "fmt" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/auth" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" - "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/spf13/cobra" ) -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "login", Short: "Logs in to the STACKIT CLI", @@ -25,12 +25,12 @@ func NewCmd(p *print.Printer) *cobra.Command { "$ stackit auth login"), ), RunE: func(_ *cobra.Command, _ []string) error { - err := auth.AuthorizeUser(p, false) + err := auth.AuthorizeUser(params.Printer, false) if err != nil { return fmt.Errorf("authorization failed: %w", err) } - p.Outputln("Successfully logged into STACKIT CLI.\n") + params.Printer.Outputln("Successfully logged into STACKIT CLI.\n") return nil }, diff --git a/internal/cmd/auth/logout/logout.go b/internal/cmd/auth/logout/logout.go index adc6f7069..4b318b2e7 100644 --- a/internal/cmd/auth/logout/logout.go +++ b/internal/cmd/auth/logout/logout.go @@ -3,15 +3,15 @@ package logout import ( "fmt" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/auth" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" - "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/spf13/cobra" ) -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "logout", Short: "Logs the user account out of the STACKIT CLI", @@ -28,7 +28,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("log out failed: %w", err) } - p.Info("Successfully logged out of the STACKIT CLI.\n") + params.Printer.Info("Successfully logged out of the STACKIT CLI.\n") return nil }, } diff --git a/internal/cmd/beta/alb/alb.go b/internal/cmd/beta/alb/alb.go index 94f082da8..fde1da311 100644 --- a/internal/cmd/beta/alb/alb.go +++ b/internal/cmd/beta/alb/alb.go @@ -11,15 +11,15 @@ import ( "github.com/stackitcloud/stackit-cli/internal/cmd/beta/alb/quotas" "github.com/stackitcloud/stackit-cli/internal/cmd/beta/alb/template" "github.com/stackitcloud/stackit-cli/internal/cmd/beta/alb/update" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" - "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" "github.com/spf13/cobra" ) -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "alb", Short: "Manages application loadbalancers", @@ -27,21 +27,21 @@ func NewCmd(p *print.Printer) *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd, p) + addSubcommands(cmd, params) return cmd } -func addSubcommands(cmd *cobra.Command, p *print.Printer) { +func addSubcommands(cmd *cobra.Command, params *params.CmdParams) { cmd.AddCommand( - list.NewCmd(p), - template.NewCmd(p), - create.NewCmd(p), - update.NewCmd(p), - observabilitycredentials.NewCmd(p), - describe.NewCmd(p), - delete.NewCmd(p), - pool.NewCmd(p), - plans.NewCmd(p), - quotas.NewCmd(p), + list.NewCmd(params), + template.NewCmd(params), + create.NewCmd(params), + update.NewCmd(params), + observabilitycredentials.NewCmd(params), + describe.NewCmd(params), + delete.NewCmd(params), + pool.NewCmd(params), + plans.NewCmd(params), + quotas.NewCmd(params), ) } diff --git a/internal/cmd/beta/alb/create/create.go b/internal/cmd/beta/alb/create/create.go index 462d4e381..4d5abe3c1 100644 --- a/internal/cmd/beta/alb/create/create.go +++ b/internal/cmd/beta/alb/create/create.go @@ -9,6 +9,7 @@ import ( "strings" "github.com/goccy/go-yaml" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -34,7 +35,7 @@ type inputModel struct { Configuration *string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "create", Short: "Creates an application loadbalancer", @@ -47,26 +48,26 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } - projectLabel, err := projectname.GetProjectName(ctx, p, cmd) + projectLabel, err := projectname.GetProjectName(ctx, params.Printer, cmd) if err != nil { - p.Debug(print.ErrorLevel, "get project name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get project name: %v", err) projectLabel = model.ProjectId } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to create an application loadbalancer for project %q?", projectLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -84,7 +85,7 @@ func NewCmd(p *print.Printer) *cobra.Command { // Wait for async operation, if async mode not enabled if !model.Async { - s := spinner.New(p) + s := spinner.New(params.Printer) s.Start("Creating loadbalancer") _, err = wait.CreateOrUpdateLoadbalancerWaitHandler(ctx, apiClient, model.ProjectId, model.Region, *resp.Name).WaitWithContext(ctx) if err != nil { @@ -93,7 +94,7 @@ func NewCmd(p *print.Printer) *cobra.Command { s.Stop() } - return outputResult(p, model, projectLabel, resp) + return outputResult(params.Printer, model, projectLabel, resp) }, } configureFlags(cmd) diff --git a/internal/cmd/beta/alb/create/create_test.go b/internal/cmd/beta/alb/create/create_test.go index d77fb2022..82517b0f5 100644 --- a/internal/cmd/beta/alb/create/create_test.go +++ b/internal/cmd/beta/alb/create/create_test.go @@ -10,6 +10,7 @@ import ( "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" "github.com/google/uuid" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -134,7 +135,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -248,7 +249,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.model, tt.args.projectLabel, tt.args.resp); (err != nil) != tt.wantErr { diff --git a/internal/cmd/beta/alb/delete/delete.go b/internal/cmd/beta/alb/delete/delete.go index c3709e8a9..0c443d593 100644 --- a/internal/cmd/beta/alb/delete/delete.go +++ b/internal/cmd/beta/alb/delete/delete.go @@ -4,6 +4,7 @@ import ( "context" "fmt" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" @@ -24,7 +25,7 @@ type inputModel struct { Name string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("delete %s", loadbalancerNameArg), Short: "Deletes an application loadbalancer", @@ -38,26 +39,26 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } - projectLabel, err := projectname.GetProjectName(ctx, p, cmd) + projectLabel, err := projectname.GetProjectName(ctx, params.Printer, cmd) if err != nil { - p.Debug(print.ErrorLevel, "get project name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get project name: %v", err) projectLabel = model.ProjectId } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to delete the application loadbalancer %q for project %q?", model.Name, projectLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -70,7 +71,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("delete loadbalancer: %w", err) } - p.Outputf("Load balancer %q deleted.", model.Name) + params.Printer.Outputf("Load balancer %q deleted.", model.Name) return nil }, } diff --git a/internal/cmd/beta/alb/delete/delete_test.go b/internal/cmd/beta/alb/delete/delete_test.go index 3bc65bfd1..ce6808fbd 100644 --- a/internal/cmd/beta/alb/delete/delete_test.go +++ b/internal/cmd/beta/alb/delete/delete_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" @@ -112,7 +113,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/beta/alb/describe/describe.go b/internal/cmd/beta/alb/describe/describe.go index 8a1403585..db4418ddf 100644 --- a/internal/cmd/beta/alb/describe/describe.go +++ b/internal/cmd/beta/alb/describe/describe.go @@ -6,6 +6,7 @@ import ( "fmt" "strings" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" "github.com/stackitcloud/stackit-cli/internal/pkg/args" @@ -29,7 +30,7 @@ type inputModel struct { Name string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("describe %s", loadbalancerNameArg), Short: "Describes an application loadbalancer", @@ -43,13 +44,13 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -62,9 +63,9 @@ func NewCmd(p *print.Printer) *cobra.Command { } if loadbalancer := resp; loadbalancer != nil { - return outputResult(p, model.OutputFormat, loadbalancer) + return outputResult(params.Printer, model.OutputFormat, loadbalancer) } - p.Outputln("No load balancer found.") + params.Printer.Outputln("No load balancer found.") return nil }, } diff --git a/internal/cmd/beta/alb/describe/describe_test.go b/internal/cmd/beta/alb/describe/describe_test.go index 0f621c72e..4d1195313 100644 --- a/internal/cmd/beta/alb/describe/describe_test.go +++ b/internal/cmd/beta/alb/describe/describe_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" @@ -112,7 +113,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -213,7 +214,7 @@ func Test_outputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.response); (err != nil) != tt.wantErr { diff --git a/internal/cmd/beta/alb/list/list.go b/internal/cmd/beta/alb/list/list.go index 4050e8791..cf1d478c5 100644 --- a/internal/cmd/beta/alb/list/list.go +++ b/internal/cmd/beta/alb/list/list.go @@ -7,6 +7,7 @@ import ( "github.com/goccy/go-yaml" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -30,7 +31,7 @@ const ( limitFlag = "limit" ) -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "list", Short: "Lists albs", @@ -48,20 +49,20 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } - projectLabel, err := projectname.GetProjectName(ctx, p, cmd) + projectLabel, err := projectname.GetProjectName(ctx, params.Printer, cmd) if err != nil { - p.Debug(print.ErrorLevel, "get project name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get project name: %v", err) projectLabel = model.ProjectId } else if projectLabel == "" { projectLabel = model.ProjectId @@ -76,12 +77,12 @@ func NewCmd(p *print.Printer) *cobra.Command { } if items := response.LoadBalancers; items == nil || len(*items) == 0 { - p.Info("No load balancers found for project %q", projectLabel) + params.Printer.Info("No load balancers found for project %q", projectLabel) } else { if model.Limit != nil && len(*items) > int(*model.Limit) { *items = (*items)[:*model.Limit] } - if err := outputResult(p, model.OutputFormat, *items); err != nil { + if err := outputResult(params.Printer, model.OutputFormat, *items); err != nil { return fmt.Errorf("output loadbalancers: %w", err) } } diff --git a/internal/cmd/beta/alb/list/list_test.go b/internal/cmd/beta/alb/list/list_test.go index 77c915266..547787166 100644 --- a/internal/cmd/beta/alb/list/list_test.go +++ b/internal/cmd/beta/alb/list/list_test.go @@ -5,6 +5,7 @@ import ( "strconv" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" @@ -99,7 +100,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) if err := globalflags.Configure(cmd.Flags()); err != nil { t.Errorf("cannot configure global flags: %v", err) } @@ -196,7 +197,7 @@ func Test_outputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.items); (err != nil) != tt.wantErr { diff --git a/internal/cmd/beta/alb/observability-credentials/add/add.go b/internal/cmd/beta/alb/observability-credentials/add/add.go index 806af5e13..e0eff7449 100644 --- a/internal/cmd/beta/alb/observability-credentials/add/add.go +++ b/internal/cmd/beta/alb/observability-credentials/add/add.go @@ -5,6 +5,7 @@ import ( "encoding/json" "fmt" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" "github.com/stackitcloud/stackit-cli/internal/pkg/flags" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" @@ -30,7 +31,7 @@ type inputModel struct { Password *string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "add", Short: "Adds observability credentials to an application load balancer", @@ -44,20 +45,20 @@ func NewCmd(p *print.Printer) *cobra.Command { RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } if !model.AssumeYes { prompt := "Are your sure you want to add credentials?" - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -70,7 +71,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("add credential: %w", err) } - return outputResult(p, model.GlobalFlagModel.OutputFormat, resp) + return outputResult(params.Printer, model.GlobalFlagModel.OutputFormat, resp) }, } configureFlags(cmd) diff --git a/internal/cmd/beta/alb/observability-credentials/add/add_test.go b/internal/cmd/beta/alb/observability-credentials/add/add_test.go index a84b1405b..b5fa2bac6 100644 --- a/internal/cmd/beta/alb/observability-credentials/add/add_test.go +++ b/internal/cmd/beta/alb/observability-credentials/add/add_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" @@ -101,7 +102,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -202,7 +203,7 @@ func Test_outputResult(t *testing.T) { } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.item); (err != nil) != tt.wantErr { diff --git a/internal/cmd/beta/alb/observability-credentials/delete/delete.go b/internal/cmd/beta/alb/observability-credentials/delete/delete.go index e71a6fee4..df9f462f0 100644 --- a/internal/cmd/beta/alb/observability-credentials/delete/delete.go +++ b/internal/cmd/beta/alb/observability-credentials/delete/delete.go @@ -4,6 +4,7 @@ import ( "context" "fmt" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" @@ -23,7 +24,7 @@ type inputModel struct { CredentialsRef string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("delete %s", credentialRefArg), Short: "Deletes credentials", @@ -37,20 +38,20 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to delete credentials %q?", model.CredentialsRef) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -63,7 +64,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("delete credential: %w", err) } - p.Info("Deleted credential %q\n", model.CredentialsRef) + params.Printer.Info("Deleted credential %q\n", model.CredentialsRef) return nil }, diff --git a/internal/cmd/beta/alb/observability-credentials/delete/delete_test.go b/internal/cmd/beta/alb/observability-credentials/delete/delete_test.go index 6f5ff0dd8..09ecaa532 100644 --- a/internal/cmd/beta/alb/observability-credentials/delete/delete_test.go +++ b/internal/cmd/beta/alb/observability-credentials/delete/delete_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-sdk-go/services/alb" @@ -112,7 +113,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/beta/alb/observability-credentials/describe/describe.go b/internal/cmd/beta/alb/observability-credentials/describe/describe.go index 091d08ef2..4081421ca 100644 --- a/internal/cmd/beta/alb/observability-credentials/describe/describe.go +++ b/internal/cmd/beta/alb/observability-credentials/describe/describe.go @@ -5,6 +5,7 @@ import ( "encoding/json" "fmt" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" "github.com/stackitcloud/stackit-cli/internal/pkg/args" @@ -28,7 +29,7 @@ type inputModel struct { CredentialRef string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("describe %s", credentialRefArg), Short: "Describes observability credentials for the Application Load Balancer", @@ -42,13 +43,13 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -61,9 +62,9 @@ func NewCmd(p *print.Printer) *cobra.Command { } if credential := resp; credential != nil && credential.Credential != nil { - return outputResult(p, model.OutputFormat, *credential.Credential) + return outputResult(params.Printer, model.OutputFormat, *credential.Credential) } - p.Outputln("No credentials found.") + params.Printer.Outputln("No credentials found.") return nil }, } diff --git a/internal/cmd/beta/alb/observability-credentials/describe/describe_test.go b/internal/cmd/beta/alb/observability-credentials/describe/describe_test.go index 1d4f201a5..79412281b 100644 --- a/internal/cmd/beta/alb/observability-credentials/describe/describe_test.go +++ b/internal/cmd/beta/alb/observability-credentials/describe/describe_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" @@ -112,7 +113,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -213,7 +214,7 @@ func Test_outputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.response); (err != nil) != tt.wantErr { diff --git a/internal/cmd/beta/alb/observability-credentials/list/list.go b/internal/cmd/beta/alb/observability-credentials/list/list.go index d08a9026e..bbbdd6db4 100644 --- a/internal/cmd/beta/alb/observability-credentials/list/list.go +++ b/internal/cmd/beta/alb/observability-credentials/list/list.go @@ -5,6 +5,7 @@ import ( "encoding/json" "fmt" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" "github.com/stackitcloud/stackit-cli/internal/pkg/args" @@ -30,7 +31,7 @@ type inputModel struct { Limit *int64 } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "list", Short: "Lists all credentials", @@ -52,13 +53,13 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -71,7 +72,7 @@ func NewCmd(p *print.Printer) *cobra.Command { } if resp.Credentials == nil || len(*resp.Credentials) == 0 { - p.Info("No credentials found\n") + params.Printer.Info("No credentials found\n") return nil } @@ -80,7 +81,7 @@ func NewCmd(p *print.Printer) *cobra.Command { items = items[:*model.Limit] } - return outputResult(p, model.OutputFormat, items) + return outputResult(params.Printer, model.OutputFormat, items) }, } configureFlags(cmd) diff --git a/internal/cmd/beta/alb/observability-credentials/list/list_test.go b/internal/cmd/beta/alb/observability-credentials/list/list_test.go index f3c305298..77863ded8 100644 --- a/internal/cmd/beta/alb/observability-credentials/list/list_test.go +++ b/internal/cmd/beta/alb/observability-credentials/list/list_test.go @@ -5,6 +5,7 @@ import ( "strconv" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -120,7 +121,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -216,7 +217,7 @@ func Test_outputResult(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) if err := outputResult(p, tt.args.outputFormat, tt.args.response); (err != nil) != tt.wantErr { t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr) diff --git a/internal/cmd/beta/alb/observability-credentials/observability-credentials.go b/internal/cmd/beta/alb/observability-credentials/observability-credentials.go index 04dee2407..3d18486ee 100644 --- a/internal/cmd/beta/alb/observability-credentials/observability-credentials.go +++ b/internal/cmd/beta/alb/observability-credentials/observability-credentials.go @@ -8,11 +8,11 @@ import ( "github.com/stackitcloud/stackit-cli/internal/cmd/beta/alb/observability-credentials/describe" "github.com/stackitcloud/stackit-cli/internal/cmd/beta/alb/observability-credentials/list" "github.com/stackitcloud/stackit-cli/internal/cmd/beta/alb/observability-credentials/update" - "github.com/stackitcloud/stackit-cli/internal/pkg/print" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" ) -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "observability-credentials", Short: "Provides functionality for application loadbalancer credentials", @@ -20,14 +20,14 @@ func NewCmd(p *print.Printer) *cobra.Command { Args: cobra.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd, p) + addSubcommands(cmd, params) return cmd } -func addSubcommands(cmd *cobra.Command, p *print.Printer) { - cmd.AddCommand(add.NewCmd(p)) - cmd.AddCommand(delete.NewCmd(p)) - cmd.AddCommand(describe.NewCmd(p)) - cmd.AddCommand(list.NewCmd(p)) - cmd.AddCommand(update.NewCmd(p)) +func addSubcommands(cmd *cobra.Command, params *params.CmdParams) { + cmd.AddCommand(add.NewCmd(params)) + cmd.AddCommand(delete.NewCmd(params)) + cmd.AddCommand(describe.NewCmd(params)) + cmd.AddCommand(list.NewCmd(params)) + cmd.AddCommand(update.NewCmd(params)) } diff --git a/internal/cmd/beta/alb/observability-credentials/update/update.go b/internal/cmd/beta/alb/observability-credentials/update/update.go index 03a9fe8ad..3231f0d95 100644 --- a/internal/cmd/beta/alb/observability-credentials/update/update.go +++ b/internal/cmd/beta/alb/observability-credentials/update/update.go @@ -5,6 +5,7 @@ import ( "encoding/json" "fmt" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" "github.com/stackitcloud/stackit-cli/internal/pkg/flags" @@ -34,7 +35,7 @@ type inputModel struct { CredentialsRef *string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("update %s", credentialRefArg), Short: "Update credentials", @@ -47,10 +48,10 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model := parseInput(p, cmd, args) + model := parseInput(params.Printer, cmd, args) // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -59,14 +60,14 @@ func NewCmd(p *print.Printer) *cobra.Command { if err != nil { return err } - projectLabel, err := projectname.GetProjectName(ctx, p, cmd) + projectLabel, err := projectname.GetProjectName(ctx, params.Printer, cmd) if err != nil { - p.Debug(print.ErrorLevel, "get project name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get project name: %v", err) projectLabel = model.ProjectId } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to update credential %q for %q?", *model.CredentialsRef, projectLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return fmt.Errorf("update credential: %w", err) } @@ -81,7 +82,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("response is nil") } - return outputResult(p, model, resp) + return outputResult(params.Printer, model, resp) }, } configureFlags(cmd) diff --git a/internal/cmd/beta/alb/observability-credentials/update/update_test.go b/internal/cmd/beta/alb/observability-credentials/update/update_test.go index bdfab0080..4d3780ba1 100644 --- a/internal/cmd/beta/alb/observability-credentials/update/update_test.go +++ b/internal/cmd/beta/alb/observability-credentials/update/update_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" @@ -126,7 +127,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -216,7 +217,7 @@ func Test_outputResult(t *testing.T) { } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.model, tt.args.item); (err != nil) != tt.wantErr { diff --git a/internal/cmd/beta/alb/plans/plans.go b/internal/cmd/beta/alb/plans/plans.go index 13719baac..f7d09bec6 100644 --- a/internal/cmd/beta/alb/plans/plans.go +++ b/internal/cmd/beta/alb/plans/plans.go @@ -7,6 +7,7 @@ import ( "github.com/goccy/go-yaml" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -23,7 +24,7 @@ type inputModel struct { *globalflags.GlobalFlagModel } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "plans", Short: "Lists the application load balancer plans", @@ -37,20 +38,20 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } - projectLabel, err := projectname.GetProjectName(ctx, p, cmd) + projectLabel, err := projectname.GetProjectName(ctx, params.Printer, cmd) if err != nil { - p.Debug(print.ErrorLevel, "get project name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get project name: %v", err) projectLabel = model.ProjectId } else if projectLabel == "" { projectLabel = model.ProjectId @@ -65,9 +66,9 @@ func NewCmd(p *print.Printer) *cobra.Command { } if items := response.ValidPlans; items == nil || len(*items) == 0 { - p.Info("No plans found for project %q", projectLabel) + params.Printer.Info("No plans found for project %q", projectLabel) } else { - if err := outputResult(p, model.OutputFormat, *items); err != nil { + if err := outputResult(params.Printer, model.OutputFormat, *items); err != nil { return fmt.Errorf("output plans: %w", err) } } diff --git a/internal/cmd/beta/alb/plans/plans_test.go b/internal/cmd/beta/alb/plans/plans_test.go index 3184f5696..bfbda8586 100644 --- a/internal/cmd/beta/alb/plans/plans_test.go +++ b/internal/cmd/beta/alb/plans/plans_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" @@ -95,7 +96,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) if err := globalflags.Configure(cmd.Flags()); err != nil { t.Errorf("cannot configure global flags: %v", err) } @@ -192,7 +193,7 @@ func Test_outputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.items); (err != nil) != tt.wantErr { diff --git a/internal/cmd/beta/alb/pool/pool.go b/internal/cmd/beta/alb/pool/pool.go index 5d8d5fd41..d40f8733a 100644 --- a/internal/cmd/beta/alb/pool/pool.go +++ b/internal/cmd/beta/alb/pool/pool.go @@ -2,15 +2,15 @@ package pool import ( "github.com/stackitcloud/stackit-cli/internal/cmd/beta/alb/pool/update" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" - "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" "github.com/spf13/cobra" ) -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "pool", Short: "Manages target pools for application loadbalancers", @@ -18,10 +18,10 @@ func NewCmd(p *print.Printer) *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd, p) + addSubcommands(cmd, params) return cmd } -func addSubcommands(cmd *cobra.Command, p *print.Printer) { - cmd.AddCommand(update.NewCmd(p)) +func addSubcommands(cmd *cobra.Command, params *params.CmdParams) { + cmd.AddCommand(update.NewCmd(params)) } diff --git a/internal/cmd/beta/alb/pool/update/update.go b/internal/cmd/beta/alb/pool/update/update.go index e8b2aa2fa..94bdd2872 100644 --- a/internal/cmd/beta/alb/pool/update/update.go +++ b/internal/cmd/beta/alb/pool/update/update.go @@ -9,6 +9,7 @@ import ( "strings" "github.com/goccy/go-yaml" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -34,7 +35,7 @@ type inputModel struct { AlbName *string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "update", Short: "Updates an application target pool", @@ -47,26 +48,26 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } - projectLabel, err := projectname.GetProjectName(ctx, p, cmd) + projectLabel, err := projectname.GetProjectName(ctx, params.Printer, cmd) if err != nil { - p.Debug(print.ErrorLevel, "get project name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get project name: %v", err) projectLabel = model.ProjectId } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to update an application target pool for project %q?", projectLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -82,7 +83,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("update application target pool: %w", err) } - return outputResult(p, model, projectLabel, resp) + return outputResult(params.Printer, model, projectLabel, resp) }, } configureFlags(cmd) diff --git a/internal/cmd/beta/alb/pool/update/update_test.go b/internal/cmd/beta/alb/pool/update/update_test.go index b9b9ec080..f279c1f01 100644 --- a/internal/cmd/beta/alb/pool/update/update_test.go +++ b/internal/cmd/beta/alb/pool/update/update_test.go @@ -10,6 +10,7 @@ import ( "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" "github.com/google/uuid" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -142,7 +143,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -257,7 +258,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.model, tt.args.projectLabel, tt.args.resp); (err != nil) != tt.wantErr { diff --git a/internal/cmd/beta/alb/quotas/quotas.go b/internal/cmd/beta/alb/quotas/quotas.go index 07bc2eb21..09735a914 100644 --- a/internal/cmd/beta/alb/quotas/quotas.go +++ b/internal/cmd/beta/alb/quotas/quotas.go @@ -7,6 +7,7 @@ import ( "github.com/goccy/go-yaml" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -22,7 +23,7 @@ type inputModel struct { *globalflags.GlobalFlagModel } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "quotas", Short: "Shows the application load balancer quotas", @@ -36,13 +37,13 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -56,11 +57,11 @@ func NewCmd(p *print.Printer) *cobra.Command { } if response == nil { - p.Outputln("no quotas found") + params.Printer.Outputln("no quotas found") return nil } - if err := outputResult(p, model.OutputFormat, *response); err != nil { + if err := outputResult(params.Printer, model.OutputFormat, *response); err != nil { return fmt.Errorf("output loadbalancers: %w", err) } diff --git a/internal/cmd/beta/alb/quotas/quotas_test.go b/internal/cmd/beta/alb/quotas/quotas_test.go index b840d9ef0..1ff2aaacd 100644 --- a/internal/cmd/beta/alb/quotas/quotas_test.go +++ b/internal/cmd/beta/alb/quotas/quotas_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" @@ -95,7 +96,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) if err := globalflags.Configure(cmd.Flags()); err != nil { t.Errorf("cannot configure global flags: %v", err) } @@ -192,7 +193,7 @@ func Test_outputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.response); (err != nil) != tt.wantErr { diff --git a/internal/cmd/beta/alb/template/template.go b/internal/cmd/beta/alb/template/template.go index 18d0cd427..c33ca2846 100644 --- a/internal/cmd/beta/alb/template/template.go +++ b/internal/cmd/beta/alb/template/template.go @@ -8,6 +8,7 @@ import ( "github.com/goccy/go-yaml" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -36,7 +37,7 @@ var ( templatePool string ) -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "template", Short: "creates configuration templates to use for resource creation", @@ -53,7 +54,7 @@ func NewCmd(p *print.Printer) *cobra.Command { ), ), RunE: func(cmd *cobra.Command, _ []string) error { - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } @@ -73,7 +74,7 @@ func NewCmd(p *print.Printer) *cobra.Command { } if model.Format == nil || *model.Format == "yaml" { - p.Outputln(template) + params.Printer.Outputln(template) } else if *model.Format == "json" { if err := yaml.Unmarshal([]byte(template), &target); err != nil { return fmt.Errorf("cannot unmarshal template: %w", err) diff --git a/internal/cmd/beta/alb/template/template_test.go b/internal/cmd/beta/alb/template/template_test.go index 3ca5404e8..be7bed72d 100644 --- a/internal/cmd/beta/alb/template/template_test.go +++ b/internal/cmd/beta/alb/template/template_test.go @@ -3,6 +3,7 @@ package template import ( "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -138,7 +139,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) if err := globalflags.Configure(cmd.Flags()); err != nil { t.Errorf("cannot configure global flags: %v", err) } diff --git a/internal/cmd/beta/alb/update/update.go b/internal/cmd/beta/alb/update/update.go index 546f89990..10e0b9bc5 100644 --- a/internal/cmd/beta/alb/update/update.go +++ b/internal/cmd/beta/alb/update/update.go @@ -9,6 +9,7 @@ import ( "strings" "github.com/goccy/go-yaml" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -35,7 +36,7 @@ type inputModel struct { Version *string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "update", Short: "Updates an application loadbalancer", @@ -48,26 +49,26 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } - projectLabel, err := projectname.GetProjectName(ctx, p, cmd) + projectLabel, err := projectname.GetProjectName(ctx, params.Printer, cmd) if err != nil { - p.Debug(print.ErrorLevel, "get project name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get project name: %v", err) projectLabel = model.ProjectId } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to update an application loadbalancer for project %q?", projectLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -90,7 +91,7 @@ func NewCmd(p *print.Printer) *cobra.Command { // Wait for async operation, if async mode not enabled if !model.Async { - s := spinner.New(p) + s := spinner.New(params.Printer) s.Start("updating loadbalancer") _, err = wait.CreateOrUpdateLoadbalancerWaitHandler(ctx, apiClient, model.ProjectId, model.Region, *resp.Name). WaitWithContext(ctx) @@ -100,7 +101,7 @@ func NewCmd(p *print.Printer) *cobra.Command { s.Stop() } - return outputResult(p, model, projectLabel, resp) + return outputResult(params.Printer, model, projectLabel, resp) }, } configureFlags(cmd) diff --git a/internal/cmd/beta/alb/update/update_test.go b/internal/cmd/beta/alb/update/update_test.go index 176a130a3..5749b8b86 100644 --- a/internal/cmd/beta/alb/update/update_test.go +++ b/internal/cmd/beta/alb/update/update_test.go @@ -10,6 +10,7 @@ import ( "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" "github.com/google/uuid" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -137,7 +138,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -251,7 +252,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.model, tt.args.projectLabel, tt.args.resp); (err != nil) != tt.wantErr { diff --git a/internal/cmd/beta/beta.go b/internal/cmd/beta/beta.go index 37b5e4bd9..5a007b87e 100644 --- a/internal/cmd/beta/beta.go +++ b/internal/cmd/beta/beta.go @@ -5,15 +5,15 @@ import ( "github.com/stackitcloud/stackit-cli/internal/cmd/beta/alb" "github.com/stackitcloud/stackit-cli/internal/cmd/beta/sqlserverflex" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" - "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" "github.com/spf13/cobra" ) -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "beta", Short: "Contains beta STACKIT CLI commands", @@ -31,11 +31,11 @@ func NewCmd(p *print.Printer) *cobra.Command { "$ stackit beta MY_COMMAND"), ), } - addSubcommands(cmd, p) + addSubcommands(cmd, params) return cmd } -func addSubcommands(cmd *cobra.Command, p *print.Printer) { - cmd.AddCommand(sqlserverflex.NewCmd(p)) - cmd.AddCommand(alb.NewCmd(p)) +func addSubcommands(cmd *cobra.Command, params *params.CmdParams) { + cmd.AddCommand(sqlserverflex.NewCmd(params)) + cmd.AddCommand(alb.NewCmd(params)) } diff --git a/internal/cmd/beta/sqlserverflex/database/create/create.go b/internal/cmd/beta/sqlserverflex/database/create/create.go index fe137c435..ac7e345a8 100644 --- a/internal/cmd/beta/sqlserverflex/database/create/create.go +++ b/internal/cmd/beta/sqlserverflex/database/create/create.go @@ -6,6 +6,7 @@ import ( "fmt" "github.com/goccy/go-yaml" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -33,7 +34,7 @@ type inputModel struct { Owner string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("create %s", databaseNameArg), Short: "Creates a SQLServer Flex database", @@ -49,20 +50,20 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to create database %q? (This cannot be undone)", model.DatabaseName) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -70,7 +71,7 @@ func NewCmd(p *print.Printer) *cobra.Command { // Call API req := buildRequest(ctx, model, apiClient) - s := spinner.New(p) + s := spinner.New(params.Printer) s.Start("Creating database") resp, err := req.Execute() if err != nil { @@ -79,7 +80,7 @@ func NewCmd(p *print.Printer) *cobra.Command { } s.Stop() - return outputResult(p, model.OutputFormat, model.DatabaseName, resp) + return outputResult(params.Printer, model.OutputFormat, model.DatabaseName, resp) }, } configureFlags(cmd) diff --git a/internal/cmd/beta/sqlserverflex/database/create/create_test.go b/internal/cmd/beta/sqlserverflex/database/create/create_test.go index ada824cb6..c6359313e 100644 --- a/internal/cmd/beta/sqlserverflex/database/create/create_test.go +++ b/internal/cmd/beta/sqlserverflex/database/create/create_test.go @@ -7,6 +7,7 @@ import ( "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" "github.com/google/uuid" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-sdk-go/services/sqlserverflex" @@ -177,7 +178,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -281,7 +282,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.databaseName, tt.args.resp); (err != nil) != tt.wantErr { diff --git a/internal/cmd/beta/sqlserverflex/database/database.go b/internal/cmd/beta/sqlserverflex/database/database.go index 3fbd641ce..f11cf2bd0 100644 --- a/internal/cmd/beta/sqlserverflex/database/database.go +++ b/internal/cmd/beta/sqlserverflex/database/database.go @@ -5,14 +5,14 @@ import ( "github.com/stackitcloud/stackit-cli/internal/cmd/beta/sqlserverflex/database/delete" "github.com/stackitcloud/stackit-cli/internal/cmd/beta/sqlserverflex/database/describe" "github.com/stackitcloud/stackit-cli/internal/cmd/beta/sqlserverflex/database/list" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" - "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" "github.com/spf13/cobra" ) -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "database", Short: "Provides functionality for SQLServer Flex databases", @@ -20,13 +20,13 @@ func NewCmd(p *print.Printer) *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd, p) + addSubcommands(cmd, params) return cmd } -func addSubcommands(cmd *cobra.Command, p *print.Printer) { - cmd.AddCommand(create.NewCmd(p)) - cmd.AddCommand(delete.NewCmd(p)) - cmd.AddCommand(describe.NewCmd(p)) - cmd.AddCommand(list.NewCmd(p)) +func addSubcommands(cmd *cobra.Command, params *params.CmdParams) { + cmd.AddCommand(create.NewCmd(params)) + cmd.AddCommand(delete.NewCmd(params)) + cmd.AddCommand(describe.NewCmd(params)) + cmd.AddCommand(list.NewCmd(params)) } diff --git a/internal/cmd/beta/sqlserverflex/database/delete/delete.go b/internal/cmd/beta/sqlserverflex/database/delete/delete.go index d47633cf3..b1950aa86 100644 --- a/internal/cmd/beta/sqlserverflex/database/delete/delete.go +++ b/internal/cmd/beta/sqlserverflex/database/delete/delete.go @@ -4,6 +4,7 @@ import ( "context" "fmt" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -29,7 +30,7 @@ type inputModel struct { InstanceId string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("delete %s", databaseNameArg), Short: "Deletes a SQLServer Flex database", @@ -45,20 +46,20 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to delete database %q? (This cannot be undone)", model.DatabaseName) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -66,7 +67,7 @@ func NewCmd(p *print.Printer) *cobra.Command { // Call API req := buildRequest(ctx, model, apiClient) - s := spinner.New(p) + s := spinner.New(params.Printer) s.Start("Deleting database") err = req.Execute() if err != nil { @@ -75,7 +76,7 @@ func NewCmd(p *print.Printer) *cobra.Command { } s.Stop() - p.Info("Deleted database %q\n", model.DatabaseName) + params.Printer.Info("Deleted database %q\n", model.DatabaseName) return nil }, } diff --git a/internal/cmd/beta/sqlserverflex/database/delete/delete_test.go b/internal/cmd/beta/sqlserverflex/database/delete/delete_test.go index 315341040..53a099f48 100644 --- a/internal/cmd/beta/sqlserverflex/database/delete/delete_test.go +++ b/internal/cmd/beta/sqlserverflex/database/delete/delete_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-sdk-go/services/sqlserverflex" @@ -160,7 +161,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/beta/sqlserverflex/database/describe/describe.go b/internal/cmd/beta/sqlserverflex/database/describe/describe.go index ed2caecf7..3ddbfa086 100644 --- a/internal/cmd/beta/sqlserverflex/database/describe/describe.go +++ b/internal/cmd/beta/sqlserverflex/database/describe/describe.go @@ -7,6 +7,7 @@ import ( "github.com/goccy/go-yaml" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -31,7 +32,7 @@ type inputModel struct { InstanceId string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("describe %s", databaseNameArg), Short: "Shows details of an SQLServer Flex database", @@ -47,12 +48,12 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -64,7 +65,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("read SQLServer Flex database: %w", err) } - return outputResult(p, model.OutputFormat, resp) + return outputResult(params.Printer, model.OutputFormat, resp) }, } configureFlags(cmd) diff --git a/internal/cmd/beta/sqlserverflex/database/describe/describe_test.go b/internal/cmd/beta/sqlserverflex/database/describe/describe_test.go index b55faf4a2..919b1e3c5 100644 --- a/internal/cmd/beta/sqlserverflex/database/describe/describe_test.go +++ b/internal/cmd/beta/sqlserverflex/database/describe/describe_test.go @@ -7,6 +7,7 @@ import ( "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" "github.com/google/uuid" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-sdk-go/services/sqlserverflex" @@ -159,7 +160,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -269,7 +270,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.resp); (err != nil) != tt.wantErr { diff --git a/internal/cmd/beta/sqlserverflex/database/list/list.go b/internal/cmd/beta/sqlserverflex/database/list/list.go index 6d660f8a1..ba22c593e 100644 --- a/internal/cmd/beta/sqlserverflex/database/list/list.go +++ b/internal/cmd/beta/sqlserverflex/database/list/list.go @@ -7,6 +7,7 @@ import ( "github.com/goccy/go-yaml" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -31,7 +32,7 @@ type inputModel struct { Limit *int64 } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "list", Short: "Lists all SQLServer Flex databases", @@ -50,13 +51,13 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -68,12 +69,12 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("get SQLServer Flex databases: %w", err) } if resp.Databases == nil || len(*resp.Databases) == 0 { - projectLabel, err := projectname.GetProjectName(ctx, p, cmd) + projectLabel, err := projectname.GetProjectName(ctx, params.Printer, cmd) if err != nil { - p.Debug(print.ErrorLevel, "get project name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get project name: %v", err) projectLabel = model.ProjectId } - p.Info("No databases found for instance %s on project %s\n", model.InstanceId, projectLabel) + params.Printer.Info("No databases found for instance %s on project %s\n", model.InstanceId, projectLabel) return nil } databases := *resp.Databases @@ -83,7 +84,7 @@ func NewCmd(p *print.Printer) *cobra.Command { databases = databases[:*model.Limit] } - return outputResult(p, model.OutputFormat, databases) + return outputResult(params.Printer, model.OutputFormat, databases) }, } diff --git a/internal/cmd/beta/sqlserverflex/database/list/list_test.go b/internal/cmd/beta/sqlserverflex/database/list/list_test.go index 1b10b22a8..0aa4979f7 100644 --- a/internal/cmd/beta/sqlserverflex/database/list/list_test.go +++ b/internal/cmd/beta/sqlserverflex/database/list/list_test.go @@ -8,6 +8,7 @@ import ( "github.com/google/go-cmp/cmp/cmpopts" "github.com/google/uuid" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -235,7 +236,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.databases); (err != nil) != tt.wantErr { diff --git a/internal/cmd/beta/sqlserverflex/instance/create/create.go b/internal/cmd/beta/sqlserverflex/instance/create/create.go index 904b2f6f8..9003de50b 100644 --- a/internal/cmd/beta/sqlserverflex/instance/create/create.go +++ b/internal/cmd/beta/sqlserverflex/instance/create/create.go @@ -7,6 +7,7 @@ import ( "fmt" "github.com/goccy/go-yaml" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" cliErr "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -65,7 +66,7 @@ type inputModel struct { RetentionDays *int64 } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "create", Short: "Creates a SQLServer Flex instance", @@ -86,26 +87,26 @@ func NewCmd(p *print.Printer) *cobra.Command { RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } - projectLabel, err := projectname.GetProjectName(ctx, p, cmd) + projectLabel, err := projectname.GetProjectName(ctx, params.Printer, cmd) if err != nil { - p.Debug(print.ErrorLevel, "get project name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get project name: %v", err) projectLabel = model.ProjectId } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to create a SQLServer Flex instance for project %q?", projectLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -124,7 +125,7 @@ func NewCmd(p *print.Printer) *cobra.Command { // Wait for async operation, if async mode not enabled if !model.Async { - s := spinner.New(p) + s := spinner.New(params.Printer) s.Start("Creating instance") _, err = wait.CreateInstanceWaitHandler(ctx, apiClient, model.ProjectId, instanceId, model.Region).WaitWithContext(ctx) if err != nil { @@ -133,7 +134,7 @@ func NewCmd(p *print.Printer) *cobra.Command { s.Stop() } - return outputResult(p, model, projectLabel, resp) + return outputResult(params.Printer, model, projectLabel, resp) }, } configureFlags(cmd) diff --git a/internal/cmd/beta/sqlserverflex/instance/create/create_test.go b/internal/cmd/beta/sqlserverflex/instance/create/create_test.go index eaf4a5463..851d3d1fd 100644 --- a/internal/cmd/beta/sqlserverflex/instance/create/create_test.go +++ b/internal/cmd/beta/sqlserverflex/instance/create/create_test.go @@ -8,6 +8,7 @@ import ( "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" "github.com/google/uuid" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -251,7 +252,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -524,7 +525,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.model, tt.args.projectLabel, tt.args.resp); (err != nil) != tt.wantErr { diff --git a/internal/cmd/beta/sqlserverflex/instance/delete/delete.go b/internal/cmd/beta/sqlserverflex/instance/delete/delete.go index c0456b08d..2b5723c50 100644 --- a/internal/cmd/beta/sqlserverflex/instance/delete/delete.go +++ b/internal/cmd/beta/sqlserverflex/instance/delete/delete.go @@ -4,6 +4,7 @@ import ( "context" "fmt" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -28,7 +29,7 @@ type inputModel struct { InstanceId string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("delete %s", instanceIdArg), Short: "Deletes a SQLServer Flex instance", @@ -41,26 +42,26 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } instanceLabel, err := sqlserverflexUtils.GetInstanceName(ctx, apiClient, model.ProjectId, model.InstanceId, model.Region) if err != nil { - p.Debug(print.ErrorLevel, "get instance name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get instance name: %v", err) instanceLabel = model.InstanceId } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to delete instance %q? (This cannot be undone)", instanceLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -75,7 +76,7 @@ func NewCmd(p *print.Printer) *cobra.Command { // Wait for async operation, if async mode not enabled if !model.Async { - s := spinner.New(p) + s := spinner.New(params.Printer) s.Start("Deleting instance") _, err = wait.DeleteInstanceWaitHandler(ctx, apiClient, model.ProjectId, model.InstanceId, model.Region).WaitWithContext(ctx) if err != nil { @@ -88,7 +89,7 @@ func NewCmd(p *print.Printer) *cobra.Command { if model.Async { operationState = "Triggered deletion of" } - p.Info("%s instance %q\n", operationState, instanceLabel) + params.Printer.Info("%s instance %q\n", operationState, instanceLabel) return nil }, } diff --git a/internal/cmd/beta/sqlserverflex/instance/delete/delete_test.go b/internal/cmd/beta/sqlserverflex/instance/delete/delete_test.go index 753379175..ae971f31d 100644 --- a/internal/cmd/beta/sqlserverflex/instance/delete/delete_test.go +++ b/internal/cmd/beta/sqlserverflex/instance/delete/delete_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" @@ -139,7 +140,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/beta/sqlserverflex/instance/describe/describe.go b/internal/cmd/beta/sqlserverflex/instance/describe/describe.go index c5d8e68e1..7dec66a7d 100644 --- a/internal/cmd/beta/sqlserverflex/instance/describe/describe.go +++ b/internal/cmd/beta/sqlserverflex/instance/describe/describe.go @@ -7,6 +7,7 @@ import ( "strings" "github.com/goccy/go-yaml" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -29,7 +30,7 @@ type inputModel struct { InstanceId string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("describe %s", instanceIdArg), Short: "Shows details of a SQLServer Flex instance", @@ -45,12 +46,12 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -62,7 +63,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("read SQLServer Flex instance: %w", err) } - return outputResult(p, model.OutputFormat, resp.Item) + return outputResult(params.Printer, model.OutputFormat, resp.Item) }, } return cmd diff --git a/internal/cmd/beta/sqlserverflex/instance/describe/describe_test.go b/internal/cmd/beta/sqlserverflex/instance/describe/describe_test.go index 29edcede2..7412418c6 100644 --- a/internal/cmd/beta/sqlserverflex/instance/describe/describe_test.go +++ b/internal/cmd/beta/sqlserverflex/instance/describe/describe_test.go @@ -7,6 +7,7 @@ import ( "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" "github.com/google/uuid" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-sdk-go/services/sqlserverflex" @@ -138,7 +139,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -241,7 +242,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.instance); (err != nil) != tt.wantErr { diff --git a/internal/cmd/beta/sqlserverflex/instance/instance.go b/internal/cmd/beta/sqlserverflex/instance/instance.go index a74e41085..4ed452eb1 100644 --- a/internal/cmd/beta/sqlserverflex/instance/instance.go +++ b/internal/cmd/beta/sqlserverflex/instance/instance.go @@ -6,14 +6,14 @@ import ( "github.com/stackitcloud/stackit-cli/internal/cmd/beta/sqlserverflex/instance/describe" "github.com/stackitcloud/stackit-cli/internal/cmd/beta/sqlserverflex/instance/list" "github.com/stackitcloud/stackit-cli/internal/cmd/beta/sqlserverflex/instance/update" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" - "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" "github.com/spf13/cobra" ) -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "instance", Short: "Provides functionality for SQLServer Flex instances", @@ -21,14 +21,14 @@ func NewCmd(p *print.Printer) *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd, p) + addSubcommands(cmd, params) return cmd } -func addSubcommands(cmd *cobra.Command, p *print.Printer) { - cmd.AddCommand(create.NewCmd(p)) - cmd.AddCommand(delete.NewCmd(p)) - cmd.AddCommand(describe.NewCmd(p)) - cmd.AddCommand(list.NewCmd(p)) - cmd.AddCommand(update.NewCmd(p)) +func addSubcommands(cmd *cobra.Command, params *params.CmdParams) { + cmd.AddCommand(create.NewCmd(params)) + cmd.AddCommand(delete.NewCmd(params)) + cmd.AddCommand(describe.NewCmd(params)) + cmd.AddCommand(list.NewCmd(params)) + cmd.AddCommand(update.NewCmd(params)) } diff --git a/internal/cmd/beta/sqlserverflex/instance/list/list.go b/internal/cmd/beta/sqlserverflex/instance/list/list.go index 18666a7d5..3f9abdb22 100644 --- a/internal/cmd/beta/sqlserverflex/instance/list/list.go +++ b/internal/cmd/beta/sqlserverflex/instance/list/list.go @@ -7,6 +7,7 @@ import ( "github.com/goccy/go-yaml" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -29,7 +30,7 @@ type inputModel struct { Limit *int64 } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "list", Short: "Lists all SQLServer Flex instances", @@ -48,13 +49,13 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -66,12 +67,12 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("get SQLServer Flex instances: %w", err) } if resp.Items == nil || len(*resp.Items) == 0 { - projectLabel, err := projectname.GetProjectName(ctx, p, cmd) + projectLabel, err := projectname.GetProjectName(ctx, params.Printer, cmd) if err != nil { - p.Debug(print.ErrorLevel, "get project name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get project name: %v", err) projectLabel = model.ProjectId } - p.Info("No instances found for project %q\n", projectLabel) + params.Printer.Info("No instances found for project %q\n", projectLabel) return nil } instances := *resp.Items @@ -81,7 +82,7 @@ func NewCmd(p *print.Printer) *cobra.Command { instances = instances[:*model.Limit] } - return outputResult(p, model.OutputFormat, instances) + return outputResult(params.Printer, model.OutputFormat, instances) }, } diff --git a/internal/cmd/beta/sqlserverflex/instance/list/list_test.go b/internal/cmd/beta/sqlserverflex/instance/list/list_test.go index 094774724..be4b5debe 100644 --- a/internal/cmd/beta/sqlserverflex/instance/list/list_test.go +++ b/internal/cmd/beta/sqlserverflex/instance/list/list_test.go @@ -8,6 +8,7 @@ import ( "github.com/google/go-cmp/cmp/cmpopts" "github.com/google/uuid" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -211,7 +212,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.instances); (err != nil) != tt.wantErr { diff --git a/internal/cmd/beta/sqlserverflex/instance/update/update.go b/internal/cmd/beta/sqlserverflex/instance/update/update.go index 39c5989dc..53921ea75 100644 --- a/internal/cmd/beta/sqlserverflex/instance/update/update.go +++ b/internal/cmd/beta/sqlserverflex/instance/update/update.go @@ -7,6 +7,7 @@ import ( "fmt" "github.com/goccy/go-yaml" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" cliErr "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -60,7 +61,7 @@ type inputModel struct { Version *string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("update %s", instanceIdArg), Short: "Updates a SQLServer Flex instance", @@ -77,26 +78,26 @@ func NewCmd(p *print.Printer) *cobra.Command { RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } instanceLabel, err := sqlserverflexUtils.GetInstanceName(ctx, apiClient, model.ProjectId, model.InstanceId, model.Region) if err != nil { - p.Debug(print.ErrorLevel, "get instance name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get instance name: %v", err) instanceLabel = model.InstanceId } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to update instance %q?", instanceLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -115,7 +116,7 @@ func NewCmd(p *print.Printer) *cobra.Command { // Wait for async operation, if async mode not enabled if !model.Async { - s := spinner.New(p) + s := spinner.New(params.Printer) s.Start("Updating instance") _, err = wait.PartialUpdateInstanceWaitHandler(ctx, apiClient, model.ProjectId, instanceId, model.Region).WaitWithContext(ctx) if err != nil { @@ -124,7 +125,7 @@ func NewCmd(p *print.Printer) *cobra.Command { s.Stop() } - return outputResult(p, model, instanceLabel, resp) + return outputResult(params.Printer, model, instanceLabel, resp) }, } configureFlags(cmd) diff --git a/internal/cmd/beta/sqlserverflex/instance/update/update_test.go b/internal/cmd/beta/sqlserverflex/instance/update/update_test.go index 4c9c92dd5..5db0655ba 100644 --- a/internal/cmd/beta/sqlserverflex/instance/update/update_test.go +++ b/internal/cmd/beta/sqlserverflex/instance/update/update_test.go @@ -8,6 +8,7 @@ import ( "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" "github.com/google/uuid" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -281,7 +282,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -520,7 +521,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.model, tt.args.instanceLabel, tt.args.resp); (err != nil) != tt.wantErr { diff --git a/internal/cmd/beta/sqlserverflex/options/options.go b/internal/cmd/beta/sqlserverflex/options/options.go index a4b815a44..d66f082c9 100644 --- a/internal/cmd/beta/sqlserverflex/options/options.go +++ b/internal/cmd/beta/sqlserverflex/options/options.go @@ -6,6 +6,7 @@ import ( "fmt" "github.com/goccy/go-yaml" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" "github.com/stackitcloud/stackit-cli/internal/pkg/flags" @@ -87,7 +88,7 @@ type instanceDBCompatibilities struct { DBCompatibilities []sqlserverflex.MssqlDatabaseCompatibility `json:"dbCompatibilities"` } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "options", Short: "Lists SQL Server Flex options", @@ -109,19 +110,19 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } // Call API - err = buildAndExecuteRequest(ctx, p, model, apiClient) + err = buildAndExecuteRequest(ctx, params.Printer, model, apiClient) if err != nil { return fmt.Errorf("get SQL Server Flex options: %w", err) } diff --git a/internal/cmd/beta/sqlserverflex/options/options_test.go b/internal/cmd/beta/sqlserverflex/options/options_test.go index 5e76ad539..1f527d85e 100644 --- a/internal/cmd/beta/sqlserverflex/options/options_test.go +++ b/internal/cmd/beta/sqlserverflex/options/options_test.go @@ -7,6 +7,7 @@ import ( "github.com/google/go-cmp/cmp" "github.com/google/uuid" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -263,7 +264,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -472,7 +473,7 @@ func TestBuildAndExecuteRequest(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) p.Cmd = cmd client := &sqlServerFlexClientMocked{ listFlavorsFails: tt.listFlavorsFails, @@ -544,7 +545,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.model, tt.args.flavors, tt.args.versions, tt.args.storages, tt.args.userRoles, tt.args.dbCollations, tt.args.dbCompatibilities); (err != nil) != tt.wantErr { diff --git a/internal/cmd/beta/sqlserverflex/sqlserverflex.go b/internal/cmd/beta/sqlserverflex/sqlserverflex.go index a65ca22e8..51c93beb8 100644 --- a/internal/cmd/beta/sqlserverflex/sqlserverflex.go +++ b/internal/cmd/beta/sqlserverflex/sqlserverflex.go @@ -5,14 +5,14 @@ import ( "github.com/stackitcloud/stackit-cli/internal/cmd/beta/sqlserverflex/instance" "github.com/stackitcloud/stackit-cli/internal/cmd/beta/sqlserverflex/options" "github.com/stackitcloud/stackit-cli/internal/cmd/beta/sqlserverflex/user" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" - "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" "github.com/spf13/cobra" ) -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "sqlserverflex", Short: "Provides functionality for SQLServer Flex", @@ -20,13 +20,13 @@ func NewCmd(p *print.Printer) *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd, p) + addSubcommands(cmd, params) return cmd } -func addSubcommands(cmd *cobra.Command, p *print.Printer) { - cmd.AddCommand(database.NewCmd(p)) - cmd.AddCommand(instance.NewCmd(p)) - cmd.AddCommand(options.NewCmd(p)) - cmd.AddCommand(user.NewCmd(p)) +func addSubcommands(cmd *cobra.Command, params *params.CmdParams) { + cmd.AddCommand(database.NewCmd(params)) + cmd.AddCommand(instance.NewCmd(params)) + cmd.AddCommand(options.NewCmd(params)) + cmd.AddCommand(user.NewCmd(params)) } diff --git a/internal/cmd/beta/sqlserverflex/user/create/create.go b/internal/cmd/beta/sqlserverflex/user/create/create.go index 0cd99180c..501746b1e 100644 --- a/internal/cmd/beta/sqlserverflex/user/create/create.go +++ b/internal/cmd/beta/sqlserverflex/user/create/create.go @@ -8,6 +8,7 @@ import ( "github.com/goccy/go-yaml" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -34,7 +35,7 @@ type inputModel struct { Roles *[]string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "create", Short: "Creates a SQLServer Flex user", @@ -58,26 +59,26 @@ func NewCmd(p *print.Printer) *cobra.Command { Args: args.NoArgs, RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } instanceLabel, err := sqlserverflexUtils.GetInstanceName(ctx, apiClient, model.ProjectId, model.InstanceId, model.Region) if err != nil { - p.Debug(print.ErrorLevel, "get instance name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get instance name: %v", err) instanceLabel = model.InstanceId } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to create a user for instance %q?", instanceLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -91,7 +92,7 @@ func NewCmd(p *print.Printer) *cobra.Command { } user := resp.Item - return outputResult(p, model, instanceLabel, user) + return outputResult(params.Printer, model, instanceLabel, user) }, } diff --git a/internal/cmd/beta/sqlserverflex/user/create/create_test.go b/internal/cmd/beta/sqlserverflex/user/create/create_test.go index a53878b3d..4c294d6d9 100644 --- a/internal/cmd/beta/sqlserverflex/user/create/create_test.go +++ b/internal/cmd/beta/sqlserverflex/user/create/create_test.go @@ -8,6 +8,7 @@ import ( "github.com/google/go-cmp/cmp/cmpopts" "github.com/google/uuid" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -247,7 +248,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.model, tt.args.instanceLabel, tt.args.user); (err != nil) != tt.wantErr { diff --git a/internal/cmd/beta/sqlserverflex/user/delete/delete.go b/internal/cmd/beta/sqlserverflex/user/delete/delete.go index b85272ba3..dea859e97 100644 --- a/internal/cmd/beta/sqlserverflex/user/delete/delete.go +++ b/internal/cmd/beta/sqlserverflex/user/delete/delete.go @@ -4,6 +4,7 @@ import ( "context" "fmt" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -30,7 +31,7 @@ type inputModel struct { UserId string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("delete %s", userIdArg), Short: "Deletes a SQLServer Flex user", @@ -46,32 +47,32 @@ func NewCmd(p *print.Printer) *cobra.Command { Args: args.SingleArg(userIdArg, nil), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } instanceLabel, err := sqlserverflexUtils.GetInstanceName(ctx, apiClient, model.ProjectId, model.InstanceId, model.Region) if err != nil { - p.Debug(print.ErrorLevel, "get instance name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get instance name: %v", err) instanceLabel = model.InstanceId } userLabel, err := sqlserverflexUtils.GetUserName(ctx, apiClient, model.ProjectId, model.InstanceId, model.UserId, model.Region) if err != nil { - p.Debug(print.ErrorLevel, "get user name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get user name: %v", err) userLabel = model.UserId } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to delete user %q of instance %q? (This cannot be undone)", userLabel, instanceLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -84,7 +85,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("delete SQLServer Flex user: %w", err) } - p.Info("Deleted user %q of instance %q\n", userLabel, instanceLabel) + params.Printer.Info("Deleted user %q of instance %q\n", userLabel, instanceLabel) return nil }, } diff --git a/internal/cmd/beta/sqlserverflex/user/delete/delete_test.go b/internal/cmd/beta/sqlserverflex/user/delete/delete_test.go index 79053edf7..c4345ba6a 100644 --- a/internal/cmd/beta/sqlserverflex/user/delete/delete_test.go +++ b/internal/cmd/beta/sqlserverflex/user/delete/delete_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" @@ -154,7 +155,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/beta/sqlserverflex/user/describe/describe.go b/internal/cmd/beta/sqlserverflex/user/describe/describe.go index e9822bdd0..7ccba07f1 100644 --- a/internal/cmd/beta/sqlserverflex/user/describe/describe.go +++ b/internal/cmd/beta/sqlserverflex/user/describe/describe.go @@ -7,6 +7,7 @@ import ( "strings" "github.com/goccy/go-yaml" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -34,7 +35,7 @@ type inputModel struct { UserId string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("describe %s", userIdArg), Short: "Shows details of a SQLServer Flex user", @@ -54,13 +55,13 @@ func NewCmd(p *print.Printer) *cobra.Command { Args: args.SingleArg(userIdArg, nil), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -72,7 +73,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("get SQLServer Flex user: %w", err) } - return outputResult(p, model.OutputFormat, resp.Item) + return outputResult(params.Printer, model.OutputFormat, resp.Item) }, } diff --git a/internal/cmd/beta/sqlserverflex/user/describe/describe_test.go b/internal/cmd/beta/sqlserverflex/user/describe/describe_test.go index c0d6be049..8f44c6db7 100644 --- a/internal/cmd/beta/sqlserverflex/user/describe/describe_test.go +++ b/internal/cmd/beta/sqlserverflex/user/describe/describe_test.go @@ -7,6 +7,7 @@ import ( "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" "github.com/google/uuid" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-sdk-go/services/sqlserverflex" @@ -153,7 +154,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -256,7 +257,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.user); (err != nil) != tt.wantErr { diff --git a/internal/cmd/beta/sqlserverflex/user/list/list.go b/internal/cmd/beta/sqlserverflex/user/list/list.go index c4fcd2152..29e2c803c 100644 --- a/internal/cmd/beta/sqlserverflex/user/list/list.go +++ b/internal/cmd/beta/sqlserverflex/user/list/list.go @@ -7,6 +7,7 @@ import ( "github.com/goccy/go-yaml" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -32,7 +33,7 @@ type inputModel struct { Limit *int64 } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "list", Short: "Lists all SQLServer Flex users of an instance", @@ -51,13 +52,13 @@ func NewCmd(p *print.Printer) *cobra.Command { Args: args.NoArgs, RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -71,10 +72,10 @@ func NewCmd(p *print.Printer) *cobra.Command { if resp.Items == nil || len(*resp.Items) == 0 { instanceLabel, err := sqlserverflexUtils.GetInstanceName(ctx, apiClient, model.ProjectId, *model.InstanceId, model.Region) if err != nil { - p.Debug(print.ErrorLevel, "get instance name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get instance name: %v", err) instanceLabel = *model.InstanceId } - p.Info("No users found for instance %q\n", instanceLabel) + params.Printer.Info("No users found for instance %q\n", instanceLabel) return nil } users := *resp.Items @@ -84,7 +85,7 @@ func NewCmd(p *print.Printer) *cobra.Command { users = users[:*model.Limit] } - return outputResult(p, model.OutputFormat, users) + return outputResult(params.Printer, model.OutputFormat, users) }, } diff --git a/internal/cmd/beta/sqlserverflex/user/list/list_test.go b/internal/cmd/beta/sqlserverflex/user/list/list_test.go index cc3dabc34..7ec60722f 100644 --- a/internal/cmd/beta/sqlserverflex/user/list/list_test.go +++ b/internal/cmd/beta/sqlserverflex/user/list/list_test.go @@ -8,6 +8,7 @@ import ( "github.com/google/go-cmp/cmp/cmpopts" "github.com/google/uuid" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -228,7 +229,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.users); (err != nil) != tt.wantErr { diff --git a/internal/cmd/beta/sqlserverflex/user/reset-password/reset_password.go b/internal/cmd/beta/sqlserverflex/user/reset-password/reset_password.go index 182a9e147..ff560a2cf 100644 --- a/internal/cmd/beta/sqlserverflex/user/reset-password/reset_password.go +++ b/internal/cmd/beta/sqlserverflex/user/reset-password/reset_password.go @@ -6,6 +6,7 @@ import ( "fmt" "github.com/goccy/go-yaml" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -33,7 +34,7 @@ type inputModel struct { UserId string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("reset-password %s", userIdArg), Short: "Resets the password of a SQLServer Flex user", @@ -49,32 +50,32 @@ func NewCmd(p *print.Printer) *cobra.Command { Args: args.SingleArg(userIdArg, nil), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } instanceLabel, err := sqlserverflexUtils.GetInstanceName(ctx, apiClient, model.ProjectId, model.InstanceId, model.Region) if err != nil { - p.Debug(print.ErrorLevel, "get instance name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get instance name: %v", err) instanceLabel = model.InstanceId } userLabel, err := sqlserverflexUtils.GetUserName(ctx, apiClient, model.ProjectId, model.InstanceId, model.UserId, model.Region) if err != nil { - p.Debug(print.ErrorLevel, "get user name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get user name: %v", err) userLabel = model.UserId } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to reset the password of user %q of instance %q? (This cannot be undone)", userLabel, instanceLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -87,7 +88,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("reset SQLServer Flex user password: %w", err) } - return outputResult(p, model.OutputFormat, userLabel, instanceLabel, user.Item) + return outputResult(params.Printer, model.OutputFormat, userLabel, instanceLabel, user.Item) }, } diff --git a/internal/cmd/beta/sqlserverflex/user/reset-password/reset_password_test.go b/internal/cmd/beta/sqlserverflex/user/reset-password/reset_password_test.go index 8eda0631f..eeeb230a1 100644 --- a/internal/cmd/beta/sqlserverflex/user/reset-password/reset_password_test.go +++ b/internal/cmd/beta/sqlserverflex/user/reset-password/reset_password_test.go @@ -7,6 +7,7 @@ import ( "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" "github.com/google/uuid" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-sdk-go/services/sqlserverflex" @@ -153,7 +154,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -258,7 +259,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.userLabel, tt.args.instanceLabel, tt.args.user); (err != nil) != tt.wantErr { diff --git a/internal/cmd/beta/sqlserverflex/user/user.go b/internal/cmd/beta/sqlserverflex/user/user.go index 9426f3bbf..0c83d54e4 100644 --- a/internal/cmd/beta/sqlserverflex/user/user.go +++ b/internal/cmd/beta/sqlserverflex/user/user.go @@ -6,14 +6,14 @@ import ( "github.com/stackitcloud/stackit-cli/internal/cmd/beta/sqlserverflex/user/describe" "github.com/stackitcloud/stackit-cli/internal/cmd/beta/sqlserverflex/user/list" resetpassword "github.com/stackitcloud/stackit-cli/internal/cmd/beta/sqlserverflex/user/reset-password" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" - "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" "github.com/spf13/cobra" ) -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "user", Short: "Provides functionality for SQLServer Flex users", @@ -21,14 +21,14 @@ func NewCmd(p *print.Printer) *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd, p) + addSubcommands(cmd, params) return cmd } -func addSubcommands(cmd *cobra.Command, p *print.Printer) { - cmd.AddCommand(create.NewCmd(p)) - cmd.AddCommand(delete.NewCmd(p)) - cmd.AddCommand(describe.NewCmd(p)) - cmd.AddCommand(list.NewCmd(p)) - cmd.AddCommand(resetpassword.NewCmd(p)) +func addSubcommands(cmd *cobra.Command, params *params.CmdParams) { + cmd.AddCommand(create.NewCmd(params)) + cmd.AddCommand(delete.NewCmd(params)) + cmd.AddCommand(describe.NewCmd(params)) + cmd.AddCommand(list.NewCmd(params)) + cmd.AddCommand(resetpassword.NewCmd(params)) } diff --git a/internal/cmd/config/config.go b/internal/cmd/config/config.go index a96355a54..46b21b96a 100644 --- a/internal/cmd/config/config.go +++ b/internal/cmd/config/config.go @@ -7,14 +7,14 @@ import ( "github.com/stackitcloud/stackit-cli/internal/cmd/config/profile" "github.com/stackitcloud/stackit-cli/internal/cmd/config/set" "github.com/stackitcloud/stackit-cli/internal/cmd/config/unset" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" - "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" "github.com/spf13/cobra" ) -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "config", Short: "Provides functionality for CLI configuration options", @@ -28,13 +28,13 @@ func NewCmd(p *print.Printer) *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd, p) + addSubcommands(cmd, params) return cmd } -func addSubcommands(cmd *cobra.Command, p *print.Printer) { - cmd.AddCommand(list.NewCmd(p)) - cmd.AddCommand(set.NewCmd(p)) - cmd.AddCommand(unset.NewCmd(p)) - cmd.AddCommand(profile.NewCmd(p)) +func addSubcommands(cmd *cobra.Command, params *params.CmdParams) { + cmd.AddCommand(list.NewCmd(params)) + cmd.AddCommand(set.NewCmd(params)) + cmd.AddCommand(unset.NewCmd(params)) + cmd.AddCommand(profile.NewCmd(params)) } diff --git a/internal/cmd/config/list/list.go b/internal/cmd/config/list/list.go index 7fd7b51ce..6dbc78e7e 100644 --- a/internal/cmd/config/list/list.go +++ b/internal/cmd/config/list/list.go @@ -10,6 +10,7 @@ import ( "strings" "github.com/goccy/go-yaml" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/config" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -25,7 +26,7 @@ type inputModel struct { *globalflags.GlobalFlagModel } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "list", Short: "Lists the current CLI configuration values", @@ -50,14 +51,14 @@ func NewCmd(p *print.Printer) *cobra.Command { RunE: func(cmd *cobra.Command, _ []string) error { configData := viper.AllSettings() - model := parseInput(p, cmd) + model := parseInput(params.Printer, cmd) activeProfile, err := config.GetProfile() if err != nil { return fmt.Errorf("get profile: %w", err) } - return outputResult(p, model.OutputFormat, configData, activeProfile) + return outputResult(params.Printer, model.OutputFormat, configData, activeProfile) }, } return cmd diff --git a/internal/cmd/config/list/list_test.go b/internal/cmd/config/list/list_test.go index d32d5d76a..936e5a76d 100644 --- a/internal/cmd/config/list/list_test.go +++ b/internal/cmd/config/list/list_test.go @@ -3,6 +3,7 @@ package list import ( "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/print" ) @@ -24,7 +25,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.configData, tt.args.activeProfile); (err != nil) != tt.wantErr { diff --git a/internal/cmd/config/profile/create/create.go b/internal/cmd/config/profile/create/create.go index ceec4ee04..8ab83f574 100644 --- a/internal/cmd/config/profile/create/create.go +++ b/internal/cmd/config/profile/create/create.go @@ -3,6 +3,7 @@ package create import ( "fmt" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/auth" "github.com/stackitcloud/stackit-cli/internal/pkg/config" @@ -28,7 +29,7 @@ type inputModel struct { Profile string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("create %s", profileArg), Short: "Creates a CLI configuration profile", @@ -49,30 +50,30 @@ func NewCmd(p *print.Printer) *cobra.Command { "$ stackit config profile create my-profile --empty --no-set"), ), RunE: func(cmd *cobra.Command, args []string) error { - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } - err = config.CreateProfile(p, model.Profile, !model.NoSet, model.FromEmptyProfile) + err = config.CreateProfile(params.Printer, model.Profile, !model.NoSet, model.FromEmptyProfile) if err != nil { return fmt.Errorf("create profile: %w", err) } if model.NoSet { - p.Info("Successfully created profile %q\n", model.Profile) + params.Printer.Info("Successfully created profile %q\n", model.Profile) return nil } - p.Info("Successfully created and set active profile to %q\n", model.Profile) + params.Printer.Info("Successfully created and set active profile to %q\n", model.Profile) flow, err := auth.GetAuthFlow() if err != nil { - p.Debug(print.WarningLevel, "both keyring and text file storage failed to find a valid authentication flow for the active profile") - p.Warn("The active profile %q is not authenticated, please login using the 'stackit auth login' command.\n", model.Profile) + params.Printer.Debug(print.WarningLevel, "both keyring and text file storage failed to find a valid authentication flow for the active profile") + params.Printer.Warn("The active profile %q is not authenticated, please login using the 'stackit auth login' command.\n", model.Profile) return nil } - p.Debug(print.DebugLevel, "found valid authentication flow for active profile: %s", flow) + params.Printer.Debug(print.DebugLevel, "found valid authentication flow for active profile: %s", flow) return nil }, diff --git a/internal/cmd/config/profile/create/create_test.go b/internal/cmd/config/profile/create/create_test.go index 0cc32cc9d..097071947 100644 --- a/internal/cmd/config/profile/create/create_test.go +++ b/internal/cmd/config/profile/create/create_test.go @@ -3,6 +3,7 @@ package create import ( "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" @@ -104,7 +105,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/config/profile/delete/delete.go b/internal/cmd/config/profile/delete/delete.go index 992750c29..88d00907f 100644 --- a/internal/cmd/config/profile/delete/delete.go +++ b/internal/cmd/config/profile/delete/delete.go @@ -3,6 +3,7 @@ package delete import ( "fmt" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/auth" "github.com/stackitcloud/stackit-cli/internal/pkg/config" @@ -23,7 +24,7 @@ type inputModel struct { Profile string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("delete %s", profileArg), Short: "Delete a CLI configuration profile", @@ -38,7 +39,7 @@ func NewCmd(p *print.Printer) *cobra.Command { "$ stackit config profile delete my-profile"), ), RunE: func(cmd *cobra.Command, args []string) error { - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } @@ -60,18 +61,18 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("get profile: %w", err) } if activeProfile == model.Profile { - p.Warn("The profile you are trying to delete is the active profile. The default profile will be set to active.\n") + params.Printer.Warn("The profile you are trying to delete is the active profile. The default profile will be set to active.\n") } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to delete profile %q? (This cannot be undone)", model.Profile) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } } - err = config.DeleteProfile(p, model.Profile) + err = config.DeleteProfile(params.Printer, model.Profile) if err != nil { return fmt.Errorf("delete profile: %w", err) } @@ -81,7 +82,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("delete profile authentication: %w", err) } - p.Info("Successfully deleted profile %q\n", model.Profile) + params.Printer.Info("Successfully deleted profile %q\n", model.Profile) return nil }, diff --git a/internal/cmd/config/profile/delete/delete_test.go b/internal/cmd/config/profile/delete/delete_test.go index 3919460b7..018ded22a 100644 --- a/internal/cmd/config/profile/delete/delete_test.go +++ b/internal/cmd/config/profile/delete/delete_test.go @@ -3,6 +3,7 @@ package delete import ( "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" @@ -80,7 +81,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/config/profile/export/export.go b/internal/cmd/config/profile/export/export.go index 9aa585971..59389b47a 100644 --- a/internal/cmd/config/profile/export/export.go +++ b/internal/cmd/config/profile/export/export.go @@ -4,6 +4,7 @@ import ( "fmt" "path/filepath" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/config" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -28,7 +29,7 @@ type inputModel struct { FilePath string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("export %s", profileNameArg), Short: "Exports a CLI configuration profile", @@ -45,17 +46,17 @@ func NewCmd(p *print.Printer) *cobra.Command { ), Args: args.SingleArg(profileNameArg, nil), RunE: func(cmd *cobra.Command, args []string) error { - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } - err = config.ExportProfile(p, model.ProfileName, model.FilePath) + err = config.ExportProfile(params.Printer, model.ProfileName, model.FilePath) if err != nil { return fmt.Errorf("could not export profile: %w", err) } - p.Info("Exported profile %q to %q\n", model.ProfileName, model.FilePath) + params.Printer.Info("Exported profile %q to %q\n", model.ProfileName, model.FilePath) return nil }, diff --git a/internal/cmd/config/profile/export/export_test.go b/internal/cmd/config/profile/export/export_test.go index 4dffd3a22..d15885813 100644 --- a/internal/cmd/config/profile/export/export_test.go +++ b/internal/cmd/config/profile/export/export_test.go @@ -4,6 +4,7 @@ import ( "fmt" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" @@ -102,7 +103,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/config/profile/import/import.go b/internal/cmd/config/profile/import/import.go index d57cb1929..84bd9b3aa 100644 --- a/internal/cmd/config/profile/import/import.go +++ b/internal/cmd/config/profile/import/import.go @@ -2,6 +2,7 @@ package importProfile import ( "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/config" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" @@ -24,7 +25,7 @@ type inputModel struct { NoSet bool } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "import", Short: "Imports a CLI configuration profile", @@ -41,17 +42,17 @@ func NewCmd(p *print.Printer) *cobra.Command { ), Args: args.NoArgs, RunE: func(cmd *cobra.Command, _ []string) error { - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } - err = config.ImportProfile(p, model.ProfileName, model.Config, !model.NoSet) + err = config.ImportProfile(params.Printer, model.ProfileName, model.Config, !model.NoSet) if err != nil { return err } - p.Info("Successfully imported profile %q\n", model.ProfileName) + params.Printer.Info("Successfully imported profile %q\n", model.ProfileName) return nil }, diff --git a/internal/cmd/config/profile/import/import_test.go b/internal/cmd/config/profile/import/import_test.go index 7e028ab59..121b7adb9 100644 --- a/internal/cmd/config/profile/import/import_test.go +++ b/internal/cmd/config/profile/import/import_test.go @@ -5,6 +5,7 @@ import ( "strconv" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" @@ -75,7 +76,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/config/profile/list/list.go b/internal/cmd/config/profile/list/list.go index 10f2e239b..2a43aa417 100644 --- a/internal/cmd/config/profile/list/list.go +++ b/internal/cmd/config/profile/list/list.go @@ -5,6 +5,7 @@ import ( "fmt" "github.com/goccy/go-yaml" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/auth" "github.com/stackitcloud/stackit-cli/internal/pkg/config" @@ -20,7 +21,7 @@ type inputModel struct { *globalflags.GlobalFlagModel } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "list", Short: "Lists all CLI configuration profiles", @@ -35,7 +36,7 @@ func NewCmd(p *print.Printer) *cobra.Command { "$ stackit config profile list --output-format json"), ), RunE: func(cmd *cobra.Command, _ []string) error { - model := parseInput(p, cmd) + model := parseInput(params.Printer, cmd) profiles, err := config.ListProfiles() if err != nil { @@ -49,7 +50,7 @@ func NewCmd(p *print.Printer) *cobra.Command { outputProfiles := buildOutput(profiles, activeProfile) - return outputResult(p, model.OutputFormat, outputProfiles) + return outputResult(params.Printer, model.OutputFormat, outputProfiles) }, } return cmd diff --git a/internal/cmd/config/profile/list/list_test.go b/internal/cmd/config/profile/list/list_test.go index 80e080c56..8016b6071 100644 --- a/internal/cmd/config/profile/list/list_test.go +++ b/internal/cmd/config/profile/list/list_test.go @@ -3,6 +3,7 @@ package list import ( "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/print" ) @@ -23,7 +24,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.profiles); (err != nil) != tt.wantErr { diff --git a/internal/cmd/config/profile/profile.go b/internal/cmd/config/profile/profile.go index 848a60382..f6ad03ece 100644 --- a/internal/cmd/config/profile/profile.go +++ b/internal/cmd/config/profile/profile.go @@ -10,14 +10,14 @@ import ( "github.com/stackitcloud/stackit-cli/internal/cmd/config/profile/list" "github.com/stackitcloud/stackit-cli/internal/cmd/config/profile/set" "github.com/stackitcloud/stackit-cli/internal/cmd/config/profile/unset" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" - "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" "github.com/spf13/cobra" ) -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "profile", Short: "Manage the CLI configuration profiles", @@ -30,16 +30,16 @@ func NewCmd(p *print.Printer) *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd, p) + addSubcommands(cmd, params) return cmd } -func addSubcommands(cmd *cobra.Command, p *print.Printer) { - cmd.AddCommand(set.NewCmd(p)) - cmd.AddCommand(unset.NewCmd(p)) - cmd.AddCommand(create.NewCmd(p)) - cmd.AddCommand(list.NewCmd(p)) - cmd.AddCommand(delete.NewCmd(p)) - cmd.AddCommand(importProfile.NewCmd(p)) - cmd.AddCommand(export.NewCmd(p)) +func addSubcommands(cmd *cobra.Command, params *params.CmdParams) { + cmd.AddCommand(set.NewCmd(params)) + cmd.AddCommand(unset.NewCmd(params)) + cmd.AddCommand(create.NewCmd(params)) + cmd.AddCommand(list.NewCmd(params)) + cmd.AddCommand(delete.NewCmd(params)) + cmd.AddCommand(importProfile.NewCmd(params)) + cmd.AddCommand(export.NewCmd(params)) } diff --git a/internal/cmd/config/profile/set/set.go b/internal/cmd/config/profile/set/set.go index ac43977b3..0784d4654 100644 --- a/internal/cmd/config/profile/set/set.go +++ b/internal/cmd/config/profile/set/set.go @@ -3,6 +3,7 @@ package set import ( "fmt" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/auth" "github.com/stackitcloud/stackit-cli/internal/pkg/config" @@ -23,7 +24,7 @@ type inputModel struct { Profile string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("set %s", profileArg), Short: "Set a CLI configuration profile", @@ -40,7 +41,7 @@ func NewCmd(p *print.Printer) *cobra.Command { "$ stackit config profile set my-profile"), ), RunE: func(cmd *cobra.Command, args []string) error { - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } @@ -53,20 +54,20 @@ func NewCmd(p *print.Printer) *cobra.Command { return &errors.SetInexistentProfile{Profile: model.Profile} } - err = config.SetProfile(p, model.Profile) + err = config.SetProfile(params.Printer, model.Profile) if err != nil { return fmt.Errorf("set profile: %w", err) } - p.Info("Successfully set active profile to %q\n", model.Profile) + params.Printer.Info("Successfully set active profile to %q\n", model.Profile) flow, err := auth.GetAuthFlow() if err != nil { - p.Debug(print.WarningLevel, "both keyring and text file storage failed to find a valid authentication flow for the active profile") - p.Warn("The active profile %q is not authenticated, please login using the 'stackit auth login' command.\n", model.Profile) + params.Printer.Debug(print.WarningLevel, "both keyring and text file storage failed to find a valid authentication flow for the active profile") + params.Printer.Warn("The active profile %q is not authenticated, please login using the 'stackit auth login' command.\n", model.Profile) return nil } - p.Debug(print.DebugLevel, "found valid authentication flow for active profile: %s", flow) + params.Printer.Debug(print.DebugLevel, "found valid authentication flow for active profile: %s", flow) return nil }, diff --git a/internal/cmd/config/profile/set/set_test.go b/internal/cmd/config/profile/set/set_test.go index 47f56ca0b..a12b0f328 100644 --- a/internal/cmd/config/profile/set/set_test.go +++ b/internal/cmd/config/profile/set/set_test.go @@ -3,6 +3,7 @@ package set import ( "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" @@ -80,7 +81,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/config/profile/unset/unset.go b/internal/cmd/config/profile/unset/unset.go index 4c06edb60..9c7f923cd 100644 --- a/internal/cmd/config/profile/unset/unset.go +++ b/internal/cmd/config/profile/unset/unset.go @@ -3,6 +3,7 @@ package unset import ( "fmt" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/auth" "github.com/stackitcloud/stackit-cli/internal/pkg/config" @@ -12,7 +13,7 @@ import ( "github.com/spf13/cobra" ) -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "unset", Short: "Unset the current active CLI configuration profile", @@ -27,20 +28,20 @@ func NewCmd(p *print.Printer) *cobra.Command { "$ stackit config profile unset"), ), RunE: func(_ *cobra.Command, _ []string) error { - err := config.UnsetProfile(p) + err := config.UnsetProfile(params.Printer) if err != nil { return fmt.Errorf("unset profile: %w", err) } - p.Info("Profile unset successfully. The default profile will be used.\n") + params.Printer.Info("Profile unset successfully. The default profile will be used.\n") flow, err := auth.GetAuthFlow() if err != nil { - p.Debug(print.WarningLevel, "both keyring and text file storage failed to find a valid authentication flow for the active profile") - p.Warn("The default profile is not authenticated, please login using the 'stackit auth login' command.\n") + params.Printer.Debug(print.WarningLevel, "both keyring and text file storage failed to find a valid authentication flow for the active profile") + params.Printer.Warn("The default profile is not authenticated, please login using the 'stackit auth login' command.\n") return nil } - p.Debug(print.DebugLevel, "found valid authentication flow for active profile: %s", flow) + params.Printer.Debug(print.DebugLevel, "found valid authentication flow for active profile: %s", flow) return nil }, diff --git a/internal/cmd/config/set/set.go b/internal/cmd/config/set/set.go index b8defa692..54f9f527d 100644 --- a/internal/cmd/config/set/set.go +++ b/internal/cmd/config/set/set.go @@ -4,6 +4,7 @@ import ( "fmt" "time" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/config" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" @@ -53,7 +54,7 @@ type inputModel struct { ProjectIdSet bool } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "set", Short: "Sets CLI configuration options", @@ -76,13 +77,13 @@ func NewCmd(p *print.Printer) *cobra.Command { "$ stackit config set --dns-custom-endpoint https://dns.stackit.cloud"), ), RunE: func(cmd *cobra.Command, _ []string) error { - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } if model.SessionTimeLimit != nil { - p.Warn("Authenticate again to apply changes to session time limit\n") + params.Printer.Warn("Authenticate again to apply changes to session time limit\n") viper.Set(config.SessionTimeLimitKey, *model.SessionTimeLimit) } diff --git a/internal/cmd/config/set/set_test.go b/internal/cmd/config/set/set_test.go index 9b317b5ad..e9647e268 100644 --- a/internal/cmd/config/set/set_test.go +++ b/internal/cmd/config/set/set_test.go @@ -3,6 +3,7 @@ package set import ( "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -122,7 +123,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/config/unset/unset.go b/internal/cmd/config/unset/unset.go index 72d79aeb3..a288be6a3 100644 --- a/internal/cmd/config/unset/unset.go +++ b/internal/cmd/config/unset/unset.go @@ -3,6 +3,7 @@ package unset import ( "fmt" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/config" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -88,7 +89,7 @@ type inputModel struct { TokenCustomEndpoint bool } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "unset", Short: "Unsets CLI configuration options", @@ -106,7 +107,7 @@ func NewCmd(p *print.Printer) *cobra.Command { "$ stackit config unset --dns-custom-endpoint"), ), RunE: func(cmd *cobra.Command, _ []string) error { - model := parseInput(p, cmd) + model := parseInput(params.Printer, cmd) if model.Async { viper.Set(config.AsyncKey, config.AsyncDefault) diff --git a/internal/cmd/config/unset/unset_test.go b/internal/cmd/config/unset/unset_test.go index bc262c002..a977f8aa2 100644 --- a/internal/cmd/config/unset/unset_test.go +++ b/internal/cmd/config/unset/unset_test.go @@ -4,6 +4,7 @@ import ( "fmt" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/google/go-cmp/cmp" @@ -291,7 +292,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) for flag, value := range tt.flagValues { stringBool := fmt.Sprintf("%v", value) diff --git a/internal/cmd/curl/curl.go b/internal/cmd/curl/curl.go index 9959e50f1..15b79627a 100644 --- a/internal/cmd/curl/curl.go +++ b/internal/cmd/curl/curl.go @@ -11,6 +11,7 @@ import ( "strings" "time" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/auth" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" @@ -45,7 +46,7 @@ type inputModel struct { OutputFile *string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("curl %s", urlArg), Short: "Executes an authenticated HTTP request to an endpoint", @@ -70,12 +71,12 @@ func NewCmd(p *print.Printer) *cobra.Command { ), Args: args.SingleArg(urlArg, utils.ValidateURLDomain), RunE: func(cmd *cobra.Command, args []string) (err error) { - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } - bearerToken, err := getBearerToken(p) + bearerToken, err := getBearerToken(params.Printer) if err != nil { return err } @@ -99,7 +100,7 @@ func NewCmd(p *print.Printer) *cobra.Command { } }() - err = outputResponse(p, model, resp) + err = outputResponse(params.Printer, model, resp) if err != nil { return err } diff --git a/internal/cmd/curl/curl_test.go b/internal/cmd/curl/curl_test.go index c7b4dcf4b..3fbab3cf4 100644 --- a/internal/cmd/curl/curl_test.go +++ b/internal/cmd/curl/curl_test.go @@ -13,6 +13,7 @@ import ( "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" "github.com/spf13/viper" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/config" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" @@ -216,7 +217,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -447,7 +448,7 @@ func TestOutputResponse(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResponse(p, tt.args.model, tt.args.resp); (err != nil) != tt.wantErr { diff --git a/internal/cmd/dns/dns.go b/internal/cmd/dns/dns.go index a89e959c5..ccba3b66d 100644 --- a/internal/cmd/dns/dns.go +++ b/internal/cmd/dns/dns.go @@ -3,14 +3,14 @@ package dns import ( recordset "github.com/stackitcloud/stackit-cli/internal/cmd/dns/record-set" "github.com/stackitcloud/stackit-cli/internal/cmd/dns/zone" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" - "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" "github.com/spf13/cobra" ) -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "dns", Short: "Provides functionality for DNS", @@ -18,11 +18,11 @@ func NewCmd(p *print.Printer) *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd, p) + addSubcommands(cmd, params) return cmd } -func addSubcommands(cmd *cobra.Command, p *print.Printer) { - cmd.AddCommand(zone.NewCmd(p)) - cmd.AddCommand(recordset.NewCmd(p)) +func addSubcommands(cmd *cobra.Command, params *params.CmdParams) { + cmd.AddCommand(zone.NewCmd(params)) + cmd.AddCommand(recordset.NewCmd(params)) } diff --git a/internal/cmd/dns/record-set/create/create.go b/internal/cmd/dns/record-set/create/create.go index 65ea58911..8441db8ac 100644 --- a/internal/cmd/dns/record-set/create/create.go +++ b/internal/cmd/dns/record-set/create/create.go @@ -7,6 +7,7 @@ import ( "github.com/goccy/go-yaml" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -43,7 +44,7 @@ type inputModel struct { Type string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "create", Short: "Creates a DNS record set", @@ -56,26 +57,26 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } zoneLabel, err := dnsUtils.GetZoneName(ctx, apiClient, model.ProjectId, model.ZoneId) if err != nil { - p.Debug(print.ErrorLevel, "get zone name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get zone name: %v", err) zoneLabel = model.ZoneId } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to create a record set for zone %s?", zoneLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -91,7 +92,7 @@ func NewCmd(p *print.Printer) *cobra.Command { // Wait for async operation, if async mode not enabled if !model.Async { - s := spinner.New(p) + s := spinner.New(params.Printer) s.Start("Creating record set") _, err = wait.CreateRecordSetWaitHandler(ctx, apiClient, model.ProjectId, model.ZoneId, recordSetId).WaitWithContext(ctx) if err != nil { @@ -100,7 +101,7 @@ func NewCmd(p *print.Printer) *cobra.Command { s.Stop() } - return outputResult(p, model, zoneLabel, resp) + return outputResult(params.Printer, model, zoneLabel, resp) }, } configureFlags(cmd) diff --git a/internal/cmd/dns/record-set/create/create_test.go b/internal/cmd/dns/record-set/create/create_test.go index eed9f9424..99a216aeb 100644 --- a/internal/cmd/dns/record-set/create/create_test.go +++ b/internal/cmd/dns/record-set/create/create_test.go @@ -9,6 +9,7 @@ import ( "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" "github.com/google/uuid" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -267,7 +268,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -395,7 +396,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.model, tt.args.zoneLabel, tt.args.resp); (err != nil) != tt.wantErr { diff --git a/internal/cmd/dns/record-set/delete/delete.go b/internal/cmd/dns/record-set/delete/delete.go index 4bb3a1c88..a4026478f 100644 --- a/internal/cmd/dns/record-set/delete/delete.go +++ b/internal/cmd/dns/record-set/delete/delete.go @@ -4,6 +4,7 @@ import ( "context" "fmt" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -32,7 +33,7 @@ type inputModel struct { RecordSetId string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("delete %s", recordSetIdArg), Short: "Deletes a DNS record set", @@ -45,32 +46,32 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } zoneLabel, err := dnsUtils.GetZoneName(ctx, apiClient, model.ProjectId, model.ZoneId) if err != nil { - p.Debug(print.ErrorLevel, "get zone name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get zone name: %v", err) zoneLabel = model.ZoneId } recordSetLabel, err := dnsUtils.GetRecordSetName(ctx, apiClient, model.ProjectId, model.ZoneId, model.RecordSetId) if err != nil { - p.Debug(print.ErrorLevel, "get record set name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get record set name: %v", err) recordSetLabel = model.RecordSetId } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to delete record set %s of zone %s? (This cannot be undone)", recordSetLabel, zoneLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -88,7 +89,7 @@ func NewCmd(p *print.Printer) *cobra.Command { // Wait for async operation, if async mode not enabled if !model.Async { - s := spinner.New(p) + s := spinner.New(params.Printer) s.Start("Deleting record set") _, err = wait.DeleteRecordSetWaitHandler(ctx, apiClient, model.ProjectId, model.ZoneId, model.RecordSetId).WaitWithContext(ctx) if err != nil { @@ -101,7 +102,7 @@ func NewCmd(p *print.Printer) *cobra.Command { if model.Async { operationState = "Triggered deletion of" } - p.Info("%s record set %s of zone %s\n", operationState, recordSetLabel, zoneLabel) + params.Printer.Info("%s record set %s of zone %s\n", operationState, recordSetLabel, zoneLabel) return nil }, } diff --git a/internal/cmd/dns/record-set/delete/delete_test.go b/internal/cmd/dns/record-set/delete/delete_test.go index 137c85267..55b534fde 100644 --- a/internal/cmd/dns/record-set/delete/delete_test.go +++ b/internal/cmd/dns/record-set/delete/delete_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" @@ -165,7 +166,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/dns/record-set/describe/describe.go b/internal/cmd/dns/record-set/describe/describe.go index 03a757a7a..8f80674b8 100644 --- a/internal/cmd/dns/record-set/describe/describe.go +++ b/internal/cmd/dns/record-set/describe/describe.go @@ -7,6 +7,7 @@ import ( "strings" "github.com/goccy/go-yaml" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -33,7 +34,7 @@ type inputModel struct { RecordSetId string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("describe %s", recordSetIdArg), Short: "Shows details of a DNS record set", @@ -49,13 +50,13 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -68,7 +69,7 @@ func NewCmd(p *print.Printer) *cobra.Command { } recordSet := resp.Rrset - return outputResult(p, model.OutputFormat, recordSet) + return outputResult(params.Printer, model.OutputFormat, recordSet) }, } configureFlags(cmd) diff --git a/internal/cmd/dns/record-set/describe/describe_test.go b/internal/cmd/dns/record-set/describe/describe_test.go index a2cbc48e7..55a032bd8 100644 --- a/internal/cmd/dns/record-set/describe/describe_test.go +++ b/internal/cmd/dns/record-set/describe/describe_test.go @@ -7,6 +7,7 @@ import ( "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" "github.com/google/uuid" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-sdk-go/services/dns" @@ -164,7 +165,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -269,7 +270,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.recordSet); (err != nil) != tt.wantErr { diff --git a/internal/cmd/dns/record-set/list/list.go b/internal/cmd/dns/record-set/list/list.go index 2062a7c84..99a07bb5a 100644 --- a/internal/cmd/dns/record-set/list/list.go +++ b/internal/cmd/dns/record-set/list/list.go @@ -9,6 +9,7 @@ import ( "github.com/goccy/go-yaml" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -50,7 +51,7 @@ type inputModel struct { PageSize int64 } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "list", Short: "Lists DNS record sets", @@ -75,13 +76,13 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -94,13 +95,13 @@ func NewCmd(p *print.Printer) *cobra.Command { if len(recordSets) == 0 { zoneLabel, err := dnsUtils.GetZoneName(ctx, apiClient, model.ProjectId, model.ZoneId) if err != nil { - p.Debug(print.ErrorLevel, "get zone name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get zone name: %v", err) zoneLabel = model.ZoneId } - p.Info("No record sets found for zone %s matching the criteria\n", zoneLabel) + params.Printer.Info("No record sets found for zone %s matching the criteria\n", zoneLabel) return nil } - return outputResult(p, model.OutputFormat, recordSets) + return outputResult(params.Printer, model.OutputFormat, recordSets) }, } diff --git a/internal/cmd/dns/record-set/list/list_test.go b/internal/cmd/dns/record-set/list/list_test.go index d41e1f304..122a2b29d 100644 --- a/internal/cmd/dns/record-set/list/list_test.go +++ b/internal/cmd/dns/record-set/list/list_test.go @@ -11,6 +11,7 @@ import ( "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" "github.com/google/uuid" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -214,7 +215,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -518,7 +519,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.recordSets); (err != nil) != tt.wantErr { diff --git a/internal/cmd/dns/record-set/record_set.go b/internal/cmd/dns/record-set/record_set.go index 698750f4b..d00152da0 100644 --- a/internal/cmd/dns/record-set/record_set.go +++ b/internal/cmd/dns/record-set/record_set.go @@ -6,14 +6,14 @@ import ( "github.com/stackitcloud/stackit-cli/internal/cmd/dns/record-set/describe" "github.com/stackitcloud/stackit-cli/internal/cmd/dns/record-set/list" "github.com/stackitcloud/stackit-cli/internal/cmd/dns/record-set/update" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" - "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" "github.com/spf13/cobra" ) -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "record-set", Short: "Provides functionality for DNS record set", @@ -21,14 +21,14 @@ func NewCmd(p *print.Printer) *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd, p) + addSubcommands(cmd, params) return cmd } -func addSubcommands(cmd *cobra.Command, p *print.Printer) { - cmd.AddCommand(list.NewCmd(p)) - cmd.AddCommand(create.NewCmd(p)) - cmd.AddCommand(describe.NewCmd(p)) - cmd.AddCommand(delete.NewCmd(p)) - cmd.AddCommand(update.NewCmd(p)) +func addSubcommands(cmd *cobra.Command, params *params.CmdParams) { + cmd.AddCommand(list.NewCmd(params)) + cmd.AddCommand(create.NewCmd(params)) + cmd.AddCommand(describe.NewCmd(params)) + cmd.AddCommand(delete.NewCmd(params)) + cmd.AddCommand(update.NewCmd(params)) } diff --git a/internal/cmd/dns/record-set/update/update.go b/internal/cmd/dns/record-set/update/update.go index 2b3d258aa..6d2a7ea06 100644 --- a/internal/cmd/dns/record-set/update/update.go +++ b/internal/cmd/dns/record-set/update/update.go @@ -4,6 +4,7 @@ import ( "context" "fmt" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -42,7 +43,7 @@ type inputModel struct { Type *string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("update %s", recordSetIdArg), Short: "Updates a DNS record set", @@ -55,32 +56,32 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } zoneLabel, err := dnsUtils.GetZoneName(ctx, apiClient, model.ProjectId, model.ZoneId) if err != nil { - p.Debug(print.ErrorLevel, "get zone name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get zone name: %v", err) zoneLabel = model.ZoneId } recordSetLabel, err := dnsUtils.GetRecordSetName(ctx, apiClient, model.ProjectId, model.ZoneId, model.RecordSetId) if err != nil { - p.Debug(print.ErrorLevel, "get record set name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get record set name: %v", err) recordSetLabel = model.RecordSetId } typeLabel, err := dnsUtils.GetRecordSetType(ctx, apiClient, model.ProjectId, model.ZoneId, model.RecordSetId) if err != nil { - p.Debug(print.ErrorLevel, "get record set type: %v", err) + params.Printer.Debug(print.ErrorLevel, "get record set type: %v", err) } model.Type = typeLabel @@ -93,7 +94,7 @@ func NewCmd(p *print.Printer) *cobra.Command { if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to update record set %s of zone %s?", recordSetLabel, zoneLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -108,7 +109,7 @@ func NewCmd(p *print.Printer) *cobra.Command { // Wait for async operation, if async mode not enabled if !model.Async { - s := spinner.New(p) + s := spinner.New(params.Printer) s.Start("Updating record set") _, err = wait.PartialUpdateRecordSetWaitHandler(ctx, apiClient, model.ProjectId, model.ZoneId, model.RecordSetId).WaitWithContext(ctx) if err != nil { @@ -121,7 +122,7 @@ func NewCmd(p *print.Printer) *cobra.Command { if model.Async { operationState = "Triggered update of" } - p.Info("%s record set %s of zone %s\n", operationState, recordSetLabel, zoneLabel) + params.Printer.Info("%s record set %s of zone %s\n", operationState, recordSetLabel, zoneLabel) return nil }, } diff --git a/internal/cmd/dns/record-set/update/update_test.go b/internal/cmd/dns/record-set/update/update_test.go index 9b07ffabb..99e32d0f9 100644 --- a/internal/cmd/dns/record-set/update/update_test.go +++ b/internal/cmd/dns/record-set/update/update_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -253,7 +254,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/dns/zone/clone/clone.go b/internal/cmd/dns/zone/clone/clone.go index a7a4e42a0..1894b2291 100644 --- a/internal/cmd/dns/zone/clone/clone.go +++ b/internal/cmd/dns/zone/clone/clone.go @@ -6,6 +6,7 @@ import ( "fmt" "github.com/goccy/go-yaml" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -39,7 +40,7 @@ type inputModel struct { ZoneId string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("clone %s", zoneIdArg), Short: "Clones a DNS zone", @@ -58,26 +59,26 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } zoneLabel, err := dnsUtils.GetZoneName(ctx, apiClient, model.ProjectId, model.ZoneId) if err != nil { - p.Debug(print.ErrorLevel, "get zone name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get zone name: %v", err) zoneLabel = model.ZoneId } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to clone the zone %q?", zoneLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -93,7 +94,7 @@ func NewCmd(p *print.Printer) *cobra.Command { // Wait for async operation, if async mode not enabled if !model.Async { - s := spinner.New(p) + s := spinner.New(params.Printer) s.Start("Cloning zone") _, err = wait.CreateZoneWaitHandler(ctx, apiClient, model.ProjectId, zoneId).WaitWithContext(ctx) if err != nil { @@ -102,7 +103,7 @@ func NewCmd(p *print.Printer) *cobra.Command { s.Stop() } - return outputResult(p, model, zoneLabel, resp) + return outputResult(params.Printer, model, zoneLabel, resp) }, } configureFlags(cmd) diff --git a/internal/cmd/dns/zone/clone/clone_test.go b/internal/cmd/dns/zone/clone/clone_test.go index ea4f0621b..01f738f3a 100644 --- a/internal/cmd/dns/zone/clone/clone_test.go +++ b/internal/cmd/dns/zone/clone/clone_test.go @@ -7,6 +7,7 @@ import ( "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" "github.com/google/uuid" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -169,7 +170,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -291,7 +292,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.model, tt.args.projectLabel, tt.args.resp); (err != nil) != tt.wantErr { diff --git a/internal/cmd/dns/zone/create/create.go b/internal/cmd/dns/zone/create/create.go index fadfbd183..d23d35f39 100644 --- a/internal/cmd/dns/zone/create/create.go +++ b/internal/cmd/dns/zone/create/create.go @@ -6,6 +6,7 @@ import ( "fmt" "github.com/goccy/go-yaml" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -55,7 +56,7 @@ type inputModel struct { ContactEmail *string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "create", Short: "Creates a DNS zone", @@ -71,26 +72,26 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } - projectLabel, err := projectname.GetProjectName(ctx, p, cmd) + projectLabel, err := projectname.GetProjectName(ctx, params.Printer, cmd) if err != nil { - p.Debug(print.ErrorLevel, "get project name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get project name: %v", err) projectLabel = model.ProjectId } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to create a zone for project %q?", projectLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -106,7 +107,7 @@ func NewCmd(p *print.Printer) *cobra.Command { // Wait for async operation, if async mode not enabled if !model.Async { - s := spinner.New(p) + s := spinner.New(params.Printer) s.Start("Creating zone") _, err = wait.CreateZoneWaitHandler(ctx, apiClient, model.ProjectId, zoneId).WaitWithContext(ctx) if err != nil { @@ -115,7 +116,7 @@ func NewCmd(p *print.Printer) *cobra.Command { s.Stop() } - return outputResult(p, model, projectLabel, resp) + return outputResult(params.Printer, model, projectLabel, resp) }, } configureFlags(cmd) diff --git a/internal/cmd/dns/zone/create/create_test.go b/internal/cmd/dns/zone/create/create_test.go index 5bcc8728a..39412b8ac 100644 --- a/internal/cmd/dns/zone/create/create_test.go +++ b/internal/cmd/dns/zone/create/create_test.go @@ -7,6 +7,7 @@ import ( "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" "github.com/google/uuid" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -216,7 +217,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -339,7 +340,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.model, tt.args.projectLabel, tt.args.resp); (err != nil) != tt.wantErr { diff --git a/internal/cmd/dns/zone/delete/delete.go b/internal/cmd/dns/zone/delete/delete.go index 2ad4373dd..8c5b01bd9 100644 --- a/internal/cmd/dns/zone/delete/delete.go +++ b/internal/cmd/dns/zone/delete/delete.go @@ -4,6 +4,7 @@ import ( "context" "fmt" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -28,7 +29,7 @@ type inputModel struct { ZoneId string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("delete %s", zoneIdArg), Short: "Deletes a DNS zone", @@ -41,25 +42,25 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } zoneLabel, err := dnsUtils.GetZoneName(ctx, apiClient, model.ProjectId, model.ZoneId) if err != nil { - p.Debug(print.ErrorLevel, "get zone name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get zone name: %v", err) zoneLabel = model.ZoneId } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to delete zone %q? (This cannot be undone)", zoneLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -77,7 +78,7 @@ func NewCmd(p *print.Printer) *cobra.Command { // Wait for async operation, if async mode not enabled if !model.Async { - s := spinner.New(p) + s := spinner.New(params.Printer) s.Start("Deleting zone") _, err = wait.DeleteZoneWaitHandler(ctx, apiClient, model.ProjectId, model.ZoneId).WaitWithContext(ctx) if err != nil { @@ -90,7 +91,7 @@ func NewCmd(p *print.Printer) *cobra.Command { if model.Async { operationState = "Triggered deletion of" } - p.Info("%s zone %s\n", operationState, zoneLabel) + params.Printer.Info("%s zone %s\n", operationState, zoneLabel) return nil }, } diff --git a/internal/cmd/dns/zone/delete/delete_test.go b/internal/cmd/dns/zone/delete/delete_test.go index 38c534910..77cb37649 100644 --- a/internal/cmd/dns/zone/delete/delete_test.go +++ b/internal/cmd/dns/zone/delete/delete_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" @@ -138,7 +139,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/dns/zone/describe/describe.go b/internal/cmd/dns/zone/describe/describe.go index bd6ada66d..b97ba0445 100644 --- a/internal/cmd/dns/zone/describe/describe.go +++ b/internal/cmd/dns/zone/describe/describe.go @@ -6,6 +6,7 @@ import ( "fmt" "github.com/goccy/go-yaml" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -28,7 +29,7 @@ type inputModel struct { ZoneId string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("describe %s", zoneIdArg), Short: "Shows details of a DNS zone", @@ -44,12 +45,12 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -62,7 +63,7 @@ func NewCmd(p *print.Printer) *cobra.Command { } zone := resp.Zone - return outputResult(p, model.OutputFormat, zone) + return outputResult(params.Printer, model.OutputFormat, zone) }, } return cmd diff --git a/internal/cmd/dns/zone/describe/describe_test.go b/internal/cmd/dns/zone/describe/describe_test.go index e97810889..ed58cbb14 100644 --- a/internal/cmd/dns/zone/describe/describe_test.go +++ b/internal/cmd/dns/zone/describe/describe_test.go @@ -7,6 +7,7 @@ import ( "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" "github.com/google/uuid" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-sdk-go/services/dns" @@ -137,7 +138,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -242,7 +243,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.zone); (err != nil) != tt.wantErr { diff --git a/internal/cmd/dns/zone/list/list.go b/internal/cmd/dns/zone/list/list.go index 73239b2c3..017a171df 100644 --- a/internal/cmd/dns/zone/list/list.go +++ b/internal/cmd/dns/zone/list/list.go @@ -8,6 +8,7 @@ import ( "strings" "github.com/goccy/go-yaml" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -49,7 +50,7 @@ type inputModel struct { PageSize int64 } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "list", Short: "Lists DNS zones", @@ -71,13 +72,13 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -88,16 +89,16 @@ func NewCmd(p *print.Printer) *cobra.Command { return err } if len(zones) == 0 { - projectLabel, err := projectname.GetProjectName(ctx, p, cmd) + projectLabel, err := projectname.GetProjectName(ctx, params.Printer, cmd) if err != nil { - p.Debug(print.ErrorLevel, "get project name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get project name: %v", err) projectLabel = model.ProjectId } - p.Info("No zones found for project %q matching the criteria\n", projectLabel) + params.Printer.Info("No zones found for project %q matching the criteria\n", projectLabel) return nil } - return outputResult(p, model.OutputFormat, zones) + return outputResult(params.Printer, model.OutputFormat, zones) }, } configureFlags(cmd) diff --git a/internal/cmd/dns/zone/list/list_test.go b/internal/cmd/dns/zone/list/list_test.go index d96b980d7..844c2afa6 100644 --- a/internal/cmd/dns/zone/list/list_test.go +++ b/internal/cmd/dns/zone/list/list_test.go @@ -11,6 +11,7 @@ import ( "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" "github.com/google/uuid" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -209,7 +210,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -512,7 +513,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.zones); (err != nil) != tt.wantErr { diff --git a/internal/cmd/dns/zone/update/update.go b/internal/cmd/dns/zone/update/update.go index 0e0a6b628..09c4036fd 100644 --- a/internal/cmd/dns/zone/update/update.go +++ b/internal/cmd/dns/zone/update/update.go @@ -4,6 +4,7 @@ import ( "context" "fmt" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -50,7 +51,7 @@ type inputModel struct { ContactEmail *string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("update %s", zoneIdArg), Short: "Updates a DNS zone", @@ -63,26 +64,26 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } zoneLabel, err := dnsUtils.GetZoneName(ctx, apiClient, model.ProjectId, model.ZoneId) if err != nil { - p.Debug(print.ErrorLevel, "get zone name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get zone name: %v", err) zoneLabel = model.ZoneId } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to update zone %s?", zoneLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -100,7 +101,7 @@ func NewCmd(p *print.Printer) *cobra.Command { // Wait for async operation, if async mode not enabled if !model.Async { - s := spinner.New(p) + s := spinner.New(params.Printer) s.Start("Updating zone") _, err = wait.PartialUpdateZoneWaitHandler(ctx, apiClient, model.ProjectId, model.ZoneId).WaitWithContext(ctx) if err != nil { @@ -113,7 +114,7 @@ func NewCmd(p *print.Printer) *cobra.Command { if model.Async { operationState = "Triggered update of" } - p.Info("%s zone %s\n", operationState, zoneLabel) + params.Printer.Info("%s zone %s\n", operationState, zoneLabel) return nil }, } diff --git a/internal/cmd/dns/zone/update/update_test.go b/internal/cmd/dns/zone/update/update_test.go index 82222ad5d..adea3637d 100644 --- a/internal/cmd/dns/zone/update/update_test.go +++ b/internal/cmd/dns/zone/update/update_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -246,7 +247,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/dns/zone/zone.go b/internal/cmd/dns/zone/zone.go index 77247e3c4..b81a143ff 100644 --- a/internal/cmd/dns/zone/zone.go +++ b/internal/cmd/dns/zone/zone.go @@ -7,14 +7,14 @@ import ( "github.com/stackitcloud/stackit-cli/internal/cmd/dns/zone/describe" "github.com/stackitcloud/stackit-cli/internal/cmd/dns/zone/list" "github.com/stackitcloud/stackit-cli/internal/cmd/dns/zone/update" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" - "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" "github.com/spf13/cobra" ) -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "zone", Short: "Provides functionality for DNS zones", @@ -22,15 +22,15 @@ func NewCmd(p *print.Printer) *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd, p) + addSubcommands(cmd, params) return cmd } -func addSubcommands(cmd *cobra.Command, p *print.Printer) { - cmd.AddCommand(list.NewCmd(p)) - cmd.AddCommand(create.NewCmd(p)) - cmd.AddCommand(describe.NewCmd(p)) - cmd.AddCommand(update.NewCmd(p)) - cmd.AddCommand(delete.NewCmd(p)) - cmd.AddCommand(clone.NewCmd(p)) +func addSubcommands(cmd *cobra.Command, params *params.CmdParams) { + cmd.AddCommand(list.NewCmd(params)) + cmd.AddCommand(create.NewCmd(params)) + cmd.AddCommand(describe.NewCmd(params)) + cmd.AddCommand(update.NewCmd(params)) + cmd.AddCommand(delete.NewCmd(params)) + cmd.AddCommand(clone.NewCmd(params)) } diff --git a/internal/cmd/image/create/create.go b/internal/cmd/image/create/create.go index b29048efb..704872c11 100644 --- a/internal/cmd/image/create/create.go +++ b/internal/cmd/image/create/create.go @@ -13,6 +13,7 @@ import ( "github.com/goccy/go-yaml" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -81,7 +82,7 @@ type inputModel struct { NoProgressIndicator *bool } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "create", Short: "Creates images", @@ -99,13 +100,13 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) (err error) { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -123,7 +124,7 @@ func NewCmd(p *print.Printer) *cobra.Command { if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to create the image %q?", model.Name) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -141,11 +142,11 @@ func NewCmd(p *print.Printer) *cobra.Command { if !ok { return fmt.Errorf("create image: no upload URL has been provided") } - if err := uploadAsync(ctx, p, model, file, url); err != nil { + if err := uploadAsync(ctx, params.Printer, model, file, url); err != nil { return err } - if err := outputResult(p, model, result); err != nil { + if err := outputResult(params.Printer, model, result); err != nil { return err } diff --git a/internal/cmd/image/create/create_test.go b/internal/cmd/image/create/create_test.go index 262dc029e..cdf15ce81 100644 --- a/internal/cmd/image/create/create_test.go +++ b/internal/cmd/image/create/create_test.go @@ -9,6 +9,7 @@ import ( "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" "github.com/google/uuid" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -273,7 +274,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) if err := globalflags.Configure(cmd.Flags()); err != nil { t.Errorf("cannot configure global flags: %v", err) } @@ -422,7 +423,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.model, tt.args.resp); (err != nil) != tt.wantErr { diff --git a/internal/cmd/image/delete/delete.go b/internal/cmd/image/delete/delete.go index 57ae4f026..456867bdd 100644 --- a/internal/cmd/image/delete/delete.go +++ b/internal/cmd/image/delete/delete.go @@ -5,6 +5,7 @@ import ( "fmt" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -24,7 +25,7 @@ type inputModel struct { const imageIdArg = "IMAGE_ID" -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("delete %s", imageIdArg), Short: "Deletes an image", @@ -35,26 +36,26 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } - projectLabel, err := projectname.GetProjectName(ctx, p, cmd) + projectLabel, err := projectname.GetProjectName(ctx, params.Printer, cmd) if err != nil { - p.Debug(print.ErrorLevel, "get project name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get project name: %v", err) projectLabel = model.ProjectId } imageName, err := iaasUtils.GetImageName(ctx, apiClient, model.ProjectId, model.ImageId) if err != nil { - p.Debug(print.ErrorLevel, "get image name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get image name: %v", err) imageName = model.ImageId } else if imageName == "" { imageName = model.ImageId @@ -62,7 +63,7 @@ func NewCmd(p *print.Printer) *cobra.Command { if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to delete the image %q for %q?", imageName, projectLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -74,7 +75,7 @@ func NewCmd(p *print.Printer) *cobra.Command { if err := request.Execute(); err != nil { return fmt.Errorf("delete image: %w", err) } - p.Info("Deleted image %q for %q\n", imageName, projectLabel) + params.Printer.Info("Deleted image %q for %q\n", imageName, projectLabel) return nil }, diff --git a/internal/cmd/image/delete/delete_test.go b/internal/cmd/image/delete/delete_test.go index 1fa1ed5bc..93b2a656a 100644 --- a/internal/cmd/image/delete/delete_test.go +++ b/internal/cmd/image/delete/delete_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" @@ -105,7 +106,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/image/describe/describe.go b/internal/cmd/image/describe/describe.go index 20ab802fc..5c0b8a824 100644 --- a/internal/cmd/image/describe/describe.go +++ b/internal/cmd/image/describe/describe.go @@ -8,6 +8,7 @@ import ( "github.com/goccy/go-yaml" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -26,7 +27,7 @@ type inputModel struct { const imageIdArg = "IMAGE_ID" -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("describe %s", imageIdArg), Short: "Describes image", @@ -37,13 +38,13 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -56,7 +57,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("get image: %w", err) } - if err := outputResult(p, model.OutputFormat, image); err != nil { + if err := outputResult(params.Printer, model.OutputFormat, image); err != nil { return err } diff --git a/internal/cmd/image/describe/describe_test.go b/internal/cmd/image/describe/describe_test.go index 036648fee..a5e421a9d 100644 --- a/internal/cmd/image/describe/describe_test.go +++ b/internal/cmd/image/describe/describe_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" @@ -120,7 +121,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) if err := globalflags.Configure(cmd.Flags()); err != nil { t.Errorf("cannot configure global flags: %v", err) } @@ -217,7 +218,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.resp); (err != nil) != tt.wantErr { diff --git a/internal/cmd/image/image.go b/internal/cmd/image/image.go index 899a62ce9..b722f2b91 100644 --- a/internal/cmd/image/image.go +++ b/internal/cmd/image/image.go @@ -6,15 +6,15 @@ import ( "github.com/stackitcloud/stackit-cli/internal/cmd/image/describe" "github.com/stackitcloud/stackit-cli/internal/cmd/image/list" "github.com/stackitcloud/stackit-cli/internal/cmd/image/update" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" - "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" "github.com/spf13/cobra" ) -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "image", Short: "Manage server images", @@ -22,16 +22,16 @@ func NewCmd(p *print.Printer) *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd, p) + addSubcommands(cmd, params) return cmd } -func addSubcommands(cmd *cobra.Command, p *print.Printer) { +func addSubcommands(cmd *cobra.Command, params *params.CmdParams) { cmd.AddCommand( - create.NewCmd(p), - list.NewCmd(p), - delete.NewCmd(p), - describe.NewCmd(p), - update.NewCmd(p), + create.NewCmd(params), + list.NewCmd(params), + delete.NewCmd(params), + describe.NewCmd(params), + update.NewCmd(params), ) } diff --git a/internal/cmd/image/list/list.go b/internal/cmd/image/list/list.go index 47779fb83..eff221cba 100644 --- a/internal/cmd/image/list/list.go +++ b/internal/cmd/image/list/list.go @@ -7,6 +7,7 @@ import ( "github.com/goccy/go-yaml" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -31,7 +32,7 @@ const ( limitFlag = "limit" ) -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "list", Short: "Lists images", @@ -53,20 +54,20 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } - projectLabel, err := projectname.GetProjectName(ctx, p, cmd) + projectLabel, err := projectname.GetProjectName(ctx, params.Printer, cmd) if err != nil { - p.Debug(print.ErrorLevel, "get project name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get project name: %v", err) projectLabel = model.ProjectId } else if projectLabel == "" { projectLabel = model.ProjectId @@ -81,12 +82,12 @@ func NewCmd(p *print.Printer) *cobra.Command { } if items := response.GetItems(); len(items) == 0 { - p.Info("No images found for project %q", projectLabel) + params.Printer.Info("No images found for project %q", projectLabel) } else { if model.Limit != nil && len(items) > int(*model.Limit) { items = (items)[:*model.Limit] } - if err := outputResult(p, model.OutputFormat, items); err != nil { + if err := outputResult(params.Printer, model.OutputFormat, items); err != nil { return fmt.Errorf("output images: %w", err) } } diff --git a/internal/cmd/image/list/list_test.go b/internal/cmd/image/list/list_test.go index 49a27a16e..c357d3bc1 100644 --- a/internal/cmd/image/list/list_test.go +++ b/internal/cmd/image/list/list_test.go @@ -5,6 +5,7 @@ import ( "strconv" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -124,7 +125,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) if err := globalflags.Configure(cmd.Flags()); err != nil { t.Errorf("cannot configure global flags: %v", err) } @@ -239,7 +240,7 @@ func Test_outputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.items); (err != nil) != tt.wantErr { diff --git a/internal/cmd/image/update/update.go b/internal/cmd/image/update/update.go index 8e6a8a2dd..e123d4fe1 100644 --- a/internal/cmd/image/update/update.go +++ b/internal/cmd/image/update/update.go @@ -5,6 +5,7 @@ import ( "fmt" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -101,7 +102,7 @@ const ( protectedFlag = "protected" ) -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("update %s", imageIdArg), Short: "Updates an image", @@ -113,26 +114,26 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } - projectLabel, err := projectname.GetProjectName(ctx, p, cmd) + projectLabel, err := projectname.GetProjectName(ctx, params.Printer, cmd) if err != nil { - p.Debug(print.ErrorLevel, "get project name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get project name: %v", err) projectLabel = model.ProjectId } imageLabel, err := iaasUtils.GetImageName(ctx, apiClient, model.ProjectId, model.Id) if err != nil { - p.Debug(print.WarningLevel, "cannot retrieve image name: %v", err) + params.Printer.Debug(print.WarningLevel, "cannot retrieve image name: %v", err) imageLabel = model.Id } else if imageLabel == "" { imageLabel = model.Id @@ -140,7 +141,7 @@ func NewCmd(p *print.Printer) *cobra.Command { if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to update the image %q?", imageLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -153,7 +154,7 @@ func NewCmd(p *print.Printer) *cobra.Command { if err != nil { return fmt.Errorf("update image: %w", err) } - p.Info("Updated image \"%v\" for %q\n", utils.PtrString(resp.Name), projectLabel) + params.Printer.Info("Updated image \"%v\" for %q\n", utils.PtrString(resp.Name), projectLabel) return nil }, diff --git a/internal/cmd/image/update/update_test.go b/internal/cmd/image/update/update_test.go index b08d3ceca..bb313b3c2 100644 --- a/internal/cmd/image/update/update_test.go +++ b/internal/cmd/image/update/update_test.go @@ -6,6 +6,7 @@ import ( "strings" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -302,7 +303,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) if err := globalflags.Configure(cmd.Flags()); err != nil { t.Errorf("cannot configure global flags: %v", err) } diff --git a/internal/cmd/key-pair/create/create.go b/internal/cmd/key-pair/create/create.go index 0c96810ba..b59fe4fb0 100644 --- a/internal/cmd/key-pair/create/create.go +++ b/internal/cmd/key-pair/create/create.go @@ -5,6 +5,7 @@ import ( "encoding/json" "fmt" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" "github.com/stackitcloud/stackit-cli/internal/pkg/flags" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" @@ -30,7 +31,7 @@ type inputModel struct { Labels *map[string]string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "create", Short: "Creates a key pair", @@ -56,20 +57,20 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } if !model.AssumeYes { prompt := "Are your sure you want to create a key pair?" - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -82,7 +83,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("create key pair: %w", err) } - return outputResult(p, model.GlobalFlagModel.OutputFormat, resp) + return outputResult(params.Printer, model.GlobalFlagModel.OutputFormat, resp) }, } configureFlags(cmd) diff --git a/internal/cmd/key-pair/create/create_test.go b/internal/cmd/key-pair/create/create_test.go index f912892d5..32a0516b6 100644 --- a/internal/cmd/key-pair/create/create_test.go +++ b/internal/cmd/key-pair/create/create_test.go @@ -5,6 +5,7 @@ import ( "os" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -123,7 +124,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -224,7 +225,7 @@ func Test_outputResult(t *testing.T) { } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.item); (err != nil) != tt.wantErr { diff --git a/internal/cmd/key-pair/delete/delete.go b/internal/cmd/key-pair/delete/delete.go index ba9b5a9ce..8bccdd45d 100644 --- a/internal/cmd/key-pair/delete/delete.go +++ b/internal/cmd/key-pair/delete/delete.go @@ -4,6 +4,7 @@ import ( "context" "fmt" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" @@ -23,7 +24,7 @@ type inputModel struct { KeyPairName string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("delete %s", keyPairNameArg), Short: "Deletes a key pair", @@ -37,20 +38,20 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to delete key pair %q?", model.KeyPairName) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -63,7 +64,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("delete key pair: %w", err) } - p.Info("Deleted key pair %q\n", model.KeyPairName) + params.Printer.Info("Deleted key pair %q\n", model.KeyPairName) return nil }, diff --git a/internal/cmd/key-pair/delete/delete_test.go b/internal/cmd/key-pair/delete/delete_test.go index cfbcd29e8..c5c8dc913 100644 --- a/internal/cmd/key-pair/delete/delete_test.go +++ b/internal/cmd/key-pair/delete/delete_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-sdk-go/services/iaas" @@ -96,7 +97,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/key-pair/describe/describe.go b/internal/cmd/key-pair/describe/describe.go index f4ee01f94..b355e7433 100644 --- a/internal/cmd/key-pair/describe/describe.go +++ b/internal/cmd/key-pair/describe/describe.go @@ -6,6 +6,7 @@ import ( "fmt" "strings" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" "github.com/stackitcloud/stackit-cli/internal/pkg/args" @@ -35,7 +36,7 @@ type inputModel struct { PublicKey bool } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("describe %s", keyPairNameArg), Short: "Describes a key pair", @@ -53,13 +54,13 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -72,9 +73,9 @@ func NewCmd(p *print.Printer) *cobra.Command { } if keypair := resp; keypair != nil { - return outputResult(p, model.OutputFormat, model.PublicKey, *keypair) + return outputResult(params.Printer, model.OutputFormat, model.PublicKey, *keypair) } - p.Outputln("No keypair found.") + params.Printer.Outputln("No keypair found.") return nil }, } diff --git a/internal/cmd/key-pair/describe/describe_test.go b/internal/cmd/key-pair/describe/describe_test.go index 46374b3c8..c3dd4ffaa 100644 --- a/internal/cmd/key-pair/describe/describe_test.go +++ b/internal/cmd/key-pair/describe/describe_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" @@ -108,7 +109,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -209,7 +210,7 @@ func Test_outputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.showOnlyPublicKey, tt.args.keyPair); (err != nil) != tt.wantErr { diff --git a/internal/cmd/key-pair/key-pair.go b/internal/cmd/key-pair/key-pair.go index 90bbd648b..44cc1fef8 100644 --- a/internal/cmd/key-pair/key-pair.go +++ b/internal/cmd/key-pair/key-pair.go @@ -8,11 +8,11 @@ import ( "github.com/stackitcloud/stackit-cli/internal/cmd/key-pair/describe" "github.com/stackitcloud/stackit-cli/internal/cmd/key-pair/list" "github.com/stackitcloud/stackit-cli/internal/cmd/key-pair/update" - "github.com/stackitcloud/stackit-cli/internal/pkg/print" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" ) -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "key-pair", Short: "Provides functionality for SSH key pairs", @@ -20,14 +20,14 @@ func NewCmd(p *print.Printer) *cobra.Command { Args: cobra.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd, p) + addSubcommands(cmd, params) return cmd } -func addSubcommands(cmd *cobra.Command, p *print.Printer) { - cmd.AddCommand(create.NewCmd(p)) - cmd.AddCommand(delete.NewCmd(p)) - cmd.AddCommand(describe.NewCmd(p)) - cmd.AddCommand(list.NewCmd(p)) - cmd.AddCommand(update.NewCmd(p)) +func addSubcommands(cmd *cobra.Command, params *params.CmdParams) { + cmd.AddCommand(create.NewCmd(params)) + cmd.AddCommand(delete.NewCmd(params)) + cmd.AddCommand(describe.NewCmd(params)) + cmd.AddCommand(list.NewCmd(params)) + cmd.AddCommand(update.NewCmd(params)) } diff --git a/internal/cmd/key-pair/list/list.go b/internal/cmd/key-pair/list/list.go index 8685edb36..980625695 100644 --- a/internal/cmd/key-pair/list/list.go +++ b/internal/cmd/key-pair/list/list.go @@ -6,6 +6,7 @@ import ( "fmt" "strings" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" "github.com/stackitcloud/stackit-cli/internal/pkg/args" @@ -33,7 +34,7 @@ type inputModel struct { LabelSelector *string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "list", Short: "Lists all key pairs", @@ -59,13 +60,13 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -78,7 +79,7 @@ func NewCmd(p *print.Printer) *cobra.Command { } if resp.Items == nil || len(*resp.Items) == 0 { - p.Info("No key pairs found\n") + params.Printer.Info("No key pairs found\n") return nil } @@ -87,7 +88,7 @@ func NewCmd(p *print.Printer) *cobra.Command { items = items[:*model.Limit] } - return outputResult(p, model.OutputFormat, items) + return outputResult(params.Printer, model.OutputFormat, items) }, } configureFlags(cmd) diff --git a/internal/cmd/key-pair/list/list_test.go b/internal/cmd/key-pair/list/list_test.go index 8fa0a948f..6679cee0d 100644 --- a/internal/cmd/key-pair/list/list_test.go +++ b/internal/cmd/key-pair/list/list_test.go @@ -5,6 +5,7 @@ import ( "strconv" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -116,7 +117,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -212,7 +213,7 @@ func Test_outputResult(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) if err := outputResult(p, tt.args.outputFormat, tt.args.keyPairs); (err != nil) != tt.wantErr { t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr) diff --git a/internal/cmd/key-pair/update/update.go b/internal/cmd/key-pair/update/update.go index 1a64875b5..3af038018 100644 --- a/internal/cmd/key-pair/update/update.go +++ b/internal/cmd/key-pair/update/update.go @@ -5,6 +5,7 @@ import ( "encoding/json" "fmt" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" "github.com/stackitcloud/stackit-cli/internal/pkg/flags" @@ -29,7 +30,7 @@ type inputModel struct { KeyPairName *string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("update %s", keyPairNameArg), Short: "Updates a key pair", @@ -43,17 +44,17 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model := parseInput(p, cmd, args) + model := parseInput(params.Printer, cmd, args) // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to update key pair %q?", *model.KeyPairName) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return fmt.Errorf("update key pair: %w", err) } @@ -69,7 +70,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("response is nil") } - return outputResult(p, model, *resp) + return outputResult(params.Printer, model, *resp) }, } configureFlags(cmd) diff --git a/internal/cmd/key-pair/update/update_test.go b/internal/cmd/key-pair/update/update_test.go index 5743e2d60..2f5432deb 100644 --- a/internal/cmd/key-pair/update/update_test.go +++ b/internal/cmd/key-pair/update/update_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -109,7 +110,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -208,7 +209,7 @@ func Test_outputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.model, tt.args.keyPair); (err != nil) != tt.wantErr { diff --git a/internal/cmd/load-balancer/create/create.go b/internal/cmd/load-balancer/create/create.go index a0600f66d..541293bc2 100644 --- a/internal/cmd/load-balancer/create/create.go +++ b/internal/cmd/load-balancer/create/create.go @@ -7,6 +7,7 @@ import ( "github.com/google/uuid" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -34,7 +35,7 @@ var ( xRequestId = uuid.NewString() ) -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "create", Short: "Creates a Load Balancer", @@ -59,26 +60,26 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } - projectLabel, err := projectname.GetProjectName(ctx, p, cmd) + projectLabel, err := projectname.GetProjectName(ctx, params.Printer, cmd) if err != nil { - p.Debug(print.ErrorLevel, "get project name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get project name: %v", err) projectLabel = model.ProjectId } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to create a load balancer for project %q?", projectLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -93,7 +94,7 @@ func NewCmd(p *print.Printer) *cobra.Command { // Wait for async operation, if async mode not enabled if !model.Async { - s := spinner.New(p) + s := spinner.New(params.Printer) s.Start("Creating load balancer") _, err = wait.CreateLoadBalancerWaitHandler(ctx, apiClient, model.ProjectId, model.Region, *model.Payload.Name).WaitWithContext(ctx) if err != nil { @@ -106,7 +107,7 @@ func NewCmd(p *print.Printer) *cobra.Command { if model.Async { operationState = "Triggered creation of" } - p.Outputf("%s load balancer with name %q \n", operationState, utils.PtrString(model.Payload.Name)) + params.Printer.Outputf("%s load balancer with name %q \n", operationState, utils.PtrString(model.Payload.Name)) return nil }, } diff --git a/internal/cmd/load-balancer/create/create_test.go b/internal/cmd/load-balancer/create/create_test.go index da7bfce83..eca1281a8 100644 --- a/internal/cmd/load-balancer/create/create_test.go +++ b/internal/cmd/load-balancer/create/create_test.go @@ -6,6 +6,7 @@ import ( "github.com/stackitcloud/stackit-sdk-go/services/loadbalancer" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -277,7 +278,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/load-balancer/delete/delete.go b/internal/cmd/load-balancer/delete/delete.go index c2410db74..d5f3f6c75 100644 --- a/internal/cmd/load-balancer/delete/delete.go +++ b/internal/cmd/load-balancer/delete/delete.go @@ -4,6 +4,7 @@ import ( "context" "fmt" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -26,7 +27,7 @@ type inputModel struct { LoadBalancerName string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("delete %s", loadBalancerNameArg), Short: "Deletes a Load Balancer", @@ -39,19 +40,19 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to delete load balancer %q? (This cannot be undone)", model.LoadBalancerName) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -66,7 +67,7 @@ func NewCmd(p *print.Printer) *cobra.Command { // Wait for async operation, if async mode not enabled if !model.Async { - s := spinner.New(p) + s := spinner.New(params.Printer) s.Start("Deleting load balancer") _, err = wait.DeleteLoadBalancerWaitHandler(ctx, apiClient, model.ProjectId, model.Region, model.LoadBalancerName).WaitWithContext(ctx) if err != nil { @@ -79,7 +80,7 @@ func NewCmd(p *print.Printer) *cobra.Command { if model.Async { operationState = "Triggered deletion of" } - p.Info("%s load balancer %q\n", operationState, model.LoadBalancerName) + params.Printer.Info("%s load balancer %q\n", operationState, model.LoadBalancerName) return nil }, } diff --git a/internal/cmd/load-balancer/delete/delete_test.go b/internal/cmd/load-balancer/delete/delete_test.go index 621113c56..17cca82cb 100644 --- a/internal/cmd/load-balancer/delete/delete_test.go +++ b/internal/cmd/load-balancer/delete/delete_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" @@ -130,7 +131,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/load-balancer/describe/describe.go b/internal/cmd/load-balancer/describe/describe.go index 603bfd185..fe8669864 100644 --- a/internal/cmd/load-balancer/describe/describe.go +++ b/internal/cmd/load-balancer/describe/describe.go @@ -7,6 +7,7 @@ import ( "strings" "github.com/goccy/go-yaml" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -29,7 +30,7 @@ type inputModel struct { LoadBalancerName string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("describe %s", loadBalancerNameArg), Short: "Shows details of a Load Balancer", @@ -45,12 +46,12 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -62,7 +63,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("read load balancer: %w", err) } - return outputResult(p, model.OutputFormat, resp) + return outputResult(params.Printer, model.OutputFormat, resp) }, } return cmd diff --git a/internal/cmd/load-balancer/describe/describe_test.go b/internal/cmd/load-balancer/describe/describe_test.go index 821f942b7..2aebb5e61 100644 --- a/internal/cmd/load-balancer/describe/describe_test.go +++ b/internal/cmd/load-balancer/describe/describe_test.go @@ -7,6 +7,7 @@ import ( "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" "github.com/google/uuid" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-sdk-go/services/loadbalancer" @@ -129,7 +130,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -234,7 +235,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.loadBalancer); (err != nil) != tt.wantErr { diff --git a/internal/cmd/load-balancer/generate-payload/generate_payload.go b/internal/cmd/load-balancer/generate-payload/generate_payload.go index ba14de569..2fccfb37a 100644 --- a/internal/cmd/load-balancer/generate-payload/generate_payload.go +++ b/internal/cmd/load-balancer/generate-payload/generate_payload.go @@ -5,6 +5,7 @@ import ( "encoding/json" "fmt" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -109,7 +110,7 @@ var ( } ) -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "generate-payload", Short: "Generates a payload to create/update a Load Balancer", @@ -135,20 +136,20 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } if model.LoadBalancerName == nil { createPayload := DefaultCreateLoadBalancerPayload - return outputCreateResult(p, model.FilePath, &createPayload) + return outputCreateResult(params.Printer, model.FilePath, &createPayload) } req := buildRequest(ctx, model, apiClient) @@ -168,7 +169,7 @@ func NewCmd(p *print.Printer) *cobra.Command { TargetPools: resp.TargetPools, Version: resp.Version, } - return outputUpdateResult(p, model.FilePath, updatePayload) + return outputUpdateResult(params.Printer, model.FilePath, updatePayload) }, } configureFlags(cmd) diff --git a/internal/cmd/load-balancer/generate-payload/generate_payload_test.go b/internal/cmd/load-balancer/generate-payload/generate_payload_test.go index 0e2ee4beb..5943215b8 100644 --- a/internal/cmd/load-balancer/generate-payload/generate_payload_test.go +++ b/internal/cmd/load-balancer/generate-payload/generate_payload_test.go @@ -7,6 +7,7 @@ import ( "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" "github.com/google/uuid" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -132,7 +133,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -337,7 +338,7 @@ func TestOutputCreateResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputCreateResult(p, tt.args.filePath, tt.args.payload); (err != nil) != tt.wantErr { @@ -371,7 +372,7 @@ func TestOutputUpdateResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputUpdateResult(p, tt.args.filePath, tt.args.payload); (err != nil) != tt.wantErr { diff --git a/internal/cmd/load-balancer/list/list.go b/internal/cmd/load-balancer/list/list.go index a9611e137..5d23c5d2a 100644 --- a/internal/cmd/load-balancer/list/list.go +++ b/internal/cmd/load-balancer/list/list.go @@ -6,6 +6,7 @@ import ( "fmt" "github.com/goccy/go-yaml" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -30,7 +31,7 @@ type inputModel struct { Limit *int64 } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "list", Short: "Lists all Load Balancers", @@ -49,13 +50,13 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -68,12 +69,12 @@ func NewCmd(p *print.Printer) *cobra.Command { } if resp.LoadBalancers == nil || (resp.LoadBalancers != nil && len(*resp.LoadBalancers) == 0) { - projectLabel, err := projectname.GetProjectName(ctx, p, cmd) + projectLabel, err := projectname.GetProjectName(ctx, params.Printer, cmd) if err != nil { - p.Debug(print.ErrorLevel, "get project name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get project name: %v", err) projectLabel = model.ProjectId } - p.Info("No load balancers found for project %q\n", projectLabel) + params.Printer.Info("No load balancers found for project %q\n", projectLabel) return nil } @@ -83,7 +84,7 @@ func NewCmd(p *print.Printer) *cobra.Command { loadBalancers = loadBalancers[:*model.Limit] } - return outputResult(p, model.OutputFormat, loadBalancers) + return outputResult(params.Printer, model.OutputFormat, loadBalancers) }, } diff --git a/internal/cmd/load-balancer/list/list_test.go b/internal/cmd/load-balancer/list/list_test.go index d0210b9cb..f43d4ae62 100644 --- a/internal/cmd/load-balancer/list/list_test.go +++ b/internal/cmd/load-balancer/list/list_test.go @@ -7,6 +7,7 @@ import ( "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" "github.com/google/uuid" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -116,7 +117,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -218,7 +219,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.loadBalancers); (err != nil) != tt.wantErr { diff --git a/internal/cmd/load-balancer/load_balancer.go b/internal/cmd/load-balancer/load_balancer.go index c6f6be2f5..4a2876453 100644 --- a/internal/cmd/load-balancer/load_balancer.go +++ b/internal/cmd/load-balancer/load_balancer.go @@ -10,15 +10,15 @@ import ( "github.com/stackitcloud/stackit-cli/internal/cmd/load-balancer/quota" targetpool "github.com/stackitcloud/stackit-cli/internal/cmd/load-balancer/target-pool" "github.com/stackitcloud/stackit-cli/internal/cmd/load-balancer/update" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" - "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" "github.com/spf13/cobra" ) -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "load-balancer", Aliases: []string{"lb"}, @@ -27,18 +27,18 @@ func NewCmd(p *print.Printer) *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd, p) + addSubcommands(cmd, params) return cmd } -func addSubcommands(cmd *cobra.Command, p *print.Printer) { - cmd.AddCommand(create.NewCmd(p)) - cmd.AddCommand(delete.NewCmd(p)) - cmd.AddCommand(describe.NewCmd(p)) - cmd.AddCommand(generatepayload.NewCmd(p)) - cmd.AddCommand(list.NewCmd(p)) - cmd.AddCommand(quota.NewCmd(p)) - cmd.AddCommand(observabilitycredentials.NewCmd(p)) - cmd.AddCommand(targetpool.NewCmd(p)) - cmd.AddCommand(update.NewCmd(p)) +func addSubcommands(cmd *cobra.Command, params *params.CmdParams) { + cmd.AddCommand(create.NewCmd(params)) + cmd.AddCommand(delete.NewCmd(params)) + cmd.AddCommand(describe.NewCmd(params)) + cmd.AddCommand(generatepayload.NewCmd(params)) + cmd.AddCommand(list.NewCmd(params)) + cmd.AddCommand(quota.NewCmd(params)) + cmd.AddCommand(observabilitycredentials.NewCmd(params)) + cmd.AddCommand(targetpool.NewCmd(params)) + cmd.AddCommand(update.NewCmd(params)) } diff --git a/internal/cmd/load-balancer/observability-credentials/add/add.go b/internal/cmd/load-balancer/observability-credentials/add/add.go index a75b5e064..abbbd36a0 100644 --- a/internal/cmd/load-balancer/observability-credentials/add/add.go +++ b/internal/cmd/load-balancer/observability-credentials/add/add.go @@ -7,6 +7,7 @@ import ( "github.com/goccy/go-yaml" "github.com/google/uuid" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -34,7 +35,7 @@ type inputModel struct { Password *string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "add", Short: "Adds observability credentials to Load Balancer", @@ -50,26 +51,26 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } - projectLabel, err := projectname.GetProjectName(ctx, p, cmd) + projectLabel, err := projectname.GetProjectName(ctx, params.Printer, cmd) if err != nil { - p.Debug(print.ErrorLevel, "get project name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get project name: %v", err) projectLabel = model.ProjectId } // Prompt for password if not passed in as a flag if model.Password == nil { - pwd, err := p.PromptForPassword("Enter user password: ") + pwd, err := params.Printer.PromptForPassword("Enter user password: ") if err != nil { return fmt.Errorf("prompt for password: %w", err) } @@ -78,7 +79,7 @@ func NewCmd(p *print.Printer) *cobra.Command { if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to add observability credentials for Load Balancer on project %q?", projectLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -91,7 +92,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("add Load Balancer observability credentials: %w", err) } - return outputResult(p, model.OutputFormat, projectLabel, resp) + return outputResult(params.Printer, model.OutputFormat, projectLabel, resp) }, } configureFlags(cmd) diff --git a/internal/cmd/load-balancer/observability-credentials/add/add_test.go b/internal/cmd/load-balancer/observability-credentials/add/add_test.go index 57557613a..5ee5523f8 100644 --- a/internal/cmd/load-balancer/observability-credentials/add/add_test.go +++ b/internal/cmd/load-balancer/observability-credentials/add/add_test.go @@ -7,6 +7,7 @@ import ( "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" "github.com/google/uuid" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -125,7 +126,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -229,7 +230,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.projectLabel, tt.args.resp); (err != nil) != tt.wantErr { diff --git a/internal/cmd/load-balancer/observability-credentials/cleanup/cleanup.go b/internal/cmd/load-balancer/observability-credentials/cleanup/cleanup.go index 28b560b2a..e69b3ae98 100644 --- a/internal/cmd/load-balancer/observability-credentials/cleanup/cleanup.go +++ b/internal/cmd/load-balancer/observability-credentials/cleanup/cleanup.go @@ -4,6 +4,7 @@ import ( "context" "fmt" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -21,7 +22,7 @@ type inputModel struct { *globalflags.GlobalFlagModel } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "cleanup", Short: "Deletes observability credentials unused by any Load Balancer", @@ -34,20 +35,20 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } - projectLabel, err := projectname.GetProjectName(ctx, p, cmd) + projectLabel, err := projectname.GetProjectName(ctx, params.Printer, cmd) if err != nil { - p.Debug(print.ErrorLevel, "get project name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get project name: %v", err) projectLabel = model.ProjectId } @@ -66,7 +67,7 @@ func NewCmd(p *print.Printer) *cobra.Command { } if len(credentials) == 0 { - p.Info("No unused observability credentials found on project %q\n", projectLabel) + params.Printer.Info("No unused observability credentials found on project %q\n", projectLabel) return nil } @@ -81,7 +82,7 @@ func NewCmd(p *print.Printer) *cobra.Command { prompt += fmt.Sprintf(" - %s (username: %q)\n", name, username) } prompt += fmt.Sprintf("Are you sure you want to delete unused observability credentials on project %q? (This cannot be undone)", projectLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -100,7 +101,7 @@ func NewCmd(p *print.Printer) *cobra.Command { } } - p.Info("Deleted unused Load Balancer observability credentials on project %q\n", projectLabel) + params.Printer.Info("Deleted unused Load Balancer observability credentials on project %q\n", projectLabel) return nil }, } diff --git a/internal/cmd/load-balancer/observability-credentials/cleanup/cleanup_test.go b/internal/cmd/load-balancer/observability-credentials/cleanup/cleanup_test.go index ae7b8e3c4..7154b55ee 100644 --- a/internal/cmd/load-balancer/observability-credentials/cleanup/cleanup_test.go +++ b/internal/cmd/load-balancer/observability-credentials/cleanup/cleanup_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-sdk-go/services/loadbalancer" @@ -116,7 +117,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/load-balancer/observability-credentials/delete/delete.go b/internal/cmd/load-balancer/observability-credentials/delete/delete.go index 73c9db46d..12b260489 100644 --- a/internal/cmd/load-balancer/observability-credentials/delete/delete.go +++ b/internal/cmd/load-balancer/observability-credentials/delete/delete.go @@ -4,6 +4,7 @@ import ( "context" "fmt" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -26,7 +27,7 @@ type inputModel struct { CredentialsRef string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("delete %s", credentialsRefArg), Short: "Deletes observability credentials for Load Balancer", @@ -39,32 +40,32 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } - projectLabel, err := projectname.GetProjectName(ctx, p, cmd) + projectLabel, err := projectname.GetProjectName(ctx, params.Printer, cmd) if err != nil { - p.Debug(print.ErrorLevel, "get project name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get project name: %v", err) projectLabel = model.ProjectId } credentialsLabel, err := loadbalancerUtils.GetCredentialsDisplayName(ctx, apiClient, model.ProjectId, model.Region, model.CredentialsRef) if err != nil { - p.Debug(print.ErrorLevel, "get observability credentials display name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get observability credentials display name: %v", err) credentialsLabel = model.CredentialsRef } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to delete observability credentials %q on project %q?(This cannot be undone)", credentialsLabel, projectLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -77,7 +78,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("delete Load Balancer observability credentials: %w", err) } - p.Info("Deleted observability credentials %q on project %q\n", credentialsLabel, projectLabel) + params.Printer.Info("Deleted observability credentials %q on project %q\n", credentialsLabel, projectLabel) return nil }, } diff --git a/internal/cmd/load-balancer/observability-credentials/delete/delete_test.go b/internal/cmd/load-balancer/observability-credentials/delete/delete_test.go index d4f22f799..4730f6a31 100644 --- a/internal/cmd/load-balancer/observability-credentials/delete/delete_test.go +++ b/internal/cmd/load-balancer/observability-credentials/delete/delete_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-sdk-go/services/loadbalancer" @@ -136,7 +137,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/load-balancer/observability-credentials/describe/describe.go b/internal/cmd/load-balancer/observability-credentials/describe/describe.go index 9f622f750..a09a94685 100644 --- a/internal/cmd/load-balancer/observability-credentials/describe/describe.go +++ b/internal/cmd/load-balancer/observability-credentials/describe/describe.go @@ -6,6 +6,7 @@ import ( "fmt" "github.com/goccy/go-yaml" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -28,7 +29,7 @@ type inputModel struct { CredentialsRef string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("describe %s", credentialsRefArg), Short: "Shows details of observability credentials for Load Balancer", @@ -41,13 +42,13 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -59,7 +60,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("describe Load Balancer observability credentials: %w", err) } - return outputResult(p, model.OutputFormat, resp) + return outputResult(params.Printer, model.OutputFormat, resp) }, } return cmd diff --git a/internal/cmd/load-balancer/observability-credentials/describe/describe_test.go b/internal/cmd/load-balancer/observability-credentials/describe/describe_test.go index a67979219..55335f68b 100644 --- a/internal/cmd/load-balancer/observability-credentials/describe/describe_test.go +++ b/internal/cmd/load-balancer/observability-credentials/describe/describe_test.go @@ -7,6 +7,7 @@ import ( "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" "github.com/google/uuid" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-sdk-go/services/loadbalancer" @@ -135,7 +136,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -245,7 +246,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.credentials); (err != nil) != tt.wantErr { diff --git a/internal/cmd/load-balancer/observability-credentials/list/list.go b/internal/cmd/load-balancer/observability-credentials/list/list.go index 87e149053..92dcb9602 100644 --- a/internal/cmd/load-balancer/observability-credentials/list/list.go +++ b/internal/cmd/load-balancer/observability-credentials/list/list.go @@ -7,6 +7,7 @@ import ( "github.com/goccy/go-yaml" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -35,7 +36,7 @@ type inputModel struct { Unused bool } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "list", Short: "Lists observability credentials for Load Balancer", @@ -60,20 +61,20 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } - projectLabel, err := projectname.GetProjectName(ctx, p, cmd) + projectLabel, err := projectname.GetProjectName(ctx, params.Printer, cmd) if err != nil { - p.Debug(print.ErrorLevel, "get project name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get project name: %v", err) projectLabel = model.ProjectId } @@ -105,7 +106,7 @@ func NewCmd(p *print.Printer) *cobra.Command { } else if model.Unused { opLabel += "unused" } - p.Info("%s observability credentials found for Load Balancer on project %q\n", opLabel, projectLabel) + params.Printer.Info("%s observability credentials found for Load Balancer on project %q\n", opLabel, projectLabel) return nil } @@ -113,7 +114,7 @@ func NewCmd(p *print.Printer) *cobra.Command { if model.Limit != nil && len(credentials) > int(*model.Limit) { credentials = credentials[:*model.Limit] } - return outputResult(p, model.OutputFormat, credentials) + return outputResult(params.Printer, model.OutputFormat, credentials) }, } configureFlags(cmd) diff --git a/internal/cmd/load-balancer/observability-credentials/list/list_test.go b/internal/cmd/load-balancer/observability-credentials/list/list_test.go index ddf04272e..00d107c2e 100644 --- a/internal/cmd/load-balancer/observability-credentials/list/list_test.go +++ b/internal/cmd/load-balancer/observability-credentials/list/list_test.go @@ -7,6 +7,7 @@ import ( "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" "github.com/google/uuid" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" lbUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/load-balancer/utils" @@ -145,7 +146,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -297,7 +298,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.credentials); (err != nil) != tt.wantErr { diff --git a/internal/cmd/load-balancer/observability-credentials/observability-credentials.go b/internal/cmd/load-balancer/observability-credentials/observability-credentials.go index 390b83caf..03613a8de 100644 --- a/internal/cmd/load-balancer/observability-credentials/observability-credentials.go +++ b/internal/cmd/load-balancer/observability-credentials/observability-credentials.go @@ -7,14 +7,14 @@ import ( "github.com/stackitcloud/stackit-cli/internal/cmd/load-balancer/observability-credentials/describe" "github.com/stackitcloud/stackit-cli/internal/cmd/load-balancer/observability-credentials/list" "github.com/stackitcloud/stackit-cli/internal/cmd/load-balancer/observability-credentials/update" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" - "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" "github.com/spf13/cobra" ) -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "observability-credentials", Short: "Provides functionality for Load Balancer observability credentials", @@ -23,15 +23,15 @@ func NewCmd(p *print.Printer) *cobra.Command { Aliases: []string{"credentials"}, Run: utils.CmdHelp, } - addSubcommands(cmd, p) + addSubcommands(cmd, params) return cmd } -func addSubcommands(cmd *cobra.Command, p *print.Printer) { - cmd.AddCommand(add.NewCmd(p)) - cmd.AddCommand(describe.NewCmd(p)) - cmd.AddCommand(delete.NewCmd(p)) - cmd.AddCommand(update.NewCmd(p)) - cmd.AddCommand(list.NewCmd(p)) - cmd.AddCommand(cleanup.NewCmd(p)) +func addSubcommands(cmd *cobra.Command, params *params.CmdParams) { + cmd.AddCommand(add.NewCmd(params)) + cmd.AddCommand(describe.NewCmd(params)) + cmd.AddCommand(delete.NewCmd(params)) + cmd.AddCommand(update.NewCmd(params)) + cmd.AddCommand(list.NewCmd(params)) + cmd.AddCommand(cleanup.NewCmd(params)) } diff --git a/internal/cmd/load-balancer/observability-credentials/update/update.go b/internal/cmd/load-balancer/observability-credentials/update/update.go index 4c76da9f9..942c20131 100644 --- a/internal/cmd/load-balancer/observability-credentials/update/update.go +++ b/internal/cmd/load-balancer/observability-credentials/update/update.go @@ -4,6 +4,7 @@ import ( "context" "fmt" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -45,7 +46,7 @@ type inputModel struct { Password *string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("update %s", credentialsRefArg), Short: "Updates observability credentials for Load Balancer", @@ -61,32 +62,32 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } - projectLabel, err := projectname.GetProjectName(ctx, p, cmd) + projectLabel, err := projectname.GetProjectName(ctx, params.Printer, cmd) if err != nil { - p.Debug(print.ErrorLevel, "get project name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get project name: %v", err) projectLabel = model.ProjectId } credentialsLabel, err := loadBalancerUtils.GetCredentialsDisplayName(ctx, apiClient, model.ProjectId, model.Region, model.CredentialsRef) if err != nil { - p.Debug(print.ErrorLevel, "get credentials display name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get credentials display name: %v", err) credentialsLabel = model.CredentialsRef } // Prompt for password if not passed in as a flag if model.Password == nil { - pwd, err := p.PromptForPassword("Enter new password: ") + pwd, err := params.Printer.PromptForPassword("Enter new password: ") if err != nil { return fmt.Errorf("prompt for password: %w", err) } @@ -95,7 +96,7 @@ func NewCmd(p *print.Printer) *cobra.Command { if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to update observability credentials %q for Load Balancer on project %q?", credentialsLabel, projectLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -112,7 +113,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("update Load Balancer observability credentials: %w", err) } - p.Info("Updated observability credentials %q for Load Balancer on project %q\n", credentialsLabel, projectLabel) + params.Printer.Info("Updated observability credentials %q for Load Balancer on project %q\n", credentialsLabel, projectLabel) return nil }, } diff --git a/internal/cmd/load-balancer/observability-credentials/update/update_test.go b/internal/cmd/load-balancer/observability-credentials/update/update_test.go index 87677fa86..5aa4faa50 100644 --- a/internal/cmd/load-balancer/observability-credentials/update/update_test.go +++ b/internal/cmd/load-balancer/observability-credentials/update/update_test.go @@ -5,6 +5,7 @@ import ( "fmt" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -180,7 +181,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/load-balancer/quota/quota.go b/internal/cmd/load-balancer/quota/quota.go index 9c2fb2840..fdb173fb0 100644 --- a/internal/cmd/load-balancer/quota/quota.go +++ b/internal/cmd/load-balancer/quota/quota.go @@ -7,6 +7,7 @@ import ( "strconv" "github.com/goccy/go-yaml" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -22,7 +23,7 @@ type inputModel struct { *globalflags.GlobalFlagModel } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "quota", Short: "Shows the configured Load Balancer quota", @@ -35,12 +36,12 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -52,7 +53,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("get load balancer quota: %w", err) } - return outputResult(p, model.OutputFormat, resp) + return outputResult(params.Printer, model.OutputFormat, resp) }, } return cmd diff --git a/internal/cmd/load-balancer/quota/quota_test.go b/internal/cmd/load-balancer/quota/quota_test.go index e1ba5a893..16c14e857 100644 --- a/internal/cmd/load-balancer/quota/quota_test.go +++ b/internal/cmd/load-balancer/quota/quota_test.go @@ -7,6 +7,7 @@ import ( "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" "github.com/google/uuid" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-sdk-go/services/loadbalancer" @@ -94,7 +95,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -194,7 +195,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.quota); (err != nil) != tt.wantErr { diff --git a/internal/cmd/load-balancer/target-pool/add-target/add_target.go b/internal/cmd/load-balancer/target-pool/add-target/add_target.go index 139dfda26..5dec7bfca 100644 --- a/internal/cmd/load-balancer/target-pool/add-target/add_target.go +++ b/internal/cmd/load-balancer/target-pool/add-target/add_target.go @@ -4,6 +4,7 @@ import ( "context" "fmt" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -34,7 +35,7 @@ type inputModel struct { IP string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("add-target %s", ipArg), Short: "Adds a target to a target pool", @@ -49,20 +50,20 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to add a target with IP %q to target pool %q of load balancer %q?", model.IP, model.TargetPoolName, model.LBName) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -78,7 +79,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("add target to target pool: %w", err) } - p.Info("Added target to target pool of load balancer %q\n", model.LBName) + params.Printer.Info("Added target to target pool of load balancer %q\n", model.LBName) return nil }, } diff --git a/internal/cmd/load-balancer/target-pool/add-target/add_target_test.go b/internal/cmd/load-balancer/target-pool/add-target/add_target_test.go index 6fd3da6c9..61d2fe4a6 100644 --- a/internal/cmd/load-balancer/target-pool/add-target/add_target_test.go +++ b/internal/cmd/load-balancer/target-pool/add-target/add_target_test.go @@ -5,6 +5,7 @@ import ( "fmt" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -260,7 +261,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { diff --git a/internal/cmd/load-balancer/target-pool/describe/describe.go b/internal/cmd/load-balancer/target-pool/describe/describe.go index a4ffa6d65..b9699e7c0 100644 --- a/internal/cmd/load-balancer/target-pool/describe/describe.go +++ b/internal/cmd/load-balancer/target-pool/describe/describe.go @@ -9,6 +9,7 @@ import ( "github.com/goccy/go-yaml" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -34,7 +35,7 @@ type inputModel struct { LBName string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("describe %s", targetPoolNameArg), Short: "Shows details of a target pool in a Load Balancer", @@ -50,12 +51,12 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -74,7 +75,7 @@ func NewCmd(p *print.Printer) *cobra.Command { listener := lbUtils.FindLoadBalancerListenerByTargetPool(*resp.Listeners, *targetPool.Name) - return outputResult(p, model.OutputFormat, *targetPool, listener) + return outputResult(params.Printer, model.OutputFormat, *targetPool, listener) }, } configureFlags(cmd) diff --git a/internal/cmd/load-balancer/target-pool/describe/describe_test.go b/internal/cmd/load-balancer/target-pool/describe/describe_test.go index e1acadb53..d59a0fe6b 100644 --- a/internal/cmd/load-balancer/target-pool/describe/describe_test.go +++ b/internal/cmd/load-balancer/target-pool/describe/describe_test.go @@ -7,6 +7,7 @@ import ( "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" "github.com/google/uuid" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-sdk-go/services/loadbalancer" @@ -136,7 +137,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { @@ -244,7 +245,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.targetPool, tt.args.listener); (err != nil) != tt.wantErr { diff --git a/internal/cmd/load-balancer/target-pool/remove-target/remove_target.go b/internal/cmd/load-balancer/target-pool/remove-target/remove_target.go index 2fbb0c447..5d8297665 100644 --- a/internal/cmd/load-balancer/target-pool/remove-target/remove_target.go +++ b/internal/cmd/load-balancer/target-pool/remove-target/remove_target.go @@ -4,6 +4,7 @@ import ( "context" "fmt" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -32,7 +33,7 @@ type inputModel struct { IP string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("remove-target %s", ipArg), Short: "Removes a target from a target pool", @@ -45,26 +46,26 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } targetLabel, err := utils.GetTargetName(ctx, apiClient, model.ProjectId, model.Region, model.LBName, model.TargetPoolName, model.IP) if err != nil { - p.Debug(print.ErrorLevel, "get target name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get target name: %v", err) targetLabel = model.IP } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to remove target %q from target pool %q of load balancer %q?", targetLabel, model.TargetPoolName, model.LBName) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -80,7 +81,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("remove target from target pool: %w", err) } - p.Info("Removed target from target pool of load balancer %q\n", model.LBName) + params.Printer.Info("Removed target from target pool of load balancer %q\n", model.LBName) return nil }, } diff --git a/internal/cmd/load-balancer/target-pool/remove-target/remove_target_test.go b/internal/cmd/load-balancer/target-pool/remove-target/remove_target_test.go index e961447d2..d945f3c8e 100644 --- a/internal/cmd/load-balancer/target-pool/remove-target/remove_target_test.go +++ b/internal/cmd/load-balancer/target-pool/remove-target/remove_target_test.go @@ -5,6 +5,7 @@ import ( "fmt" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -249,7 +250,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { diff --git a/internal/cmd/load-balancer/target-pool/target_pool.go b/internal/cmd/load-balancer/target-pool/target_pool.go index 78a8d50c7..da2e0f7fa 100644 --- a/internal/cmd/load-balancer/target-pool/target_pool.go +++ b/internal/cmd/load-balancer/target-pool/target_pool.go @@ -4,14 +4,14 @@ import ( addtarget "github.com/stackitcloud/stackit-cli/internal/cmd/load-balancer/target-pool/add-target" "github.com/stackitcloud/stackit-cli/internal/cmd/load-balancer/target-pool/describe" removetarget "github.com/stackitcloud/stackit-cli/internal/cmd/load-balancer/target-pool/remove-target" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" - "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" "github.com/spf13/cobra" ) -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "target-pool", Short: "Provides functionality for target pools", @@ -19,12 +19,12 @@ func NewCmd(p *print.Printer) *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd, p) + addSubcommands(cmd, params) return cmd } -func addSubcommands(cmd *cobra.Command, p *print.Printer) { - cmd.AddCommand(addtarget.NewCmd(p)) - cmd.AddCommand(removetarget.NewCmd(p)) - cmd.AddCommand(describe.NewCmd(p)) +func addSubcommands(cmd *cobra.Command, params *params.CmdParams) { + cmd.AddCommand(addtarget.NewCmd(params)) + cmd.AddCommand(removetarget.NewCmd(params)) + cmd.AddCommand(describe.NewCmd(params)) } diff --git a/internal/cmd/load-balancer/update/update.go b/internal/cmd/load-balancer/update/update.go index 154e9d992..ab2a82d88 100644 --- a/internal/cmd/load-balancer/update/update.go +++ b/internal/cmd/load-balancer/update/update.go @@ -5,6 +5,7 @@ import ( "encoding/json" "fmt" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -28,7 +29,7 @@ type inputModel struct { Payload loadbalancer.UpdateLoadBalancerPayload } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("update %s", loadBalancerNameArg), Short: "Updates a Load Balancer", @@ -53,20 +54,20 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to update load balancer %q?", model.LoadBalancerName) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -80,7 +81,7 @@ func NewCmd(p *print.Printer) *cobra.Command { } // The API has no status to wait on, so async mode is default - p.Info("Updated load balancer with name %q\n", model.LoadBalancerName) + params.Printer.Info("Updated load balancer with name %q\n", model.LoadBalancerName) return nil }, } diff --git a/internal/cmd/load-balancer/update/update_test.go b/internal/cmd/load-balancer/update/update_test.go index b6c998138..9d97f07fc 100644 --- a/internal/cmd/load-balancer/update/update_test.go +++ b/internal/cmd/load-balancer/update/update_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -300,7 +301,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/logme/credentials/create/create.go b/internal/cmd/logme/credentials/create/create.go index 61df00f15..80783e6da 100644 --- a/internal/cmd/logme/credentials/create/create.go +++ b/internal/cmd/logme/credentials/create/create.go @@ -7,6 +7,7 @@ import ( "github.com/goccy/go-yaml" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -30,7 +31,7 @@ type inputModel struct { ShowPassword bool } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "create", Short: "Creates credentials for a LogMe instance", @@ -46,26 +47,26 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } instanceLabel, err := logmeUtils.GetInstanceName(ctx, apiClient, model.ProjectId, model.InstanceId) if err != nil { - p.Debug(print.ErrorLevel, "get instance name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get instance name: %v", err) instanceLabel = model.InstanceId } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to create credentials for instance %q?", instanceLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -78,7 +79,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("create LogMe credentials: %w", err) } - return outputResult(p, model.OutputFormat, model.ShowPassword, instanceLabel, resp) + return outputResult(params.Printer, model.OutputFormat, model.ShowPassword, instanceLabel, resp) }, } configureFlags(cmd) diff --git a/internal/cmd/logme/credentials/create/create_test.go b/internal/cmd/logme/credentials/create/create_test.go index 3ad8aec16..d577d1422 100644 --- a/internal/cmd/logme/credentials/create/create_test.go +++ b/internal/cmd/logme/credentials/create/create_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" @@ -130,7 +131,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -227,7 +228,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.showPassword, tt.args.instanceLabel, tt.args.credentials); (err != nil) != tt.wantErr { diff --git a/internal/cmd/logme/credentials/credentials.go b/internal/cmd/logme/credentials/credentials.go index 1b884797a..9f7cd2d7e 100644 --- a/internal/cmd/logme/credentials/credentials.go +++ b/internal/cmd/logme/credentials/credentials.go @@ -5,14 +5,14 @@ import ( "github.com/stackitcloud/stackit-cli/internal/cmd/logme/credentials/delete" "github.com/stackitcloud/stackit-cli/internal/cmd/logme/credentials/describe" "github.com/stackitcloud/stackit-cli/internal/cmd/logme/credentials/list" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" - "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" "github.com/spf13/cobra" ) -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "credentials", Short: "Provides functionality for LogMe credentials", @@ -20,13 +20,13 @@ func NewCmd(p *print.Printer) *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd, p) + addSubcommands(cmd, params) return cmd } -func addSubcommands(cmd *cobra.Command, p *print.Printer) { - cmd.AddCommand(create.NewCmd(p)) - cmd.AddCommand(delete.NewCmd(p)) - cmd.AddCommand(describe.NewCmd(p)) - cmd.AddCommand(list.NewCmd(p)) +func addSubcommands(cmd *cobra.Command, params *params.CmdParams) { + cmd.AddCommand(create.NewCmd(params)) + cmd.AddCommand(delete.NewCmd(params)) + cmd.AddCommand(describe.NewCmd(params)) + cmd.AddCommand(list.NewCmd(params)) } diff --git a/internal/cmd/logme/credentials/delete/delete.go b/internal/cmd/logme/credentials/delete/delete.go index 2f884ac0e..2295be928 100644 --- a/internal/cmd/logme/credentials/delete/delete.go +++ b/internal/cmd/logme/credentials/delete/delete.go @@ -4,6 +4,7 @@ import ( "context" "fmt" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -30,7 +31,7 @@ type inputModel struct { CredentialsId string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("delete %s", credentialsIdArg), Short: "Deletes credentials of a LogMe instance", @@ -43,32 +44,32 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } instanceLabel, err := logmeUtils.GetInstanceName(ctx, apiClient, model.ProjectId, model.InstanceId) if err != nil { - p.Debug(print.ErrorLevel, "get instance name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get instance name: %v", err) instanceLabel = model.InstanceId } credentialsLabel, err := logmeUtils.GetCredentialsUsername(ctx, apiClient, model.ProjectId, model.InstanceId, model.CredentialsId) if err != nil { - p.Debug(print.ErrorLevel, "get credentials username: %v", err) + params.Printer.Debug(print.ErrorLevel, "get credentials username: %v", err) credentialsLabel = model.CredentialsId } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to delete credentials %s of instance %q? (This cannot be undone)", credentialsLabel, instanceLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -81,7 +82,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("delete LogMe credentials: %w", err) } - p.Info("Deleted credentials %s of instance %q\n", credentialsLabel, instanceLabel) + params.Printer.Info("Deleted credentials %s of instance %q\n", credentialsLabel, instanceLabel) return nil }, } diff --git a/internal/cmd/logme/credentials/delete/delete_test.go b/internal/cmd/logme/credentials/delete/delete_test.go index ea6d637f2..96aff20b2 100644 --- a/internal/cmd/logme/credentials/delete/delete_test.go +++ b/internal/cmd/logme/credentials/delete/delete_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" @@ -165,7 +166,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/logme/credentials/describe/describe.go b/internal/cmd/logme/credentials/describe/describe.go index 2a73c9a15..faf1537b6 100644 --- a/internal/cmd/logme/credentials/describe/describe.go +++ b/internal/cmd/logme/credentials/describe/describe.go @@ -6,6 +6,7 @@ import ( "fmt" "github.com/goccy/go-yaml" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -32,7 +33,7 @@ type inputModel struct { CredentialsId string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("describe %s", credentialsIdArg), Short: "Shows details of credentials of a LogMe instance", @@ -48,13 +49,13 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -66,7 +67,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("describe LogMe credentials: %w", err) } - return outputResult(p, model.OutputFormat, resp) + return outputResult(params.Printer, model.OutputFormat, resp) }, } configureFlags(cmd) diff --git a/internal/cmd/logme/credentials/describe/describe_test.go b/internal/cmd/logme/credentials/describe/describe_test.go index 3ef166834..ae7f38696 100644 --- a/internal/cmd/logme/credentials/describe/describe_test.go +++ b/internal/cmd/logme/credentials/describe/describe_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" @@ -165,7 +166,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -268,7 +269,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.credentials); (err != nil) != tt.wantErr { diff --git a/internal/cmd/logme/credentials/list/list.go b/internal/cmd/logme/credentials/list/list.go index a149a6090..107824d39 100644 --- a/internal/cmd/logme/credentials/list/list.go +++ b/internal/cmd/logme/credentials/list/list.go @@ -6,6 +6,7 @@ import ( "fmt" "github.com/goccy/go-yaml" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -32,7 +33,7 @@ type inputModel struct { Limit *int64 } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "list", Short: "Lists all credentials' IDs for a LogMe instance", @@ -51,13 +52,13 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -72,10 +73,10 @@ func NewCmd(p *print.Printer) *cobra.Command { if len(credentials) == 0 { instanceLabel, err := logmeUtils.GetInstanceName(ctx, apiClient, model.ProjectId, model.InstanceId) if err != nil { - p.Debug(print.ErrorLevel, "get instance name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get instance name: %v", err) instanceLabel = model.InstanceId } - p.Info("No credentials found for instance %q\n", instanceLabel) + params.Printer.Info("No credentials found for instance %q\n", instanceLabel) return nil } @@ -83,7 +84,7 @@ func NewCmd(p *print.Printer) *cobra.Command { if model.Limit != nil && len(credentials) > int(*model.Limit) { credentials = credentials[:*model.Limit] } - return outputResult(p, model.OutputFormat, credentials) + return outputResult(params.Printer, model.OutputFormat, credentials) }, } configureFlags(cmd) diff --git a/internal/cmd/logme/credentials/list/list_test.go b/internal/cmd/logme/credentials/list/list_test.go index dd9b71e66..55c522bbf 100644 --- a/internal/cmd/logme/credentials/list/list_test.go +++ b/internal/cmd/logme/credentials/list/list_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -137,7 +138,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -239,7 +240,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.credentials); (err != nil) != tt.wantErr { diff --git a/internal/cmd/logme/instance/create/create.go b/internal/cmd/logme/instance/create/create.go index df11b20a2..89f2d9dc0 100644 --- a/internal/cmd/logme/instance/create/create.go +++ b/internal/cmd/logme/instance/create/create.go @@ -8,6 +8,7 @@ import ( "strings" "github.com/goccy/go-yaml" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" cliErr "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -55,7 +56,7 @@ type inputModel struct { PlanId *string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "create", Short: "Creates a LogMe instance", @@ -74,26 +75,26 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } - projectLabel, err := projectname.GetProjectName(ctx, p, cmd) + projectLabel, err := projectname.GetProjectName(ctx, params.Printer, cmd) if err != nil { - p.Debug(print.ErrorLevel, "get project name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get project name: %v", err) projectLabel = model.ProjectId } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to create a LogMe instance for project %q?", projectLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -116,7 +117,7 @@ func NewCmd(p *print.Printer) *cobra.Command { // Wait for async operation, if async mode not enabled if !model.Async { - s := spinner.New(p) + s := spinner.New(params.Printer) s.Start("Creating instance") _, err = wait.CreateInstanceWaitHandler(ctx, apiClient, model.ProjectId, instanceId).WaitWithContext(ctx) if err != nil { @@ -125,7 +126,7 @@ func NewCmd(p *print.Printer) *cobra.Command { s.Stop() } - return outputResult(p, model.OutputFormat, model.Async, projectLabel, resp) + return outputResult(params.Printer, model.OutputFormat, model.Async, projectLabel, resp) }, } configureFlags(cmd) diff --git a/internal/cmd/logme/instance/create/create_test.go b/internal/cmd/logme/instance/create/create_test.go index ac8928edf..9f452d086 100644 --- a/internal/cmd/logme/instance/create/create_test.go +++ b/internal/cmd/logme/instance/create/create_test.go @@ -5,6 +5,7 @@ import ( "fmt" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -262,7 +263,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -490,7 +491,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.async, tt.args.projectLabel, tt.args.resp); (err != nil) != tt.wantErr { diff --git a/internal/cmd/logme/instance/delete/delete.go b/internal/cmd/logme/instance/delete/delete.go index 315a2b3be..25693c798 100644 --- a/internal/cmd/logme/instance/delete/delete.go +++ b/internal/cmd/logme/instance/delete/delete.go @@ -4,6 +4,7 @@ import ( "context" "fmt" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -28,7 +29,7 @@ type inputModel struct { InstanceId string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("delete %s", instanceIdArg), Short: "Deletes a LogMe instance", @@ -41,26 +42,26 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } instanceLabel, err := logmeUtils.GetInstanceName(ctx, apiClient, model.ProjectId, model.InstanceId) if err != nil { - p.Debug(print.ErrorLevel, "get instance name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get instance name: %v", err) instanceLabel = model.InstanceId } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to delete instance %q? (This cannot be undone)", instanceLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -75,7 +76,7 @@ func NewCmd(p *print.Printer) *cobra.Command { // Wait for async operation, if async mode not enabled if !model.Async { - s := spinner.New(p) + s := spinner.New(params.Printer) s.Start("Deleting instance") _, err = wait.DeleteInstanceWaitHandler(ctx, apiClient, model.ProjectId, model.InstanceId).WaitWithContext(ctx) if err != nil { @@ -88,7 +89,7 @@ func NewCmd(p *print.Printer) *cobra.Command { if model.Async { operationState = "Triggered deletion of" } - p.Info("%s instance %q\n", operationState, instanceLabel) + params.Printer.Info("%s instance %q\n", operationState, instanceLabel) return nil }, } diff --git a/internal/cmd/logme/instance/delete/delete_test.go b/internal/cmd/logme/instance/delete/delete_test.go index 607bc9bd2..dde9637bb 100644 --- a/internal/cmd/logme/instance/delete/delete_test.go +++ b/internal/cmd/logme/instance/delete/delete_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" @@ -138,7 +139,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/logme/instance/describe/describe.go b/internal/cmd/logme/instance/describe/describe.go index d32944bdf..29b86a0a7 100644 --- a/internal/cmd/logme/instance/describe/describe.go +++ b/internal/cmd/logme/instance/describe/describe.go @@ -6,6 +6,7 @@ import ( "fmt" "github.com/goccy/go-yaml" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -30,7 +31,7 @@ type inputModel struct { InstanceId string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("describe %s", instanceIdArg), Short: "Shows details of a LogMe instance", @@ -46,12 +47,12 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -63,7 +64,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("read LogMe instance: %w", err) } - return outputResult(p, model.OutputFormat, resp) + return outputResult(params.Printer, model.OutputFormat, resp) }, } return cmd diff --git a/internal/cmd/logme/instance/describe/describe_test.go b/internal/cmd/logme/instance/describe/describe_test.go index 3cc0b5e38..00041b4a9 100644 --- a/internal/cmd/logme/instance/describe/describe_test.go +++ b/internal/cmd/logme/instance/describe/describe_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" @@ -138,7 +139,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -241,7 +242,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.instance); (err != nil) != tt.wantErr { diff --git a/internal/cmd/logme/instance/instance.go b/internal/cmd/logme/instance/instance.go index 534151d57..aa86b4f08 100644 --- a/internal/cmd/logme/instance/instance.go +++ b/internal/cmd/logme/instance/instance.go @@ -6,14 +6,14 @@ import ( "github.com/stackitcloud/stackit-cli/internal/cmd/logme/instance/describe" "github.com/stackitcloud/stackit-cli/internal/cmd/logme/instance/list" "github.com/stackitcloud/stackit-cli/internal/cmd/logme/instance/update" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" - "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" "github.com/spf13/cobra" ) -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "instance", Short: "Provides functionality for LogMe instances", @@ -21,14 +21,14 @@ func NewCmd(p *print.Printer) *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd, p) + addSubcommands(cmd, params) return cmd } -func addSubcommands(cmd *cobra.Command, p *print.Printer) { - cmd.AddCommand(create.NewCmd(p)) - cmd.AddCommand(delete.NewCmd(p)) - cmd.AddCommand(describe.NewCmd(p)) - cmd.AddCommand(list.NewCmd(p)) - cmd.AddCommand(update.NewCmd(p)) +func addSubcommands(cmd *cobra.Command, params *params.CmdParams) { + cmd.AddCommand(create.NewCmd(params)) + cmd.AddCommand(delete.NewCmd(params)) + cmd.AddCommand(describe.NewCmd(params)) + cmd.AddCommand(list.NewCmd(params)) + cmd.AddCommand(update.NewCmd(params)) } diff --git a/internal/cmd/logme/instance/list/list.go b/internal/cmd/logme/instance/list/list.go index e59d09cb6..821727852 100644 --- a/internal/cmd/logme/instance/list/list.go +++ b/internal/cmd/logme/instance/list/list.go @@ -7,6 +7,7 @@ import ( "github.com/goccy/go-yaml" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -29,7 +30,7 @@ type inputModel struct { Limit *int64 } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "list", Short: "Lists all LogMe instances", @@ -48,13 +49,13 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -67,12 +68,12 @@ func NewCmd(p *print.Printer) *cobra.Command { } instances := *resp.Instances if len(instances) == 0 { - projectLabel, err := projectname.GetProjectName(ctx, p, cmd) + projectLabel, err := projectname.GetProjectName(ctx, params.Printer, cmd) if err != nil { - p.Debug(print.ErrorLevel, "get project name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get project name: %v", err) projectLabel = model.ProjectId } - p.Info("No instances found for project %q\n", projectLabel) + params.Printer.Info("No instances found for project %q\n", projectLabel) return nil } @@ -81,7 +82,7 @@ func NewCmd(p *print.Printer) *cobra.Command { instances = instances[:*model.Limit] } - return outputResult(p, model.OutputFormat, instances) + return outputResult(params.Printer, model.OutputFormat, instances) }, } diff --git a/internal/cmd/logme/instance/list/list_test.go b/internal/cmd/logme/instance/list/list_test.go index 97df7394c..0168ee409 100644 --- a/internal/cmd/logme/instance/list/list_test.go +++ b/internal/cmd/logme/instance/list/list_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -218,7 +219,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.instances); (err != nil) != tt.wantErr { diff --git a/internal/cmd/logme/instance/update/update.go b/internal/cmd/logme/instance/update/update.go index 08972358e..79802ee58 100644 --- a/internal/cmd/logme/instance/update/update.go +++ b/internal/cmd/logme/instance/update/update.go @@ -6,6 +6,7 @@ import ( "fmt" "strings" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" cliErr "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -55,7 +56,7 @@ type inputModel struct { PlanId *string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("update %s", instanceIdArg), Short: "Updates a LogMe instance", @@ -71,26 +72,26 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } instanceLabel, err := logmeUtils.GetInstanceName(ctx, apiClient, model.ProjectId, model.InstanceId) if err != nil { - p.Debug(print.ErrorLevel, "get instance name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get instance name: %v", err) instanceLabel = model.InstanceId } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to update instance %q?", instanceLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -113,7 +114,7 @@ func NewCmd(p *print.Printer) *cobra.Command { // Wait for async operation, if async mode not enabled if !model.Async { - s := spinner.New(p) + s := spinner.New(params.Printer) s.Start("Updating instance") _, err = wait.PartialUpdateInstanceWaitHandler(ctx, apiClient, model.ProjectId, instanceId).WaitWithContext(ctx) if err != nil { @@ -126,7 +127,7 @@ func NewCmd(p *print.Printer) *cobra.Command { if model.Async { operationState = "Triggered update of" } - p.Info("%s instance %q\n", operationState, instanceLabel) + params.Printer.Info("%s instance %q\n", operationState, instanceLabel) return nil }, } diff --git a/internal/cmd/logme/instance/update/update_test.go b/internal/cmd/logme/instance/update/update_test.go index 4577de0ec..dc3518985 100644 --- a/internal/cmd/logme/instance/update/update_test.go +++ b/internal/cmd/logme/instance/update/update_test.go @@ -5,6 +5,7 @@ import ( "fmt" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -267,7 +268,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/logme/logme.go b/internal/cmd/logme/logme.go index 9dc8b77fd..a4e3f4715 100644 --- a/internal/cmd/logme/logme.go +++ b/internal/cmd/logme/logme.go @@ -4,14 +4,14 @@ import ( "github.com/stackitcloud/stackit-cli/internal/cmd/logme/credentials" "github.com/stackitcloud/stackit-cli/internal/cmd/logme/instance" "github.com/stackitcloud/stackit-cli/internal/cmd/logme/plans" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" - "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" "github.com/spf13/cobra" ) -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "logme", Short: "Provides functionality for LogMe", @@ -19,12 +19,12 @@ func NewCmd(p *print.Printer) *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd, p) + addSubcommands(cmd, params) return cmd } -func addSubcommands(cmd *cobra.Command, p *print.Printer) { - cmd.AddCommand(instance.NewCmd(p)) - cmd.AddCommand(plans.NewCmd(p)) - cmd.AddCommand(credentials.NewCmd(p)) +func addSubcommands(cmd *cobra.Command, params *params.CmdParams) { + cmd.AddCommand(instance.NewCmd(params)) + cmd.AddCommand(plans.NewCmd(params)) + cmd.AddCommand(credentials.NewCmd(params)) } diff --git a/internal/cmd/logme/plans/plans.go b/internal/cmd/logme/plans/plans.go index ccfdc098b..2eb665d7e 100644 --- a/internal/cmd/logme/plans/plans.go +++ b/internal/cmd/logme/plans/plans.go @@ -7,6 +7,7 @@ import ( "github.com/goccy/go-yaml" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -29,7 +30,7 @@ type inputModel struct { Limit *int64 } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "plans", Short: "Lists all LogMe service plans", @@ -48,13 +49,13 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -67,12 +68,12 @@ func NewCmd(p *print.Printer) *cobra.Command { } plans := *resp.Offerings if len(plans) == 0 { - projectLabel, err := projectname.GetProjectName(ctx, p, cmd) + projectLabel, err := projectname.GetProjectName(ctx, params.Printer, cmd) if err != nil { - p.Debug(print.ErrorLevel, "get project name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get project name: %v", err) projectLabel = model.ProjectId } - p.Info("No plans found for project %q\n", projectLabel) + params.Printer.Info("No plans found for project %q\n", projectLabel) return nil } @@ -81,7 +82,7 @@ func NewCmd(p *print.Printer) *cobra.Command { plans = plans[:*model.Limit] } - return outputResult(p, model.OutputFormat, plans) + return outputResult(params.Printer, model.OutputFormat, plans) }, } diff --git a/internal/cmd/logme/plans/plans_test.go b/internal/cmd/logme/plans/plans_test.go index bc8c78bb7..6feb2a2a5 100644 --- a/internal/cmd/logme/plans/plans_test.go +++ b/internal/cmd/logme/plans/plans_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -218,7 +219,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.plans); (err != nil) != tt.wantErr { diff --git a/internal/cmd/mariadb/credentials/create/create.go b/internal/cmd/mariadb/credentials/create/create.go index 880444860..7e4a8a592 100644 --- a/internal/cmd/mariadb/credentials/create/create.go +++ b/internal/cmd/mariadb/credentials/create/create.go @@ -6,6 +6,7 @@ import ( "fmt" "github.com/goccy/go-yaml" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -31,7 +32,7 @@ type inputModel struct { ShowPassword bool } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "create", Short: "Creates credentials for a MariaDB instance", @@ -47,26 +48,26 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } instanceLabel, err := mariadbUtils.GetInstanceName(ctx, apiClient, model.ProjectId, model.InstanceId) if err != nil { - p.Debug(print.ErrorLevel, "get instance name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get instance name: %v", err) instanceLabel = model.InstanceId } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to create credentials for instance %q?", instanceLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -79,7 +80,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("create MariaDB credentials: %w", err) } - return outputResult(p, model.OutputFormat, model.ShowPassword, instanceLabel, resp) + return outputResult(params.Printer, model.OutputFormat, model.ShowPassword, instanceLabel, resp) }, } configureFlags(cmd) diff --git a/internal/cmd/mariadb/credentials/create/create_test.go b/internal/cmd/mariadb/credentials/create/create_test.go index d3202e596..5516a2415 100644 --- a/internal/cmd/mariadb/credentials/create/create_test.go +++ b/internal/cmd/mariadb/credentials/create/create_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" @@ -130,7 +131,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -228,7 +229,7 @@ func TestOutputResult(t *testing.T) { } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.showPassword, tt.args.instanceLabel, tt.args.credentials); (err != nil) != tt.wantErr { diff --git a/internal/cmd/mariadb/credentials/credentials.go b/internal/cmd/mariadb/credentials/credentials.go index f8fb1c5d2..e23c3887f 100644 --- a/internal/cmd/mariadb/credentials/credentials.go +++ b/internal/cmd/mariadb/credentials/credentials.go @@ -5,14 +5,14 @@ import ( "github.com/stackitcloud/stackit-cli/internal/cmd/mariadb/credentials/delete" "github.com/stackitcloud/stackit-cli/internal/cmd/mariadb/credentials/describe" "github.com/stackitcloud/stackit-cli/internal/cmd/mariadb/credentials/list" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" - "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" "github.com/spf13/cobra" ) -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "credentials", Short: "Provides functionality for MariaDB credentials", @@ -20,13 +20,13 @@ func NewCmd(p *print.Printer) *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd, p) + addSubcommands(cmd, params) return cmd } -func addSubcommands(cmd *cobra.Command, p *print.Printer) { - cmd.AddCommand(create.NewCmd(p)) - cmd.AddCommand(delete.NewCmd(p)) - cmd.AddCommand(describe.NewCmd(p)) - cmd.AddCommand(list.NewCmd(p)) +func addSubcommands(cmd *cobra.Command, params *params.CmdParams) { + cmd.AddCommand(create.NewCmd(params)) + cmd.AddCommand(delete.NewCmd(params)) + cmd.AddCommand(describe.NewCmd(params)) + cmd.AddCommand(list.NewCmd(params)) } diff --git a/internal/cmd/mariadb/credentials/delete/delete.go b/internal/cmd/mariadb/credentials/delete/delete.go index 4e8185624..5df7736dc 100644 --- a/internal/cmd/mariadb/credentials/delete/delete.go +++ b/internal/cmd/mariadb/credentials/delete/delete.go @@ -4,6 +4,7 @@ import ( "context" "fmt" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -30,7 +31,7 @@ type inputModel struct { CredentialsId string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("delete %s", credentialsIdArg), Short: "Deletes credentials of a MariaDB instance", @@ -43,32 +44,32 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } instanceLabel, err := mariadbUtils.GetInstanceName(ctx, apiClient, model.ProjectId, model.InstanceId) if err != nil { - p.Debug(print.ErrorLevel, "get instance name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get instance name: %v", err) instanceLabel = model.InstanceId } credentialsLabel, err := mariadbUtils.GetCredentialsUsername(ctx, apiClient, model.ProjectId, model.InstanceId, model.CredentialsId) if err != nil { - p.Debug(print.ErrorLevel, "get credentials username: %v", err) + params.Printer.Debug(print.ErrorLevel, "get credentials username: %v", err) credentialsLabel = model.CredentialsId } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to delete credentials %s of instance %q? (This cannot be undone)", credentialsLabel, instanceLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -81,7 +82,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("delete MariaDB credentials: %w", err) } - p.Info("Deleted credentials %s of instance %q\n", credentialsLabel, instanceLabel) + params.Printer.Info("Deleted credentials %s of instance %q\n", credentialsLabel, instanceLabel) return nil }, } diff --git a/internal/cmd/mariadb/credentials/delete/delete_test.go b/internal/cmd/mariadb/credentials/delete/delete_test.go index c1b2560e5..81da9d7b3 100644 --- a/internal/cmd/mariadb/credentials/delete/delete_test.go +++ b/internal/cmd/mariadb/credentials/delete/delete_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" @@ -165,7 +166,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/mariadb/credentials/describe/describe.go b/internal/cmd/mariadb/credentials/describe/describe.go index 8e3e3f869..c868a7d60 100644 --- a/internal/cmd/mariadb/credentials/describe/describe.go +++ b/internal/cmd/mariadb/credentials/describe/describe.go @@ -6,6 +6,7 @@ import ( "fmt" "github.com/goccy/go-yaml" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -32,7 +33,7 @@ type inputModel struct { CredentialsId string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("describe %s", credentialsIdArg), Short: "Shows details of credentials of a MariaDB instance", @@ -48,13 +49,13 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -66,7 +67,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("describe MariaDB credentials: %w", err) } - return outputResult(p, model.OutputFormat, resp) + return outputResult(params.Printer, model.OutputFormat, resp) }, } configureFlags(cmd) diff --git a/internal/cmd/mariadb/credentials/describe/describe_test.go b/internal/cmd/mariadb/credentials/describe/describe_test.go index f822d88f4..522049aec 100644 --- a/internal/cmd/mariadb/credentials/describe/describe_test.go +++ b/internal/cmd/mariadb/credentials/describe/describe_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" @@ -165,7 +166,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -269,7 +270,7 @@ func TestOutputResult(t *testing.T) { } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.credentials); (err != nil) != tt.wantErr { diff --git a/internal/cmd/mariadb/credentials/list/list.go b/internal/cmd/mariadb/credentials/list/list.go index b854d6c66..acb97d02f 100644 --- a/internal/cmd/mariadb/credentials/list/list.go +++ b/internal/cmd/mariadb/credentials/list/list.go @@ -7,6 +7,7 @@ import ( "github.com/goccy/go-yaml" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -31,7 +32,7 @@ type inputModel struct { Limit *int64 } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "list", Short: "Lists all credentials' IDs for a MariaDB instance", @@ -50,13 +51,13 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -71,10 +72,10 @@ func NewCmd(p *print.Printer) *cobra.Command { if len(credentials) == 0 { instanceLabel, err := mariadbUtils.GetInstanceName(ctx, apiClient, model.ProjectId, model.InstanceId) if err != nil { - p.Debug(print.ErrorLevel, "get instance name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get instance name: %v", err) instanceLabel = model.InstanceId } - p.Info("No credentials found for instance %q\n", instanceLabel) + params.Printer.Info("No credentials found for instance %q\n", instanceLabel) return nil } @@ -82,7 +83,7 @@ func NewCmd(p *print.Printer) *cobra.Command { if model.Limit != nil && len(credentials) > int(*model.Limit) { credentials = credentials[:*model.Limit] } - return outputResult(p, model.OutputFormat, credentials) + return outputResult(params.Printer, model.OutputFormat, credentials) }, } configureFlags(cmd) diff --git a/internal/cmd/mariadb/credentials/list/list_test.go b/internal/cmd/mariadb/credentials/list/list_test.go index 978210a9f..c4fbce7b2 100644 --- a/internal/cmd/mariadb/credentials/list/list_test.go +++ b/internal/cmd/mariadb/credentials/list/list_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -137,7 +138,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -240,7 +241,7 @@ func TestOutputResult(t *testing.T) { } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.credentials); (err != nil) != tt.wantErr { diff --git a/internal/cmd/mariadb/instance/create/create.go b/internal/cmd/mariadb/instance/create/create.go index 3a2a4e4fb..dae8ad6b3 100644 --- a/internal/cmd/mariadb/instance/create/create.go +++ b/internal/cmd/mariadb/instance/create/create.go @@ -8,6 +8,7 @@ import ( "strings" "github.com/goccy/go-yaml" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" cliErr "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -55,7 +56,7 @@ type inputModel struct { PlanId *string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "create", Short: "Creates a MariaDB instance", @@ -74,26 +75,26 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } - projectLabel, err := projectname.GetProjectName(ctx, p, cmd) + projectLabel, err := projectname.GetProjectName(ctx, params.Printer, cmd) if err != nil { - p.Debug(print.ErrorLevel, "get project name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get project name: %v", err) projectLabel = model.ProjectId } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to create a MariaDB instance for project %q?", projectLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -116,7 +117,7 @@ func NewCmd(p *print.Printer) *cobra.Command { // Wait for async operation, if async mode not enabled if !model.Async { - s := spinner.New(p) + s := spinner.New(params.Printer) s.Start("Creating instance") _, err = wait.CreateInstanceWaitHandler(ctx, apiClient, model.ProjectId, instanceId).WaitWithContext(ctx) if err != nil { @@ -125,7 +126,7 @@ func NewCmd(p *print.Printer) *cobra.Command { s.Stop() } - return outputResult(p, model.OutputFormat, model.Async, projectLabel, resp) + return outputResult(params.Printer, model.OutputFormat, model.Async, projectLabel, resp) }, } configureFlags(cmd) diff --git a/internal/cmd/mariadb/instance/create/create_test.go b/internal/cmd/mariadb/instance/create/create_test.go index 0e797ff1e..33dedf860 100644 --- a/internal/cmd/mariadb/instance/create/create_test.go +++ b/internal/cmd/mariadb/instance/create/create_test.go @@ -5,6 +5,7 @@ import ( "fmt" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -262,7 +263,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -491,7 +492,7 @@ func TestOutputResult(t *testing.T) { } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.async, tt.args.projectLabel, tt.args.resp); (err != nil) != tt.wantErr { diff --git a/internal/cmd/mariadb/instance/delete/delete.go b/internal/cmd/mariadb/instance/delete/delete.go index e28fb3593..7b7b2d6e5 100644 --- a/internal/cmd/mariadb/instance/delete/delete.go +++ b/internal/cmd/mariadb/instance/delete/delete.go @@ -4,6 +4,7 @@ import ( "context" "fmt" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -28,7 +29,7 @@ type inputModel struct { InstanceId string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("delete %s", instanceIdArg), Short: "Deletes a MariaDB instance", @@ -41,26 +42,26 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } instanceLabel, err := mariadbUtils.GetInstanceName(ctx, apiClient, model.ProjectId, model.InstanceId) if err != nil { - p.Debug(print.ErrorLevel, "get instance name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get instance name: %v", err) instanceLabel = model.InstanceId } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to delete instance %q? (This cannot be undone)", instanceLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -75,7 +76,7 @@ func NewCmd(p *print.Printer) *cobra.Command { // Wait for async operation, if async mode not enabled if !model.Async { - s := spinner.New(p) + s := spinner.New(params.Printer) s.Start("Deleting instance") _, err = wait.DeleteInstanceWaitHandler(ctx, apiClient, model.ProjectId, model.InstanceId).WaitWithContext(ctx) if err != nil { @@ -88,7 +89,7 @@ func NewCmd(p *print.Printer) *cobra.Command { if model.Async { operationState = "Triggered deletion of" } - p.Info("%s instance %q\n", operationState, instanceLabel) + params.Printer.Info("%s instance %q\n", operationState, instanceLabel) return nil }, } diff --git a/internal/cmd/mariadb/instance/delete/delete_test.go b/internal/cmd/mariadb/instance/delete/delete_test.go index 4dbac6693..737f2affc 100644 --- a/internal/cmd/mariadb/instance/delete/delete_test.go +++ b/internal/cmd/mariadb/instance/delete/delete_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" @@ -138,7 +139,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/mariadb/instance/describe/describe.go b/internal/cmd/mariadb/instance/describe/describe.go index b3b8013a6..978cdcf24 100644 --- a/internal/cmd/mariadb/instance/describe/describe.go +++ b/internal/cmd/mariadb/instance/describe/describe.go @@ -6,6 +6,7 @@ import ( "fmt" "github.com/goccy/go-yaml" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -30,7 +31,7 @@ type inputModel struct { InstanceId string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("describe %s", instanceIdArg), Short: "Shows details of a MariaDB instance", @@ -46,12 +47,12 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -63,7 +64,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("read MariaDB instance: %w", err) } - return outputResult(p, model.OutputFormat, resp) + return outputResult(params.Printer, model.OutputFormat, resp) }, } return cmd diff --git a/internal/cmd/mariadb/instance/describe/describe_test.go b/internal/cmd/mariadb/instance/describe/describe_test.go index 555f7bcbf..3d5cc03ec 100644 --- a/internal/cmd/mariadb/instance/describe/describe_test.go +++ b/internal/cmd/mariadb/instance/describe/describe_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" @@ -138,7 +139,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -242,7 +243,7 @@ func TestOutputResult(t *testing.T) { } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.instance); (err != nil) != tt.wantErr { diff --git a/internal/cmd/mariadb/instance/instance.go b/internal/cmd/mariadb/instance/instance.go index 71e25309a..3b16f4864 100644 --- a/internal/cmd/mariadb/instance/instance.go +++ b/internal/cmd/mariadb/instance/instance.go @@ -6,14 +6,14 @@ import ( "github.com/stackitcloud/stackit-cli/internal/cmd/mariadb/instance/describe" "github.com/stackitcloud/stackit-cli/internal/cmd/mariadb/instance/list" "github.com/stackitcloud/stackit-cli/internal/cmd/mariadb/instance/update" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" - "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" "github.com/spf13/cobra" ) -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "instance", Short: "Provides functionality for MariaDB instances", @@ -21,14 +21,14 @@ func NewCmd(p *print.Printer) *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd, p) + addSubcommands(cmd, params) return cmd } -func addSubcommands(cmd *cobra.Command, p *print.Printer) { - cmd.AddCommand(create.NewCmd(p)) - cmd.AddCommand(delete.NewCmd(p)) - cmd.AddCommand(describe.NewCmd(p)) - cmd.AddCommand(list.NewCmd(p)) - cmd.AddCommand(update.NewCmd(p)) +func addSubcommands(cmd *cobra.Command, params *params.CmdParams) { + cmd.AddCommand(create.NewCmd(params)) + cmd.AddCommand(delete.NewCmd(params)) + cmd.AddCommand(describe.NewCmd(params)) + cmd.AddCommand(list.NewCmd(params)) + cmd.AddCommand(update.NewCmd(params)) } diff --git a/internal/cmd/mariadb/instance/list/list.go b/internal/cmd/mariadb/instance/list/list.go index 2b2dbe70c..3bba56507 100644 --- a/internal/cmd/mariadb/instance/list/list.go +++ b/internal/cmd/mariadb/instance/list/list.go @@ -7,6 +7,7 @@ import ( "github.com/goccy/go-yaml" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -29,7 +30,7 @@ type inputModel struct { Limit *int64 } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "list", Short: "Lists all MariaDB instances", @@ -48,13 +49,13 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -67,12 +68,12 @@ func NewCmd(p *print.Printer) *cobra.Command { } instances := *resp.Instances if len(instances) == 0 { - projectLabel, err := projectname.GetProjectName(ctx, p, cmd) + projectLabel, err := projectname.GetProjectName(ctx, params.Printer, cmd) if err != nil { - p.Debug(print.ErrorLevel, "get project name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get project name: %v", err) projectLabel = model.ProjectId } - p.Info("No instances found for project %q\n", projectLabel) + params.Printer.Info("No instances found for project %q\n", projectLabel) return nil } @@ -81,7 +82,7 @@ func NewCmd(p *print.Printer) *cobra.Command { instances = instances[:*model.Limit] } - return outputResult(p, model.OutputFormat, instances) + return outputResult(params.Printer, model.OutputFormat, instances) }, } diff --git a/internal/cmd/mariadb/instance/list/list_test.go b/internal/cmd/mariadb/instance/list/list_test.go index a7b29714f..7f199d16c 100644 --- a/internal/cmd/mariadb/instance/list/list_test.go +++ b/internal/cmd/mariadb/instance/list/list_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -219,7 +220,7 @@ func TestOutputResult(t *testing.T) { } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.instances); (err != nil) != tt.wantErr { diff --git a/internal/cmd/mariadb/instance/update/update.go b/internal/cmd/mariadb/instance/update/update.go index a2b70759e..80cabdac5 100644 --- a/internal/cmd/mariadb/instance/update/update.go +++ b/internal/cmd/mariadb/instance/update/update.go @@ -6,6 +6,7 @@ import ( "fmt" "strings" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" cliErr "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -53,7 +54,7 @@ type inputModel struct { PlanId *string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("update %s", instanceIdArg), Short: "Updates a MariaDB instance", @@ -69,26 +70,26 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } instanceLabel, err := mariadbUtils.GetInstanceName(ctx, apiClient, model.ProjectId, model.InstanceId) if err != nil { - p.Debug(print.ErrorLevel, "get instance name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get instance name: %v", err) instanceLabel = model.InstanceId } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to update instance %q?", instanceLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -111,7 +112,7 @@ func NewCmd(p *print.Printer) *cobra.Command { // Wait for async operation, if async mode not enabled if !model.Async { - s := spinner.New(p) + s := spinner.New(params.Printer) s.Start("Updating instance") _, err = wait.PartialUpdateInstanceWaitHandler(ctx, apiClient, model.ProjectId, instanceId).WaitWithContext(ctx) if err != nil { @@ -124,7 +125,7 @@ func NewCmd(p *print.Printer) *cobra.Command { if model.Async { operationState = "Triggered update of" } - p.Info("%s instance %q\n", operationState, instanceLabel) + params.Printer.Info("%s instance %q\n", operationState, instanceLabel) return nil }, } diff --git a/internal/cmd/mariadb/instance/update/update_test.go b/internal/cmd/mariadb/instance/update/update_test.go index 514820867..5fb7369c5 100644 --- a/internal/cmd/mariadb/instance/update/update_test.go +++ b/internal/cmd/mariadb/instance/update/update_test.go @@ -5,6 +5,7 @@ import ( "fmt" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -278,7 +279,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/mariadb/mariadb.go b/internal/cmd/mariadb/mariadb.go index 7058813f9..602949253 100644 --- a/internal/cmd/mariadb/mariadb.go +++ b/internal/cmd/mariadb/mariadb.go @@ -4,14 +4,14 @@ import ( "github.com/stackitcloud/stackit-cli/internal/cmd/mariadb/credentials" "github.com/stackitcloud/stackit-cli/internal/cmd/mariadb/instance" "github.com/stackitcloud/stackit-cli/internal/cmd/mariadb/plans" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" - "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" "github.com/spf13/cobra" ) -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "mariadb", Short: "Provides functionality for MariaDB", @@ -19,12 +19,12 @@ func NewCmd(p *print.Printer) *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd, p) + addSubcommands(cmd, params) return cmd } -func addSubcommands(cmd *cobra.Command, p *print.Printer) { - cmd.AddCommand(instance.NewCmd(p)) - cmd.AddCommand(plans.NewCmd(p)) - cmd.AddCommand(credentials.NewCmd(p)) +func addSubcommands(cmd *cobra.Command, params *params.CmdParams) { + cmd.AddCommand(instance.NewCmd(params)) + cmd.AddCommand(plans.NewCmd(params)) + cmd.AddCommand(credentials.NewCmd(params)) } diff --git a/internal/cmd/mariadb/plans/plans.go b/internal/cmd/mariadb/plans/plans.go index e4d3bf021..5b342f29e 100644 --- a/internal/cmd/mariadb/plans/plans.go +++ b/internal/cmd/mariadb/plans/plans.go @@ -7,6 +7,7 @@ import ( "github.com/goccy/go-yaml" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -29,7 +30,7 @@ type inputModel struct { Limit *int64 } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "plans", Short: "Lists all MariaDB service plans", @@ -48,13 +49,13 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -67,12 +68,12 @@ func NewCmd(p *print.Printer) *cobra.Command { } plans := *resp.Offerings if len(plans) == 0 { - projectLabel, err := projectname.GetProjectName(ctx, p, cmd) + projectLabel, err := projectname.GetProjectName(ctx, params.Printer, cmd) if err != nil { - p.Debug(print.ErrorLevel, "get project name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get project name: %v", err) projectLabel = model.ProjectId } - p.Info("No plans found for project %q\n", projectLabel) + params.Printer.Info("No plans found for project %q\n", projectLabel) return nil } @@ -81,7 +82,7 @@ func NewCmd(p *print.Printer) *cobra.Command { plans = plans[:*model.Limit] } - return outputResult(p, model.OutputFormat, plans) + return outputResult(params.Printer, model.OutputFormat, plans) }, } diff --git a/internal/cmd/mariadb/plans/plans_test.go b/internal/cmd/mariadb/plans/plans_test.go index 3d8f56a28..f80f6c9cd 100644 --- a/internal/cmd/mariadb/plans/plans_test.go +++ b/internal/cmd/mariadb/plans/plans_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -219,7 +220,7 @@ func TestOutputResult(t *testing.T) { } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.plans); (err != nil) != tt.wantErr { diff --git a/internal/cmd/mongodbflex/backup/backup.go b/internal/cmd/mongodbflex/backup/backup.go index 738363d78..af716446b 100644 --- a/internal/cmd/mongodbflex/backup/backup.go +++ b/internal/cmd/mongodbflex/backup/backup.go @@ -7,14 +7,14 @@ import ( restorejobs "github.com/stackitcloud/stackit-cli/internal/cmd/mongodbflex/backup/restore-jobs" "github.com/stackitcloud/stackit-cli/internal/cmd/mongodbflex/backup/schedule" updateschedule "github.com/stackitcloud/stackit-cli/internal/cmd/mongodbflex/backup/update-schedule" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" - "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" "github.com/spf13/cobra" ) -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "backup", Short: "Provides functionality for MongoDB Flex instance backups", @@ -22,15 +22,15 @@ func NewCmd(p *print.Printer) *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd, p) + addSubcommands(cmd, params) return cmd } -func addSubcommands(cmd *cobra.Command, p *print.Printer) { - cmd.AddCommand(updateschedule.NewCmd(p)) - cmd.AddCommand(schedule.NewCmd(p)) - cmd.AddCommand(restore.NewCmd(p)) - cmd.AddCommand(list.NewCmd(p)) - cmd.AddCommand(describe.NewCmd(p)) - cmd.AddCommand(restorejobs.NewCmd(p)) +func addSubcommands(cmd *cobra.Command, params *params.CmdParams) { + cmd.AddCommand(updateschedule.NewCmd(params)) + cmd.AddCommand(schedule.NewCmd(params)) + cmd.AddCommand(restore.NewCmd(params)) + cmd.AddCommand(list.NewCmd(params)) + cmd.AddCommand(describe.NewCmd(params)) + cmd.AddCommand(restorejobs.NewCmd(params)) } diff --git a/internal/cmd/mongodbflex/backup/describe/describe.go b/internal/cmd/mongodbflex/backup/describe/describe.go index a30fb3f63..43e8a0b81 100644 --- a/internal/cmd/mongodbflex/backup/describe/describe.go +++ b/internal/cmd/mongodbflex/backup/describe/describe.go @@ -7,6 +7,7 @@ import ( "github.com/goccy/go-yaml" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -33,7 +34,7 @@ type inputModel struct { BackupId string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("describe %s", backupIdArg), Short: "Shows details of a backup for a MongoDB Flex instance", @@ -49,20 +50,20 @@ func NewCmd(p *print.Printer) *cobra.Command { Args: args.SingleArg(backupIdArg, nil), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } instanceLabel, err := mongoUtils.GetInstanceName(ctx, apiClient, model.ProjectId, model.InstanceId) if err != nil { - p.Debug(print.ErrorLevel, "get instance name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get instance name: %v", err) instanceLabel = model.InstanceId } @@ -80,7 +81,7 @@ func NewCmd(p *print.Printer) *cobra.Command { } restoreJobState := mongoUtils.GetRestoreStatus(model.BackupId, restoreJobs) - return outputResult(p, model.OutputFormat, restoreJobState, *resp.Item) + return outputResult(params.Printer, model.OutputFormat, restoreJobState, *resp.Item) }, } configureFlags(cmd) diff --git a/internal/cmd/mongodbflex/backup/describe/describe_test.go b/internal/cmd/mongodbflex/backup/describe/describe_test.go index 0b761904d..914f27259 100644 --- a/internal/cmd/mongodbflex/backup/describe/describe_test.go +++ b/internal/cmd/mongodbflex/backup/describe/describe_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" @@ -159,7 +160,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -263,7 +264,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.restoreStatus, tt.args.backup); (err != nil) != tt.wantErr { diff --git a/internal/cmd/mongodbflex/backup/list/list.go b/internal/cmd/mongodbflex/backup/list/list.go index b145685fb..75ddca216 100644 --- a/internal/cmd/mongodbflex/backup/list/list.go +++ b/internal/cmd/mongodbflex/backup/list/list.go @@ -6,6 +6,7 @@ import ( "fmt" "github.com/goccy/go-yaml" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -33,7 +34,7 @@ type inputModel struct { Limit *int64 } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "list", Short: "Lists all backups which are available for a MongoDB Flex instance", @@ -52,20 +53,20 @@ func NewCmd(p *print.Printer) *cobra.Command { Args: args.NoArgs, RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } instanceLabel, err := mongodbflexUtils.GetInstanceName(ctx, apiClient, model.ProjectId, *model.InstanceId) if err != nil { - p.Debug(print.ErrorLevel, "get instance name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get instance name: %v", err) instanceLabel = *model.InstanceId } @@ -91,7 +92,7 @@ func NewCmd(p *print.Printer) *cobra.Command { backups = backups[:*model.Limit] } - return outputResult(p, model.OutputFormat, backups, restoreJobs) + return outputResult(params.Printer, model.OutputFormat, backups, restoreJobs) }, } diff --git a/internal/cmd/mongodbflex/backup/list/list_test.go b/internal/cmd/mongodbflex/backup/list/list_test.go index 362b35c78..3328c6030 100644 --- a/internal/cmd/mongodbflex/backup/list/list_test.go +++ b/internal/cmd/mongodbflex/backup/list/list_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -137,7 +138,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -248,7 +249,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.backups, tt.args.restoreJobs); (err != nil) != tt.wantErr { diff --git a/internal/cmd/mongodbflex/backup/restore-jobs/restore_jobs.go b/internal/cmd/mongodbflex/backup/restore-jobs/restore_jobs.go index 30a48be34..7119dccbb 100644 --- a/internal/cmd/mongodbflex/backup/restore-jobs/restore_jobs.go +++ b/internal/cmd/mongodbflex/backup/restore-jobs/restore_jobs.go @@ -7,6 +7,7 @@ import ( "github.com/goccy/go-yaml" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -32,7 +33,7 @@ type inputModel struct { Limit *int64 } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "restore-jobs", Short: "Lists all restore jobs which have been run for a MongoDB Flex instance", @@ -51,20 +52,20 @@ func NewCmd(p *print.Printer) *cobra.Command { Args: args.NoArgs, RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } instanceLabel, err := mongodbflexUtils.GetInstanceName(ctx, apiClient, model.ProjectId, *model.InstanceId) if err != nil { - p.Debug(print.ErrorLevel, "get instance name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get instance name: %v", err) instanceLabel = *model.InstanceId } @@ -85,7 +86,7 @@ func NewCmd(p *print.Printer) *cobra.Command { restoreJobs = restoreJobs[:*model.Limit] } - return outputResult(p, model.OutputFormat, restoreJobs) + return outputResult(params.Printer, model.OutputFormat, restoreJobs) }, } diff --git a/internal/cmd/mongodbflex/backup/restore-jobs/restore_jobs_test.go b/internal/cmd/mongodbflex/backup/restore-jobs/restore_jobs_test.go index 5135be262..61f911ea7 100644 --- a/internal/cmd/mongodbflex/backup/restore-jobs/restore_jobs_test.go +++ b/internal/cmd/mongodbflex/backup/restore-jobs/restore_jobs_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -137,7 +138,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -239,7 +240,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.restoreJobs); (err != nil) != tt.wantErr { diff --git a/internal/cmd/mongodbflex/backup/restore/restore.go b/internal/cmd/mongodbflex/backup/restore/restore.go index c8de137fc..067c0e56e 100644 --- a/internal/cmd/mongodbflex/backup/restore/restore.go +++ b/internal/cmd/mongodbflex/backup/restore/restore.go @@ -4,6 +4,7 @@ import ( "context" "fmt" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" cliErr "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -34,7 +35,7 @@ type inputModel struct { Timestamp string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "restore", Short: "Restores a MongoDB Flex instance from a backup", @@ -58,26 +59,26 @@ func NewCmd(p *print.Printer) *cobra.Command { RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } instanceLabel, err := mongodbUtils.GetInstanceName(ctx, apiClient, model.ProjectId, model.InstanceId) if err != nil { - p.Debug(print.ErrorLevel, "get instance name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get instance name: %v", err) instanceLabel = model.ProjectId } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to restore MongoDB Flex instance %q?", instanceLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -99,7 +100,7 @@ func NewCmd(p *print.Printer) *cobra.Command { } if !model.Async { - s := spinner.New(p) + s := spinner.New(params.Printer) s.Start("Restoring instance") _, err = wait.RestoreInstanceWaitHandler(ctx, apiClient, model.ProjectId, model.InstanceId, model.BackupId).WaitWithContext(ctx) if err != nil { @@ -108,7 +109,7 @@ func NewCmd(p *print.Printer) *cobra.Command { s.Stop() } - p.Outputf("Restored instance %q with backup %q\n", model.InstanceId, model.BackupId) + params.Printer.Outputf("Restored instance %q with backup %q\n", model.InstanceId, model.BackupId) return nil } @@ -120,7 +121,7 @@ func NewCmd(p *print.Printer) *cobra.Command { } if !model.Async { - s := spinner.New(p) + s := spinner.New(params.Printer) s.Start("Cloning instance") _, err = wait.CloneInstanceWaitHandler(ctx, apiClient, model.ProjectId, model.InstanceId).WaitWithContext(ctx) if err != nil { @@ -129,7 +130,7 @@ func NewCmd(p *print.Printer) *cobra.Command { s.Stop() } - p.Outputf("Cloned instance %q from backup with timestamp %q\n", model.InstanceId, model.Timestamp) + params.Printer.Outputf("Cloned instance %q from backup with timestamp %q\n", model.InstanceId, model.Timestamp) return nil }, } diff --git a/internal/cmd/mongodbflex/backup/restore/restore_test.go b/internal/cmd/mongodbflex/backup/restore/restore_test.go index 63e06d9af..bffa02934 100644 --- a/internal/cmd/mongodbflex/backup/restore/restore_test.go +++ b/internal/cmd/mongodbflex/backup/restore/restore_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -170,7 +171,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/mongodbflex/backup/schedule/schedule.go b/internal/cmd/mongodbflex/backup/schedule/schedule.go index b21167724..55272a544 100644 --- a/internal/cmd/mongodbflex/backup/schedule/schedule.go +++ b/internal/cmd/mongodbflex/backup/schedule/schedule.go @@ -7,6 +7,7 @@ import ( "github.com/goccy/go-yaml" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -28,7 +29,7 @@ type inputModel struct { InstanceId string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "schedule", Short: "Shows details of the backup schedule and retention policy of a MongoDB Flex instance", @@ -44,12 +45,12 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -61,7 +62,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("read MongoDB Flex instance: %w", err) } - return outputResult(p, model.OutputFormat, resp.Item) + return outputResult(params.Printer, model.OutputFormat, resp.Item) }, } configureFlags(cmd) diff --git a/internal/cmd/mongodbflex/backup/schedule/schedule_test.go b/internal/cmd/mongodbflex/backup/schedule/schedule_test.go index 018cefc28..705bd7432 100644 --- a/internal/cmd/mongodbflex/backup/schedule/schedule_test.go +++ b/internal/cmd/mongodbflex/backup/schedule/schedule_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" @@ -218,7 +219,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.instance); (err != nil) != tt.wantErr { diff --git a/internal/cmd/mongodbflex/backup/update-schedule/update_schedule.go b/internal/cmd/mongodbflex/backup/update-schedule/update_schedule.go index 8f75c6964..d417ccbf4 100644 --- a/internal/cmd/mongodbflex/backup/update-schedule/update_schedule.go +++ b/internal/cmd/mongodbflex/backup/update-schedule/update_schedule.go @@ -6,6 +6,7 @@ import ( "strconv" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" cliErr "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -46,7 +47,7 @@ type inputModel struct { MonthlySnapshotRetentionMonths *int64 } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "update-schedule", Short: "Updates the backup schedule and retention policy for a MongoDB Flex instance", @@ -69,26 +70,26 @@ func NewCmd(p *print.Printer) *cobra.Command { RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } instanceLabel, err := mongoDBflexUtils.GetInstanceName(ctx, apiClient, model.ProjectId, *model.InstanceId) if err != nil { - p.Debug(print.ErrorLevel, "get instance name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get instance name: %v", err) instanceLabel = *model.InstanceId } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to update backup schedule of instance %q?", instanceLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } diff --git a/internal/cmd/mongodbflex/instance/create/create.go b/internal/cmd/mongodbflex/instance/create/create.go index c6c4d3322..2b8905364 100644 --- a/internal/cmd/mongodbflex/instance/create/create.go +++ b/internal/cmd/mongodbflex/instance/create/create.go @@ -8,6 +8,7 @@ import ( "github.com/goccy/go-yaml" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" cliErr "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -56,7 +57,7 @@ type inputModel struct { Type *string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "create", Short: "Creates a MongoDB Flex instance", @@ -76,26 +77,26 @@ func NewCmd(p *print.Printer) *cobra.Command { RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } - projectLabel, err := projectname.GetProjectName(ctx, p, cmd) + projectLabel, err := projectname.GetProjectName(ctx, params.Printer, cmd) if err != nil { - p.Debug(print.ErrorLevel, "get project name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get project name: %v", err) projectLabel = model.ProjectId } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to create a MongoDB Flex instance for project %q?", projectLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -123,7 +124,7 @@ func NewCmd(p *print.Printer) *cobra.Command { // Wait for async operation, if async mode not enabled if !model.Async { - s := spinner.New(p) + s := spinner.New(params.Printer) s.Start("Creating instance") _, err = wait.CreateInstanceWaitHandler(ctx, apiClient, model.ProjectId, instanceId).WaitWithContext(ctx) if err != nil { @@ -132,7 +133,7 @@ func NewCmd(p *print.Printer) *cobra.Command { s.Stop() } - return outputResult(p, model.OutputFormat, model.Async, projectLabel, resp) + return outputResult(params.Printer, model.OutputFormat, model.Async, projectLabel, resp) }, } configureFlags(cmd) diff --git a/internal/cmd/mongodbflex/instance/create/create_test.go b/internal/cmd/mongodbflex/instance/create/create_test.go index 23fa109b7..72ab744b7 100644 --- a/internal/cmd/mongodbflex/instance/create/create_test.go +++ b/internal/cmd/mongodbflex/instance/create/create_test.go @@ -5,6 +5,7 @@ import ( "fmt" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -250,7 +251,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -573,7 +574,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.async, tt.args.projectLabel, tt.args.createInstanceResponse); (err != nil) != tt.wantErr { diff --git a/internal/cmd/mongodbflex/instance/delete/delete.go b/internal/cmd/mongodbflex/instance/delete/delete.go index 237681df0..86653c5a3 100644 --- a/internal/cmd/mongodbflex/instance/delete/delete.go +++ b/internal/cmd/mongodbflex/instance/delete/delete.go @@ -4,6 +4,7 @@ import ( "context" "fmt" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -28,7 +29,7 @@ type inputModel struct { InstanceId string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("delete %s", instanceIdArg), Short: "Deletes a MongoDB Flex instance", @@ -41,26 +42,26 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } instanceLabel, err := mongodbflexUtils.GetInstanceName(ctx, apiClient, model.ProjectId, model.InstanceId) if err != nil { - p.Debug(print.ErrorLevel, "get instance name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get instance name: %v", err) instanceLabel = model.InstanceId } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to delete instance %q? (This cannot be undone)", instanceLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -75,7 +76,7 @@ func NewCmd(p *print.Printer) *cobra.Command { // Wait for async operation, if async mode not enabled if !model.Async { - s := spinner.New(p) + s := spinner.New(params.Printer) s.Start("Deleting instance") _, err = wait.DeleteInstanceWaitHandler(ctx, apiClient, model.ProjectId, model.InstanceId).WaitWithContext(ctx) if err != nil { @@ -88,7 +89,7 @@ func NewCmd(p *print.Printer) *cobra.Command { if model.Async { operationState = "Triggered deletion of" } - p.Info("%s instance %q\n", operationState, instanceLabel) + params.Printer.Info("%s instance %q\n", operationState, instanceLabel) return nil }, } diff --git a/internal/cmd/mongodbflex/instance/delete/delete_test.go b/internal/cmd/mongodbflex/instance/delete/delete_test.go index ec3913381..1c02e7f87 100644 --- a/internal/cmd/mongodbflex/instance/delete/delete_test.go +++ b/internal/cmd/mongodbflex/instance/delete/delete_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" @@ -138,7 +139,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/mongodbflex/instance/describe/describe.go b/internal/cmd/mongodbflex/instance/describe/describe.go index 79ca55b64..2b64e3432 100644 --- a/internal/cmd/mongodbflex/instance/describe/describe.go +++ b/internal/cmd/mongodbflex/instance/describe/describe.go @@ -7,6 +7,7 @@ import ( "strings" "github.com/goccy/go-yaml" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -30,7 +31,7 @@ type inputModel struct { InstanceId string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("describe %s", instanceIdArg), Short: "Shows details of a MongoDB Flex instance", @@ -46,12 +47,12 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -63,7 +64,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("read MongoDB Flex instance: %w", err) } - return outputResult(p, model.OutputFormat, resp.Item) + return outputResult(params.Printer, model.OutputFormat, resp.Item) }, } return cmd diff --git a/internal/cmd/mongodbflex/instance/describe/describe_test.go b/internal/cmd/mongodbflex/instance/describe/describe_test.go index 1238f7bc0..c933d86e4 100644 --- a/internal/cmd/mongodbflex/instance/describe/describe_test.go +++ b/internal/cmd/mongodbflex/instance/describe/describe_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" @@ -138,7 +139,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -241,7 +242,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.instance); (err != nil) != tt.wantErr { diff --git a/internal/cmd/mongodbflex/instance/instance.go b/internal/cmd/mongodbflex/instance/instance.go index 1a770bcc1..a4cc5b231 100644 --- a/internal/cmd/mongodbflex/instance/instance.go +++ b/internal/cmd/mongodbflex/instance/instance.go @@ -6,14 +6,14 @@ import ( "github.com/stackitcloud/stackit-cli/internal/cmd/mongodbflex/instance/describe" "github.com/stackitcloud/stackit-cli/internal/cmd/mongodbflex/instance/list" "github.com/stackitcloud/stackit-cli/internal/cmd/mongodbflex/instance/update" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" - "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" "github.com/spf13/cobra" ) -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "instance", Short: "Provides functionality for MongoDB Flex instances", @@ -21,14 +21,14 @@ func NewCmd(p *print.Printer) *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd, p) + addSubcommands(cmd, params) return cmd } -func addSubcommands(cmd *cobra.Command, p *print.Printer) { - cmd.AddCommand(create.NewCmd(p)) - cmd.AddCommand(delete.NewCmd(p)) - cmd.AddCommand(describe.NewCmd(p)) - cmd.AddCommand(list.NewCmd(p)) - cmd.AddCommand(update.NewCmd(p)) +func addSubcommands(cmd *cobra.Command, params *params.CmdParams) { + cmd.AddCommand(create.NewCmd(params)) + cmd.AddCommand(delete.NewCmd(params)) + cmd.AddCommand(describe.NewCmd(params)) + cmd.AddCommand(list.NewCmd(params)) + cmd.AddCommand(update.NewCmd(params)) } diff --git a/internal/cmd/mongodbflex/instance/list/list.go b/internal/cmd/mongodbflex/instance/list/list.go index 5b65f8d5e..a016550cc 100644 --- a/internal/cmd/mongodbflex/instance/list/list.go +++ b/internal/cmd/mongodbflex/instance/list/list.go @@ -6,6 +6,7 @@ import ( "fmt" "github.com/goccy/go-yaml" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -30,7 +31,7 @@ type inputModel struct { Limit *int64 } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "list", Short: "Lists all MongoDB Flex instances", @@ -49,13 +50,13 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -67,12 +68,12 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("get MongoDB Flex instances: %w", err) } if resp.Items == nil || len(*resp.Items) == 0 { - projectLabel, err := projectname.GetProjectName(ctx, p, cmd) + projectLabel, err := projectname.GetProjectName(ctx, params.Printer, cmd) if err != nil { - p.Debug(print.ErrorLevel, "get project name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get project name: %v", err) projectLabel = model.ProjectId } - p.Info("No instances found for project %q\n", projectLabel) + params.Printer.Info("No instances found for project %q\n", projectLabel) return nil } instances := *resp.Items @@ -82,7 +83,7 @@ func NewCmd(p *print.Printer) *cobra.Command { instances = instances[:*model.Limit] } - return outputResult(p, model.OutputFormat, instances) + return outputResult(params.Printer, model.OutputFormat, instances) }, } diff --git a/internal/cmd/mongodbflex/instance/list/list_test.go b/internal/cmd/mongodbflex/instance/list/list_test.go index e6a9c1416..e8a95d34c 100644 --- a/internal/cmd/mongodbflex/instance/list/list_test.go +++ b/internal/cmd/mongodbflex/instance/list/list_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -218,7 +219,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.instanceList); (err != nil) != tt.wantErr { diff --git a/internal/cmd/mongodbflex/instance/update/update.go b/internal/cmd/mongodbflex/instance/update/update.go index 5f854005c..e1882bfd9 100644 --- a/internal/cmd/mongodbflex/instance/update/update.go +++ b/internal/cmd/mongodbflex/instance/update/update.go @@ -7,6 +7,7 @@ import ( "fmt" "github.com/goccy/go-yaml" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" cliErr "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -54,7 +55,7 @@ type inputModel struct { Type *string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("update %s", instanceIdArg), Short: "Updates a MongoDB Flex instance", @@ -71,26 +72,26 @@ func NewCmd(p *print.Printer) *cobra.Command { RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } instanceLabel, err := mongodbflexUtils.GetInstanceName(ctx, apiClient, model.ProjectId, model.InstanceId) if err != nil { - p.Debug(print.ErrorLevel, "get instance name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get instance name: %v", err) instanceLabel = model.InstanceId } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to update instance %q?", instanceLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -109,7 +110,7 @@ func NewCmd(p *print.Printer) *cobra.Command { // Wait for async operation, if async mode not enabled if !model.Async { - s := spinner.New(p) + s := spinner.New(params.Printer) s.Start("Updating instance") _, err = wait.PartialUpdateInstanceWaitHandler(ctx, apiClient, model.ProjectId, instanceId).WaitWithContext(ctx) if err != nil { @@ -118,7 +119,7 @@ func NewCmd(p *print.Printer) *cobra.Command { s.Stop() } - return outputResult(p, model.OutputFormat, model.Async, instanceLabel, resp) + return outputResult(params.Printer, model.OutputFormat, model.Async, instanceLabel, resp) }, } configureFlags(cmd) diff --git a/internal/cmd/mongodbflex/instance/update/update_test.go b/internal/cmd/mongodbflex/instance/update/update_test.go index a36f33454..0379f2c0f 100644 --- a/internal/cmd/mongodbflex/instance/update/update_test.go +++ b/internal/cmd/mongodbflex/instance/update/update_test.go @@ -5,6 +5,7 @@ import ( "fmt" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -280,7 +281,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -617,7 +618,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.async, tt.args.instanceLabel, tt.args.resp); (err != nil) != tt.wantErr { diff --git a/internal/cmd/mongodbflex/mongodbflex.go b/internal/cmd/mongodbflex/mongodbflex.go index e7373b9b9..50ba29f78 100644 --- a/internal/cmd/mongodbflex/mongodbflex.go +++ b/internal/cmd/mongodbflex/mongodbflex.go @@ -5,14 +5,14 @@ import ( "github.com/stackitcloud/stackit-cli/internal/cmd/mongodbflex/instance" "github.com/stackitcloud/stackit-cli/internal/cmd/mongodbflex/options" "github.com/stackitcloud/stackit-cli/internal/cmd/mongodbflex/user" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" - "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" "github.com/spf13/cobra" ) -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "mongodbflex", Short: "Provides functionality for MongoDB Flex", @@ -20,13 +20,13 @@ func NewCmd(p *print.Printer) *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd, p) + addSubcommands(cmd, params) return cmd } -func addSubcommands(cmd *cobra.Command, p *print.Printer) { - cmd.AddCommand(instance.NewCmd(p)) - cmd.AddCommand(user.NewCmd(p)) - cmd.AddCommand(options.NewCmd(p)) - cmd.AddCommand(backup.NewCmd(p)) +func addSubcommands(cmd *cobra.Command, params *params.CmdParams) { + cmd.AddCommand(instance.NewCmd(params)) + cmd.AddCommand(user.NewCmd(params)) + cmd.AddCommand(options.NewCmd(params)) + cmd.AddCommand(backup.NewCmd(params)) } diff --git a/internal/cmd/mongodbflex/options/options.go b/internal/cmd/mongodbflex/options/options.go index 82b43cbc5..92c6c55bb 100644 --- a/internal/cmd/mongodbflex/options/options.go +++ b/internal/cmd/mongodbflex/options/options.go @@ -5,6 +5,8 @@ import ( "encoding/json" "fmt" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" + "github.com/goccy/go-yaml" "github.com/spf13/cobra" "github.com/stackitcloud/stackit-cli/internal/pkg/args" @@ -45,7 +47,7 @@ type flavorStorages struct { Storages *mongodbflex.ListStoragesResponse `json:"storages"` } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "options", Short: "Lists MongoDB Flex options", @@ -64,19 +66,19 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } // Call API - err = buildAndExecuteRequest(ctx, p, model, apiClient) + err = buildAndExecuteRequest(ctx, params.Printer, model, apiClient) if err != nil { return fmt.Errorf("get MongoDB Flex options: %w", err) } diff --git a/internal/cmd/mongodbflex/options/options_test.go b/internal/cmd/mongodbflex/options/options_test.go index 9b48a710b..dc1eb60c8 100644 --- a/internal/cmd/mongodbflex/options/options_test.go +++ b/internal/cmd/mongodbflex/options/options_test.go @@ -5,6 +5,7 @@ import ( "fmt" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -167,7 +168,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -291,7 +292,7 @@ func TestBuildAndExecuteRequest(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := &print.Printer{} - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) p.Cmd = cmd client := &mongoDBFlexClientMocked{ listFlavorsFails: tt.listFlavorsFails, @@ -406,7 +407,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.inputModel, tt.args.flavors, tt.args.versions, tt.args.storages); (err != nil) != tt.wantErr { @@ -455,7 +456,7 @@ func TestOutputResultAsTable(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResultAsTable(p, tt.args.model, tt.args.options); (err != nil) != tt.wantErr { diff --git a/internal/cmd/mongodbflex/user/create/create.go b/internal/cmd/mongodbflex/user/create/create.go index be494fcdb..afc26fe76 100644 --- a/internal/cmd/mongodbflex/user/create/create.go +++ b/internal/cmd/mongodbflex/user/create/create.go @@ -7,6 +7,7 @@ import ( "github.com/goccy/go-yaml" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -39,7 +40,7 @@ type inputModel struct { Roles *[]string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "create", Short: "Creates a MongoDB Flex user", @@ -60,26 +61,26 @@ func NewCmd(p *print.Printer) *cobra.Command { Args: args.NoArgs, RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } instanceLabel, err := mongodbflexUtils.GetInstanceName(ctx, apiClient, model.ProjectId, model.InstanceId) if err != nil { - p.Debug(print.ErrorLevel, "get instance name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get instance name: %v", err) instanceLabel = model.InstanceId } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to create a user for instance %q?", instanceLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -93,7 +94,7 @@ func NewCmd(p *print.Printer) *cobra.Command { } user := resp.Item - return outputResult(p, model.OutputFormat, instanceLabel, user) + return outputResult(params.Printer, model.OutputFormat, instanceLabel, user) }, } diff --git a/internal/cmd/mongodbflex/user/create/create_test.go b/internal/cmd/mongodbflex/user/create/create_test.go index 73f184a65..75e02fef0 100644 --- a/internal/cmd/mongodbflex/user/create/create_test.go +++ b/internal/cmd/mongodbflex/user/create/create_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -270,7 +271,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.instanceLabel, tt.args.user); (err != nil) != tt.wantErr { diff --git a/internal/cmd/mongodbflex/user/delete/delete.go b/internal/cmd/mongodbflex/user/delete/delete.go index 7eec0fbd5..d57879062 100644 --- a/internal/cmd/mongodbflex/user/delete/delete.go +++ b/internal/cmd/mongodbflex/user/delete/delete.go @@ -4,6 +4,7 @@ import ( "context" "fmt" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -31,7 +32,7 @@ type inputModel struct { UserId string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("delete %s", userIdArg), Short: "Deletes a MongoDB Flex user", @@ -47,32 +48,32 @@ func NewCmd(p *print.Printer) *cobra.Command { Args: args.SingleArg(userIdArg, utils.ValidateUUID), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } instanceLabel, err := mongodbflexUtils.GetInstanceName(ctx, apiClient, model.ProjectId, model.InstanceId) if err != nil { - p.Debug(print.ErrorLevel, "get instance name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get instance name: %v", err) instanceLabel = model.InstanceId } userLabel, err := mongodbflexUtils.GetUserName(ctx, apiClient, model.ProjectId, model.InstanceId, model.UserId) if err != nil { - p.Debug(print.ErrorLevel, "get user name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get user name: %v", err) userLabel = model.UserId } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to delete user %q of instance %q? (This cannot be undone)", userLabel, instanceLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -85,7 +86,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("delete MongoDB Flex user: %w", err) } - p.Info("Deleted user %q of instance %q\n", userLabel, instanceLabel) + params.Printer.Info("Deleted user %q of instance %q\n", userLabel, instanceLabel) return nil }, } diff --git a/internal/cmd/mongodbflex/user/delete/delete_test.go b/internal/cmd/mongodbflex/user/delete/delete_test.go index 36e356790..b373f7f56 100644 --- a/internal/cmd/mongodbflex/user/delete/delete_test.go +++ b/internal/cmd/mongodbflex/user/delete/delete_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" @@ -165,7 +166,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/mongodbflex/user/describe/describe.go b/internal/cmd/mongodbflex/user/describe/describe.go index b94f4b453..89f919f7b 100644 --- a/internal/cmd/mongodbflex/user/describe/describe.go +++ b/internal/cmd/mongodbflex/user/describe/describe.go @@ -6,6 +6,7 @@ import ( "fmt" "github.com/goccy/go-yaml" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -33,7 +34,7 @@ type inputModel struct { UserId string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("describe %s", userIdArg), Short: "Shows details of a MongoDB Flex user", @@ -53,13 +54,13 @@ func NewCmd(p *print.Printer) *cobra.Command { Args: args.SingleArg(userIdArg, utils.ValidateUUID), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -71,7 +72,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("get MongoDB Flex user: %w", err) } - return outputResult(p, model.OutputFormat, *resp.Item) + return outputResult(params.Printer, model.OutputFormat, *resp.Item) }, } diff --git a/internal/cmd/mongodbflex/user/describe/describe_test.go b/internal/cmd/mongodbflex/user/describe/describe_test.go index 7d5201075..c151b4da0 100644 --- a/internal/cmd/mongodbflex/user/describe/describe_test.go +++ b/internal/cmd/mongodbflex/user/describe/describe_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" @@ -165,7 +166,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -268,7 +269,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.instanceResponseUser); (err != nil) != tt.wantErr { diff --git a/internal/cmd/mongodbflex/user/list/list.go b/internal/cmd/mongodbflex/user/list/list.go index ae690568b..299319462 100644 --- a/internal/cmd/mongodbflex/user/list/list.go +++ b/internal/cmd/mongodbflex/user/list/list.go @@ -6,6 +6,7 @@ import ( "fmt" "github.com/goccy/go-yaml" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -33,7 +34,7 @@ type inputModel struct { Limit *int64 } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "list", Short: "Lists all MongoDB Flex users of an instance", @@ -52,13 +53,13 @@ func NewCmd(p *print.Printer) *cobra.Command { Args: args.NoArgs, RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -72,10 +73,10 @@ func NewCmd(p *print.Printer) *cobra.Command { if resp.Items == nil || len(*resp.Items) == 0 { instanceLabel, err := mongodbflexUtils.GetInstanceName(ctx, apiClient, model.ProjectId, *model.InstanceId) if err != nil { - p.Debug(print.ErrorLevel, "get instance name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get instance name: %v", err) instanceLabel = *model.InstanceId } - p.Info("No users found for instance %q\n", instanceLabel) + params.Printer.Info("No users found for instance %q\n", instanceLabel) return nil } users := *resp.Items @@ -85,7 +86,7 @@ func NewCmd(p *print.Printer) *cobra.Command { users = users[:*model.Limit] } - return outputResult(p, model.OutputFormat, users) + return outputResult(params.Printer, model.OutputFormat, users) }, } diff --git a/internal/cmd/mongodbflex/user/list/list_test.go b/internal/cmd/mongodbflex/user/list/list_test.go index 8cc901aef..18e9325f5 100644 --- a/internal/cmd/mongodbflex/user/list/list_test.go +++ b/internal/cmd/mongodbflex/user/list/list_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -235,7 +236,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.users); (err != nil) != tt.wantErr { diff --git a/internal/cmd/mongodbflex/user/reset-password/reset_password.go b/internal/cmd/mongodbflex/user/reset-password/reset_password.go index 5541b89a3..72d7ad9d4 100644 --- a/internal/cmd/mongodbflex/user/reset-password/reset_password.go +++ b/internal/cmd/mongodbflex/user/reset-password/reset_password.go @@ -6,6 +6,7 @@ import ( "fmt" "github.com/goccy/go-yaml" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -33,7 +34,7 @@ type inputModel struct { UserId string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("reset-password %s", userIdArg), Short: "Resets the password of a MongoDB Flex user", @@ -49,32 +50,32 @@ func NewCmd(p *print.Printer) *cobra.Command { Args: args.SingleArg(userIdArg, utils.ValidateUUID), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } instanceLabel, err := mongodbflexUtils.GetInstanceName(ctx, apiClient, model.ProjectId, model.InstanceId) if err != nil { - p.Debug(print.ErrorLevel, "get instance name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get instance name: %v", err) instanceLabel = model.InstanceId } userLabel, err := mongodbflexUtils.GetUserName(ctx, apiClient, model.ProjectId, model.InstanceId, model.UserId) if err != nil { - p.Debug(print.ErrorLevel, "get user name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get user name: %v", err) userLabel = model.UserId } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to reset the password of user %q of instance %q? (This cannot be undone)", userLabel, instanceLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -87,7 +88,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("reset MongoDB Flex user password: %w", err) } - return outputResult(p, model.OutputFormat, userLabel, instanceLabel, user) + return outputResult(params.Printer, model.OutputFormat, userLabel, instanceLabel, user) }, } diff --git a/internal/cmd/mongodbflex/user/reset-password/reset_password_test.go b/internal/cmd/mongodbflex/user/reset-password/reset_password_test.go index 98abb2136..d86e57206 100644 --- a/internal/cmd/mongodbflex/user/reset-password/reset_password_test.go +++ b/internal/cmd/mongodbflex/user/reset-password/reset_password_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" @@ -165,7 +166,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -270,7 +271,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.userLabel, tt.args.instanceLabel, tt.args.user); (err != nil) != tt.wantErr { diff --git a/internal/cmd/mongodbflex/user/update/update.go b/internal/cmd/mongodbflex/user/update/update.go index ee2a9ea1d..3ebdf4e6d 100644 --- a/internal/cmd/mongodbflex/user/update/update.go +++ b/internal/cmd/mongodbflex/user/update/update.go @@ -4,6 +4,7 @@ import ( "context" "fmt" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -35,7 +36,7 @@ type inputModel struct { Roles *[]string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("update %s", userIdArg), Short: "Updates a MongoDB Flex user", @@ -48,32 +49,32 @@ func NewCmd(p *print.Printer) *cobra.Command { Args: args.SingleArg(userIdArg, utils.ValidateUUID), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } instanceLabel, err := mongodbflexUtils.GetInstanceName(ctx, apiClient, model.ProjectId, model.InstanceId) if err != nil { - p.Debug(print.ErrorLevel, "get instance name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get instance name: %v", err) instanceLabel = model.InstanceId } userLabel, err := mongodbflexUtils.GetUserName(ctx, apiClient, model.ProjectId, model.InstanceId, model.UserId) if err != nil { - p.Debug(print.ErrorLevel, "get user name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get user name: %v", err) userLabel = model.UserId } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to update user %q of instance %q?", userLabel, instanceLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -86,7 +87,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("update MongoDB Flex user: %w", err) } - p.Info("Updated user %q of instance %q\n", userLabel, instanceLabel) + params.Printer.Info("Updated user %q of instance %q\n", userLabel, instanceLabel) return nil }, } diff --git a/internal/cmd/mongodbflex/user/update/update_test.go b/internal/cmd/mongodbflex/user/update/update_test.go index fc2872c2d..e4f74eb0f 100644 --- a/internal/cmd/mongodbflex/user/update/update_test.go +++ b/internal/cmd/mongodbflex/user/update/update_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -197,7 +198,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/mongodbflex/user/user.go b/internal/cmd/mongodbflex/user/user.go index 614b7c2f9..0e0600ed6 100644 --- a/internal/cmd/mongodbflex/user/user.go +++ b/internal/cmd/mongodbflex/user/user.go @@ -7,14 +7,14 @@ import ( "github.com/stackitcloud/stackit-cli/internal/cmd/mongodbflex/user/list" resetpassword "github.com/stackitcloud/stackit-cli/internal/cmd/mongodbflex/user/reset-password" "github.com/stackitcloud/stackit-cli/internal/cmd/mongodbflex/user/update" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" - "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" "github.com/spf13/cobra" ) -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "user", Short: "Provides functionality for MongoDB Flex users", @@ -22,15 +22,15 @@ func NewCmd(p *print.Printer) *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd, p) + addSubcommands(cmd, params) return cmd } -func addSubcommands(cmd *cobra.Command, p *print.Printer) { - cmd.AddCommand(create.NewCmd(p)) - cmd.AddCommand(delete.NewCmd(p)) - cmd.AddCommand(describe.NewCmd(p)) - cmd.AddCommand(list.NewCmd(p)) - cmd.AddCommand(resetpassword.NewCmd(p)) - cmd.AddCommand(update.NewCmd(p)) +func addSubcommands(cmd *cobra.Command, params *params.CmdParams) { + cmd.AddCommand(create.NewCmd(params)) + cmd.AddCommand(delete.NewCmd(params)) + cmd.AddCommand(describe.NewCmd(params)) + cmd.AddCommand(list.NewCmd(params)) + cmd.AddCommand(resetpassword.NewCmd(params)) + cmd.AddCommand(update.NewCmd(params)) } diff --git a/internal/cmd/network-area/create/create.go b/internal/cmd/network-area/create/create.go index 4ce917af7..80c14dc37 100644 --- a/internal/cmd/network-area/create/create.go +++ b/internal/cmd/network-area/create/create.go @@ -6,6 +6,7 @@ import ( "fmt" "github.com/goccy/go-yaml" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" "github.com/stackitcloud/stackit-cli/internal/pkg/flags" @@ -45,7 +46,7 @@ type inputModel struct { Labels *map[string]string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "create", Short: "Creates a STACKIT Network Area (SNA)", @@ -71,34 +72,34 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } var orgLabel string - rmApiClient, err := rmClient.ConfigureClient(p) + rmApiClient, err := rmClient.ConfigureClient(params.Printer) if err == nil { orgLabel, err = rmUtils.GetOrganizationName(ctx, rmApiClient, *model.OrganizationId) if err != nil { - p.Debug(print.ErrorLevel, "get organization name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get organization name: %v", err) orgLabel = *model.OrganizationId } else if orgLabel == "" { orgLabel = *model.OrganizationId } } else { - p.Debug(print.ErrorLevel, "configure resource manager client: %v", err) + params.Printer.Debug(print.ErrorLevel, "configure resource manager client: %v", err) } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to create a network area for organization %q?", orgLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -111,7 +112,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("create network area: %w", err) } - return outputResult(p, model.OutputFormat, orgLabel, resp) + return outputResult(params.Printer, model.OutputFormat, orgLabel, resp) }, } configureFlags(cmd) diff --git a/internal/cmd/network-area/create/create_test.go b/internal/cmd/network-area/create/create_test.go index ee055ad5c..7b9d235cd 100644 --- a/internal/cmd/network-area/create/create_test.go +++ b/internal/cmd/network-area/create/create_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -193,7 +194,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -289,7 +290,7 @@ func Test_outputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.orgLabel, tt.args.networkArea); (err != nil) != tt.wantErr { diff --git a/internal/cmd/network-area/delete/delete.go b/internal/cmd/network-area/delete/delete.go index d617d8496..49f618338 100644 --- a/internal/cmd/network-area/delete/delete.go +++ b/internal/cmd/network-area/delete/delete.go @@ -4,6 +4,7 @@ import ( "context" "fmt" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" "github.com/stackitcloud/stackit-cli/internal/pkg/flags" @@ -30,7 +31,7 @@ type inputModel struct { AreaId string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("delete %s", areaIdArg), Short: "Deletes a STACKIT Network Area (SNA)", @@ -47,20 +48,20 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } networkAreaLabel, err := iaasUtils.GetNetworkAreaName(ctx, apiClient, *model.OrganizationId, model.AreaId) if err != nil { - p.Debug(print.ErrorLevel, "get network area name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get network area name: %v", err) networkAreaLabel = model.AreaId } else if networkAreaLabel == "" { networkAreaLabel = model.AreaId @@ -68,7 +69,7 @@ func NewCmd(p *print.Printer) *cobra.Command { if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to delete network area %q?", networkAreaLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -83,7 +84,7 @@ func NewCmd(p *print.Printer) *cobra.Command { // Wait for async operation, if async mode not enabled if !model.Async { - s := spinner.New(p) + s := spinner.New(params.Printer) s.Start("Deleting network area") _, err = wait.DeleteNetworkAreaWaitHandler(ctx, apiClient, *model.OrganizationId, model.AreaId).WaitWithContext(ctx) if err != nil { @@ -96,7 +97,7 @@ func NewCmd(p *print.Printer) *cobra.Command { if model.Async { operationState = "Triggered deletion of" } - p.Info("%s STACKIT Network Area %q\n", operationState, networkAreaLabel) + params.Printer.Info("%s STACKIT Network Area %q\n", operationState, networkAreaLabel) return nil }, } diff --git a/internal/cmd/network-area/delete/delete_test.go b/internal/cmd/network-area/delete/delete_test.go index a3ff4f430..4b8077590 100644 --- a/internal/cmd/network-area/delete/delete_test.go +++ b/internal/cmd/network-area/delete/delete_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" @@ -136,7 +137,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/network-area/describe/describe.go b/internal/cmd/network-area/describe/describe.go index 35be06f3c..6658b1feb 100644 --- a/internal/cmd/network-area/describe/describe.go +++ b/internal/cmd/network-area/describe/describe.go @@ -7,6 +7,7 @@ import ( "strings" "github.com/goccy/go-yaml" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" "github.com/stackitcloud/stackit-cli/internal/pkg/flags" @@ -34,7 +35,7 @@ type inputModel struct { ShowAttachedProjects bool } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("describe %s", areaIdArg), Short: "Shows details of a STACKIT Network Area", @@ -56,13 +57,13 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -83,7 +84,7 @@ func NewCmd(p *print.Printer) *cobra.Command { } } - return outputResult(p, model.OutputFormat, resp, projects) + return outputResult(params.Printer, model.OutputFormat, resp, projects) }, } configureFlags(cmd) diff --git a/internal/cmd/network-area/describe/describe_test.go b/internal/cmd/network-area/describe/describe_test.go index 24c18cb06..7160f6a01 100644 --- a/internal/cmd/network-area/describe/describe_test.go +++ b/internal/cmd/network-area/describe/describe_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" @@ -149,7 +150,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -253,7 +254,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.networkArea, tt.args.attachedProjects); (err != nil) != tt.wantErr { diff --git a/internal/cmd/network-area/list/list.go b/internal/cmd/network-area/list/list.go index 4e112ae53..82147c17b 100644 --- a/internal/cmd/network-area/list/list.go +++ b/internal/cmd/network-area/list/list.go @@ -7,6 +7,7 @@ import ( "github.com/goccy/go-yaml" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -34,7 +35,7 @@ type inputModel struct { LabelSelector *string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "list", Short: "Lists all STACKIT Network Areas (SNA) of an organization", @@ -60,13 +61,13 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -80,19 +81,19 @@ func NewCmd(p *print.Printer) *cobra.Command { if resp.Items == nil || len(*resp.Items) == 0 { var orgLabel string - rmApiClient, err := rmClient.ConfigureClient(p) + rmApiClient, err := rmClient.ConfigureClient(params.Printer) if err == nil { orgLabel, err = rmUtils.GetOrganizationName(ctx, rmApiClient, *model.OrganizationId) if err != nil { - p.Debug(print.ErrorLevel, "get organization name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get organization name: %v", err) orgLabel = *model.OrganizationId } else if orgLabel == "" { orgLabel = *model.OrganizationId } } else { - p.Debug(print.ErrorLevel, "configure resource manager client: %v", err) + params.Printer.Debug(print.ErrorLevel, "configure resource manager client: %v", err) } - p.Info("No STACKIT Network Areas found for organization %q\n", orgLabel) + params.Printer.Info("No STACKIT Network Areas found for organization %q\n", orgLabel) return nil } @@ -102,7 +103,7 @@ func NewCmd(p *print.Printer) *cobra.Command { items = items[:*model.Limit] } - return outputResult(p, model.OutputFormat, items) + return outputResult(params.Printer, model.OutputFormat, items) }, } configureFlags(cmd) diff --git a/internal/cmd/network-area/list/list_test.go b/internal/cmd/network-area/list/list_test.go index 4b71b3dda..d41ae9ad8 100644 --- a/internal/cmd/network-area/list/list_test.go +++ b/internal/cmd/network-area/list/list_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -130,7 +131,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -232,7 +233,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.networkAreas); (err != nil) != tt.wantErr { diff --git a/internal/cmd/network-area/network-range/create/create.go b/internal/cmd/network-area/network-range/create/create.go index 98fa25a60..7d81e2358 100644 --- a/internal/cmd/network-area/network-range/create/create.go +++ b/internal/cmd/network-area/network-range/create/create.go @@ -6,6 +6,7 @@ import ( "fmt" "github.com/goccy/go-yaml" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" "github.com/stackitcloud/stackit-cli/internal/pkg/flags" @@ -32,7 +33,7 @@ type inputModel struct { NetworkRange *string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "create", Short: "Creates a network range in a STACKIT Network Area (SNA)", @@ -46,13 +47,13 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -60,13 +61,13 @@ func NewCmd(p *print.Printer) *cobra.Command { // Get network area label networkAreaLabel, err := iaasUtils.GetNetworkAreaName(ctx, apiClient, *model.OrganizationId, *model.NetworkAreaId) if err != nil { - p.Debug(print.ErrorLevel, "get network area name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get network area name: %v", err) networkAreaLabel = *model.NetworkAreaId } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to create a network range for STACKIT Network Area (SNA) %q?", networkAreaLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -88,7 +89,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return err } - return outputResult(p, model.OutputFormat, networkAreaLabel, networkRange) + return outputResult(params.Printer, model.OutputFormat, networkAreaLabel, networkRange) }, } configureFlags(cmd) diff --git a/internal/cmd/network-area/network-range/create/create_test.go b/internal/cmd/network-area/network-range/create/create_test.go index f3eb51bf1..478854cc2 100644 --- a/internal/cmd/network-area/network-range/create/create_test.go +++ b/internal/cmd/network-area/network-range/create/create_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -145,7 +146,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -241,7 +242,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.networkAreaLabel, tt.args.networkRange); (err != nil) != tt.wantErr { diff --git a/internal/cmd/network-area/network-range/delete/delete.go b/internal/cmd/network-area/network-range/delete/delete.go index 4b4ab2eda..4b7397796 100644 --- a/internal/cmd/network-area/network-range/delete/delete.go +++ b/internal/cmd/network-area/network-range/delete/delete.go @@ -4,6 +4,7 @@ import ( "context" "fmt" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" "github.com/stackitcloud/stackit-cli/internal/pkg/flags" @@ -31,7 +32,7 @@ type inputModel struct { NetworkRangeId string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("delete %s", networkRangeIdArg), Short: "Deletes a network range in a STACKIT Network Area (SNA)", @@ -45,27 +46,27 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } networkAreaLabel, err := iaasUtils.GetNetworkAreaName(ctx, apiClient, *model.OrganizationId, *model.NetworkAreaId) if err != nil { - p.Debug(print.ErrorLevel, "get network area name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get network area name: %v", err) networkAreaLabel = *model.NetworkAreaId } else if networkAreaLabel == "" { networkAreaLabel = *model.NetworkAreaId } networkRangeLabel, err := iaasUtils.GetNetworkRangePrefix(ctx, apiClient, *model.OrganizationId, *model.NetworkAreaId, model.NetworkRangeId) if err != nil { - p.Debug(print.ErrorLevel, "get network range prefix: %v", err) + params.Printer.Debug(print.ErrorLevel, "get network range prefix: %v", err) networkRangeLabel = model.NetworkRangeId } else if networkRangeLabel == "" { networkRangeLabel = model.NetworkRangeId @@ -73,7 +74,7 @@ func NewCmd(p *print.Printer) *cobra.Command { if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to delete network range %q on STACKIT Network Area (SNA) %q?", networkRangeLabel, networkAreaLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -86,7 +87,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("delete network range: %w", err) } - p.Info("Deleted network range %q on SNA %q\n", networkRangeLabel, networkAreaLabel) + params.Printer.Info("Deleted network range %q on SNA %q\n", networkRangeLabel, networkAreaLabel) return nil }, diff --git a/internal/cmd/network-area/network-range/delete/delete_test.go b/internal/cmd/network-area/network-range/delete/delete_test.go index 5955ea316..5fb112ef6 100644 --- a/internal/cmd/network-area/network-range/delete/delete_test.go +++ b/internal/cmd/network-area/network-range/delete/delete_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -156,7 +157,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/network-area/network-range/describe/describe.go b/internal/cmd/network-area/network-range/describe/describe.go index 9f35dc145..45bcfa4e4 100644 --- a/internal/cmd/network-area/network-range/describe/describe.go +++ b/internal/cmd/network-area/network-range/describe/describe.go @@ -6,6 +6,7 @@ import ( "fmt" "github.com/goccy/go-yaml" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" "github.com/stackitcloud/stackit-cli/internal/pkg/flags" @@ -33,7 +34,7 @@ type inputModel struct { NetworkRangeId string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("describe %s", networkRangeIdArg), Short: "Shows details of a network range in a STACKIT Network Area (SNA)", @@ -47,13 +48,13 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -65,7 +66,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("describe network range: %w", err) } - return outputResult(p, model.OutputFormat, resp) + return outputResult(params.Printer, model.OutputFormat, resp) }, } configureFlags(cmd) diff --git a/internal/cmd/network-area/network-range/describe/describe_test.go b/internal/cmd/network-area/network-range/describe/describe_test.go index c03229238..a314d11ee 100644 --- a/internal/cmd/network-area/network-range/describe/describe_test.go +++ b/internal/cmd/network-area/network-range/describe/describe_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -156,7 +157,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -259,7 +260,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.networkRange); (err != nil) != tt.wantErr { diff --git a/internal/cmd/network-area/network-range/list/list.go b/internal/cmd/network-area/network-range/list/list.go index b9b11d0e2..aed963ba0 100644 --- a/internal/cmd/network-area/network-range/list/list.go +++ b/internal/cmd/network-area/network-range/list/list.go @@ -6,6 +6,7 @@ import ( "fmt" "github.com/goccy/go-yaml" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -34,7 +35,7 @@ type inputModel struct { NetworkAreaId *string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "list", Short: "Lists all network ranges in a STACKIT Network Area (SNA)", @@ -56,13 +57,13 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -78,12 +79,12 @@ func NewCmd(p *print.Printer) *cobra.Command { var networkAreaLabel string networkAreaLabel, err = iaasUtils.GetNetworkAreaName(ctx, apiClient, *model.OrganizationId, *model.NetworkAreaId) if err != nil { - p.Debug(print.ErrorLevel, "get organization name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get organization name: %v", err) networkAreaLabel = *model.NetworkAreaId } else if networkAreaLabel == "" { networkAreaLabel = *model.NetworkAreaId } - p.Info("No network ranges found for SNA %q\n", networkAreaLabel) + params.Printer.Info("No network ranges found for SNA %q\n", networkAreaLabel) return nil } @@ -93,7 +94,7 @@ func NewCmd(p *print.Printer) *cobra.Command { items = items[:*model.Limit] } - return outputResult(p, model.OutputFormat, items) + return outputResult(params.Printer, model.OutputFormat, items) }, } configureFlags(cmd) diff --git a/internal/cmd/network-area/network-range/list/list_test.go b/internal/cmd/network-area/network-range/list/list_test.go index 26efc608c..4e7b52627 100644 --- a/internal/cmd/network-area/network-range/list/list_test.go +++ b/internal/cmd/network-area/network-range/list/list_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -140,7 +141,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -242,7 +243,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.networkRanges); (err != nil) != tt.wantErr { diff --git a/internal/cmd/network-area/network-range/network_range.go b/internal/cmd/network-area/network-range/network_range.go index 71c849f17..1c52227a8 100644 --- a/internal/cmd/network-area/network-range/network_range.go +++ b/internal/cmd/network-area/network-range/network_range.go @@ -5,14 +5,14 @@ import ( "github.com/stackitcloud/stackit-cli/internal/cmd/network-area/network-range/delete" "github.com/stackitcloud/stackit-cli/internal/cmd/network-area/network-range/describe" "github.com/stackitcloud/stackit-cli/internal/cmd/network-area/network-range/list" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" - "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" "github.com/spf13/cobra" ) -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "network-range", Aliases: []string{"range"}, @@ -21,13 +21,13 @@ func NewCmd(p *print.Printer) *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd, p) + addSubcommands(cmd, params) return cmd } -func addSubcommands(cmd *cobra.Command, p *print.Printer) { - cmd.AddCommand(create.NewCmd(p)) - cmd.AddCommand(delete.NewCmd(p)) - cmd.AddCommand(describe.NewCmd(p)) - cmd.AddCommand(list.NewCmd(p)) +func addSubcommands(cmd *cobra.Command, params *params.CmdParams) { + cmd.AddCommand(create.NewCmd(params)) + cmd.AddCommand(delete.NewCmd(params)) + cmd.AddCommand(describe.NewCmd(params)) + cmd.AddCommand(list.NewCmd(params)) } diff --git a/internal/cmd/network-area/network_area.go b/internal/cmd/network-area/network_area.go index ef12c22a8..0b67ea3ea 100644 --- a/internal/cmd/network-area/network_area.go +++ b/internal/cmd/network-area/network_area.go @@ -8,14 +8,14 @@ import ( networkrange "github.com/stackitcloud/stackit-cli/internal/cmd/network-area/network-range" "github.com/stackitcloud/stackit-cli/internal/cmd/network-area/route" "github.com/stackitcloud/stackit-cli/internal/cmd/network-area/update" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" - "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" "github.com/spf13/cobra" ) -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "network-area", Short: "Provides functionality for STACKIT Network Area (SNA)", @@ -23,16 +23,16 @@ func NewCmd(p *print.Printer) *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd, p) + addSubcommands(cmd, params) return cmd } -func addSubcommands(cmd *cobra.Command, p *print.Printer) { - cmd.AddCommand(create.NewCmd(p)) - cmd.AddCommand(delete.NewCmd(p)) - cmd.AddCommand(describe.NewCmd(p)) - cmd.AddCommand(list.NewCmd(p)) - cmd.AddCommand(networkrange.NewCmd(p)) - cmd.AddCommand(route.NewCmd(p)) - cmd.AddCommand(update.NewCmd(p)) +func addSubcommands(cmd *cobra.Command, params *params.CmdParams) { + cmd.AddCommand(create.NewCmd(params)) + cmd.AddCommand(delete.NewCmd(params)) + cmd.AddCommand(describe.NewCmd(params)) + cmd.AddCommand(list.NewCmd(params)) + cmd.AddCommand(networkrange.NewCmd(params)) + cmd.AddCommand(route.NewCmd(params)) + cmd.AddCommand(update.NewCmd(params)) } diff --git a/internal/cmd/network-area/route/create/create.go b/internal/cmd/network-area/route/create/create.go index 05a881052..56ac716b4 100644 --- a/internal/cmd/network-area/route/create/create.go +++ b/internal/cmd/network-area/route/create/create.go @@ -6,6 +6,7 @@ import ( "fmt" "github.com/goccy/go-yaml" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" "github.com/stackitcloud/stackit-cli/internal/pkg/flags" @@ -36,7 +37,7 @@ type inputModel struct { Labels *map[string]string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "create", Short: "Creates a static route in a STACKIT Network Area (SNA)", @@ -57,13 +58,13 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -71,7 +72,7 @@ func NewCmd(p *print.Printer) *cobra.Command { // Get network area label networkAreaLabel, err := iaasUtils.GetNetworkAreaName(ctx, apiClient, *model.OrganizationId, *model.NetworkAreaId) if err != nil { - p.Debug(print.ErrorLevel, "get network area name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get network area name: %v", err) networkAreaLabel = *model.NetworkAreaId } else if networkAreaLabel == "" { networkAreaLabel = *model.NetworkAreaId @@ -79,7 +80,7 @@ func NewCmd(p *print.Printer) *cobra.Command { if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to create a static route for STACKIT Network Area (SNA) %q?", networkAreaLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -101,7 +102,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return err } - return outputResult(p, model.OutputFormat, networkAreaLabel, route) + return outputResult(params.Printer, model.OutputFormat, networkAreaLabel, route) }, } configureFlags(cmd) diff --git a/internal/cmd/network-area/route/create/create_test.go b/internal/cmd/network-area/route/create/create_test.go index 4ffca7666..182555c0c 100644 --- a/internal/cmd/network-area/route/create/create_test.go +++ b/internal/cmd/network-area/route/create/create_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -180,7 +181,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -287,7 +288,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.networkAreaLabel, tt.args.route); (err != nil) != tt.wantErr { diff --git a/internal/cmd/network-area/route/delete/delete.go b/internal/cmd/network-area/route/delete/delete.go index 144647440..f455786d2 100644 --- a/internal/cmd/network-area/route/delete/delete.go +++ b/internal/cmd/network-area/route/delete/delete.go @@ -4,6 +4,7 @@ import ( "context" "fmt" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" "github.com/stackitcloud/stackit-cli/internal/pkg/flags" @@ -31,7 +32,7 @@ type inputModel struct { RouteId string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("delete %s", routeIdArg), Short: "Deletes a static route in a STACKIT Network Area (SNA)", @@ -45,20 +46,20 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } networkAreaLabel, err := iaasUtils.GetNetworkAreaName(ctx, apiClient, *model.OrganizationId, *model.NetworkAreaId) if err != nil { - p.Debug(print.ErrorLevel, "get network area name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get network area name: %v", err) networkAreaLabel = *model.NetworkAreaId } else if networkAreaLabel == "" { networkAreaLabel = *model.NetworkAreaId @@ -66,7 +67,7 @@ func NewCmd(p *print.Printer) *cobra.Command { if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to delete static route %q on STACKIT Network Area (SNA) %q?", model.RouteId, networkAreaLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -79,7 +80,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("delete static route: %w", err) } - p.Info("Deleted static route %q on SNA %q\n", model.RouteId, networkAreaLabel) + params.Printer.Info("Deleted static route %q on SNA %q\n", model.RouteId, networkAreaLabel) return nil }, } diff --git a/internal/cmd/network-area/route/delete/delete_test.go b/internal/cmd/network-area/route/delete/delete_test.go index 8a63e08c7..0358eba8c 100644 --- a/internal/cmd/network-area/route/delete/delete_test.go +++ b/internal/cmd/network-area/route/delete/delete_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -156,7 +157,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/network-area/route/describe/describe.go b/internal/cmd/network-area/route/describe/describe.go index 8708f12e1..8e48f1ee6 100644 --- a/internal/cmd/network-area/route/describe/describe.go +++ b/internal/cmd/network-area/route/describe/describe.go @@ -7,6 +7,7 @@ import ( "strings" "github.com/goccy/go-yaml" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" "github.com/stackitcloud/stackit-cli/internal/pkg/flags" @@ -34,7 +35,7 @@ type inputModel struct { RouteId string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("describe %s", routeIdArg), Short: "Shows details of a static route in a STACKIT Network Area (SNA)", @@ -52,13 +53,13 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -70,7 +71,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("describe static route: %w", err) } - return outputResult(p, model.OutputFormat, *resp) + return outputResult(params.Printer, model.OutputFormat, *resp) }, } configureFlags(cmd) diff --git a/internal/cmd/network-area/route/describe/describe_test.go b/internal/cmd/network-area/route/describe/describe_test.go index d052d69f9..0c674de81 100644 --- a/internal/cmd/network-area/route/describe/describe_test.go +++ b/internal/cmd/network-area/route/describe/describe_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -156,7 +157,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -259,7 +260,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.route); (err != nil) != tt.wantErr { diff --git a/internal/cmd/network-area/route/list/list.go b/internal/cmd/network-area/route/list/list.go index f8bada766..f8159617b 100644 --- a/internal/cmd/network-area/route/list/list.go +++ b/internal/cmd/network-area/route/list/list.go @@ -7,6 +7,7 @@ import ( "github.com/goccy/go-yaml" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -33,7 +34,7 @@ type inputModel struct { NetworkAreaId *string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "list", Short: "Lists all static routes in a STACKIT Network Area (SNA)", @@ -55,13 +56,13 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -77,12 +78,12 @@ func NewCmd(p *print.Printer) *cobra.Command { var networkAreaLabel string networkAreaLabel, err = iaasUtils.GetNetworkAreaName(ctx, apiClient, *model.OrganizationId, *model.NetworkAreaId) if err != nil { - p.Debug(print.ErrorLevel, "get network area name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get network area name: %v", err) networkAreaLabel = *model.NetworkAreaId } else if networkAreaLabel == "" { networkAreaLabel = *model.NetworkAreaId } - p.Info("No static routes found for STACKIT Network Area %q\n", networkAreaLabel) + params.Printer.Info("No static routes found for STACKIT Network Area %q\n", networkAreaLabel) return nil } @@ -92,7 +93,7 @@ func NewCmd(p *print.Printer) *cobra.Command { items = items[:*model.Limit] } - return outputResult(p, model.OutputFormat, items) + return outputResult(params.Printer, model.OutputFormat, items) }, } configureFlags(cmd) diff --git a/internal/cmd/network-area/route/list/list_test.go b/internal/cmd/network-area/route/list/list_test.go index 573b332a8..2d66bad9f 100644 --- a/internal/cmd/network-area/route/list/list_test.go +++ b/internal/cmd/network-area/route/list/list_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -140,7 +141,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -242,7 +243,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.routes); (err != nil) != tt.wantErr { diff --git a/internal/cmd/network-area/route/routes.go b/internal/cmd/network-area/route/routes.go index 20fa115dd..1769f349e 100644 --- a/internal/cmd/network-area/route/routes.go +++ b/internal/cmd/network-area/route/routes.go @@ -6,14 +6,14 @@ import ( "github.com/stackitcloud/stackit-cli/internal/cmd/network-area/route/describe" "github.com/stackitcloud/stackit-cli/internal/cmd/network-area/route/list" "github.com/stackitcloud/stackit-cli/internal/cmd/network-area/route/update" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" - "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" "github.com/spf13/cobra" ) -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "route", Short: "Provides functionality for static routes in STACKIT Network Areas", @@ -21,14 +21,14 @@ func NewCmd(p *print.Printer) *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd, p) + addSubcommands(cmd, params) return cmd } -func addSubcommands(cmd *cobra.Command, p *print.Printer) { - cmd.AddCommand(create.NewCmd(p)) - cmd.AddCommand(delete.NewCmd(p)) - cmd.AddCommand(describe.NewCmd(p)) - cmd.AddCommand(list.NewCmd(p)) - cmd.AddCommand(update.NewCmd(p)) +func addSubcommands(cmd *cobra.Command, params *params.CmdParams) { + cmd.AddCommand(create.NewCmd(params)) + cmd.AddCommand(delete.NewCmd(params)) + cmd.AddCommand(describe.NewCmd(params)) + cmd.AddCommand(list.NewCmd(params)) + cmd.AddCommand(update.NewCmd(params)) } diff --git a/internal/cmd/network-area/route/update/update.go b/internal/cmd/network-area/route/update/update.go index b97c9b44d..ab4c8c860 100644 --- a/internal/cmd/network-area/route/update/update.go +++ b/internal/cmd/network-area/route/update/update.go @@ -7,6 +7,7 @@ import ( "github.com/goccy/go-yaml" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -36,7 +37,7 @@ type inputModel struct { Labels *map[string]string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("update %s", routeIdArg), Short: "Updates a static route in a STACKIT Network Area (SNA)", @@ -53,13 +54,13 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -67,7 +68,7 @@ func NewCmd(p *print.Printer) *cobra.Command { // Get network area label networkAreaLabel, err := iaasUtils.GetNetworkAreaName(ctx, apiClient, *model.OrganizationId, *model.NetworkAreaId) if err != nil { - p.Debug(print.ErrorLevel, "get network area name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get network area name: %v", err) networkAreaLabel = *model.NetworkAreaId } else if networkAreaLabel == "" { networkAreaLabel = *model.NetworkAreaId @@ -80,7 +81,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("create static route: %w", err) } - return outputResult(p, model.OutputFormat, networkAreaLabel, *resp) + return outputResult(params.Printer, model.OutputFormat, networkAreaLabel, *resp) }, } configureFlags(cmd) diff --git a/internal/cmd/network-area/route/update/update_test.go b/internal/cmd/network-area/route/update/update_test.go index 813deae4d..03bdf6da2 100644 --- a/internal/cmd/network-area/route/update/update_test.go +++ b/internal/cmd/network-area/route/update/update_test.go @@ -7,6 +7,7 @@ import ( "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" "github.com/google/uuid" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -189,7 +190,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -293,7 +294,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.networkAreaLabel, tt.args.route); (err != nil) != tt.wantErr { diff --git a/internal/cmd/network-area/update/update.go b/internal/cmd/network-area/update/update.go index 3a0c4f6f7..7e9615aff 100644 --- a/internal/cmd/network-area/update/update.go +++ b/internal/cmd/network-area/update/update.go @@ -6,6 +6,7 @@ import ( "fmt" "github.com/goccy/go-yaml" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" "github.com/stackitcloud/stackit-cli/internal/pkg/flags" @@ -45,7 +46,7 @@ type inputModel struct { Labels *map[string]string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("update %s", areaIdArg), Short: "Updates a STACKIT Network Area (SNA)", @@ -59,34 +60,34 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } var orgLabel string - rmApiClient, err := rmClient.ConfigureClient(p) + rmApiClient, err := rmClient.ConfigureClient(params.Printer) if err == nil { orgLabel, err = rmUtils.GetOrganizationName(ctx, rmApiClient, *model.OrganizationId) if err != nil { - p.Debug(print.ErrorLevel, "get organization name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get organization name: %v", err) orgLabel = *model.OrganizationId } else if orgLabel == "" { orgLabel = *model.OrganizationId } } else { - p.Debug(print.ErrorLevel, "configure resource manager client: %v", err) + params.Printer.Debug(print.ErrorLevel, "configure resource manager client: %v", err) } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to update a network area for organization %q?", orgLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -99,7 +100,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("update network area: %w", err) } - return outputResult(p, model.OutputFormat, orgLabel, *resp) + return outputResult(params.Printer, model.OutputFormat, orgLabel, *resp) }, } configureFlags(cmd) diff --git a/internal/cmd/network-area/update/update_test.go b/internal/cmd/network-area/update/update_test.go index 807ec124f..f25018286 100644 --- a/internal/cmd/network-area/update/update_test.go +++ b/internal/cmd/network-area/update/update_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -206,7 +207,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -310,7 +311,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.projectLabel, tt.args.networkArea); (err != nil) != tt.wantErr { diff --git a/internal/cmd/network-interface/create/create.go b/internal/cmd/network-interface/create/create.go index a14e57b1e..08a8c7cac 100644 --- a/internal/cmd/network-interface/create/create.go +++ b/internal/cmd/network-interface/create/create.go @@ -8,6 +8,7 @@ import ( "github.com/goccy/go-yaml" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -49,7 +50,7 @@ type inputModel struct { SecurityGroups *[]string // = 36 characters + regex ^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$ } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "create", Short: "Creates a network interface", @@ -67,20 +68,20 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } - projectLabel, err := projectname.GetProjectName(ctx, p, cmd) + projectLabel, err := projectname.GetProjectName(ctx, params.Printer, cmd) if err != nil { - p.Debug(print.ErrorLevel, "get project name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get project name: %v", err) projectLabel = model.ProjectId } else if projectLabel == "" { projectLabel = model.ProjectId @@ -88,7 +89,7 @@ func NewCmd(p *print.Printer) *cobra.Command { if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to create a network interface for project %q?", projectLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -101,7 +102,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("create network interface: %w", err) } - return outputResult(p, model.OutputFormat, model.ProjectId, resp) + return outputResult(params.Printer, model.OutputFormat, model.ProjectId, resp) }, } configureFlags(cmd) diff --git a/internal/cmd/network-interface/create/create_test.go b/internal/cmd/network-interface/create/create_test.go index 843b15f69..f05ed584e 100644 --- a/internal/cmd/network-interface/create/create_test.go +++ b/internal/cmd/network-interface/create/create_test.go @@ -7,6 +7,7 @@ import ( "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" "github.com/google/uuid" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -190,7 +191,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -286,7 +287,7 @@ func Test_outputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.projectId, tt.args.nic); (err != nil) != tt.wantErr { diff --git a/internal/cmd/network-interface/delete/delete.go b/internal/cmd/network-interface/delete/delete.go index 4f2cd3302..925bf29b7 100644 --- a/internal/cmd/network-interface/delete/delete.go +++ b/internal/cmd/network-interface/delete/delete.go @@ -5,6 +5,7 @@ import ( "fmt" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" "github.com/stackitcloud/stackit-cli/internal/pkg/flags" @@ -27,7 +28,7 @@ type inputModel struct { NicId string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("delete %s", nicIdArg), Short: "Deletes a network interface", @@ -41,20 +42,20 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to delete the network interface %q? (This cannot be undone)", model.NicId) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -67,7 +68,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("delete network interface: %w", err) } - p.Info("Deleted network interface %q\n", model.NicId) + params.Printer.Info("Deleted network interface %q\n", model.NicId) return nil }, diff --git a/internal/cmd/network-interface/delete/delete_test.go b/internal/cmd/network-interface/delete/delete_test.go index 4b9fdd56b..ac03d45f6 100644 --- a/internal/cmd/network-interface/delete/delete_test.go +++ b/internal/cmd/network-interface/delete/delete_test.go @@ -7,6 +7,7 @@ import ( "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" "github.com/google/uuid" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -122,7 +123,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/network-interface/describe/describe.go b/internal/cmd/network-interface/describe/describe.go index 89b04b9b3..aba9ed65a 100644 --- a/internal/cmd/network-interface/describe/describe.go +++ b/internal/cmd/network-interface/describe/describe.go @@ -8,6 +8,7 @@ import ( "github.com/goccy/go-yaml" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -32,7 +33,7 @@ type inputModel struct { NicId string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("describe %s", nicIdArg), Short: "Describes a network interface", @@ -54,13 +55,13 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -72,7 +73,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("describe network interface: %w", err) } - return outputResult(p, model.OutputFormat, resp) + return outputResult(params.Printer, model.OutputFormat, resp) }, } configureFlags(cmd) diff --git a/internal/cmd/network-interface/describe/describe_test.go b/internal/cmd/network-interface/describe/describe_test.go index 967057b02..24163ac87 100644 --- a/internal/cmd/network-interface/describe/describe_test.go +++ b/internal/cmd/network-interface/describe/describe_test.go @@ -7,6 +7,7 @@ import ( "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" "github.com/google/uuid" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -122,7 +123,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -225,7 +226,7 @@ func Test_outputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.nic); (err != nil) != tt.wantErr { diff --git a/internal/cmd/network-interface/list/list.go b/internal/cmd/network-interface/list/list.go index 3164f956b..e7282358f 100644 --- a/internal/cmd/network-interface/list/list.go +++ b/internal/cmd/network-interface/list/list.go @@ -7,6 +7,7 @@ import ( "github.com/goccy/go-yaml" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -33,7 +34,7 @@ type inputModel struct { NetworkId *string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "list", Short: "Lists all network interfaces of a network", @@ -59,13 +60,13 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -80,12 +81,12 @@ func NewCmd(p *print.Printer) *cobra.Command { if resp.Items == nil || len(*resp.Items) == 0 { networkLabel, err := iaasUtils.GetNetworkName(ctx, apiClient, model.ProjectId, *model.NetworkId) if err != nil { - p.Debug(print.ErrorLevel, "get network name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get network name: %v", err) networkLabel = *model.NetworkId } else if networkLabel == "" { networkLabel = *model.NetworkId } - p.Info("No network interfaces found for network %q\n", networkLabel) + params.Printer.Info("No network interfaces found for network %q\n", networkLabel) return nil } @@ -95,7 +96,7 @@ func NewCmd(p *print.Printer) *cobra.Command { items = items[:*model.Limit] } - return outputResult(p, model.OutputFormat, items) + return outputResult(params.Printer, model.OutputFormat, items) }, } configureFlags(cmd) diff --git a/internal/cmd/network-interface/list/list_test.go b/internal/cmd/network-interface/list/list_test.go index 97610156a..3e976c5db 100644 --- a/internal/cmd/network-interface/list/list_test.go +++ b/internal/cmd/network-interface/list/list_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -135,7 +136,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -223,7 +224,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.nics); (err != nil) != tt.wantErr { diff --git a/internal/cmd/network-interface/network-interface.go b/internal/cmd/network-interface/network-interface.go index cada28596..f9bbca3fe 100644 --- a/internal/cmd/network-interface/network-interface.go +++ b/internal/cmd/network-interface/network-interface.go @@ -7,12 +7,12 @@ import ( "github.com/stackitcloud/stackit-cli/internal/cmd/network-interface/describe" "github.com/stackitcloud/stackit-cli/internal/cmd/network-interface/list" "github.com/stackitcloud/stackit-cli/internal/cmd/network-interface/update" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" - "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" ) -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "network-interface", Short: "Provides functionality for network interfaces", @@ -20,14 +20,14 @@ func NewCmd(p *print.Printer) *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd, p) + addSubcommands(cmd, params) return cmd } -func addSubcommands(cmd *cobra.Command, p *print.Printer) { - cmd.AddCommand(create.NewCmd(p)) - cmd.AddCommand(delete.NewCmd(p)) - cmd.AddCommand(update.NewCmd(p)) - cmd.AddCommand(describe.NewCmd(p)) - cmd.AddCommand(list.NewCmd(p)) +func addSubcommands(cmd *cobra.Command, params *params.CmdParams) { + cmd.AddCommand(create.NewCmd(params)) + cmd.AddCommand(delete.NewCmd(params)) + cmd.AddCommand(update.NewCmd(params)) + cmd.AddCommand(describe.NewCmd(params)) + cmd.AddCommand(list.NewCmd(params)) } diff --git a/internal/cmd/network-interface/update/update.go b/internal/cmd/network-interface/update/update.go index 777f2f4b4..9cf746745 100644 --- a/internal/cmd/network-interface/update/update.go +++ b/internal/cmd/network-interface/update/update.go @@ -8,6 +8,7 @@ import ( "github.com/goccy/go-yaml" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -47,7 +48,7 @@ type inputModel struct { SecurityGroups *[]string // = 36 characters + regex ^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$ } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("update %s", nicIdArg), Short: "Updates a network interface", @@ -69,20 +70,20 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to update the network interface %q?", model.NicId) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -95,7 +96,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("update network interface: %w", err) } - return outputResult(p, model.OutputFormat, model.ProjectId, resp) + return outputResult(params.Printer, model.OutputFormat, model.ProjectId, resp) }, } configureFlags(cmd) diff --git a/internal/cmd/network-interface/update/update_test.go b/internal/cmd/network-interface/update/update_test.go index 98987b829..03faa73ad 100644 --- a/internal/cmd/network-interface/update/update_test.go +++ b/internal/cmd/network-interface/update/update_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -213,7 +214,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -317,7 +318,7 @@ func Test_outputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.projectId, tt.args.nic); (err != nil) != tt.wantErr { diff --git a/internal/cmd/network/create/create.go b/internal/cmd/network/create/create.go index b89f54f49..339025b74 100644 --- a/internal/cmd/network/create/create.go +++ b/internal/cmd/network/create/create.go @@ -6,6 +6,7 @@ import ( "fmt" "github.com/goccy/go-yaml" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" cliErr "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -55,7 +56,7 @@ type inputModel struct { Labels *map[string]string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "create", Short: "Creates a network", @@ -89,20 +90,20 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } - projectLabel, err := projectname.GetProjectName(ctx, p, cmd) + projectLabel, err := projectname.GetProjectName(ctx, params.Printer, cmd) if err != nil { - p.Debug(print.ErrorLevel, "get project name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get project name: %v", err) projectLabel = model.ProjectId } else if projectLabel == "" { projectLabel = model.ProjectId @@ -110,7 +111,7 @@ func NewCmd(p *print.Printer) *cobra.Command { if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to create a network for project %q?", projectLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -126,7 +127,7 @@ func NewCmd(p *print.Printer) *cobra.Command { // Wait for async operation, if async mode not enabled if !model.Async { - s := spinner.New(p) + s := spinner.New(params.Printer) s.Start("Creating network") _, err = wait.CreateNetworkWaitHandler(ctx, apiClient, model.ProjectId, networkId).WaitWithContext(ctx) if err != nil { @@ -135,7 +136,7 @@ func NewCmd(p *print.Printer) *cobra.Command { s.Stop() } - return outputResult(p, model.OutputFormat, model.Async, projectLabel, resp) + return outputResult(params.Printer, model.OutputFormat, model.Async, projectLabel, resp) }, } configureFlags(cmd) diff --git a/internal/cmd/network/create/create_test.go b/internal/cmd/network/create/create_test.go index 19edc1d40..63c6a3635 100644 --- a/internal/cmd/network/create/create_test.go +++ b/internal/cmd/network/create/create_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -258,7 +259,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -467,7 +468,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.async, tt.args.projectLabel, tt.args.network); (err != nil) != tt.wantErr { diff --git a/internal/cmd/network/delete/delete.go b/internal/cmd/network/delete/delete.go index c1608151e..a0adcb6d0 100644 --- a/internal/cmd/network/delete/delete.go +++ b/internal/cmd/network/delete/delete.go @@ -4,6 +4,7 @@ import ( "context" "fmt" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -28,7 +29,7 @@ type inputModel struct { NetworkId string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("delete %s", networkIdArg), Short: "Deletes a network", @@ -45,20 +46,20 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } networkLabel, err := iaasUtils.GetNetworkName(ctx, apiClient, model.ProjectId, model.NetworkId) if err != nil { - p.Debug(print.ErrorLevel, "get network name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get network name: %v", err) networkLabel = model.NetworkId } else if networkLabel == "" { networkLabel = model.NetworkId @@ -66,7 +67,7 @@ func NewCmd(p *print.Printer) *cobra.Command { if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to delete network %q?", networkLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -81,7 +82,7 @@ func NewCmd(p *print.Printer) *cobra.Command { // Wait for async operation, if async mode not enabled if !model.Async { - s := spinner.New(p) + s := spinner.New(params.Printer) s.Start("Deleting network") _, err = wait.DeleteNetworkWaitHandler(ctx, apiClient, model.ProjectId, model.NetworkId).WaitWithContext(ctx) if err != nil { @@ -94,7 +95,7 @@ func NewCmd(p *print.Printer) *cobra.Command { if model.Async { operationState = "Triggered deletion of" } - p.Info("%s network %q\n", operationState, networkLabel) + params.Printer.Info("%s network %q\n", operationState, networkLabel) return nil }, } diff --git a/internal/cmd/network/delete/delete_test.go b/internal/cmd/network/delete/delete_test.go index 726364502..630a0f3fb 100644 --- a/internal/cmd/network/delete/delete_test.go +++ b/internal/cmd/network/delete/delete_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" @@ -138,7 +139,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/network/describe/describe.go b/internal/cmd/network/describe/describe.go index 13eb11133..129c52899 100644 --- a/internal/cmd/network/describe/describe.go +++ b/internal/cmd/network/describe/describe.go @@ -7,6 +7,7 @@ import ( "strings" "github.com/goccy/go-yaml" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -29,7 +30,7 @@ type inputModel struct { NetworkId string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("describe %s", networkIdArg), Short: "Shows details of a network", @@ -47,13 +48,13 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -65,7 +66,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("read network: %w", err) } - return outputResult(p, model.OutputFormat, resp) + return outputResult(params.Printer, model.OutputFormat, resp) }, } return cmd diff --git a/internal/cmd/network/describe/describe_test.go b/internal/cmd/network/describe/describe_test.go index 451b518b3..9c3b62d35 100644 --- a/internal/cmd/network/describe/describe_test.go +++ b/internal/cmd/network/describe/describe_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" @@ -138,7 +139,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -241,7 +242,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.network); (err != nil) != tt.wantErr { diff --git a/internal/cmd/network/list/list.go b/internal/cmd/network/list/list.go index 8b5577685..90e6efa0e 100644 --- a/internal/cmd/network/list/list.go +++ b/internal/cmd/network/list/list.go @@ -6,6 +6,7 @@ import ( "fmt" "github.com/goccy/go-yaml" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -32,7 +33,7 @@ type inputModel struct { LabelSelector *string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "list", Short: "Lists all networks of a project", @@ -58,13 +59,13 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -77,14 +78,14 @@ func NewCmd(p *print.Printer) *cobra.Command { } if resp.Items == nil || len(*resp.Items) == 0 { - projectLabel, err := projectname.GetProjectName(ctx, p, cmd) + projectLabel, err := projectname.GetProjectName(ctx, params.Printer, cmd) if err != nil { - p.Debug(print.ErrorLevel, "get project name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get project name: %v", err) projectLabel = model.ProjectId } else if projectLabel == "" { projectLabel = model.ProjectId } - p.Info("No networks found for project %q\n", projectLabel) + params.Printer.Info("No networks found for project %q\n", projectLabel) return nil } @@ -94,7 +95,7 @@ func NewCmd(p *print.Printer) *cobra.Command { items = items[:*model.Limit] } - return outputResult(p, model.OutputFormat, items) + return outputResult(params.Printer, model.OutputFormat, items) }, } configureFlags(cmd) diff --git a/internal/cmd/network/list/list_test.go b/internal/cmd/network/list/list_test.go index 9bc47dfb5..c0f1ac4c3 100644 --- a/internal/cmd/network/list/list_test.go +++ b/internal/cmd/network/list/list_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -132,7 +133,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -229,7 +230,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.networks); (err != nil) != tt.wantErr { diff --git a/internal/cmd/network/network.go b/internal/cmd/network/network.go index b95a496c7..5fbd7e77b 100644 --- a/internal/cmd/network/network.go +++ b/internal/cmd/network/network.go @@ -6,14 +6,14 @@ import ( "github.com/stackitcloud/stackit-cli/internal/cmd/network/describe" "github.com/stackitcloud/stackit-cli/internal/cmd/network/list" "github.com/stackitcloud/stackit-cli/internal/cmd/network/update" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" - "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" "github.com/spf13/cobra" ) -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "network", Short: "Provides functionality for networks", @@ -21,14 +21,14 @@ func NewCmd(p *print.Printer) *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd, p) + addSubcommands(cmd, params) return cmd } -func addSubcommands(cmd *cobra.Command, p *print.Printer) { - cmd.AddCommand(create.NewCmd(p)) - cmd.AddCommand(delete.NewCmd(p)) - cmd.AddCommand(describe.NewCmd(p)) - cmd.AddCommand(list.NewCmd(p)) - cmd.AddCommand(update.NewCmd(p)) +func addSubcommands(cmd *cobra.Command, params *params.CmdParams) { + cmd.AddCommand(create.NewCmd(params)) + cmd.AddCommand(delete.NewCmd(params)) + cmd.AddCommand(describe.NewCmd(params)) + cmd.AddCommand(list.NewCmd(params)) + cmd.AddCommand(update.NewCmd(params)) } diff --git a/internal/cmd/network/update/update.go b/internal/cmd/network/update/update.go index 50be6c698..1b331a802 100644 --- a/internal/cmd/network/update/update.go +++ b/internal/cmd/network/update/update.go @@ -4,6 +4,7 @@ import ( "context" "fmt" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" cliErr "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -46,7 +47,7 @@ type inputModel struct { Labels *map[string]string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("update %s", networkIdArg), Short: "Updates a network", @@ -72,20 +73,20 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } networkLabel, err := iaasUtils.GetNetworkName(ctx, apiClient, model.ProjectId, model.NetworkId) if err != nil { - p.Debug(print.ErrorLevel, "get network name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get network name: %v", err) networkLabel = model.NetworkId } else if networkLabel == "" { networkLabel = model.NetworkId @@ -93,7 +94,7 @@ func NewCmd(p *print.Printer) *cobra.Command { if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to update network %q?", networkLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -109,7 +110,7 @@ func NewCmd(p *print.Printer) *cobra.Command { // Wait for async operation, if async mode not enabled if !model.Async { - s := spinner.New(p) + s := spinner.New(params.Printer) s.Start("Updating network") _, err = wait.UpdateNetworkWaitHandler(ctx, apiClient, model.ProjectId, networkId).WaitWithContext(ctx) if err != nil { @@ -122,7 +123,7 @@ func NewCmd(p *print.Printer) *cobra.Command { if model.Async { operationState = "Triggered update of" } - p.Info("%s network %q\n", operationState, networkLabel) + params.Printer.Info("%s network %q\n", operationState, networkLabel) return nil }, } diff --git a/internal/cmd/network/update/update_test.go b/internal/cmd/network/update/update_test.go index 7a1b243c5..d05624840 100644 --- a/internal/cmd/network/update/update_test.go +++ b/internal/cmd/network/update/update_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -241,7 +242,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/object-storage/bucket/bucket.go b/internal/cmd/object-storage/bucket/bucket.go index 701fc0934..62d928e54 100644 --- a/internal/cmd/object-storage/bucket/bucket.go +++ b/internal/cmd/object-storage/bucket/bucket.go @@ -5,14 +5,14 @@ import ( "github.com/stackitcloud/stackit-cli/internal/cmd/object-storage/bucket/delete" "github.com/stackitcloud/stackit-cli/internal/cmd/object-storage/bucket/describe" "github.com/stackitcloud/stackit-cli/internal/cmd/object-storage/bucket/list" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" - "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" "github.com/spf13/cobra" ) -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "bucket", Short: "Provides functionality for Object Storage buckets", @@ -20,13 +20,13 @@ func NewCmd(p *print.Printer) *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd, p) + addSubcommands(cmd, params) return cmd } -func addSubcommands(cmd *cobra.Command, p *print.Printer) { - cmd.AddCommand(delete.NewCmd(p)) - cmd.AddCommand(describe.NewCmd(p)) - cmd.AddCommand(create.NewCmd(p)) - cmd.AddCommand(list.NewCmd(p)) +func addSubcommands(cmd *cobra.Command, params *params.CmdParams) { + cmd.AddCommand(delete.NewCmd(params)) + cmd.AddCommand(describe.NewCmd(params)) + cmd.AddCommand(create.NewCmd(params)) + cmd.AddCommand(list.NewCmd(params)) } diff --git a/internal/cmd/object-storage/bucket/create/create.go b/internal/cmd/object-storage/bucket/create/create.go index e96a4206c..f4337d7e3 100644 --- a/internal/cmd/object-storage/bucket/create/create.go +++ b/internal/cmd/object-storage/bucket/create/create.go @@ -6,6 +6,7 @@ import ( "fmt" "github.com/goccy/go-yaml" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -29,7 +30,7 @@ type inputModel struct { BucketName string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("create %s", bucketNameArg), Short: "Creates an Object Storage bucket", @@ -42,20 +43,20 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to create bucket %q? (This cannot be undone)", model.BucketName) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -81,7 +82,7 @@ func NewCmd(p *print.Printer) *cobra.Command { // Wait for async operation, if async mode not enabled if !model.Async { - s := spinner.New(p) + s := spinner.New(params.Printer) s.Start("Creating bucket") _, err = wait.CreateBucketWaitHandler(ctx, apiClient, model.ProjectId, model.Region, model.BucketName).WaitWithContext(ctx) if err != nil { @@ -90,7 +91,7 @@ func NewCmd(p *print.Printer) *cobra.Command { s.Stop() } - return outputResult(p, model.OutputFormat, model.Async, model.BucketName, resp) + return outputResult(params.Printer, model.OutputFormat, model.Async, model.BucketName, resp) }, } return cmd diff --git a/internal/cmd/object-storage/bucket/create/create_test.go b/internal/cmd/object-storage/bucket/create/create_test.go index 49a089342..344c228d5 100644 --- a/internal/cmd/object-storage/bucket/create/create_test.go +++ b/internal/cmd/object-storage/bucket/create/create_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" @@ -136,7 +137,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -241,7 +242,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.async, tt.args.bucketName, tt.args.createBucketResponse); (err != nil) != tt.wantErr { diff --git a/internal/cmd/object-storage/bucket/delete/delete.go b/internal/cmd/object-storage/bucket/delete/delete.go index 1c6edbf90..0b4174f47 100644 --- a/internal/cmd/object-storage/bucket/delete/delete.go +++ b/internal/cmd/object-storage/bucket/delete/delete.go @@ -4,6 +4,7 @@ import ( "context" "fmt" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -26,7 +27,7 @@ type inputModel struct { BucketName string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("delete %s", bucketNameArg), Short: "Deletes an Object Storage bucket", @@ -39,20 +40,20 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to delete bucket %q? (This cannot be undone)", model.BucketName) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -67,7 +68,7 @@ func NewCmd(p *print.Printer) *cobra.Command { // Wait for async operation, if async mode not enabled if !model.Async { - s := spinner.New(p) + s := spinner.New(params.Printer) s.Start("Deleting bucket") _, err = wait.DeleteBucketWaitHandler(ctx, apiClient, model.ProjectId, model.Region, model.BucketName).WaitWithContext(ctx) if err != nil { @@ -80,7 +81,7 @@ func NewCmd(p *print.Printer) *cobra.Command { if model.Async { operationState = "Triggered deletion of" } - p.Info("%s bucket %q\n", operationState, model.BucketName) + params.Printer.Info("%s bucket %q\n", operationState, model.BucketName) return nil }, } diff --git a/internal/cmd/object-storage/bucket/delete/delete_test.go b/internal/cmd/object-storage/bucket/delete/delete_test.go index be5bd0028..6e06d4adc 100644 --- a/internal/cmd/object-storage/bucket/delete/delete_test.go +++ b/internal/cmd/object-storage/bucket/delete/delete_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" @@ -136,7 +137,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/object-storage/bucket/describe/describe.go b/internal/cmd/object-storage/bucket/describe/describe.go index 23a9e7011..c1fd344d6 100644 --- a/internal/cmd/object-storage/bucket/describe/describe.go +++ b/internal/cmd/object-storage/bucket/describe/describe.go @@ -6,6 +6,7 @@ import ( "fmt" "github.com/goccy/go-yaml" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -28,7 +29,7 @@ type inputModel struct { BucketName string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("describe %s", bucketNameArg), Short: "Shows details of an Object Storage bucket", @@ -44,12 +45,12 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -61,7 +62,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("read Object Storage bucket: %w", err) } - return outputResult(p, model.OutputFormat, resp.Bucket) + return outputResult(params.Printer, model.OutputFormat, resp.Bucket) }, } return cmd diff --git a/internal/cmd/object-storage/bucket/describe/describe_test.go b/internal/cmd/object-storage/bucket/describe/describe_test.go index d233fc17f..9e376132d 100644 --- a/internal/cmd/object-storage/bucket/describe/describe_test.go +++ b/internal/cmd/object-storage/bucket/describe/describe_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" @@ -136,7 +137,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -239,7 +240,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.bucket); (err != nil) != tt.wantErr { diff --git a/internal/cmd/object-storage/bucket/list/list.go b/internal/cmd/object-storage/bucket/list/list.go index 19466184b..cbfa41574 100644 --- a/internal/cmd/object-storage/bucket/list/list.go +++ b/internal/cmd/object-storage/bucket/list/list.go @@ -7,6 +7,7 @@ import ( "github.com/goccy/go-yaml" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -29,7 +30,7 @@ type inputModel struct { Limit *int64 } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "list", Short: "Lists all Object Storage buckets", @@ -48,13 +49,13 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -66,12 +67,12 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("get Object Storage buckets: %w", err) } if resp.Buckets == nil || len(*resp.Buckets) == 0 { - projectLabel, err := projectname.GetProjectName(ctx, p, cmd) + projectLabel, err := projectname.GetProjectName(ctx, params.Printer, cmd) if err != nil { - p.Debug(print.ErrorLevel, "get project name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get project name: %v", err) projectLabel = model.ProjectId } - p.Info("No buckets found for project %s\n", projectLabel) + params.Printer.Info("No buckets found for project %s\n", projectLabel) return nil } buckets := *resp.Buckets @@ -81,7 +82,7 @@ func NewCmd(p *print.Printer) *cobra.Command { buckets = buckets[:*model.Limit] } - return outputResult(p, model.OutputFormat, buckets) + return outputResult(params.Printer, model.OutputFormat, buckets) }, } diff --git a/internal/cmd/object-storage/bucket/list/list_test.go b/internal/cmd/object-storage/bucket/list/list_test.go index 31664679f..eb50f0ca7 100644 --- a/internal/cmd/object-storage/bucket/list/list_test.go +++ b/internal/cmd/object-storage/bucket/list/list_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -215,7 +216,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.buckets); (err != nil) != tt.wantErr { diff --git a/internal/cmd/object-storage/credentials-group/create/create.go b/internal/cmd/object-storage/credentials-group/create/create.go index 55530ccc7..02dfba938 100644 --- a/internal/cmd/object-storage/credentials-group/create/create.go +++ b/internal/cmd/object-storage/credentials-group/create/create.go @@ -6,6 +6,7 @@ import ( "fmt" "github.com/goccy/go-yaml" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -28,7 +29,7 @@ type inputModel struct { CredentialsGroupName string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "create", Short: "Creates a credentials group to hold Object Storage access credentials", @@ -41,20 +42,20 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to create a credentials group with name %q?", model.CredentialsGroupName) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -67,7 +68,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("create Object Storage credentials group: %w", err) } - return outputResult(p, model.OutputFormat, resp) + return outputResult(params.Printer, model.OutputFormat, resp) }, } configureFlags(cmd) diff --git a/internal/cmd/object-storage/credentials-group/create/create_test.go b/internal/cmd/object-storage/credentials-group/create/create_test.go index 60dd53fbd..f23487603 100644 --- a/internal/cmd/object-storage/credentials-group/create/create_test.go +++ b/internal/cmd/object-storage/credentials-group/create/create_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -122,7 +123,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -226,7 +227,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.createCredentialsGroupResponse); (err != nil) != tt.wantErr { diff --git a/internal/cmd/object-storage/credentials-group/credentials_group.go b/internal/cmd/object-storage/credentials-group/credentials_group.go index 0803796f4..9b9d05129 100644 --- a/internal/cmd/object-storage/credentials-group/credentials_group.go +++ b/internal/cmd/object-storage/credentials-group/credentials_group.go @@ -4,14 +4,14 @@ import ( "github.com/stackitcloud/stackit-cli/internal/cmd/object-storage/credentials-group/create" "github.com/stackitcloud/stackit-cli/internal/cmd/object-storage/credentials-group/delete" "github.com/stackitcloud/stackit-cli/internal/cmd/object-storage/credentials-group/list" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" - "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" "github.com/spf13/cobra" ) -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "credentials-group", Short: "Provides functionality for Object Storage credentials group", @@ -19,12 +19,12 @@ func NewCmd(p *print.Printer) *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd, p) + addSubcommands(cmd, params) return cmd } -func addSubcommands(cmd *cobra.Command, p *print.Printer) { - cmd.AddCommand(create.NewCmd(p)) - cmd.AddCommand(delete.NewCmd(p)) - cmd.AddCommand(list.NewCmd(p)) +func addSubcommands(cmd *cobra.Command, params *params.CmdParams) { + cmd.AddCommand(create.NewCmd(params)) + cmd.AddCommand(delete.NewCmd(params)) + cmd.AddCommand(list.NewCmd(params)) } diff --git a/internal/cmd/object-storage/credentials-group/delete/delete.go b/internal/cmd/object-storage/credentials-group/delete/delete.go index 27dda8460..11457da96 100644 --- a/internal/cmd/object-storage/credentials-group/delete/delete.go +++ b/internal/cmd/object-storage/credentials-group/delete/delete.go @@ -4,6 +4,7 @@ import ( "context" "fmt" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -26,7 +27,7 @@ type inputModel struct { CredentialsGroupId string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("delete %s", credentialsGroupIdArg), Short: "Deletes a credentials group that holds Object Storage access credentials", @@ -39,26 +40,26 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } credentialsGroupLabel, err := objectStorageUtils.GetCredentialsGroupName(ctx, apiClient, model.ProjectId, model.CredentialsGroupId, model.Region) if err != nil { - p.Debug(print.ErrorLevel, "get credentials group name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get credentials group name: %v", err) credentialsGroupLabel = model.CredentialsGroupId } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to delete credentials group %q? (This cannot be undone)", credentialsGroupLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -71,7 +72,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("delete Object Storage credentials group: %w", err) } - p.Info("Deleted credentials group %q\n", credentialsGroupLabel) + params.Printer.Info("Deleted credentials group %q\n", credentialsGroupLabel) return nil }, } diff --git a/internal/cmd/object-storage/credentials-group/delete/delete_test.go b/internal/cmd/object-storage/credentials-group/delete/delete_test.go index 1711bd33c..a5097a0b7 100644 --- a/internal/cmd/object-storage/credentials-group/delete/delete_test.go +++ b/internal/cmd/object-storage/credentials-group/delete/delete_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" @@ -142,7 +143,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/object-storage/credentials-group/list/list.go b/internal/cmd/object-storage/credentials-group/list/list.go index 876392113..e239167b0 100644 --- a/internal/cmd/object-storage/credentials-group/list/list.go +++ b/internal/cmd/object-storage/credentials-group/list/list.go @@ -7,6 +7,7 @@ import ( "github.com/goccy/go-yaml" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -28,7 +29,7 @@ type inputModel struct { Limit *int64 } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "list", Short: "Lists all credentials groups that hold Object Storage access credentials", @@ -47,13 +48,13 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -66,7 +67,7 @@ func NewCmd(p *print.Printer) *cobra.Command { } credentialsGroups := *resp.CredentialsGroups if len(credentialsGroups) == 0 { - p.Info("No credentials groups found for your project") + params.Printer.Info("No credentials groups found for your project") return nil } @@ -74,7 +75,7 @@ func NewCmd(p *print.Printer) *cobra.Command { if model.Limit != nil && len(credentialsGroups) > int(*model.Limit) { credentialsGroups = credentialsGroups[:*model.Limit] } - return outputResult(p, model.OutputFormat, credentialsGroups) + return outputResult(params.Printer, model.OutputFormat, credentialsGroups) }, } configureFlags(cmd) diff --git a/internal/cmd/object-storage/credentials-group/list/list_test.go b/internal/cmd/object-storage/credentials-group/list/list_test.go index 2a7ee01ff..b0570483b 100644 --- a/internal/cmd/object-storage/credentials-group/list/list_test.go +++ b/internal/cmd/object-storage/credentials-group/list/list_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -117,7 +118,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -219,7 +220,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.credentialsGroups); (err != nil) != tt.wantErr { diff --git a/internal/cmd/object-storage/credentials/create/create.go b/internal/cmd/object-storage/credentials/create/create.go index 71c854dd3..bce332f93 100644 --- a/internal/cmd/object-storage/credentials/create/create.go +++ b/internal/cmd/object-storage/credentials/create/create.go @@ -8,6 +8,7 @@ import ( "github.com/goccy/go-yaml" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -33,7 +34,7 @@ type inputModel struct { HidePassword bool } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "create", Short: "Creates credentials for an Object Storage credentials group", @@ -49,26 +50,26 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } credentialsGroupLabel, err := objectStorageUtils.GetCredentialsGroupName(ctx, apiClient, model.ProjectId, model.CredentialsGroupId, model.Region) if err != nil { - p.Debug(print.ErrorLevel, "get credentials group name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get credentials group name: %v", err) credentialsGroupLabel = model.CredentialsGroupId } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to create credentials in group %q?", credentialsGroupLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -81,7 +82,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("create Object Storage credentials: %w", err) } - return outputResult(p, model.OutputFormat, credentialsGroupLabel, resp) + return outputResult(params.Printer, model.OutputFormat, credentialsGroupLabel, resp) }, } configureFlags(cmd) diff --git a/internal/cmd/object-storage/credentials/create/create_test.go b/internal/cmd/object-storage/credentials/create/create_test.go index 03257beda..c2efbf566 100644 --- a/internal/cmd/object-storage/credentials/create/create_test.go +++ b/internal/cmd/object-storage/credentials/create/create_test.go @@ -5,6 +5,7 @@ import ( "testing" "time" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -181,7 +182,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -277,7 +278,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.credentialsGroupLabel, tt.args.createAccessKeyResponse); (err != nil) != tt.wantErr { diff --git a/internal/cmd/object-storage/credentials/credentials.go b/internal/cmd/object-storage/credentials/credentials.go index e96b86072..8654a6734 100644 --- a/internal/cmd/object-storage/credentials/credentials.go +++ b/internal/cmd/object-storage/credentials/credentials.go @@ -4,14 +4,14 @@ import ( "github.com/stackitcloud/stackit-cli/internal/cmd/object-storage/credentials/create" "github.com/stackitcloud/stackit-cli/internal/cmd/object-storage/credentials/delete" "github.com/stackitcloud/stackit-cli/internal/cmd/object-storage/credentials/list" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" - "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" "github.com/spf13/cobra" ) -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "credentials", Short: "Provides functionality for Object Storage credentials", @@ -19,12 +19,12 @@ func NewCmd(p *print.Printer) *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd, p) + addSubcommands(cmd, params) return cmd } -func addSubcommands(cmd *cobra.Command, p *print.Printer) { - cmd.AddCommand(create.NewCmd(p)) - cmd.AddCommand(delete.NewCmd(p)) - cmd.AddCommand(list.NewCmd(p)) +func addSubcommands(cmd *cobra.Command, params *params.CmdParams) { + cmd.AddCommand(create.NewCmd(params)) + cmd.AddCommand(delete.NewCmd(params)) + cmd.AddCommand(list.NewCmd(params)) } diff --git a/internal/cmd/object-storage/credentials/delete/delete.go b/internal/cmd/object-storage/credentials/delete/delete.go index ed7fedb5e..e8cc8a4e3 100644 --- a/internal/cmd/object-storage/credentials/delete/delete.go +++ b/internal/cmd/object-storage/credentials/delete/delete.go @@ -5,6 +5,7 @@ import ( "fmt" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -27,7 +28,7 @@ type inputModel struct { CredentialsId string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("delete %s", credentialsIdArg), Short: "Deletes credentials of an Object Storage credentials group", @@ -40,32 +41,32 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } credentialsGroupLabel, err := objectStorageUtils.GetCredentialsGroupName(ctx, apiClient, model.ProjectId, model.CredentialsGroupId, model.Region) if err != nil { - p.Debug(print.ErrorLevel, "get credentials group name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get credentials group name: %v", err) credentialsGroupLabel = model.CredentialsGroupId } credentialsLabel, err := objectStorageUtils.GetCredentialsName(ctx, apiClient, model.ProjectId, model.CredentialsGroupId, model.CredentialsId, model.Region) if err != nil { - p.Debug(print.ErrorLevel, "get credentials name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get credentials name: %v", err) credentialsLabel = model.CredentialsId } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to delete credentials %q of credentials group %q? (This cannot be undone)", credentialsLabel, credentialsGroupLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -78,7 +79,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("delete Object Storage credentials: %w", err) } - p.Info("Deleted credentials %q of credentials group %q\n", credentialsLabel, credentialsGroupLabel) + params.Printer.Info("Deleted credentials %q of credentials group %q\n", credentialsLabel, credentialsGroupLabel) return nil }, } diff --git a/internal/cmd/object-storage/credentials/delete/delete_test.go b/internal/cmd/object-storage/credentials/delete/delete_test.go index 09ee7d04b..1ba508df1 100644 --- a/internal/cmd/object-storage/credentials/delete/delete_test.go +++ b/internal/cmd/object-storage/credentials/delete/delete_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" @@ -161,7 +162,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/object-storage/credentials/list/list.go b/internal/cmd/object-storage/credentials/list/list.go index c1a6d63dd..33a41860f 100644 --- a/internal/cmd/object-storage/credentials/list/list.go +++ b/internal/cmd/object-storage/credentials/list/list.go @@ -7,6 +7,7 @@ import ( "github.com/goccy/go-yaml" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -31,7 +32,7 @@ type inputModel struct { Limit *int64 } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "list", Short: "Lists all credentials for an Object Storage credentials group", @@ -50,13 +51,13 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -71,11 +72,11 @@ func NewCmd(p *print.Printer) *cobra.Command { if len(credentials) == 0 { credentialsGroupLabel, err := objectStorageUtils.GetCredentialsGroupName(ctx, apiClient, model.ProjectId, model.CredentialsGroupId, model.Region) if err != nil { - p.Debug(print.ErrorLevel, "get credentials group name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get credentials group name: %v", err) credentialsGroupLabel = model.CredentialsGroupId } - p.Info("No credentials found for credentials group %q\n", credentialsGroupLabel) + params.Printer.Info("No credentials found for credentials group %q\n", credentialsGroupLabel) return nil } @@ -83,7 +84,7 @@ func NewCmd(p *print.Printer) *cobra.Command { if model.Limit != nil && len(credentials) > int(*model.Limit) { credentials = credentials[:*model.Limit] } - return outputResult(p, model.OutputFormat, credentials) + return outputResult(params.Printer, model.OutputFormat, credentials) }, } configureFlags(cmd) diff --git a/internal/cmd/object-storage/credentials/list/list_test.go b/internal/cmd/object-storage/credentials/list/list_test.go index a50921b69..6f6fb41a3 100644 --- a/internal/cmd/object-storage/credentials/list/list_test.go +++ b/internal/cmd/object-storage/credentials/list/list_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -142,7 +143,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -244,7 +245,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.credentials); (err != nil) != tt.wantErr { diff --git a/internal/cmd/object-storage/disable/disable.go b/internal/cmd/object-storage/disable/disable.go index 56e6f940d..0486e90ae 100644 --- a/internal/cmd/object-storage/disable/disable.go +++ b/internal/cmd/object-storage/disable/disable.go @@ -4,6 +4,7 @@ import ( "context" "fmt" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -20,7 +21,7 @@ type inputModel struct { *globalflags.GlobalFlagModel } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "disable", Short: "Disables Object Storage for a project", @@ -33,26 +34,26 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } - projectLabel, err := projectname.GetProjectName(ctx, p, cmd) + projectLabel, err := projectname.GetProjectName(ctx, params.Printer, cmd) if err != nil { - p.Debug(print.ErrorLevel, "get project name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get project name: %v", err) projectLabel = model.ProjectId } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to disable Object Storage for project %q?", projectLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -69,7 +70,7 @@ func NewCmd(p *print.Printer) *cobra.Command { if model.Async { operationState = "Triggered disablement of" } - p.Info("%s Object Storage for project %q\n", operationState, projectLabel) + params.Printer.Info("%s Object Storage for project %q\n", operationState, projectLabel) return nil }, } diff --git a/internal/cmd/object-storage/enable/enable.go b/internal/cmd/object-storage/enable/enable.go index cec0d6e7d..00959bcfa 100644 --- a/internal/cmd/object-storage/enable/enable.go +++ b/internal/cmd/object-storage/enable/enable.go @@ -4,6 +4,7 @@ import ( "context" "fmt" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -20,7 +21,7 @@ type inputModel struct { *globalflags.GlobalFlagModel } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "enable", Short: "Enables Object Storage for a project", @@ -33,26 +34,26 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } - projectLabel, err := projectname.GetProjectName(ctx, p, cmd) + projectLabel, err := projectname.GetProjectName(ctx, params.Printer, cmd) if err != nil { - p.Debug(print.ErrorLevel, "get project name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get project name: %v", err) projectLabel = model.ProjectId } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to enable Object Storage for project %q?", projectLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -69,7 +70,7 @@ func NewCmd(p *print.Printer) *cobra.Command { if model.Async { operationState = "Triggered enablement of" } - p.Info("%s Object Storage for project %q\n", operationState, projectLabel) + params.Printer.Info("%s Object Storage for project %q\n", operationState, projectLabel) return nil }, } diff --git a/internal/cmd/object-storage/object_storage.go b/internal/cmd/object-storage/object_storage.go index 0ba397592..2adfb7001 100644 --- a/internal/cmd/object-storage/object_storage.go +++ b/internal/cmd/object-storage/object_storage.go @@ -6,14 +6,14 @@ import ( credentialsGroup "github.com/stackitcloud/stackit-cli/internal/cmd/object-storage/credentials-group" "github.com/stackitcloud/stackit-cli/internal/cmd/object-storage/disable" "github.com/stackitcloud/stackit-cli/internal/cmd/object-storage/enable" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" - "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" "github.com/spf13/cobra" ) -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "object-storage", Short: "Provides functionality for Object Storage", @@ -21,14 +21,14 @@ func NewCmd(p *print.Printer) *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd, p) + addSubcommands(cmd, params) return cmd } -func addSubcommands(cmd *cobra.Command, p *print.Printer) { - cmd.AddCommand(bucket.NewCmd(p)) - cmd.AddCommand(disable.NewCmd(p)) - cmd.AddCommand(enable.NewCmd(p)) - cmd.AddCommand(credentialsGroup.NewCmd(p)) - cmd.AddCommand(credentials.NewCmd(p)) +func addSubcommands(cmd *cobra.Command, params *params.CmdParams) { + cmd.AddCommand(bucket.NewCmd(params)) + cmd.AddCommand(disable.NewCmd(params)) + cmd.AddCommand(enable.NewCmd(params)) + cmd.AddCommand(credentialsGroup.NewCmd(params)) + cmd.AddCommand(credentials.NewCmd(params)) } diff --git a/internal/cmd/observability/credentials/create/create.go b/internal/cmd/observability/credentials/create/create.go index 9852ee352..f968f0a7e 100644 --- a/internal/cmd/observability/credentials/create/create.go +++ b/internal/cmd/observability/credentials/create/create.go @@ -7,6 +7,7 @@ import ( "github.com/goccy/go-yaml" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" cliErr "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -29,7 +30,7 @@ type inputModel struct { InstanceId string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "create", Short: "Creates credentials for an Observability instance.", @@ -44,26 +45,26 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } instanceLabel, err := observabilityUtils.GetInstanceName(ctx, apiClient, model.InstanceId, model.ProjectId) if err != nil { - p.Debug(print.ErrorLevel, "get instance name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get instance name: %v", err) instanceLabel = model.InstanceId } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to create credentials for instance %q?", instanceLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -79,7 +80,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("create credentials for Observability instance: %w", err) } - return outputResult(p, model.OutputFormat, instanceLabel, resp) + return outputResult(params.Printer, model.OutputFormat, instanceLabel, resp) }, } configureFlags(cmd) diff --git a/internal/cmd/observability/credentials/create/create_test.go b/internal/cmd/observability/credentials/create/create_test.go index c261948c7..16fcc439d 100644 --- a/internal/cmd/observability/credentials/create/create_test.go +++ b/internal/cmd/observability/credentials/create/create_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-sdk-go/services/observability" @@ -224,7 +225,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.instanceLabel, tt.args.resp); (err != nil) != tt.wantErr { diff --git a/internal/cmd/observability/credentials/credentials.go b/internal/cmd/observability/credentials/credentials.go index fb84cf9d6..c4aa1e396 100644 --- a/internal/cmd/observability/credentials/credentials.go +++ b/internal/cmd/observability/credentials/credentials.go @@ -4,14 +4,14 @@ import ( "github.com/stackitcloud/stackit-cli/internal/cmd/observability/credentials/create" "github.com/stackitcloud/stackit-cli/internal/cmd/observability/credentials/delete" "github.com/stackitcloud/stackit-cli/internal/cmd/observability/credentials/list" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" - "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" "github.com/spf13/cobra" ) -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "credentials", Short: "Provides functionality for Observability credentials", @@ -19,12 +19,12 @@ func NewCmd(p *print.Printer) *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd, p) + addSubcommands(cmd, params) return cmd } -func addSubcommands(cmd *cobra.Command, p *print.Printer) { - cmd.AddCommand(create.NewCmd(p)) - cmd.AddCommand(delete.NewCmd(p)) - cmd.AddCommand(list.NewCmd(p)) +func addSubcommands(cmd *cobra.Command, params *params.CmdParams) { + cmd.AddCommand(create.NewCmd(params)) + cmd.AddCommand(delete.NewCmd(params)) + cmd.AddCommand(list.NewCmd(params)) } diff --git a/internal/cmd/observability/credentials/delete/delete.go b/internal/cmd/observability/credentials/delete/delete.go index a501b949a..0c85bf2b6 100644 --- a/internal/cmd/observability/credentials/delete/delete.go +++ b/internal/cmd/observability/credentials/delete/delete.go @@ -4,6 +4,7 @@ import ( "context" "fmt" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -29,7 +30,7 @@ type inputModel struct { Username string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("delete %s", usernameArg), Short: "Deletes credentials of an Observability instance", @@ -42,26 +43,26 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } instanceLabel, err := observabilityUtils.GetInstanceName(ctx, apiClient, model.InstanceId, model.ProjectId) if err != nil { - p.Debug(print.ErrorLevel, "get instance name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get instance name: %v", err) instanceLabel = model.InstanceId } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to delete credentials for username %q of instance %q? (This cannot be undone)", model.Username, instanceLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -74,7 +75,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("delete Observability credentials: %w", err) } - p.Info("Deleted credentials for username %q of instance %q\n", model.Username, instanceLabel) + params.Printer.Info("Deleted credentials for username %q of instance %q\n", model.Username, instanceLabel) return nil }, } diff --git a/internal/cmd/observability/credentials/list/list.go b/internal/cmd/observability/credentials/list/list.go index 9c9452832..3bf6bf231 100644 --- a/internal/cmd/observability/credentials/list/list.go +++ b/internal/cmd/observability/credentials/list/list.go @@ -6,6 +6,7 @@ import ( "fmt" "github.com/goccy/go-yaml" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -32,7 +33,7 @@ type inputModel struct { Limit *int64 } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "list", Short: "Lists the usernames of all credentials for an Observability instance", @@ -51,13 +52,13 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -72,10 +73,10 @@ func NewCmd(p *print.Printer) *cobra.Command { if len(credentials) == 0 { instanceLabel, err := observabilityUtils.GetInstanceName(ctx, apiClient, model.InstanceId, model.ProjectId) if err != nil { - p.Debug(print.ErrorLevel, "get instance name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get instance name: %v", err) instanceLabel = model.InstanceId } - p.Info("No credentials found for instance %q\n", instanceLabel) + params.Printer.Info("No credentials found for instance %q\n", instanceLabel) return nil } @@ -83,7 +84,7 @@ func NewCmd(p *print.Printer) *cobra.Command { if model.Limit != nil && len(credentials) > int(*model.Limit) { credentials = credentials[:*model.Limit] } - return outputResult(p, model.OutputFormat, credentials) + return outputResult(params.Printer, model.OutputFormat, credentials) }, } configureFlags(cmd) diff --git a/internal/cmd/observability/credentials/list/list_test.go b/internal/cmd/observability/credentials/list/list_test.go index fa23d5dfe..64d92b12f 100644 --- a/internal/cmd/observability/credentials/list/list_test.go +++ b/internal/cmd/observability/credentials/list/list_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -238,7 +239,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.credentials); (err != nil) != tt.wantErr { diff --git a/internal/cmd/observability/grafana/describe/describe.go b/internal/cmd/observability/grafana/describe/describe.go index 9a364499c..bce33d2a6 100644 --- a/internal/cmd/observability/grafana/describe/describe.go +++ b/internal/cmd/observability/grafana/describe/describe.go @@ -6,6 +6,7 @@ import ( "fmt" "github.com/goccy/go-yaml" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -31,7 +32,7 @@ type inputModel struct { ShowPassword bool } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("describe %s", instanceIdArg), Short: "Shows details of the Grafana configuration of an Observability instance", @@ -54,13 +55,13 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -77,7 +78,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("get instance: %w", err) } - return outputResult(p, model.OutputFormat, model.ShowPassword, grafanaConfigsResp, instanceResp) + return outputResult(params.Printer, model.OutputFormat, model.ShowPassword, grafanaConfigsResp, instanceResp) }, } configureFlags(cmd) diff --git a/internal/cmd/observability/grafana/describe/describe_test.go b/internal/cmd/observability/grafana/describe/describe_test.go index f282c2964..d84af41cb 100644 --- a/internal/cmd/observability/grafana/describe/describe_test.go +++ b/internal/cmd/observability/grafana/describe/describe_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" @@ -169,7 +170,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -322,7 +323,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.showPassword, tt.args.grafanaConfig, tt.args.instance); (err != nil) != tt.wantErr { diff --git a/internal/cmd/observability/grafana/grafana.go b/internal/cmd/observability/grafana/grafana.go index e9ad230d1..000da5eac 100644 --- a/internal/cmd/observability/grafana/grafana.go +++ b/internal/cmd/observability/grafana/grafana.go @@ -4,14 +4,14 @@ import ( "github.com/stackitcloud/stackit-cli/internal/cmd/observability/grafana/describe" publicreadaccess "github.com/stackitcloud/stackit-cli/internal/cmd/observability/grafana/public-read-access" singlesignon "github.com/stackitcloud/stackit-cli/internal/cmd/observability/grafana/single-sign-on" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" - "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" "github.com/spf13/cobra" ) -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "grafana", Short: "Provides functionality for the Grafana configuration of Observability instances", @@ -19,12 +19,12 @@ func NewCmd(p *print.Printer) *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd, p) + addSubcommands(cmd, params) return cmd } -func addSubcommands(cmd *cobra.Command, p *print.Printer) { - cmd.AddCommand(describe.NewCmd(p)) - cmd.AddCommand(publicreadaccess.NewCmd(p)) - cmd.AddCommand(singlesignon.NewCmd(p)) +func addSubcommands(cmd *cobra.Command, params *params.CmdParams) { + cmd.AddCommand(describe.NewCmd(params)) + cmd.AddCommand(publicreadaccess.NewCmd(params)) + cmd.AddCommand(singlesignon.NewCmd(params)) } diff --git a/internal/cmd/observability/grafana/public-read-access/disable/disable.go b/internal/cmd/observability/grafana/public-read-access/disable/disable.go index ac6cd80a4..0f122fc96 100644 --- a/internal/cmd/observability/grafana/public-read-access/disable/disable.go +++ b/internal/cmd/observability/grafana/public-read-access/disable/disable.go @@ -4,6 +4,7 @@ import ( "context" "fmt" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -26,7 +27,7 @@ type inputModel struct { InstanceId string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("disable %s", instanceIdArg), Short: "Disables public read access for Grafana on Observability instances", @@ -42,13 +43,13 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -60,7 +61,7 @@ func NewCmd(p *print.Printer) *cobra.Command { if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to disable Grafana public read access for instance %q?", instanceLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -76,7 +77,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("disable grafana public read access: %w", err) } - p.Info("Disabled Grafana public read access for instance %q\n", instanceLabel) + params.Printer.Info("Disabled Grafana public read access for instance %q\n", instanceLabel) return nil }, } diff --git a/internal/cmd/observability/grafana/public-read-access/disable/disable_test.go b/internal/cmd/observability/grafana/public-read-access/disable/disable_test.go index 7e08ac647..a8db1bd9d 100644 --- a/internal/cmd/observability/grafana/public-read-access/disable/disable_test.go +++ b/internal/cmd/observability/grafana/public-read-access/disable/disable_test.go @@ -5,6 +5,7 @@ import ( "fmt" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -186,7 +187,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/observability/grafana/public-read-access/enable/enable.go b/internal/cmd/observability/grafana/public-read-access/enable/enable.go index 9296a1f24..a13ae2c08 100644 --- a/internal/cmd/observability/grafana/public-read-access/enable/enable.go +++ b/internal/cmd/observability/grafana/public-read-access/enable/enable.go @@ -4,6 +4,7 @@ import ( "context" "fmt" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -26,7 +27,7 @@ type inputModel struct { InstanceId string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("enable %s", instanceIdArg), Short: "Enables public read access for Grafana on Observability instances", @@ -42,13 +43,13 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -60,7 +61,7 @@ func NewCmd(p *print.Printer) *cobra.Command { if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to enable Grafana public read access for instance %q?", instanceLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -76,7 +77,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("enable grafana public read access: %w", err) } - p.Info("Enabled Grafana public read access for instance %q\n", instanceLabel) + params.Printer.Info("Enabled Grafana public read access for instance %q\n", instanceLabel) return nil }, } diff --git a/internal/cmd/observability/grafana/public-read-access/enable/enable_test.go b/internal/cmd/observability/grafana/public-read-access/enable/enable_test.go index 57ebc5bc3..d425e7078 100644 --- a/internal/cmd/observability/grafana/public-read-access/enable/enable_test.go +++ b/internal/cmd/observability/grafana/public-read-access/enable/enable_test.go @@ -5,6 +5,7 @@ import ( "fmt" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -186,7 +187,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/observability/grafana/public-read-access/public_read_access.go b/internal/cmd/observability/grafana/public-read-access/public_read_access.go index 8844e1279..94c27eb36 100644 --- a/internal/cmd/observability/grafana/public-read-access/public_read_access.go +++ b/internal/cmd/observability/grafana/public-read-access/public_read_access.go @@ -5,14 +5,14 @@ import ( "github.com/stackitcloud/stackit-cli/internal/cmd/observability/grafana/public-read-access/disable" "github.com/stackitcloud/stackit-cli/internal/cmd/observability/grafana/public-read-access/enable" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" - "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" "github.com/spf13/cobra" ) -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "public-read-access", Short: "Enable or disable public read access for Grafana in Observability instances", @@ -23,11 +23,11 @@ func NewCmd(p *print.Printer) *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd, p) + addSubcommands(cmd, params) return cmd } -func addSubcommands(cmd *cobra.Command, p *print.Printer) { - cmd.AddCommand(enable.NewCmd(p)) - cmd.AddCommand(disable.NewCmd(p)) +func addSubcommands(cmd *cobra.Command, params *params.CmdParams) { + cmd.AddCommand(enable.NewCmd(params)) + cmd.AddCommand(disable.NewCmd(params)) } diff --git a/internal/cmd/observability/grafana/single-sign-on/disable/disable.go b/internal/cmd/observability/grafana/single-sign-on/disable/disable.go index 018d7827d..14084d154 100644 --- a/internal/cmd/observability/grafana/single-sign-on/disable/disable.go +++ b/internal/cmd/observability/grafana/single-sign-on/disable/disable.go @@ -4,6 +4,7 @@ import ( "context" "fmt" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -26,7 +27,7 @@ type inputModel struct { InstanceId string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("disable %s", instanceIdArg), Short: "Disables single sign-on for Grafana on Observability instances", @@ -42,13 +43,13 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -60,7 +61,7 @@ func NewCmd(p *print.Printer) *cobra.Command { if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to disable single sign-on for Grafana for instance %q?", instanceLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -76,7 +77,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("disable single sign-on for grafana: %w", err) } - p.Info("Disabled single sign-on for Grafana for instance %q\n", instanceLabel) + params.Printer.Info("Disabled single sign-on for Grafana for instance %q\n", instanceLabel) return nil }, } diff --git a/internal/cmd/observability/grafana/single-sign-on/disable/disable_test.go b/internal/cmd/observability/grafana/single-sign-on/disable/disable_test.go index 79653608e..53c087352 100644 --- a/internal/cmd/observability/grafana/single-sign-on/disable/disable_test.go +++ b/internal/cmd/observability/grafana/single-sign-on/disable/disable_test.go @@ -5,6 +5,7 @@ import ( "fmt" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -186,7 +187,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/observability/grafana/single-sign-on/enable/enable.go b/internal/cmd/observability/grafana/single-sign-on/enable/enable.go index 9a002b9f9..9956ebd9b 100644 --- a/internal/cmd/observability/grafana/single-sign-on/enable/enable.go +++ b/internal/cmd/observability/grafana/single-sign-on/enable/enable.go @@ -4,6 +4,7 @@ import ( "context" "fmt" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -26,7 +27,7 @@ type inputModel struct { InstanceId string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("enable %s", instanceIdArg), Short: "Enables single sign-on for Grafana on Observability instances", @@ -42,13 +43,13 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -60,7 +61,7 @@ func NewCmd(p *print.Printer) *cobra.Command { if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to enable single sign-on for Grafana for instance %q?", instanceLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -76,7 +77,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("enable single sign-on for grafana: %w", err) } - p.Info("Enabled single sign-on for Grafana for instance %q\n", instanceLabel) + params.Printer.Info("Enabled single sign-on for Grafana for instance %q\n", instanceLabel) return nil }, } diff --git a/internal/cmd/observability/grafana/single-sign-on/enable/enable_test.go b/internal/cmd/observability/grafana/single-sign-on/enable/enable_test.go index e59f23828..cdc464409 100644 --- a/internal/cmd/observability/grafana/single-sign-on/enable/enable_test.go +++ b/internal/cmd/observability/grafana/single-sign-on/enable/enable_test.go @@ -5,6 +5,7 @@ import ( "fmt" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" observabilityUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/observability/utils" @@ -186,7 +187,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/observability/grafana/single-sign-on/single_sign_on.go b/internal/cmd/observability/grafana/single-sign-on/single_sign_on.go index c53c4a5cf..c4a73ada2 100644 --- a/internal/cmd/observability/grafana/single-sign-on/single_sign_on.go +++ b/internal/cmd/observability/grafana/single-sign-on/single_sign_on.go @@ -5,14 +5,14 @@ import ( "github.com/stackitcloud/stackit-cli/internal/cmd/observability/grafana/single-sign-on/disable" "github.com/stackitcloud/stackit-cli/internal/cmd/observability/grafana/single-sign-on/enable" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" - "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" "github.com/spf13/cobra" ) -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "single-sign-on", Aliases: []string{"sso"}, @@ -24,11 +24,11 @@ func NewCmd(p *print.Printer) *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd, p) + addSubcommands(cmd, params) return cmd } -func addSubcommands(cmd *cobra.Command, p *print.Printer) { - cmd.AddCommand(enable.NewCmd(p)) - cmd.AddCommand(disable.NewCmd(p)) +func addSubcommands(cmd *cobra.Command, params *params.CmdParams) { + cmd.AddCommand(enable.NewCmd(params)) + cmd.AddCommand(disable.NewCmd(params)) } diff --git a/internal/cmd/observability/instance/create/create.go b/internal/cmd/observability/instance/create/create.go index 57e9d888f..5158941e5 100644 --- a/internal/cmd/observability/instance/create/create.go +++ b/internal/cmd/observability/instance/create/create.go @@ -7,6 +7,7 @@ import ( "fmt" "github.com/goccy/go-yaml" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" cliErr "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -38,7 +39,7 @@ type inputModel struct { PlanId *string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "create", Short: "Creates an Observability instance", @@ -54,26 +55,26 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } - projectLabel, err := projectname.GetProjectName(ctx, p, cmd) + projectLabel, err := projectname.GetProjectName(ctx, params.Printer, cmd) if err != nil { - p.Debug(print.ErrorLevel, "get project name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get project name: %v", err) projectLabel = model.ProjectId } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to create an Observability instance for project %q?", projectLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -96,7 +97,7 @@ func NewCmd(p *print.Printer) *cobra.Command { // Wait for async operation, if async mode not enabled if !model.Async { - s := spinner.New(p) + s := spinner.New(params.Printer) s.Start("Creating instance") _, err = wait.CreateInstanceWaitHandler(ctx, apiClient, instanceId, model.ProjectId).WaitWithContext(ctx) if err != nil { @@ -105,7 +106,7 @@ func NewCmd(p *print.Printer) *cobra.Command { s.Stop() } - return outputResult(p, model.OutputFormat, model.Async, projectLabel, resp) + return outputResult(params.Printer, model.OutputFormat, model.Async, projectLabel, resp) }, } configureFlags(cmd) diff --git a/internal/cmd/observability/instance/create/create_test.go b/internal/cmd/observability/instance/create/create_test.go index f36d5ae8a..6cdeda809 100644 --- a/internal/cmd/observability/instance/create/create_test.go +++ b/internal/cmd/observability/instance/create/create_test.go @@ -5,6 +5,7 @@ import ( "fmt" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -175,7 +176,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -361,7 +362,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.async, tt.args.projectLabel, tt.args.resp); (err != nil) != tt.wantErr { diff --git a/internal/cmd/observability/instance/delete/delete.go b/internal/cmd/observability/instance/delete/delete.go index abe93e2a0..eb1790ebd 100644 --- a/internal/cmd/observability/instance/delete/delete.go +++ b/internal/cmd/observability/instance/delete/delete.go @@ -4,6 +4,7 @@ import ( "context" "fmt" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -28,7 +29,7 @@ type inputModel struct { InstanceId string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("delete %s", instanceIdArg), Short: "Deletes an Observability instance", @@ -41,26 +42,26 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } instanceLabel, err := observabilityUtils.GetInstanceName(ctx, apiClient, model.InstanceId, model.ProjectId) if err != nil { - p.Debug(print.ErrorLevel, "get instance name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get instance name: %v", err) instanceLabel = model.InstanceId } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to delete instance %q? (This cannot be undone)", instanceLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -75,7 +76,7 @@ func NewCmd(p *print.Printer) *cobra.Command { // Wait for async operation, if async mode not enabled if !model.Async { - s := spinner.New(p) + s := spinner.New(params.Printer) s.Start("Deleting instance") _, err = wait.DeleteInstanceWaitHandler(ctx, apiClient, model.InstanceId, model.ProjectId).WaitWithContext(ctx) if err != nil { @@ -88,7 +89,7 @@ func NewCmd(p *print.Printer) *cobra.Command { if model.Async { operationState = "Triggered deletion of" } - p.Info("%s instance %q\n", operationState, instanceLabel) + params.Printer.Info("%s instance %q\n", operationState, instanceLabel) return nil }, } diff --git a/internal/cmd/observability/instance/delete/delete_test.go b/internal/cmd/observability/instance/delete/delete_test.go index 8375214bb..d8432900f 100644 --- a/internal/cmd/observability/instance/delete/delete_test.go +++ b/internal/cmd/observability/instance/delete/delete_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" @@ -138,7 +139,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/observability/instance/describe/describe.go b/internal/cmd/observability/instance/describe/describe.go index 157bd28a4..7e03dbb03 100644 --- a/internal/cmd/observability/instance/describe/describe.go +++ b/internal/cmd/observability/instance/describe/describe.go @@ -6,6 +6,7 @@ import ( "fmt" "github.com/goccy/go-yaml" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -28,7 +29,7 @@ type inputModel struct { InstanceId string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("describe %s", instanceIdArg), Short: "Shows details of an Observability instance", @@ -44,12 +45,12 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -61,7 +62,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("read Observability instance: %w", err) } - return outputResult(p, model.OutputFormat, resp) + return outputResult(params.Printer, model.OutputFormat, resp) }, } return cmd diff --git a/internal/cmd/observability/instance/describe/describe_test.go b/internal/cmd/observability/instance/describe/describe_test.go index c55020fa7..62f60d451 100644 --- a/internal/cmd/observability/instance/describe/describe_test.go +++ b/internal/cmd/observability/instance/describe/describe_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" @@ -138,7 +139,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -241,7 +242,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.instance); (err != nil) != tt.wantErr { diff --git a/internal/cmd/observability/instance/instance.go b/internal/cmd/observability/instance/instance.go index efd7d4974..47a84edf6 100644 --- a/internal/cmd/observability/instance/instance.go +++ b/internal/cmd/observability/instance/instance.go @@ -6,14 +6,14 @@ import ( "github.com/stackitcloud/stackit-cli/internal/cmd/observability/instance/describe" "github.com/stackitcloud/stackit-cli/internal/cmd/observability/instance/list" "github.com/stackitcloud/stackit-cli/internal/cmd/observability/instance/update" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" - "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" "github.com/spf13/cobra" ) -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "instance", Short: "Provides functionality for Observability instances", @@ -21,14 +21,14 @@ func NewCmd(p *print.Printer) *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd, p) + addSubcommands(cmd, params) return cmd } -func addSubcommands(cmd *cobra.Command, p *print.Printer) { - cmd.AddCommand(create.NewCmd(p)) - cmd.AddCommand(update.NewCmd(p)) - cmd.AddCommand(delete.NewCmd(p)) - cmd.AddCommand(list.NewCmd(p)) - cmd.AddCommand(describe.NewCmd(p)) +func addSubcommands(cmd *cobra.Command, params *params.CmdParams) { + cmd.AddCommand(create.NewCmd(params)) + cmd.AddCommand(update.NewCmd(params)) + cmd.AddCommand(delete.NewCmd(params)) + cmd.AddCommand(list.NewCmd(params)) + cmd.AddCommand(describe.NewCmd(params)) } diff --git a/internal/cmd/observability/instance/list/list.go b/internal/cmd/observability/instance/list/list.go index b540bee32..e82c2254b 100644 --- a/internal/cmd/observability/instance/list/list.go +++ b/internal/cmd/observability/instance/list/list.go @@ -7,6 +7,7 @@ import ( "github.com/goccy/go-yaml" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -29,7 +30,7 @@ type inputModel struct { Limit *int64 } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "list", Short: "Lists all Observability instances", @@ -48,13 +49,13 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -67,12 +68,12 @@ func NewCmd(p *print.Printer) *cobra.Command { } instances := *resp.Instances if len(instances) == 0 { - projectLabel, err := projectname.GetProjectName(ctx, p, cmd) + projectLabel, err := projectname.GetProjectName(ctx, params.Printer, cmd) if err != nil { - p.Debug(print.ErrorLevel, "get project name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get project name: %v", err) projectLabel = model.ProjectId } - p.Info("No instances found for project %q\n", projectLabel) + params.Printer.Info("No instances found for project %q\n", projectLabel) return nil } @@ -81,7 +82,7 @@ func NewCmd(p *print.Printer) *cobra.Command { instances = instances[:*model.Limit] } - return outputResult(p, model.OutputFormat, instances) + return outputResult(params.Printer, model.OutputFormat, instances) }, } diff --git a/internal/cmd/observability/instance/list/list_test.go b/internal/cmd/observability/instance/list/list_test.go index e8a87f48a..33157c100 100644 --- a/internal/cmd/observability/instance/list/list_test.go +++ b/internal/cmd/observability/instance/list/list_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -217,7 +218,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.instances); (err != nil) != tt.wantErr { diff --git a/internal/cmd/observability/instance/update/update.go b/internal/cmd/observability/instance/update/update.go index 192c84366..90411dd26 100644 --- a/internal/cmd/observability/instance/update/update.go +++ b/internal/cmd/observability/instance/update/update.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" cliErr "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -38,7 +39,7 @@ type inputModel struct { PlanId *string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("update %s", instanceIdArg), Short: "Updates an Observability instance", @@ -57,26 +58,26 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } instanceLabel, err := observabilityUtils.GetInstanceName(ctx, apiClient, model.InstanceId, model.ProjectId) if err != nil || instanceLabel == "" { - p.Debug(print.ErrorLevel, "get instance name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get instance name: %v", err) instanceLabel = model.InstanceId } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to update instance %q?", instanceLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -100,7 +101,7 @@ func NewCmd(p *print.Printer) *cobra.Command { // Wait for async operation, if async mode not enabled if !model.Async { - s := spinner.New(p) + s := spinner.New(params.Printer) s.Start("Updating instance") _, err = wait.UpdateInstanceWaitHandler(ctx, apiClient, instanceId, model.ProjectId).WaitWithContext(ctx) if err != nil { @@ -113,7 +114,7 @@ func NewCmd(p *print.Printer) *cobra.Command { if model.Async { operationState = "Triggered update of" } - p.Info("%s instance %q\n", operationState, instanceLabel) + params.Printer.Info("%s instance %q\n", operationState, instanceLabel) return nil }, } diff --git a/internal/cmd/observability/instance/update/update_test.go b/internal/cmd/observability/instance/update/update_test.go index 0b5177b27..fd798093c 100644 --- a/internal/cmd/observability/instance/update/update_test.go +++ b/internal/cmd/observability/instance/update/update_test.go @@ -5,6 +5,7 @@ import ( "fmt" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -240,7 +241,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/observability/observability.go b/internal/cmd/observability/observability.go index 8737a716e..136f29637 100644 --- a/internal/cmd/observability/observability.go +++ b/internal/cmd/observability/observability.go @@ -6,14 +6,14 @@ import ( "github.com/stackitcloud/stackit-cli/internal/cmd/observability/instance" "github.com/stackitcloud/stackit-cli/internal/cmd/observability/plans" scrapeconfig "github.com/stackitcloud/stackit-cli/internal/cmd/observability/scrape-config" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" - "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" "github.com/spf13/cobra" ) -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "observability", Short: "Provides functionality for Observability", @@ -21,14 +21,14 @@ func NewCmd(p *print.Printer) *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd, p) + addSubcommands(cmd, params) return cmd } -func addSubcommands(cmd *cobra.Command, p *print.Printer) { - cmd.AddCommand(grafana.NewCmd(p)) - cmd.AddCommand(instance.NewCmd(p)) - cmd.AddCommand(credentials.NewCmd(p)) - cmd.AddCommand(scrapeconfig.NewCmd(p)) - cmd.AddCommand(plans.NewCmd(p)) +func addSubcommands(cmd *cobra.Command, params *params.CmdParams) { + cmd.AddCommand(grafana.NewCmd(params)) + cmd.AddCommand(instance.NewCmd(params)) + cmd.AddCommand(credentials.NewCmd(params)) + cmd.AddCommand(scrapeconfig.NewCmd(params)) + cmd.AddCommand(plans.NewCmd(params)) } diff --git a/internal/cmd/observability/plans/plans.go b/internal/cmd/observability/plans/plans.go index d42cfb8a0..90867c27a 100644 --- a/internal/cmd/observability/plans/plans.go +++ b/internal/cmd/observability/plans/plans.go @@ -5,6 +5,7 @@ import ( "encoding/json" "fmt" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -30,7 +31,7 @@ type inputModel struct { Limit *int64 } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "plans", Short: "Lists all Observability service plans", @@ -49,13 +50,13 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -68,12 +69,12 @@ func NewCmd(p *print.Printer) *cobra.Command { } plans := *resp.Plans if len(plans) == 0 { - projectLabel, err := projectname.GetProjectName(ctx, p, cmd) + projectLabel, err := projectname.GetProjectName(ctx, params.Printer, cmd) if err != nil { - p.Debug(print.ErrorLevel, "get project name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get project name: %v", err) projectLabel = model.ProjectId } - p.Info("No plans found for project %q\n", projectLabel) + params.Printer.Info("No plans found for project %q\n", projectLabel) return nil } @@ -82,7 +83,7 @@ func NewCmd(p *print.Printer) *cobra.Command { plans = plans[:*model.Limit] } - return outputResult(p, model.OutputFormat, plans) + return outputResult(params.Printer, model.OutputFormat, plans) }, } diff --git a/internal/cmd/observability/plans/plans_test.go b/internal/cmd/observability/plans/plans_test.go index 047c3c5f6..2228a0402 100644 --- a/internal/cmd/observability/plans/plans_test.go +++ b/internal/cmd/observability/plans/plans_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -218,7 +219,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.plans); (err != nil) != tt.wantErr { diff --git a/internal/cmd/observability/scrape-config/create/create.go b/internal/cmd/observability/scrape-config/create/create.go index 0856e6b19..9bd89d9d7 100644 --- a/internal/cmd/observability/scrape-config/create/create.go +++ b/internal/cmd/observability/scrape-config/create/create.go @@ -5,6 +5,7 @@ import ( "encoding/json" "fmt" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -32,7 +33,7 @@ type inputModel struct { Payload *observability.CreateScrapeConfigPayload } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "create", Short: "Creates a scrape configuration for an Observability instance", @@ -61,20 +62,20 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } instanceLabel, err := observabilityUtils.GetInstanceName(ctx, apiClient, model.InstanceId, model.ProjectId) if err != nil { - p.Debug(print.ErrorLevel, "get instance name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get instance name: %v", err) instanceLabel = model.InstanceId } @@ -89,7 +90,7 @@ func NewCmd(p *print.Printer) *cobra.Command { if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to create scrape configuration %q on Observability instance %q?", *model.Payload.JobName, instanceLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -106,7 +107,7 @@ func NewCmd(p *print.Printer) *cobra.Command { // Wait for async operation, if async mode not enabled if !model.Async { - s := spinner.New(p) + s := spinner.New(params.Printer) s.Start("Creating scrape config") _, err = wait.CreateScrapeConfigWaitHandler(ctx, apiClient, model.InstanceId, *jobName, model.ProjectId).WaitWithContext(ctx) if err != nil { @@ -119,7 +120,7 @@ func NewCmd(p *print.Printer) *cobra.Command { if model.Async { operationState = "Triggered creation of" } - p.Outputf("%s scrape configuration with name %q for Observability instance %q\n", operationState, utils.PtrString(jobName), instanceLabel) + params.Printer.Outputf("%s scrape configuration with name %q for Observability instance %q\n", operationState, utils.PtrString(jobName), instanceLabel) return nil }, } diff --git a/internal/cmd/observability/scrape-config/delete/delete.go b/internal/cmd/observability/scrape-config/delete/delete.go index de04c6fcc..0bc137b3b 100644 --- a/internal/cmd/observability/scrape-config/delete/delete.go +++ b/internal/cmd/observability/scrape-config/delete/delete.go @@ -4,6 +4,7 @@ import ( "context" "fmt" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -31,7 +32,7 @@ type inputModel struct { InstanceId string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("delete %s", jobNameArg), Short: "Deletes a scrape configuration from an Observability instance", @@ -44,26 +45,26 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } instanceLabel, err := observabilityUtils.GetInstanceName(ctx, apiClient, model.InstanceId, model.ProjectId) if err != nil { - p.Debug(print.ErrorLevel, "get instance name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get instance name: %v", err) instanceLabel = model.InstanceId } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to delete scrape configuration %q on Observability instance %q? (This cannot be undone)", model.JobName, instanceLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -78,7 +79,7 @@ func NewCmd(p *print.Printer) *cobra.Command { // Wait for async operation, if async mode not enabled if !model.Async { - s := spinner.New(p) + s := spinner.New(params.Printer) s.Start("Deleting scrape config") _, err = wait.DeleteScrapeConfigWaitHandler(ctx, apiClient, model.InstanceId, model.JobName, model.ProjectId).WaitWithContext(ctx) if err != nil { @@ -91,7 +92,7 @@ func NewCmd(p *print.Printer) *cobra.Command { if model.Async { operationState = "Triggered deletion of" } - p.Info("%s scrape configuration with name %q for Observability instance %q\n", operationState, model.JobName, instanceLabel) + params.Printer.Info("%s scrape configuration with name %q for Observability instance %q\n", operationState, model.JobName, instanceLabel) return nil }, } diff --git a/internal/cmd/observability/scrape-config/describe/describe.go b/internal/cmd/observability/scrape-config/describe/describe.go index 2687a72a3..2c6e24573 100644 --- a/internal/cmd/observability/scrape-config/describe/describe.go +++ b/internal/cmd/observability/scrape-config/describe/describe.go @@ -8,6 +8,7 @@ import ( "github.com/goccy/go-yaml" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -32,7 +33,7 @@ type inputModel struct { InstanceId string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("describe %s", jobNameArg), Short: "Shows details of a scrape configuration from an Observability instance", @@ -48,12 +49,12 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -65,7 +66,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("read scrape configuration: %w", err) } - return outputResult(p, model.OutputFormat, resp.Data) + return outputResult(params.Printer, model.OutputFormat, resp.Data) }, } configureFlags(cmd) diff --git a/internal/cmd/observability/scrape-config/describe/describe_test.go b/internal/cmd/observability/scrape-config/describe/describe_test.go index a0cf31413..d975ba82c 100644 --- a/internal/cmd/observability/scrape-config/describe/describe_test.go +++ b/internal/cmd/observability/scrape-config/describe/describe_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" @@ -257,7 +258,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.config); (err != nil) != tt.wantErr { diff --git a/internal/cmd/observability/scrape-config/generate-payload/generate_payload.go b/internal/cmd/observability/scrape-config/generate-payload/generate_payload.go index fe8a9f5ec..3a1927114 100644 --- a/internal/cmd/observability/scrape-config/generate-payload/generate_payload.go +++ b/internal/cmd/observability/scrape-config/generate-payload/generate_payload.go @@ -5,6 +5,7 @@ import ( "encoding/json" "fmt" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" "github.com/stackitcloud/stackit-cli/internal/pkg/fileutils" @@ -31,7 +32,7 @@ type inputModel struct { FilePath *string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "generate-payload", Short: "Generates a payload to create/update scrape configurations for an Observability instance ", @@ -61,20 +62,20 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } if model.JobName == nil { createPayload := observabilityUtils.DefaultCreateScrapeConfigPayload - return outputCreateResult(p, model.FilePath, &createPayload) + return outputCreateResult(params.Printer, model.FilePath, &createPayload) } req := buildRequest(ctx, model, apiClient) @@ -88,7 +89,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("map update scrape config payloads: %w", err) } - return outputUpdateResult(p, model.FilePath, payload) + return outputUpdateResult(params.Printer, model.FilePath, payload) }, } configureFlags(cmd) diff --git a/internal/cmd/observability/scrape-config/generate-payload/generate_payload_test.go b/internal/cmd/observability/scrape-config/generate-payload/generate_payload_test.go index b8740da53..e9a3a076b 100644 --- a/internal/cmd/observability/scrape-config/generate-payload/generate_payload_test.go +++ b/internal/cmd/observability/scrape-config/generate-payload/generate_payload_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -274,7 +275,7 @@ func TestOutputCreateResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputCreateResult(p, tt.args.filePath, tt.args.payload); (err != nil) != tt.wantErr { @@ -308,7 +309,7 @@ func TestOutputUpdateResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputUpdateResult(p, tt.args.filePath, tt.args.payload); (err != nil) != tt.wantErr { diff --git a/internal/cmd/observability/scrape-config/list/list.go b/internal/cmd/observability/scrape-config/list/list.go index 5dbdde095..b03ec740b 100644 --- a/internal/cmd/observability/scrape-config/list/list.go +++ b/internal/cmd/observability/scrape-config/list/list.go @@ -6,6 +6,7 @@ import ( "fmt" "github.com/goccy/go-yaml" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -33,7 +34,7 @@ type inputModel struct { InstanceId string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "list", Short: "Lists all scrape configurations of an Observability instance", @@ -52,13 +53,13 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -73,10 +74,10 @@ func NewCmd(p *print.Printer) *cobra.Command { if len(configs) == 0 { instanceLabel, err := observabilityUtils.GetInstanceName(ctx, apiClient, model.InstanceId, model.ProjectId) if err != nil { - p.Debug(print.ErrorLevel, "get instance name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get instance name: %v", err) instanceLabel = model.InstanceId } - p.Info("No scrape configurations found for instance %q\n", instanceLabel) + params.Printer.Info("No scrape configurations found for instance %q\n", instanceLabel) return nil } @@ -85,7 +86,7 @@ func NewCmd(p *print.Printer) *cobra.Command { configs = configs[:*model.Limit] } - return outputResult(p, model.OutputFormat, configs) + return outputResult(params.Printer, model.OutputFormat, configs) }, } diff --git a/internal/cmd/observability/scrape-config/list/list_test.go b/internal/cmd/observability/scrape-config/list/list_test.go index 69e5b6ab5..35e53001b 100644 --- a/internal/cmd/observability/scrape-config/list/list_test.go +++ b/internal/cmd/observability/scrape-config/list/list_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -241,7 +242,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.configs); (err != nil) != tt.wantErr { diff --git a/internal/cmd/observability/scrape-config/scrape_config.go b/internal/cmd/observability/scrape-config/scrape_config.go index f056c6e60..d0934ff3b 100644 --- a/internal/cmd/observability/scrape-config/scrape_config.go +++ b/internal/cmd/observability/scrape-config/scrape_config.go @@ -7,14 +7,14 @@ import ( generatepayload "github.com/stackitcloud/stackit-cli/internal/cmd/observability/scrape-config/generate-payload" "github.com/stackitcloud/stackit-cli/internal/cmd/observability/scrape-config/list" "github.com/stackitcloud/stackit-cli/internal/cmd/observability/scrape-config/update" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" - "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" "github.com/spf13/cobra" ) -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "scrape-config", Short: "Provides functionality for scrape configurations in Observability", @@ -22,15 +22,15 @@ func NewCmd(p *print.Printer) *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd, p) + addSubcommands(cmd, params) return cmd } -func addSubcommands(cmd *cobra.Command, p *print.Printer) { - cmd.AddCommand(generatepayload.NewCmd(p)) - cmd.AddCommand(create.NewCmd(p)) - cmd.AddCommand(delete.NewCmd(p)) - cmd.AddCommand(update.NewCmd(p)) - cmd.AddCommand(list.NewCmd(p)) - cmd.AddCommand(describe.NewCmd(p)) +func addSubcommands(cmd *cobra.Command, params *params.CmdParams) { + cmd.AddCommand(generatepayload.NewCmd(params)) + cmd.AddCommand(create.NewCmd(params)) + cmd.AddCommand(delete.NewCmd(params)) + cmd.AddCommand(update.NewCmd(params)) + cmd.AddCommand(list.NewCmd(params)) + cmd.AddCommand(describe.NewCmd(params)) } diff --git a/internal/cmd/observability/scrape-config/update/update.go b/internal/cmd/observability/scrape-config/update/update.go index d07e97e1b..25b423452 100644 --- a/internal/cmd/observability/scrape-config/update/update.go +++ b/internal/cmd/observability/scrape-config/update/update.go @@ -5,6 +5,7 @@ import ( "encoding/json" "fmt" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -31,7 +32,7 @@ type inputModel struct { Payload observability.UpdateScrapeConfigPayload } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("update %s", jobNameArg), Short: "Updates a scrape configuration of an Observability instance", @@ -56,20 +57,20 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to update scrape configuration %q?", model.JobName) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -83,7 +84,7 @@ func NewCmd(p *print.Printer) *cobra.Command { } // The API has no status to wait on, so async mode is default - p.Info("Updated Observability scrape configuration with name %q\n", model.JobName) + params.Printer.Info("Updated Observability scrape configuration with name %q\n", model.JobName) return nil }, } diff --git a/internal/cmd/opensearch/credentials/create/create.go b/internal/cmd/opensearch/credentials/create/create.go index 9a4174db8..ce5a1cf9b 100644 --- a/internal/cmd/opensearch/credentials/create/create.go +++ b/internal/cmd/opensearch/credentials/create/create.go @@ -7,6 +7,7 @@ import ( "github.com/goccy/go-yaml" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -30,7 +31,7 @@ type inputModel struct { ShowPassword bool } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "create", Short: "Creates credentials for an OpenSearch instance", @@ -46,26 +47,26 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } instanceLabel, err := opensearchUtils.GetInstanceName(ctx, apiClient, model.ProjectId, model.InstanceId) if err != nil { - p.Debug(print.ErrorLevel, "get instance name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get instance name: %v", err) instanceLabel = model.InstanceId } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to create credentials for instance %q?", instanceLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -78,7 +79,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("create OpenSearch credentials: %w", err) } - return outputResult(p, model.OutputFormat, model.ShowPassword, instanceLabel, resp) + return outputResult(params.Printer, model.OutputFormat, model.ShowPassword, instanceLabel, resp) }, } configureFlags(cmd) diff --git a/internal/cmd/opensearch/credentials/create/create_test.go b/internal/cmd/opensearch/credentials/create/create_test.go index 1424c9729..6928feefc 100644 --- a/internal/cmd/opensearch/credentials/create/create_test.go +++ b/internal/cmd/opensearch/credentials/create/create_test.go @@ -7,6 +7,7 @@ import ( "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" "github.com/google/uuid" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -130,7 +131,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -271,7 +272,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.showPassword, tt.args.instanceLabel, tt.args.resp); (err != nil) != tt.wantErr { diff --git a/internal/cmd/opensearch/credentials/credentials.go b/internal/cmd/opensearch/credentials/credentials.go index 4ea7f3f76..3e1c17ba8 100644 --- a/internal/cmd/opensearch/credentials/credentials.go +++ b/internal/cmd/opensearch/credentials/credentials.go @@ -5,14 +5,14 @@ import ( "github.com/stackitcloud/stackit-cli/internal/cmd/opensearch/credentials/delete" "github.com/stackitcloud/stackit-cli/internal/cmd/opensearch/credentials/describe" "github.com/stackitcloud/stackit-cli/internal/cmd/opensearch/credentials/list" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" - "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" "github.com/spf13/cobra" ) -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "credentials", Short: "Provides functionality for OpenSearch credentials", @@ -20,13 +20,13 @@ func NewCmd(p *print.Printer) *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd, p) + addSubcommands(cmd, params) return cmd } -func addSubcommands(cmd *cobra.Command, p *print.Printer) { - cmd.AddCommand(create.NewCmd(p)) - cmd.AddCommand(delete.NewCmd(p)) - cmd.AddCommand(describe.NewCmd(p)) - cmd.AddCommand(list.NewCmd(p)) +func addSubcommands(cmd *cobra.Command, params *params.CmdParams) { + cmd.AddCommand(create.NewCmd(params)) + cmd.AddCommand(delete.NewCmd(params)) + cmd.AddCommand(describe.NewCmd(params)) + cmd.AddCommand(list.NewCmd(params)) } diff --git a/internal/cmd/opensearch/credentials/delete/delete.go b/internal/cmd/opensearch/credentials/delete/delete.go index 1931ad0fc..97e2fc14b 100644 --- a/internal/cmd/opensearch/credentials/delete/delete.go +++ b/internal/cmd/opensearch/credentials/delete/delete.go @@ -4,6 +4,7 @@ import ( "context" "fmt" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -30,7 +31,7 @@ type inputModel struct { CredentialsId string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("delete %s", credentialsIdArg), Short: "Deletes credentials of an OpenSearch instance", @@ -43,32 +44,32 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } instanceLabel, err := opensearchUtils.GetInstanceName(ctx, apiClient, model.ProjectId, model.InstanceId) if err != nil { - p.Debug(print.ErrorLevel, "get instance name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get instance name: %v", err) instanceLabel = model.InstanceId } credentialsLabel, err := opensearchUtils.GetCredentialsUsername(ctx, apiClient, model.ProjectId, model.InstanceId, model.CredentialsId) if err != nil { - p.Debug(print.ErrorLevel, "get credentials user name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get credentials user name: %v", err) credentialsLabel = model.CredentialsId } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to delete credentials %s of instance %q? (This cannot be undone)", credentialsLabel, instanceLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -81,7 +82,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("delete OpenSearch credentials: %w", err) } - p.Info("Deleted credentials %s of instance %q\n", credentialsLabel, instanceLabel) + params.Printer.Info("Deleted credentials %s of instance %q\n", credentialsLabel, instanceLabel) return nil }, } diff --git a/internal/cmd/opensearch/credentials/delete/delete_test.go b/internal/cmd/opensearch/credentials/delete/delete_test.go index 4dcdc9dfe..350175fd2 100644 --- a/internal/cmd/opensearch/credentials/delete/delete_test.go +++ b/internal/cmd/opensearch/credentials/delete/delete_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" @@ -165,7 +166,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/opensearch/credentials/describe/describe.go b/internal/cmd/opensearch/credentials/describe/describe.go index fb8012012..d70edf933 100644 --- a/internal/cmd/opensearch/credentials/describe/describe.go +++ b/internal/cmd/opensearch/credentials/describe/describe.go @@ -6,6 +6,7 @@ import ( "fmt" "github.com/goccy/go-yaml" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -32,7 +33,7 @@ type inputModel struct { CredentialsId string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("describe %s", credentialsIdArg), Short: "Shows details of credentials of an OpenSearch instance", @@ -48,13 +49,13 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -66,7 +67,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("describe OpenSearch credentials: %w", err) } - return outputResult(p, model.OutputFormat, resp) + return outputResult(params.Printer, model.OutputFormat, resp) }, } configureFlags(cmd) diff --git a/internal/cmd/opensearch/credentials/describe/describe_test.go b/internal/cmd/opensearch/credentials/describe/describe_test.go index 3618a4cc7..50ce776b5 100644 --- a/internal/cmd/opensearch/credentials/describe/describe_test.go +++ b/internal/cmd/opensearch/credentials/describe/describe_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" @@ -165,7 +166,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -268,7 +269,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.credentials); (err != nil) != tt.wantErr { diff --git a/internal/cmd/opensearch/credentials/list/list.go b/internal/cmd/opensearch/credentials/list/list.go index b58c99796..b4d200b1e 100644 --- a/internal/cmd/opensearch/credentials/list/list.go +++ b/internal/cmd/opensearch/credentials/list/list.go @@ -5,6 +5,7 @@ import ( "encoding/json" "fmt" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -32,7 +33,7 @@ type inputModel struct { Limit *int64 } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "list", Short: "Lists all credentials' IDs for an OpenSearch instance", @@ -51,13 +52,13 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -72,10 +73,10 @@ func NewCmd(p *print.Printer) *cobra.Command { if len(credentials) == 0 { instanceLabel, err := opensearchUtils.GetInstanceName(ctx, apiClient, model.ProjectId, model.InstanceId) if err != nil { - p.Debug(print.ErrorLevel, "get instance name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get instance name: %v", err) instanceLabel = model.InstanceId } - p.Info("No credentials found for instance %q\n", instanceLabel) + params.Printer.Info("No credentials found for instance %q\n", instanceLabel) return nil } @@ -83,7 +84,7 @@ func NewCmd(p *print.Printer) *cobra.Command { if model.Limit != nil && len(credentials) > int(*model.Limit) { credentials = credentials[:*model.Limit] } - return outputResult(p, model.OutputFormat, credentials) + return outputResult(params.Printer, model.OutputFormat, credentials) }, } configureFlags(cmd) diff --git a/internal/cmd/opensearch/credentials/list/list_test.go b/internal/cmd/opensearch/credentials/list/list_test.go index d6b041f57..bfe0ea554 100644 --- a/internal/cmd/opensearch/credentials/list/list_test.go +++ b/internal/cmd/opensearch/credentials/list/list_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -137,7 +138,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -239,7 +240,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.credentials); (err != nil) != tt.wantErr { diff --git a/internal/cmd/opensearch/instance/create/create.go b/internal/cmd/opensearch/instance/create/create.go index 62d447f82..336a2e5ab 100644 --- a/internal/cmd/opensearch/instance/create/create.go +++ b/internal/cmd/opensearch/instance/create/create.go @@ -8,6 +8,7 @@ import ( "strings" "github.com/goccy/go-yaml" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" cliErr "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -57,7 +58,7 @@ type inputModel struct { PlanId *string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "create", Short: "Creates an OpenSearch instance", @@ -76,26 +77,26 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } - projectLabel, err := projectname.GetProjectName(ctx, p, cmd) + projectLabel, err := projectname.GetProjectName(ctx, params.Printer, cmd) if err != nil { - p.Debug(print.ErrorLevel, "get project name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get project name: %v", err) projectLabel = model.ProjectId } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to create an OpenSearch instance for project %q?", projectLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -118,7 +119,7 @@ func NewCmd(p *print.Printer) *cobra.Command { // Wait for async operation, if async mode not enabled if !model.Async { - s := spinner.New(p) + s := spinner.New(params.Printer) s.Start("Creating instance") _, err = wait.CreateInstanceWaitHandler(ctx, apiClient, model.ProjectId, instanceId).WaitWithContext(ctx) if err != nil { @@ -127,7 +128,7 @@ func NewCmd(p *print.Printer) *cobra.Command { s.Stop() } - return outputResult(p, model.OutputFormat, model.Async, projectLabel, instanceId, resp) + return outputResult(params.Printer, model.OutputFormat, model.Async, projectLabel, instanceId, resp) }, } configureFlags(cmd) diff --git a/internal/cmd/opensearch/instance/create/create_test.go b/internal/cmd/opensearch/instance/create/create_test.go index 3aceed814..e6028b4d9 100644 --- a/internal/cmd/opensearch/instance/create/create_test.go +++ b/internal/cmd/opensearch/instance/create/create_test.go @@ -5,6 +5,7 @@ import ( "fmt" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -277,7 +278,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -516,7 +517,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.async, tt.args.projectLabel, tt.args.instanceId, tt.args.resp); (err != nil) != tt.wantErr { diff --git a/internal/cmd/opensearch/instance/delete/delete.go b/internal/cmd/opensearch/instance/delete/delete.go index 40dc60461..cd7f159d3 100644 --- a/internal/cmd/opensearch/instance/delete/delete.go +++ b/internal/cmd/opensearch/instance/delete/delete.go @@ -4,6 +4,7 @@ import ( "context" "fmt" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -28,7 +29,7 @@ type inputModel struct { InstanceId string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("delete %s", instanceIdArg), Short: "Deletes an OpenSearch instance", @@ -41,26 +42,26 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } instanceLabel, err := opensearchUtils.GetInstanceName(ctx, apiClient, model.ProjectId, model.InstanceId) if err != nil { - p.Debug(print.ErrorLevel, "get instance name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get instance name: %v", err) instanceLabel = model.InstanceId } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to delete instance %q? (This cannot be undone)", instanceLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -75,7 +76,7 @@ func NewCmd(p *print.Printer) *cobra.Command { // Wait for async operation, if async mode not enabled if !model.Async { - s := spinner.New(p) + s := spinner.New(params.Printer) s.Start("Deleting instance") _, err = wait.DeleteInstanceWaitHandler(ctx, apiClient, model.ProjectId, model.InstanceId).WaitWithContext(ctx) if err != nil { @@ -88,7 +89,7 @@ func NewCmd(p *print.Printer) *cobra.Command { if model.Async { operationState = "Triggered deletion of" } - p.Info("%s instance %q\n", operationState, instanceLabel) + params.Printer.Info("%s instance %q\n", operationState, instanceLabel) return nil }, } diff --git a/internal/cmd/opensearch/instance/delete/delete_test.go b/internal/cmd/opensearch/instance/delete/delete_test.go index 7454c04e0..217ff1b1d 100644 --- a/internal/cmd/opensearch/instance/delete/delete_test.go +++ b/internal/cmd/opensearch/instance/delete/delete_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" @@ -138,7 +139,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/opensearch/instance/describe/describe.go b/internal/cmd/opensearch/instance/describe/describe.go index 1358aef39..d8722d9d5 100644 --- a/internal/cmd/opensearch/instance/describe/describe.go +++ b/internal/cmd/opensearch/instance/describe/describe.go @@ -6,6 +6,7 @@ import ( "fmt" "github.com/goccy/go-yaml" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -30,7 +31,7 @@ type inputModel struct { InstanceId string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("describe %s", instanceIdArg), Short: "Shows details of an OpenSearch instance", @@ -46,12 +47,12 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -63,7 +64,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("read OpenSearch instance: %w", err) } - return outputResult(p, model.OutputFormat, resp) + return outputResult(params.Printer, model.OutputFormat, resp) }, } return cmd diff --git a/internal/cmd/opensearch/instance/describe/describe_test.go b/internal/cmd/opensearch/instance/describe/describe_test.go index d4b58656f..6e17a8e85 100644 --- a/internal/cmd/opensearch/instance/describe/describe_test.go +++ b/internal/cmd/opensearch/instance/describe/describe_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" @@ -138,7 +139,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -241,7 +242,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.instance); (err != nil) != tt.wantErr { diff --git a/internal/cmd/opensearch/instance/instance.go b/internal/cmd/opensearch/instance/instance.go index 649d439ce..05d92bbc6 100644 --- a/internal/cmd/opensearch/instance/instance.go +++ b/internal/cmd/opensearch/instance/instance.go @@ -6,14 +6,14 @@ import ( "github.com/stackitcloud/stackit-cli/internal/cmd/opensearch/instance/describe" "github.com/stackitcloud/stackit-cli/internal/cmd/opensearch/instance/list" "github.com/stackitcloud/stackit-cli/internal/cmd/opensearch/instance/update" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" - "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" "github.com/spf13/cobra" ) -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "instance", Short: "Provides functionality for OpenSearch instances", @@ -21,14 +21,14 @@ func NewCmd(p *print.Printer) *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd, p) + addSubcommands(cmd, params) return cmd } -func addSubcommands(cmd *cobra.Command, p *print.Printer) { - cmd.AddCommand(create.NewCmd(p)) - cmd.AddCommand(delete.NewCmd(p)) - cmd.AddCommand(describe.NewCmd(p)) - cmd.AddCommand(list.NewCmd(p)) - cmd.AddCommand(update.NewCmd(p)) +func addSubcommands(cmd *cobra.Command, params *params.CmdParams) { + cmd.AddCommand(create.NewCmd(params)) + cmd.AddCommand(delete.NewCmd(params)) + cmd.AddCommand(describe.NewCmd(params)) + cmd.AddCommand(list.NewCmd(params)) + cmd.AddCommand(update.NewCmd(params)) } diff --git a/internal/cmd/opensearch/instance/list/list.go b/internal/cmd/opensearch/instance/list/list.go index 024619beb..0b0aa93c8 100644 --- a/internal/cmd/opensearch/instance/list/list.go +++ b/internal/cmd/opensearch/instance/list/list.go @@ -7,6 +7,7 @@ import ( "github.com/goccy/go-yaml" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -29,7 +30,7 @@ type inputModel struct { Limit *int64 } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "list", Short: "Lists all OpenSearch instances", @@ -48,13 +49,13 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -67,12 +68,12 @@ func NewCmd(p *print.Printer) *cobra.Command { } instances := *resp.Instances if len(instances) == 0 { - projectLabel, err := projectname.GetProjectName(ctx, p, cmd) + projectLabel, err := projectname.GetProjectName(ctx, params.Printer, cmd) if err != nil { - p.Debug(print.ErrorLevel, "get project name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get project name: %v", err) projectLabel = model.ProjectId } - p.Info("No instances found for project %q\n", projectLabel) + params.Printer.Info("No instances found for project %q\n", projectLabel) return nil } @@ -81,7 +82,7 @@ func NewCmd(p *print.Printer) *cobra.Command { instances = instances[:*model.Limit] } - return outputResult(p, model.OutputFormat, instances) + return outputResult(params.Printer, model.OutputFormat, instances) }, } diff --git a/internal/cmd/opensearch/instance/list/list_test.go b/internal/cmd/opensearch/instance/list/list_test.go index baa22ac9f..766115689 100644 --- a/internal/cmd/opensearch/instance/list/list_test.go +++ b/internal/cmd/opensearch/instance/list/list_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -218,7 +219,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.instances); (err != nil) != tt.wantErr { diff --git a/internal/cmd/opensearch/instance/update/update.go b/internal/cmd/opensearch/instance/update/update.go index 57072b0a8..85ba3f133 100644 --- a/internal/cmd/opensearch/instance/update/update.go +++ b/internal/cmd/opensearch/instance/update/update.go @@ -6,6 +6,7 @@ import ( "fmt" "strings" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" cliErr "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -56,7 +57,7 @@ type inputModel struct { PlanId *string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("update %s", instanceIdArg), Short: "Updates an OpenSearch instance", @@ -72,26 +73,26 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } instanceLabel, err := opensearchUtils.GetInstanceName(ctx, apiClient, model.ProjectId, model.InstanceId) if err != nil { - p.Debug(print.ErrorLevel, "get instance name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get instance name: %v", err) instanceLabel = model.InstanceId } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to update instance %q?", instanceLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -114,7 +115,7 @@ func NewCmd(p *print.Printer) *cobra.Command { // Wait for async operation, if async mode not enabled if !model.Async { - s := spinner.New(p) + s := spinner.New(params.Printer) s.Start("Updating instance") _, err = wait.PartialUpdateInstanceWaitHandler(ctx, apiClient, model.ProjectId, instanceId).WaitWithContext(ctx) if err != nil { @@ -127,7 +128,7 @@ func NewCmd(p *print.Printer) *cobra.Command { if model.Async { operationState = "Triggered update of" } - p.Info("%s instance %q\n", operationState, instanceLabel) + params.Printer.Info("%s instance %q\n", operationState, instanceLabel) return nil }, } diff --git a/internal/cmd/opensearch/instance/update/update_test.go b/internal/cmd/opensearch/instance/update/update_test.go index 478199985..70168bbc2 100644 --- a/internal/cmd/opensearch/instance/update/update_test.go +++ b/internal/cmd/opensearch/instance/update/update_test.go @@ -5,6 +5,7 @@ import ( "fmt" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -294,7 +295,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/opensearch/opensearch.go b/internal/cmd/opensearch/opensearch.go index 08103e63c..766065ed2 100644 --- a/internal/cmd/opensearch/opensearch.go +++ b/internal/cmd/opensearch/opensearch.go @@ -4,14 +4,14 @@ import ( "github.com/stackitcloud/stackit-cli/internal/cmd/opensearch/credentials" "github.com/stackitcloud/stackit-cli/internal/cmd/opensearch/instance" "github.com/stackitcloud/stackit-cli/internal/cmd/opensearch/plans" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" - "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" "github.com/spf13/cobra" ) -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "opensearch", Short: "Provides functionality for OpenSearch", @@ -19,12 +19,12 @@ func NewCmd(p *print.Printer) *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd, p) + addSubcommands(cmd, params) return cmd } -func addSubcommands(cmd *cobra.Command, p *print.Printer) { - cmd.AddCommand(instance.NewCmd(p)) - cmd.AddCommand(plans.NewCmd(p)) - cmd.AddCommand(credentials.NewCmd(p)) +func addSubcommands(cmd *cobra.Command, params *params.CmdParams) { + cmd.AddCommand(instance.NewCmd(params)) + cmd.AddCommand(plans.NewCmd(params)) + cmd.AddCommand(credentials.NewCmd(params)) } diff --git a/internal/cmd/opensearch/plans/plans.go b/internal/cmd/opensearch/plans/plans.go index 013e486e1..e2678f163 100644 --- a/internal/cmd/opensearch/plans/plans.go +++ b/internal/cmd/opensearch/plans/plans.go @@ -7,6 +7,7 @@ import ( "github.com/goccy/go-yaml" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -29,7 +30,7 @@ type inputModel struct { Limit *int64 } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "plans", Short: "Lists all OpenSearch service plans", @@ -48,13 +49,13 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -67,12 +68,12 @@ func NewCmd(p *print.Printer) *cobra.Command { } plans := *resp.Offerings if len(plans) == 0 { - projectLabel, err := projectname.GetProjectName(ctx, p, cmd) + projectLabel, err := projectname.GetProjectName(ctx, params.Printer, cmd) if err != nil { - p.Debug(print.ErrorLevel, "get project name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get project name: %v", err) projectLabel = model.ProjectId } - p.Info("No plans found for project %q\n", projectLabel) + params.Printer.Info("No plans found for project %q\n", projectLabel) return nil } @@ -81,7 +82,7 @@ func NewCmd(p *print.Printer) *cobra.Command { plans = plans[:*model.Limit] } - return outputResult(p, model.OutputFormat, plans) + return outputResult(params.Printer, model.OutputFormat, plans) }, } diff --git a/internal/cmd/opensearch/plans/plans_test.go b/internal/cmd/opensearch/plans/plans_test.go index a1d162d4a..0b4f95cec 100644 --- a/internal/cmd/opensearch/plans/plans_test.go +++ b/internal/cmd/opensearch/plans/plans_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -218,7 +219,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.plans); (err != nil) != tt.wantErr { diff --git a/internal/cmd/organization/member/add/add.go b/internal/cmd/organization/member/add/add.go index b03ae495f..246a7f27f 100644 --- a/internal/cmd/organization/member/add/add.go +++ b/internal/cmd/organization/member/add/add.go @@ -5,6 +5,7 @@ import ( "fmt" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" "github.com/stackitcloud/stackit-cli/internal/pkg/flags" @@ -32,7 +33,7 @@ type inputModel struct { Role *string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("add %s", subjectArg), Short: "Adds a member to an organization", @@ -51,21 +52,21 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to add the %s role to %s on organization with ID %q?", *model.Role, model.Subject, *model.OrganizationId) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -77,7 +78,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("add member: %w", err) } - p.Info("Member added") + params.Printer.Info("Member added") return nil }, } diff --git a/internal/cmd/organization/member/add/add_test.go b/internal/cmd/organization/member/add/add_test.go index 996439285..a98f06ecc 100644 --- a/internal/cmd/organization/member/add/add_test.go +++ b/internal/cmd/organization/member/add/add_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -125,7 +126,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/organization/member/list/list.go b/internal/cmd/organization/member/list/list.go index e3376d584..105f6b14a 100644 --- a/internal/cmd/organization/member/list/list.go +++ b/internal/cmd/organization/member/list/list.go @@ -8,6 +8,7 @@ import ( "github.com/goccy/go-yaml" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -38,7 +39,7 @@ type inputModel struct { SortBy string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "list", Short: "Lists members of an organization", @@ -57,13 +58,13 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -76,7 +77,7 @@ func NewCmd(p *print.Printer) *cobra.Command { } members := *resp.Members if len(members) == 0 { - p.Info("No members found for organization with ID %q\n", *model.OrganizationId) + params.Printer.Info("No members found for organization with ID %q\n", *model.OrganizationId) return nil } @@ -85,7 +86,7 @@ func NewCmd(p *print.Printer) *cobra.Command { members = members[:*model.Limit] } - return outputResult(p, model.OutputFormat, model.SortBy, members) + return outputResult(params.Printer, model.OutputFormat, model.SortBy, members) }, } configureFlags(cmd) diff --git a/internal/cmd/organization/member/list/list_test.go b/internal/cmd/organization/member/list/list_test.go index 2c73681c0..ff90898b2 100644 --- a/internal/cmd/organization/member/list/list_test.go +++ b/internal/cmd/organization/member/list/list_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -237,7 +238,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.sortBy, tt.args.members); (err != nil) != tt.wantErr { diff --git a/internal/cmd/organization/member/member.go b/internal/cmd/organization/member/member.go index cf7c515d8..fe2b67e5d 100644 --- a/internal/cmd/organization/member/member.go +++ b/internal/cmd/organization/member/member.go @@ -4,14 +4,14 @@ import ( "github.com/stackitcloud/stackit-cli/internal/cmd/organization/member/add" "github.com/stackitcloud/stackit-cli/internal/cmd/organization/member/list" "github.com/stackitcloud/stackit-cli/internal/cmd/organization/member/remove" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" - "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" "github.com/spf13/cobra" ) -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "member", Short: "Manages organization members", @@ -19,12 +19,12 @@ func NewCmd(p *print.Printer) *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd, p) + addSubcommands(cmd, params) return cmd } -func addSubcommands(cmd *cobra.Command, p *print.Printer) { - cmd.AddCommand(add.NewCmd(p)) - cmd.AddCommand(list.NewCmd(p)) - cmd.AddCommand(remove.NewCmd(p)) +func addSubcommands(cmd *cobra.Command, params *params.CmdParams) { + cmd.AddCommand(add.NewCmd(params)) + cmd.AddCommand(list.NewCmd(params)) + cmd.AddCommand(remove.NewCmd(params)) } diff --git a/internal/cmd/organization/member/remove/remove.go b/internal/cmd/organization/member/remove/remove.go index de9a09ceb..7ff3c9147 100644 --- a/internal/cmd/organization/member/remove/remove.go +++ b/internal/cmd/organization/member/remove/remove.go @@ -4,6 +4,7 @@ import ( "context" "fmt" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" "github.com/stackitcloud/stackit-cli/internal/pkg/flags" @@ -35,7 +36,7 @@ type inputModel struct { Force bool } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("remove %s", subjectArg), Short: "Removes a member from an organization", @@ -55,13 +56,13 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -71,7 +72,7 @@ func NewCmd(p *print.Printer) *cobra.Command { if model.Force { prompt = fmt.Sprintf("%s This will also remove other roles of the subject that would stop the removal of the requested role", prompt) } - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -84,7 +85,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("remove member: %w", err) } - p.Info("Member removed") + params.Printer.Info("Member removed") return nil }, } diff --git a/internal/cmd/organization/member/remove/remove_test.go b/internal/cmd/organization/member/remove/remove_test.go index fff1648f8..1b2a3e702 100644 --- a/internal/cmd/organization/member/remove/remove_test.go +++ b/internal/cmd/organization/member/remove/remove_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -138,7 +139,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/organization/organization.go b/internal/cmd/organization/organization.go index 3d34090fd..e7d1376b5 100644 --- a/internal/cmd/organization/organization.go +++ b/internal/cmd/organization/organization.go @@ -5,14 +5,14 @@ import ( "github.com/stackitcloud/stackit-cli/internal/cmd/organization/member" "github.com/stackitcloud/stackit-cli/internal/cmd/organization/role" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" - "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" "github.com/spf13/cobra" ) -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "organization", Short: "Manages organizations", @@ -23,11 +23,11 @@ func NewCmd(p *print.Printer) *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd, p) + addSubcommands(cmd, params) return cmd } -func addSubcommands(cmd *cobra.Command, p *print.Printer) { - cmd.AddCommand(member.NewCmd(p)) - cmd.AddCommand(role.NewCmd(p)) +func addSubcommands(cmd *cobra.Command, params *params.CmdParams) { + cmd.AddCommand(member.NewCmd(params)) + cmd.AddCommand(role.NewCmd(params)) } diff --git a/internal/cmd/organization/role/list/list.go b/internal/cmd/organization/role/list/list.go index c9e175323..c8925c90e 100644 --- a/internal/cmd/organization/role/list/list.go +++ b/internal/cmd/organization/role/list/list.go @@ -7,6 +7,7 @@ import ( "github.com/goccy/go-yaml" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -33,7 +34,7 @@ type inputModel struct { Limit *int64 } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "list", Short: "Lists roles and permissions of an organization", @@ -52,13 +53,13 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -71,7 +72,7 @@ func NewCmd(p *print.Printer) *cobra.Command { } roles := *resp.Roles if len(roles) == 0 { - p.Info("No roles found for organization with ID %q\n", *model.OrganizationId) + params.Printer.Info("No roles found for organization with ID %q\n", *model.OrganizationId) return nil } @@ -80,7 +81,7 @@ func NewCmd(p *print.Printer) *cobra.Command { roles = roles[:*model.Limit] } - return outputRolesResult(p, model.OutputFormat, roles) + return outputRolesResult(params.Printer, model.OutputFormat, roles) }, } configureFlags(cmd) diff --git a/internal/cmd/organization/role/list/list_test.go b/internal/cmd/organization/role/list/list_test.go index 1268fb039..5633b3f60 100644 --- a/internal/cmd/organization/role/list/list_test.go +++ b/internal/cmd/organization/role/list/list_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -199,7 +200,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputRolesResult(p, tt.args.outputFormat, tt.args.roles); (err != nil) != tt.wantErr { diff --git a/internal/cmd/organization/role/role.go b/internal/cmd/organization/role/role.go index f32189bf5..286783661 100644 --- a/internal/cmd/organization/role/role.go +++ b/internal/cmd/organization/role/role.go @@ -2,14 +2,14 @@ package role import ( "github.com/stackitcloud/stackit-cli/internal/cmd/organization/role/list" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" - "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" "github.com/spf13/cobra" ) -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "role", Short: "Manages organization roles", @@ -17,10 +17,10 @@ func NewCmd(p *print.Printer) *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd, p) + addSubcommands(cmd, params) return cmd } -func addSubcommands(cmd *cobra.Command, p *print.Printer) { - cmd.AddCommand(list.NewCmd(p)) +func addSubcommands(cmd *cobra.Command, params *params.CmdParams) { + cmd.AddCommand(list.NewCmd(params)) } diff --git a/internal/cmd/params/cmd_params.go b/internal/cmd/params/cmd_params.go new file mode 100644 index 000000000..572c80706 --- /dev/null +++ b/internal/cmd/params/cmd_params.go @@ -0,0 +1,10 @@ +package params + +import ( + "github.com/stackitcloud/stackit-cli/internal/pkg/print" +) + +type CmdParams struct { + Printer *print.Printer + CliVersion string +} diff --git a/internal/cmd/postgresflex/backup/backup.go b/internal/cmd/postgresflex/backup/backup.go index 85b08b5b2..bac6c4a72 100644 --- a/internal/cmd/postgresflex/backup/backup.go +++ b/internal/cmd/postgresflex/backup/backup.go @@ -1,17 +1,17 @@ package backup import ( + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/cmd/postgresflex/backup/describe" "github.com/stackitcloud/stackit-cli/internal/cmd/postgresflex/backup/list" updateschedule "github.com/stackitcloud/stackit-cli/internal/cmd/postgresflex/backup/update-schedule" "github.com/stackitcloud/stackit-cli/internal/pkg/args" - "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" "github.com/spf13/cobra" ) -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "backup", Short: "Provides functionality for PostgreSQL Flex instance backups", @@ -19,12 +19,12 @@ func NewCmd(p *print.Printer) *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd, p) + addSubcommands(cmd, params) return cmd } -func addSubcommands(cmd *cobra.Command, p *print.Printer) { - cmd.AddCommand(list.NewCmd(p)) - cmd.AddCommand(describe.NewCmd(p)) - cmd.AddCommand(updateschedule.NewCmd(p)) +func addSubcommands(cmd *cobra.Command, params *params.CmdParams) { + cmd.AddCommand(list.NewCmd(params)) + cmd.AddCommand(describe.NewCmd(params)) + cmd.AddCommand(updateschedule.NewCmd(params)) } diff --git a/internal/cmd/postgresflex/backup/describe/describe.go b/internal/cmd/postgresflex/backup/describe/describe.go index dbb8fe4ce..758fb955a 100644 --- a/internal/cmd/postgresflex/backup/describe/describe.go +++ b/internal/cmd/postgresflex/backup/describe/describe.go @@ -8,6 +8,7 @@ import ( "github.com/goccy/go-yaml" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -37,7 +38,7 @@ type inputModel struct { BackupId string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("describe %s", backupIdArg), Short: "Shows details of a backup for a PostgreSQL Flex instance", @@ -53,13 +54,13 @@ func NewCmd(p *print.Printer) *cobra.Command { Args: args.SingleArg(backupIdArg, nil), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -72,7 +73,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("describe backup for PostgreSQL Flex instance: %w", err) } - return outputResult(p, model.OutputFormat, *resp.Item) + return outputResult(params.Printer, model.OutputFormat, *resp.Item) }, } configureFlags(cmd) diff --git a/internal/cmd/postgresflex/backup/describe/describe_test.go b/internal/cmd/postgresflex/backup/describe/describe_test.go index 5c3479254..6e47eabb0 100644 --- a/internal/cmd/postgresflex/backup/describe/describe_test.go +++ b/internal/cmd/postgresflex/backup/describe/describe_test.go @@ -8,6 +8,7 @@ import ( "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" "github.com/google/uuid" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -262,7 +263,7 @@ func Test_outputResult(t *testing.T) { }}, false}, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { diff --git a/internal/cmd/postgresflex/backup/list/list.go b/internal/cmd/postgresflex/backup/list/list.go index 7e1d6c398..3f463a670 100644 --- a/internal/cmd/postgresflex/backup/list/list.go +++ b/internal/cmd/postgresflex/backup/list/list.go @@ -6,6 +6,7 @@ import ( "fmt" "github.com/goccy/go-yaml" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -38,7 +39,7 @@ type inputModel struct { Limit *int64 } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "list", Short: "Lists all backups which are available for a PostgreSQL Flex instance", @@ -57,20 +58,20 @@ func NewCmd(p *print.Printer) *cobra.Command { Args: args.NoArgs, RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } instanceLabel, err := postgresflexUtils.GetInstanceName(ctx, apiClient, model.ProjectId, model.Region, *model.InstanceId) if err != nil { - p.Debug(print.ErrorLevel, "get instance name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get instance name: %v", err) instanceLabel = *model.InstanceId } @@ -91,7 +92,7 @@ func NewCmd(p *print.Printer) *cobra.Command { backups = backups[:*model.Limit] } - return outputResult(p, model.OutputFormat, backups) + return outputResult(params.Printer, model.OutputFormat, backups) }, } diff --git a/internal/cmd/postgresflex/backup/list/list_test.go b/internal/cmd/postgresflex/backup/list/list_test.go index f34d184cf..9dee772fe 100644 --- a/internal/cmd/postgresflex/backup/list/list_test.go +++ b/internal/cmd/postgresflex/backup/list/list_test.go @@ -8,6 +8,7 @@ import ( "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" "github.com/google/uuid" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -243,7 +244,7 @@ func Test_outputResult(t *testing.T) { }, false}, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { diff --git a/internal/cmd/postgresflex/backup/update-schedule/update_schedule.go b/internal/cmd/postgresflex/backup/update-schedule/update_schedule.go index 9d5ff6626..a9f3bb70d 100644 --- a/internal/cmd/postgresflex/backup/update-schedule/update_schedule.go +++ b/internal/cmd/postgresflex/backup/update-schedule/update_schedule.go @@ -5,6 +5,7 @@ import ( "fmt" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" cliErr "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -28,7 +29,7 @@ type inputModel struct { BackupSchedule *string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "update-schedule", Short: "Updates backup schedule for a PostgreSQL Flex instance", @@ -43,26 +44,26 @@ func NewCmd(p *print.Printer) *cobra.Command { RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } instanceLabel, err := postgresflexUtils.GetInstanceName(ctx, apiClient, model.ProjectId, model.Region, *model.InstanceId) if err != nil { - p.Debug(print.ErrorLevel, "get instance name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get instance name: %v", err) instanceLabel = *model.InstanceId } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to update backup schedule of instance %q?", instanceLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } diff --git a/internal/cmd/postgresflex/instance/clone/clone.go b/internal/cmd/postgresflex/instance/clone/clone.go index 1d753cfeb..b8a31cd4e 100644 --- a/internal/cmd/postgresflex/instance/clone/clone.go +++ b/internal/cmd/postgresflex/instance/clone/clone.go @@ -6,6 +6,7 @@ import ( "fmt" "github.com/goccy/go-yaml" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" cliErr "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -40,7 +41,7 @@ type inputModel struct { RecoveryDate *string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("clone %s", instanceIdArg), Short: "Clones a PostgreSQL Flex instance", @@ -61,26 +62,26 @@ func NewCmd(p *print.Printer) *cobra.Command { RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } instanceLabel, err := postgresflexUtils.GetInstanceName(ctx, apiClient, model.ProjectId, model.Region, model.InstanceId) if err != nil { - p.Debug(print.ErrorLevel, "get instance name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get instance name: %v", err) instanceLabel = model.InstanceId } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to clone instance %q?", instanceLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -99,7 +100,7 @@ func NewCmd(p *print.Printer) *cobra.Command { // Wait for async operation, if async mode not enabled if !model.Async { - s := spinner.New(p) + s := spinner.New(params.Printer) s.Start("Cloning instance") _, err = wait.CreateInstanceWaitHandler(ctx, apiClient, model.ProjectId, model.Region, instanceId).WaitWithContext(ctx) if err != nil { @@ -108,7 +109,7 @@ func NewCmd(p *print.Printer) *cobra.Command { s.Stop() } - return outputResult(p, model.OutputFormat, model.Async, instanceLabel, instanceId, resp) + return outputResult(params.Printer, model.OutputFormat, model.Async, instanceLabel, instanceId, resp) }, } configureFlags(cmd) diff --git a/internal/cmd/postgresflex/instance/clone/clone_test.go b/internal/cmd/postgresflex/instance/clone/clone_test.go index eed05d8d3..04dbd1954 100644 --- a/internal/cmd/postgresflex/instance/clone/clone_test.go +++ b/internal/cmd/postgresflex/instance/clone/clone_test.go @@ -9,6 +9,7 @@ import ( "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" "github.com/google/uuid" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -302,7 +303,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -553,7 +554,7 @@ func Test_outputResult(t *testing.T) { }, false}, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { diff --git a/internal/cmd/postgresflex/instance/create/create.go b/internal/cmd/postgresflex/instance/create/create.go index 191d982f5..32301a4d4 100644 --- a/internal/cmd/postgresflex/instance/create/create.go +++ b/internal/cmd/postgresflex/instance/create/create.go @@ -7,6 +7,7 @@ import ( "fmt" "github.com/goccy/go-yaml" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" cliErr "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -57,7 +58,7 @@ type inputModel struct { Type *string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "create", Short: "Creates a PostgreSQL Flex instance", @@ -77,26 +78,26 @@ func NewCmd(p *print.Printer) *cobra.Command { RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } - projectLabel, err := projectname.GetProjectName(ctx, p, cmd) + projectLabel, err := projectname.GetProjectName(ctx, params.Printer, cmd) if err != nil { - p.Debug(print.ErrorLevel, "get project name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get project name: %v", err) projectLabel = model.ProjectId } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to create a PostgreSQL Flex instance for project %q?", projectLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -124,7 +125,7 @@ func NewCmd(p *print.Printer) *cobra.Command { // Wait for async operation, if async mode not enabled if !model.Async { - s := spinner.New(p) + s := spinner.New(params.Printer) s.Start("Creating instance") _, err = wait.CreateInstanceWaitHandler(ctx, apiClient, model.ProjectId, model.Region, instanceId).WaitWithContext(ctx) if err != nil { @@ -133,7 +134,7 @@ func NewCmd(p *print.Printer) *cobra.Command { s.Stop() } - return outputResult(p, model.OutputFormat, model.Async, projectLabel, instanceId, resp) + return outputResult(params.Printer, model.OutputFormat, model.Async, projectLabel, instanceId, resp) }, } configureFlags(cmd) diff --git a/internal/cmd/postgresflex/instance/create/create_test.go b/internal/cmd/postgresflex/instance/create/create_test.go index 6adbfb09b..32d63d5d8 100644 --- a/internal/cmd/postgresflex/instance/create/create_test.go +++ b/internal/cmd/postgresflex/instance/create/create_test.go @@ -8,6 +8,7 @@ import ( "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" "github.com/google/uuid" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -250,7 +251,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -547,7 +548,7 @@ func Test_outputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.async, tt.args.projectLabel, tt.args.instanceId, tt.args.resp); (err != nil) != tt.wantErr { diff --git a/internal/cmd/postgresflex/instance/delete/delete.go b/internal/cmd/postgresflex/instance/delete/delete.go index 4c9077a6a..acf78c5ba 100644 --- a/internal/cmd/postgresflex/instance/delete/delete.go +++ b/internal/cmd/postgresflex/instance/delete/delete.go @@ -4,6 +4,7 @@ import ( "context" "fmt" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -32,7 +33,7 @@ type inputModel struct { ForceDelete bool } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("delete %s", instanceIdArg), Short: "Deletes a PostgreSQL Flex instance", @@ -52,26 +53,26 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } instanceLabel, err := postgresflexUtils.GetInstanceName(ctx, apiClient, model.ProjectId, model.Region, model.InstanceId) if err != nil { - p.Debug(print.ErrorLevel, "get instance name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get instance name: %v", err) instanceLabel = model.InstanceId } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to delete instance %q? (This cannot be undone)", instanceLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -92,7 +93,7 @@ func NewCmd(p *print.Printer) *cobra.Command { // Wait for async operation, if async mode not enabled if !model.Async { - s := spinner.New(p) + s := spinner.New(params.Printer) s.Start("Deleting instance") _, err = wait.DeleteInstanceWaitHandler(ctx, apiClient, model.ProjectId, model.Region, model.InstanceId).WaitWithContext(ctx) if err != nil { @@ -112,7 +113,7 @@ func NewCmd(p *print.Printer) *cobra.Command { // Wait for async operation, if async mode not enabled if !model.Async { - s := spinner.New(p) + s := spinner.New(params.Printer) s.Start("Forcing deletion of instance") _, err = wait.ForceDeleteInstanceWaitHandler(ctx, apiClient, model.ProjectId, model.Region, model.InstanceId).WaitWithContext(ctx) if err != nil { @@ -132,7 +133,7 @@ func NewCmd(p *print.Printer) *cobra.Command { operationState = "Triggered forced deletion of" } } - p.Info("%s instance %q\n", operationState, instanceLabel) + params.Printer.Info("%s instance %q\n", operationState, instanceLabel) return nil }, } diff --git a/internal/cmd/postgresflex/instance/delete/delete_test.go b/internal/cmd/postgresflex/instance/delete/delete_test.go index 23f662a5d..13f33bfbd 100644 --- a/internal/cmd/postgresflex/instance/delete/delete_test.go +++ b/internal/cmd/postgresflex/instance/delete/delete_test.go @@ -5,6 +5,7 @@ import ( "fmt" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -171,7 +172,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/postgresflex/instance/describe/describe.go b/internal/cmd/postgresflex/instance/describe/describe.go index 2d59f5306..13604fb58 100644 --- a/internal/cmd/postgresflex/instance/describe/describe.go +++ b/internal/cmd/postgresflex/instance/describe/describe.go @@ -6,6 +6,7 @@ import ( "fmt" "github.com/goccy/go-yaml" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -31,7 +32,7 @@ type inputModel struct { InstanceId string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("describe %s", instanceIdArg), Short: "Shows details of a PostgreSQL Flex instance", @@ -47,12 +48,12 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -64,7 +65,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("read PostgreSQL Flex instance: %w", err) } - return outputResult(p, model.OutputFormat, resp.Item) + return outputResult(params.Printer, model.OutputFormat, resp.Item) }, } return cmd diff --git a/internal/cmd/postgresflex/instance/describe/describe_test.go b/internal/cmd/postgresflex/instance/describe/describe_test.go index c6945faa1..69e100822 100644 --- a/internal/cmd/postgresflex/instance/describe/describe_test.go +++ b/internal/cmd/postgresflex/instance/describe/describe_test.go @@ -7,6 +7,7 @@ import ( "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" "github.com/google/uuid" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-sdk-go/services/postgresflex" @@ -138,7 +139,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -259,7 +260,7 @@ func Test_outputResult(t *testing.T) { }, false}, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { diff --git a/internal/cmd/postgresflex/instance/instance.go b/internal/cmd/postgresflex/instance/instance.go index 4a005c13a..f43979195 100644 --- a/internal/cmd/postgresflex/instance/instance.go +++ b/internal/cmd/postgresflex/instance/instance.go @@ -1,6 +1,7 @@ package instance import ( + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/cmd/postgresflex/instance/clone" "github.com/stackitcloud/stackit-cli/internal/cmd/postgresflex/instance/create" "github.com/stackitcloud/stackit-cli/internal/cmd/postgresflex/instance/delete" @@ -8,13 +9,12 @@ import ( "github.com/stackitcloud/stackit-cli/internal/cmd/postgresflex/instance/list" "github.com/stackitcloud/stackit-cli/internal/cmd/postgresflex/instance/update" "github.com/stackitcloud/stackit-cli/internal/pkg/args" - "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" "github.com/spf13/cobra" ) -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "instance", Short: "Provides functionality for PostgreSQL Flex instances", @@ -22,15 +22,15 @@ func NewCmd(p *print.Printer) *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd, p) + addSubcommands(cmd, params) return cmd } -func addSubcommands(cmd *cobra.Command, p *print.Printer) { - cmd.AddCommand(list.NewCmd(p)) - cmd.AddCommand(create.NewCmd(p)) - cmd.AddCommand(describe.NewCmd(p)) - cmd.AddCommand(update.NewCmd(p)) - cmd.AddCommand(delete.NewCmd(p)) - cmd.AddCommand(clone.NewCmd(p)) +func addSubcommands(cmd *cobra.Command, params *params.CmdParams) { + cmd.AddCommand(list.NewCmd(params)) + cmd.AddCommand(create.NewCmd(params)) + cmd.AddCommand(describe.NewCmd(params)) + cmd.AddCommand(update.NewCmd(params)) + cmd.AddCommand(delete.NewCmd(params)) + cmd.AddCommand(clone.NewCmd(params)) } diff --git a/internal/cmd/postgresflex/instance/list/list.go b/internal/cmd/postgresflex/instance/list/list.go index 040a9de6b..d3870566b 100644 --- a/internal/cmd/postgresflex/instance/list/list.go +++ b/internal/cmd/postgresflex/instance/list/list.go @@ -7,6 +7,7 @@ import ( "github.com/goccy/go-yaml" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -31,7 +32,7 @@ type inputModel struct { Limit *int64 } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "list", Short: "Lists all PostgreSQL Flex instances", @@ -50,13 +51,13 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -68,12 +69,12 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("get PostgreSQL Flex instances: %w", err) } if resp.Items == nil || len(*resp.Items) == 0 { - projectLabel, err := projectname.GetProjectName(ctx, p, cmd) + projectLabel, err := projectname.GetProjectName(ctx, params.Printer, cmd) if err != nil { - p.Debug(print.ErrorLevel, "get project name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get project name: %v", err) projectLabel = model.ProjectId } - p.Info("No instances found for project %q\n", projectLabel) + params.Printer.Info("No instances found for project %q\n", projectLabel) return nil } instances := *resp.Items @@ -83,7 +84,7 @@ func NewCmd(p *print.Printer) *cobra.Command { instances = instances[:*model.Limit] } - return outputResult(p, model.OutputFormat, instances) + return outputResult(params.Printer, model.OutputFormat, instances) }, } diff --git a/internal/cmd/postgresflex/instance/list/list_test.go b/internal/cmd/postgresflex/instance/list/list_test.go index 09880f92c..3cfc85652 100644 --- a/internal/cmd/postgresflex/instance/list/list_test.go +++ b/internal/cmd/postgresflex/instance/list/list_test.go @@ -8,6 +8,7 @@ import ( "github.com/google/go-cmp/cmp/cmpopts" "github.com/google/uuid" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -218,7 +219,7 @@ func Test_outputResult(t *testing.T) { }}, false}, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { diff --git a/internal/cmd/postgresflex/instance/update/update.go b/internal/cmd/postgresflex/instance/update/update.go index 66022fb1d..980a358df 100644 --- a/internal/cmd/postgresflex/instance/update/update.go +++ b/internal/cmd/postgresflex/instance/update/update.go @@ -7,6 +7,7 @@ import ( "fmt" "github.com/goccy/go-yaml" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" cliErr "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -54,7 +55,7 @@ type inputModel struct { Type *string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("update %s", instanceIdArg), Short: "Updates a PostgreSQL Flex instance", @@ -71,26 +72,26 @@ func NewCmd(p *print.Printer) *cobra.Command { RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } instanceLabel, err := postgresflexUtils.GetInstanceName(ctx, apiClient, model.ProjectId, model.Region, model.InstanceId) if err != nil { - p.Debug(print.ErrorLevel, "get instance name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get instance name: %v", err) instanceLabel = model.InstanceId } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to update instance %q? (This may cause downtime)", instanceLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -109,7 +110,7 @@ func NewCmd(p *print.Printer) *cobra.Command { // Wait for async operation, if async mode not enabled if !model.Async { - s := spinner.New(p) + s := spinner.New(params.Printer) s.Start("Updating instance") _, err = wait.PartialUpdateInstanceWaitHandler(ctx, apiClient, model.ProjectId, model.Region, instanceId).WaitWithContext(ctx) if err != nil { @@ -118,7 +119,7 @@ func NewCmd(p *print.Printer) *cobra.Command { s.Stop() } - return outputResult(p, model.OutputFormat, model.Async, instanceLabel, resp) + return outputResult(params.Printer, model.OutputFormat, model.Async, instanceLabel, resp) }, } configureFlags(cmd) diff --git a/internal/cmd/postgresflex/instance/update/update_test.go b/internal/cmd/postgresflex/instance/update/update_test.go index 46b157bbe..0c4ed96b6 100644 --- a/internal/cmd/postgresflex/instance/update/update_test.go +++ b/internal/cmd/postgresflex/instance/update/update_test.go @@ -8,6 +8,7 @@ import ( "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" "github.com/google/uuid" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -282,7 +283,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -620,7 +621,7 @@ func Test_outputResult(t *testing.T) { }, false}, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { diff --git a/internal/cmd/postgresflex/options/options.go b/internal/cmd/postgresflex/options/options.go index 8d53c2017..cbf87326d 100644 --- a/internal/cmd/postgresflex/options/options.go +++ b/internal/cmd/postgresflex/options/options.go @@ -5,6 +5,8 @@ import ( "encoding/json" "fmt" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" + "github.com/goccy/go-yaml" "github.com/spf13/cobra" "github.com/stackitcloud/stackit-cli/internal/pkg/args" @@ -45,7 +47,7 @@ type flavorStorages struct { Storages *postgresflex.ListStoragesResponse `json:"storages"` } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "options", Short: "Lists PostgreSQL Flex options", @@ -64,19 +66,19 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } // Call API - err = buildAndExecuteRequest(ctx, p, model, apiClient) + err = buildAndExecuteRequest(ctx, params.Printer, model, apiClient) if err != nil { return fmt.Errorf("get PostgreSQL Flex options: %w", err) } diff --git a/internal/cmd/postgresflex/options/options_test.go b/internal/cmd/postgresflex/options/options_test.go index 3854aeaca..7643dad69 100644 --- a/internal/cmd/postgresflex/options/options_test.go +++ b/internal/cmd/postgresflex/options/options_test.go @@ -6,6 +6,7 @@ import ( "testing" "github.com/google/go-cmp/cmp" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -166,7 +167,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -290,7 +291,7 @@ func TestBuildAndExecuteRequest(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := &print.Printer{} - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) p.Cmd = cmd client := &postgresFlexClientMocked{ listFlavorsFails: tt.listFlavorsFails, @@ -360,7 +361,7 @@ func Test_outputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.model, tt.args.flavors, tt.args.versions, tt.args.storages); (err != nil) != tt.wantErr { diff --git a/internal/cmd/postgresflex/postgresflex.go b/internal/cmd/postgresflex/postgresflex.go index 7820cded2..3a86086c2 100644 --- a/internal/cmd/postgresflex/postgresflex.go +++ b/internal/cmd/postgresflex/postgresflex.go @@ -1,18 +1,18 @@ package postgresflex import ( + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/cmd/postgresflex/backup" "github.com/stackitcloud/stackit-cli/internal/cmd/postgresflex/instance" "github.com/stackitcloud/stackit-cli/internal/cmd/postgresflex/options" "github.com/stackitcloud/stackit-cli/internal/cmd/postgresflex/user" "github.com/stackitcloud/stackit-cli/internal/pkg/args" - "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" "github.com/spf13/cobra" ) -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "postgresflex", Aliases: []string{"postgresqlflex"}, @@ -21,13 +21,13 @@ func NewCmd(p *print.Printer) *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd, p) + addSubcommands(cmd, params) return cmd } -func addSubcommands(cmd *cobra.Command, p *print.Printer) { - cmd.AddCommand(instance.NewCmd(p)) - cmd.AddCommand(user.NewCmd(p)) - cmd.AddCommand(options.NewCmd(p)) - cmd.AddCommand(backup.NewCmd(p)) +func addSubcommands(cmd *cobra.Command, params *params.CmdParams) { + cmd.AddCommand(instance.NewCmd(params)) + cmd.AddCommand(user.NewCmd(params)) + cmd.AddCommand(options.NewCmd(params)) + cmd.AddCommand(backup.NewCmd(params)) } diff --git a/internal/cmd/postgresflex/user/create/create.go b/internal/cmd/postgresflex/user/create/create.go index ce2912e3a..8808e676e 100644 --- a/internal/cmd/postgresflex/user/create/create.go +++ b/internal/cmd/postgresflex/user/create/create.go @@ -7,6 +7,7 @@ import ( "github.com/goccy/go-yaml" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -37,7 +38,7 @@ type inputModel struct { Roles *[]string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "create", Short: "Creates a PostgreSQL Flex user", @@ -58,26 +59,26 @@ func NewCmd(p *print.Printer) *cobra.Command { Args: args.NoArgs, RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } instanceLabel, err := postgresflexUtils.GetInstanceName(ctx, apiClient, model.ProjectId, model.Region, model.InstanceId) if err != nil { - p.Debug(print.ErrorLevel, "get instance name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get instance name: %v", err) instanceLabel = model.InstanceId } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to create a user for instance %q?", instanceLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -90,7 +91,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("create PostgreSQL Flex user: %w", err) } - return outputResult(p, model.OutputFormat, instanceLabel, resp) + return outputResult(params.Printer, model.OutputFormat, instanceLabel, resp) }, } diff --git a/internal/cmd/postgresflex/user/create/create_test.go b/internal/cmd/postgresflex/user/create/create_test.go index 6c0fe71ba..e75d785e9 100644 --- a/internal/cmd/postgresflex/user/create/create_test.go +++ b/internal/cmd/postgresflex/user/create/create_test.go @@ -8,6 +8,7 @@ import ( "github.com/google/go-cmp/cmp/cmpopts" "github.com/google/uuid" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -258,7 +259,7 @@ func Test_outputResult(t *testing.T) { }}, false}, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { diff --git a/internal/cmd/postgresflex/user/delete/delete.go b/internal/cmd/postgresflex/user/delete/delete.go index f9f166291..2ed42cd8f 100644 --- a/internal/cmd/postgresflex/user/delete/delete.go +++ b/internal/cmd/postgresflex/user/delete/delete.go @@ -4,6 +4,7 @@ import ( "context" "fmt" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -30,7 +31,7 @@ type inputModel struct { UserId string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("delete %s", userIdArg), Short: "Deletes a PostgreSQL Flex user", @@ -47,32 +48,32 @@ func NewCmd(p *print.Printer) *cobra.Command { Args: args.SingleArg(userIdArg, nil), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } instanceLabel, err := postgresflexUtils.GetInstanceName(ctx, apiClient, model.ProjectId, model.Region, model.InstanceId) if err != nil { - p.Debug(print.ErrorLevel, "get instance name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get instance name: %v", err) instanceLabel = model.InstanceId } userLabel, err := postgresflexUtils.GetUserName(ctx, apiClient, model.ProjectId, model.Region, model.InstanceId, model.UserId) if err != nil { - p.Debug(print.ErrorLevel, "get user name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get user name: %v", err) userLabel = model.UserId } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to delete user %q of instance %q? (This cannot be undone)", userLabel, instanceLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -85,7 +86,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("delete PostgreSQL Flex user: %w", err) } - p.Info("Deleted user %q of instance %q\n", userLabel, instanceLabel) + params.Printer.Info("Deleted user %q of instance %q\n", userLabel, instanceLabel) return nil }, } diff --git a/internal/cmd/postgresflex/user/delete/delete_test.go b/internal/cmd/postgresflex/user/delete/delete_test.go index cd6dfa986..867635736 100644 --- a/internal/cmd/postgresflex/user/delete/delete_test.go +++ b/internal/cmd/postgresflex/user/delete/delete_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" @@ -160,7 +161,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/postgresflex/user/describe/describe.go b/internal/cmd/postgresflex/user/describe/describe.go index cc02a616b..75e239d68 100644 --- a/internal/cmd/postgresflex/user/describe/describe.go +++ b/internal/cmd/postgresflex/user/describe/describe.go @@ -7,6 +7,7 @@ import ( "github.com/goccy/go-yaml" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -32,7 +33,7 @@ type inputModel struct { UserId string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("describe %s", userIdArg), Short: "Shows details of a PostgreSQL Flex user", @@ -52,13 +53,13 @@ func NewCmd(p *print.Printer) *cobra.Command { Args: args.SingleArg(userIdArg, nil), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -70,7 +71,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("get MongoDB Flex user: %w", err) } - return outputResult(p, model.OutputFormat, *resp.Item) + return outputResult(params.Printer, model.OutputFormat, *resp.Item) }, } diff --git a/internal/cmd/postgresflex/user/describe/describe_test.go b/internal/cmd/postgresflex/user/describe/describe_test.go index 4ff8819b9..8b91301b7 100644 --- a/internal/cmd/postgresflex/user/describe/describe_test.go +++ b/internal/cmd/postgresflex/user/describe/describe_test.go @@ -7,6 +7,7 @@ import ( "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" "github.com/google/uuid" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-sdk-go/services/postgresflex" @@ -159,7 +160,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -259,7 +260,7 @@ func Test_outputResult(t *testing.T) { }}, false}, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { diff --git a/internal/cmd/postgresflex/user/list/list.go b/internal/cmd/postgresflex/user/list/list.go index 0b530674d..3c8f79e77 100644 --- a/internal/cmd/postgresflex/user/list/list.go +++ b/internal/cmd/postgresflex/user/list/list.go @@ -7,6 +7,7 @@ import ( "github.com/goccy/go-yaml" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -32,7 +33,7 @@ type inputModel struct { Limit *int64 } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "list", Short: "Lists all PostgreSQL Flex users of an instance", @@ -51,13 +52,13 @@ func NewCmd(p *print.Printer) *cobra.Command { Args: args.NoArgs, RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -71,10 +72,10 @@ func NewCmd(p *print.Printer) *cobra.Command { if resp.Items == nil || len(*resp.Items) == 0 { instanceLabel, err := postgresflexUtils.GetInstanceName(ctx, apiClient, model.ProjectId, model.Region, *model.InstanceId) if err != nil { - p.Debug(print.ErrorLevel, "get instance name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get instance name: %v", err) instanceLabel = *model.InstanceId } - p.Info("No users found for instance %q\n", instanceLabel) + params.Printer.Info("No users found for instance %q\n", instanceLabel) return nil } users := *resp.Items @@ -84,7 +85,7 @@ func NewCmd(p *print.Printer) *cobra.Command { users = users[:*model.Limit] } - return outputResult(p, model.OutputFormat, users) + return outputResult(params.Printer, model.OutputFormat, users) }, } diff --git a/internal/cmd/postgresflex/user/list/list_test.go b/internal/cmd/postgresflex/user/list/list_test.go index ed82bbb26..20adc9e13 100644 --- a/internal/cmd/postgresflex/user/list/list_test.go +++ b/internal/cmd/postgresflex/user/list/list_test.go @@ -8,6 +8,7 @@ import ( "github.com/google/go-cmp/cmp/cmpopts" "github.com/google/uuid" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -222,7 +223,7 @@ func Test_outputResult(t *testing.T) { }}}, false}, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { diff --git a/internal/cmd/postgresflex/user/reset-password/reset_password.go b/internal/cmd/postgresflex/user/reset-password/reset_password.go index d2d240585..0d324a07b 100644 --- a/internal/cmd/postgresflex/user/reset-password/reset_password.go +++ b/internal/cmd/postgresflex/user/reset-password/reset_password.go @@ -7,6 +7,7 @@ import ( "github.com/goccy/go-yaml" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -32,7 +33,7 @@ type inputModel struct { UserId string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("reset-password %s", userIdArg), Short: "Resets the password of a PostgreSQL Flex user", @@ -48,32 +49,32 @@ func NewCmd(p *print.Printer) *cobra.Command { Args: args.SingleArg(userIdArg, nil), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } instanceLabel, err := postgresflexUtils.GetInstanceName(ctx, apiClient, model.ProjectId, model.Region, model.InstanceId) if err != nil { - p.Debug(print.ErrorLevel, "get instance name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get instance name: %v", err) instanceLabel = model.InstanceId } userLabel, err := postgresflexUtils.GetUserName(ctx, apiClient, model.ProjectId, model.Region, model.InstanceId, model.UserId) if err != nil { - p.Debug(print.ErrorLevel, "get user name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get user name: %v", err) userLabel = model.UserId } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to reset the password of user %q of instance %q? (This cannot be undone)", userLabel, instanceLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -86,7 +87,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("reset PostgreSQL Flex user password: %w", err) } - return outputResult(p, model.OutputFormat, userLabel, instanceLabel, user) + return outputResult(params.Printer, model.OutputFormat, userLabel, instanceLabel, user) }, } diff --git a/internal/cmd/postgresflex/user/reset-password/reset_password_test.go b/internal/cmd/postgresflex/user/reset-password/reset_password_test.go index 8cde43c41..b12c31d3a 100644 --- a/internal/cmd/postgresflex/user/reset-password/reset_password_test.go +++ b/internal/cmd/postgresflex/user/reset-password/reset_password_test.go @@ -7,6 +7,7 @@ import ( "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" "github.com/google/uuid" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-sdk-go/services/postgresflex" @@ -159,7 +160,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -260,7 +261,7 @@ func Test_outputResult(t *testing.T) { }}, false}, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { diff --git a/internal/cmd/postgresflex/user/update/update.go b/internal/cmd/postgresflex/user/update/update.go index 47872ecc5..5466348d3 100644 --- a/internal/cmd/postgresflex/user/update/update.go +++ b/internal/cmd/postgresflex/user/update/update.go @@ -4,6 +4,7 @@ import ( "context" "fmt" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -32,7 +33,7 @@ type inputModel struct { Roles *[]string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("update %s", userIdArg), Short: "Updates a PostgreSQL Flex user", @@ -45,32 +46,32 @@ func NewCmd(p *print.Printer) *cobra.Command { Args: args.SingleArg(userIdArg, nil), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } instanceLabel, err := postgresflexUtils.GetInstanceName(ctx, apiClient, model.ProjectId, model.Region, model.InstanceId) if err != nil { - p.Debug(print.ErrorLevel, "get instance name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get instance name: %v", err) instanceLabel = model.InstanceId } userLabel, err := postgresflexUtils.GetUserName(ctx, apiClient, model.ProjectId, model.Region, model.InstanceId, model.UserId) if err != nil { - p.Debug(print.ErrorLevel, "get user name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get user name: %v", err) userLabel = model.UserId } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to update user %q of instance %q?", userLabel, instanceLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -83,7 +84,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("update PostgreSQL Flex user: %w", err) } - p.Info("Updated user %q of instance %q\n", userLabel, instanceLabel) + params.Printer.Info("Updated user %q of instance %q\n", userLabel, instanceLabel) return nil }, } diff --git a/internal/cmd/postgresflex/user/update/update_test.go b/internal/cmd/postgresflex/user/update/update_test.go index fb0fe9cd3..787dc992d 100644 --- a/internal/cmd/postgresflex/user/update/update_test.go +++ b/internal/cmd/postgresflex/user/update/update_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -169,7 +170,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/postgresflex/user/user.go b/internal/cmd/postgresflex/user/user.go index 018244197..af9d371ef 100644 --- a/internal/cmd/postgresflex/user/user.go +++ b/internal/cmd/postgresflex/user/user.go @@ -1,6 +1,7 @@ package user import ( + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/cmd/postgresflex/user/create" "github.com/stackitcloud/stackit-cli/internal/cmd/postgresflex/user/delete" "github.com/stackitcloud/stackit-cli/internal/cmd/postgresflex/user/describe" @@ -8,13 +9,12 @@ import ( resetpassword "github.com/stackitcloud/stackit-cli/internal/cmd/postgresflex/user/reset-password" "github.com/stackitcloud/stackit-cli/internal/cmd/postgresflex/user/update" "github.com/stackitcloud/stackit-cli/internal/pkg/args" - "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" "github.com/spf13/cobra" ) -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "user", Short: "Provides functionality for PostgreSQL Flex users", @@ -22,15 +22,15 @@ func NewCmd(p *print.Printer) *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd, p) + addSubcommands(cmd, params) return cmd } -func addSubcommands(cmd *cobra.Command, p *print.Printer) { - cmd.AddCommand(create.NewCmd(p)) - cmd.AddCommand(list.NewCmd(p)) - cmd.AddCommand(describe.NewCmd(p)) - cmd.AddCommand(update.NewCmd(p)) - cmd.AddCommand(delete.NewCmd(p)) - cmd.AddCommand(resetpassword.NewCmd(p)) +func addSubcommands(cmd *cobra.Command, params *params.CmdParams) { + cmd.AddCommand(create.NewCmd(params)) + cmd.AddCommand(list.NewCmd(params)) + cmd.AddCommand(describe.NewCmd(params)) + cmd.AddCommand(update.NewCmd(params)) + cmd.AddCommand(delete.NewCmd(params)) + cmd.AddCommand(resetpassword.NewCmd(params)) } diff --git a/internal/cmd/project/create/create.go b/internal/cmd/project/create/create.go index 99f62fadd..d8c687bbd 100644 --- a/internal/cmd/project/create/create.go +++ b/internal/cmd/project/create/create.go @@ -7,6 +7,7 @@ import ( "regexp" "github.com/goccy/go-yaml" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/auth" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" @@ -41,7 +42,7 @@ type inputModel struct { NetworkAreaId *string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "create", Short: "Creates a STACKIT project", @@ -66,20 +67,20 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to create a project under the parent with ID %q?", *model.ParentId) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -95,7 +96,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("create project: %w", err) } - return outputResult(p, *model, resp) + return outputResult(params.Printer, *model, resp) }, } configureFlags(cmd) diff --git a/internal/cmd/project/create/create_test.go b/internal/cmd/project/create/create_test.go index 8c977d3c6..f6cf9fbde 100644 --- a/internal/cmd/project/create/create_test.go +++ b/internal/cmd/project/create/create_test.go @@ -7,6 +7,7 @@ import ( "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" "github.com/google/uuid" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/auth" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" @@ -177,7 +178,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -374,7 +375,7 @@ func Test_outputResult(t *testing.T) { } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.model, tt.args.resp); (err != nil) != tt.wantErr { diff --git a/internal/cmd/project/delete/delete.go b/internal/cmd/project/delete/delete.go index 9b3178bce..c0d4e121e 100644 --- a/internal/cmd/project/delete/delete.go +++ b/internal/cmd/project/delete/delete.go @@ -4,6 +4,7 @@ import ( "context" "fmt" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -20,7 +21,7 @@ type inputModel struct { *globalflags.GlobalFlagModel } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "delete", Short: "Deletes a STACKIT project", @@ -36,26 +37,26 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } - projectLabel, err := projectname.GetProjectName(ctx, p, cmd) + projectLabel, err := projectname.GetProjectName(ctx, params.Printer, cmd) if err != nil { - p.Debug(print.ErrorLevel, "get project name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get project name: %v", err) projectLabel = model.ProjectId } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to delete the project %q?", projectLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -68,8 +69,8 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("delete project: %w", err) } - p.Info("Deleted project %q\n", projectLabel) - p.Warn("%s", fmt.Sprintf("%s\n%s\n", + params.Printer.Info("Deleted project %q\n", projectLabel) + params.Printer.Warn("%s", fmt.Sprintf("%s\n%s\n", "If this was your default project, consider configuring a new project ID by running:", " $ stackit config set --project-id ", )) diff --git a/internal/cmd/project/describe/describe.go b/internal/cmd/project/describe/describe.go index 26162f859..7fe4c7b9b 100644 --- a/internal/cmd/project/describe/describe.go +++ b/internal/cmd/project/describe/describe.go @@ -6,6 +6,7 @@ import ( "fmt" "github.com/goccy/go-yaml" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" "github.com/stackitcloud/stackit-cli/internal/pkg/flags" @@ -31,7 +32,7 @@ type inputModel struct { IncludeParents bool } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "describe", Short: "Shows details of a STACKIT project", @@ -50,13 +51,13 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -68,7 +69,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("read project details: %w", err) } - return outputResult(p, model.OutputFormat, resp) + return outputResult(params.Printer, model.OutputFormat, resp) }, } configureFlags(cmd) diff --git a/internal/cmd/project/describe/describe_test.go b/internal/cmd/project/describe/describe_test.go index 3c2e5f658..f32dbc491 100644 --- a/internal/cmd/project/describe/describe_test.go +++ b/internal/cmd/project/describe/describe_test.go @@ -8,6 +8,7 @@ import ( "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" "github.com/google/uuid" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -137,7 +138,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -238,7 +239,7 @@ func Test_outputResult(t *testing.T) { }, false}, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.project); (err != nil) != tt.wantErr { diff --git a/internal/cmd/project/list/list.go b/internal/cmd/project/list/list.go index 4459558ad..c2aaccad5 100644 --- a/internal/cmd/project/list/list.go +++ b/internal/cmd/project/list/list.go @@ -8,6 +8,7 @@ import ( "github.com/goccy/go-yaml" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/auth" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" @@ -43,7 +44,7 @@ type inputModel struct { PageSize int64 } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "list", Short: "Lists STACKIT projects", @@ -65,13 +66,13 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -82,11 +83,11 @@ func NewCmd(p *print.Printer) *cobra.Command { return err } if len(projects) == 0 { - p.Info("No projects found matching the criteria\n") + params.Printer.Info("No projects found matching the criteria\n") return nil } - return outputResult(p, model.OutputFormat, projects) + return outputResult(params.Printer, model.OutputFormat, projects) }, } configureFlags(cmd) diff --git a/internal/cmd/project/list/list_test.go b/internal/cmd/project/list/list_test.go index 47db45bac..79397409f 100644 --- a/internal/cmd/project/list/list_test.go +++ b/internal/cmd/project/list/list_test.go @@ -12,6 +12,7 @@ import ( "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" "github.com/google/uuid" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/auth" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" @@ -194,7 +195,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -539,7 +540,7 @@ func Test_outputResult(t *testing.T) { } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { diff --git a/internal/cmd/project/member/add/add.go b/internal/cmd/project/member/add/add.go index 23436f039..5f9456602 100644 --- a/internal/cmd/project/member/add/add.go +++ b/internal/cmd/project/member/add/add.go @@ -4,6 +4,7 @@ import ( "context" "fmt" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -33,7 +34,7 @@ type inputModel struct { Role *string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("add %s", subjectArg), Short: "Adds a member to a project", @@ -52,26 +53,26 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } - projectLabel, err := projectname.GetProjectName(ctx, p, cmd) + projectLabel, err := projectname.GetProjectName(ctx, params.Printer, cmd) if err != nil { - p.Debug(print.ErrorLevel, "get project name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get project name: %v", err) projectLabel = model.ProjectId } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to add the role %q to %s on project %q?", *model.Role, model.Subject, projectLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -84,7 +85,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("add member: %w", err) } - p.Info("Added the role %q to %s on project %q\n", utils.PtrString(model.Role), model.Subject, projectLabel) + params.Printer.Info("Added the role %q to %s on project %q\n", utils.PtrString(model.Role), model.Subject, projectLabel) return nil }, } diff --git a/internal/cmd/project/member/add/add_test.go b/internal/cmd/project/member/add/add_test.go index e9fc2b4d6..9d24461bf 100644 --- a/internal/cmd/project/member/add/add_test.go +++ b/internal/cmd/project/member/add/add_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -124,7 +125,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/project/member/list/list.go b/internal/cmd/project/member/list/list.go index 861423dc5..c7c220121 100644 --- a/internal/cmd/project/member/list/list.go +++ b/internal/cmd/project/member/list/list.go @@ -8,6 +8,7 @@ import ( "github.com/goccy/go-yaml" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -37,7 +38,7 @@ type inputModel struct { SortBy string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "list", Short: "Lists members of a project", @@ -56,13 +57,13 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -75,12 +76,12 @@ func NewCmd(p *print.Printer) *cobra.Command { } members := *resp.Members if len(members) == 0 { - projectLabel, err := projectname.GetProjectName(ctx, p, cmd) + projectLabel, err := projectname.GetProjectName(ctx, params.Printer, cmd) if err != nil { - p.Debug(print.ErrorLevel, "get project name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get project name: %v", err) projectLabel = model.ProjectId } - p.Info("No members found for project %q\n", projectLabel) + params.Printer.Info("No members found for project %q\n", projectLabel) return nil } @@ -89,7 +90,7 @@ func NewCmd(p *print.Printer) *cobra.Command { members = members[:*model.Limit] } - return outputResult(p, *model, members) + return outputResult(params.Printer, *model, members) }, } configureFlags(cmd) diff --git a/internal/cmd/project/member/list/list_test.go b/internal/cmd/project/member/list/list_test.go index 4aecdee12..a710f7b5d 100644 --- a/internal/cmd/project/member/list/list_test.go +++ b/internal/cmd/project/member/list/list_test.go @@ -8,6 +8,7 @@ import ( "github.com/google/go-cmp/cmp/cmpopts" "github.com/google/uuid" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -240,7 +241,7 @@ func Test_outputResult(t *testing.T) { false}, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.model, tt.args.members); (err != nil) != tt.wantErr { diff --git a/internal/cmd/project/member/member.go b/internal/cmd/project/member/member.go index 658f6ff7c..cef271820 100644 --- a/internal/cmd/project/member/member.go +++ b/internal/cmd/project/member/member.go @@ -1,17 +1,17 @@ package member import ( + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/cmd/project/member/add" "github.com/stackitcloud/stackit-cli/internal/cmd/project/member/list" "github.com/stackitcloud/stackit-cli/internal/cmd/project/member/remove" "github.com/stackitcloud/stackit-cli/internal/pkg/args" - "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" "github.com/spf13/cobra" ) -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "member", Short: "Manages project members", @@ -19,12 +19,12 @@ func NewCmd(p *print.Printer) *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd, p) + addSubcommands(cmd, params) return cmd } -func addSubcommands(cmd *cobra.Command, p *print.Printer) { - cmd.AddCommand(add.NewCmd(p)) - cmd.AddCommand(list.NewCmd(p)) - cmd.AddCommand(remove.NewCmd(p)) +func addSubcommands(cmd *cobra.Command, params *params.CmdParams) { + cmd.AddCommand(add.NewCmd(params)) + cmd.AddCommand(list.NewCmd(params)) + cmd.AddCommand(remove.NewCmd(params)) } diff --git a/internal/cmd/project/member/remove/remove.go b/internal/cmd/project/member/remove/remove.go index 56004a03b..9fc60a116 100644 --- a/internal/cmd/project/member/remove/remove.go +++ b/internal/cmd/project/member/remove/remove.go @@ -4,6 +4,7 @@ import ( "context" "fmt" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -35,7 +36,7 @@ type inputModel struct { Force bool } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("remove %s", subjectArg), Short: "Removes a member from a project", @@ -55,20 +56,20 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } - projectLabel, err := projectname.GetProjectName(ctx, p, cmd) + projectLabel, err := projectname.GetProjectName(ctx, params.Printer, cmd) if err != nil { - p.Debug(print.ErrorLevel, "get project name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get project name: %v", err) projectLabel = model.ProjectId } @@ -77,7 +78,7 @@ func NewCmd(p *print.Printer) *cobra.Command { if model.Force { prompt = fmt.Sprintf("%s This will also remove other roles of the subject that would stop the removal of the requested role", prompt) } - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -90,7 +91,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("remove member: %w", err) } - p.Info("Removed the role %q from %s on project %q\n", utils.PtrString(model.Role), model.Subject, projectLabel) + params.Printer.Info("Removed the role %q from %s on project %q\n", utils.PtrString(model.Role), model.Subject, projectLabel) return nil }, } diff --git a/internal/cmd/project/member/remove/remove_test.go b/internal/cmd/project/member/remove/remove_test.go index 626efe0d6..c3cb414e1 100644 --- a/internal/cmd/project/member/remove/remove_test.go +++ b/internal/cmd/project/member/remove/remove_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -137,7 +138,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/project/project.go b/internal/cmd/project/project.go index ded8f8444..f888fbbc5 100644 --- a/internal/cmd/project/project.go +++ b/internal/cmd/project/project.go @@ -3,6 +3,7 @@ package project import ( "fmt" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/cmd/project/create" "github.com/stackitcloud/stackit-cli/internal/cmd/project/delete" "github.com/stackitcloud/stackit-cli/internal/cmd/project/describe" @@ -11,13 +12,12 @@ import ( "github.com/stackitcloud/stackit-cli/internal/cmd/project/role" "github.com/stackitcloud/stackit-cli/internal/cmd/project/update" "github.com/stackitcloud/stackit-cli/internal/pkg/args" - "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" "github.com/spf13/cobra" ) -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "project", Short: "Manages projects", @@ -28,16 +28,16 @@ func NewCmd(p *print.Printer) *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd, p) + addSubcommands(cmd, params) return cmd } -func addSubcommands(cmd *cobra.Command, p *print.Printer) { - cmd.AddCommand(create.NewCmd(p)) - cmd.AddCommand(update.NewCmd(p)) - cmd.AddCommand(delete.NewCmd(p)) - cmd.AddCommand(describe.NewCmd(p)) - cmd.AddCommand(list.NewCmd(p)) - cmd.AddCommand(member.NewCmd(p)) - cmd.AddCommand(role.NewCmd(p)) +func addSubcommands(cmd *cobra.Command, params *params.CmdParams) { + cmd.AddCommand(create.NewCmd(params)) + cmd.AddCommand(update.NewCmd(params)) + cmd.AddCommand(delete.NewCmd(params)) + cmd.AddCommand(describe.NewCmd(params)) + cmd.AddCommand(list.NewCmd(params)) + cmd.AddCommand(member.NewCmd(params)) + cmd.AddCommand(role.NewCmd(params)) } diff --git a/internal/cmd/project/role/list/list.go b/internal/cmd/project/role/list/list.go index 4b98bf977..9ffd80067 100644 --- a/internal/cmd/project/role/list/list.go +++ b/internal/cmd/project/role/list/list.go @@ -7,6 +7,7 @@ import ( "github.com/goccy/go-yaml" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -32,7 +33,7 @@ type inputModel struct { Limit *int64 } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "list", Short: "Lists roles and permissions of a project", @@ -51,13 +52,13 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -70,12 +71,12 @@ func NewCmd(p *print.Printer) *cobra.Command { } roles := *resp.Roles if len(roles) == 0 { - projectLabel, err := projectname.GetProjectName(ctx, p, cmd) + projectLabel, err := projectname.GetProjectName(ctx, params.Printer, cmd) if err != nil { - p.Debug(print.ErrorLevel, "get project name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get project name: %v", err) projectLabel = model.ProjectId } - p.Info("No roles found for project %q\n", projectLabel) + params.Printer.Info("No roles found for project %q\n", projectLabel) return nil } @@ -84,7 +85,7 @@ func NewCmd(p *print.Printer) *cobra.Command { roles = roles[:*model.Limit] } - return outputRolesResult(p, model.OutputFormat, roles) + return outputRolesResult(params.Printer, model.OutputFormat, roles) }, } configureFlags(cmd) diff --git a/internal/cmd/project/role/list/list_test.go b/internal/cmd/project/role/list/list_test.go index 4e8744528..b1f14f432 100644 --- a/internal/cmd/project/role/list/list_test.go +++ b/internal/cmd/project/role/list/list_test.go @@ -8,6 +8,7 @@ import ( "github.com/google/go-cmp/cmp/cmpopts" "github.com/google/uuid" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -196,7 +197,7 @@ func Test_outputRolesResult(t *testing.T) { }}, false}, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputRolesResult(p, tt.args.outputFormat, tt.args.roles); (err != nil) != tt.wantErr { diff --git a/internal/cmd/project/role/role.go b/internal/cmd/project/role/role.go index b460e54b7..bb0781f08 100644 --- a/internal/cmd/project/role/role.go +++ b/internal/cmd/project/role/role.go @@ -1,15 +1,15 @@ package role import ( + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/cmd/project/role/list" "github.com/stackitcloud/stackit-cli/internal/pkg/args" - "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" "github.com/spf13/cobra" ) -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "role", Short: "Manages project roles", @@ -17,10 +17,10 @@ func NewCmd(p *print.Printer) *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd, p) + addSubcommands(cmd, params) return cmd } -func addSubcommands(cmd *cobra.Command, p *print.Printer) { - cmd.AddCommand(list.NewCmd(p)) +func addSubcommands(cmd *cobra.Command, params *params.CmdParams) { + cmd.AddCommand(list.NewCmd(params)) } diff --git a/internal/cmd/project/update/update.go b/internal/cmd/project/update/update.go index ea91ccdae..e217f55f5 100644 --- a/internal/cmd/project/update/update.go +++ b/internal/cmd/project/update/update.go @@ -5,6 +5,7 @@ import ( "fmt" "regexp" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -35,7 +36,7 @@ type inputModel struct { Labels *map[string]string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "update", Short: "Updates a STACKIT project", @@ -55,26 +56,26 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } - projectLabel, err := projectname.GetProjectName(ctx, p, cmd) + projectLabel, err := projectname.GetProjectName(ctx, params.Printer, cmd) if err != nil { - p.Debug(print.ErrorLevel, "get project name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get project name: %v", err) projectLabel = model.ProjectId } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to update project %q?", projectLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -87,7 +88,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("update project: %w", err) } - p.Info("Updated project %q\n", projectLabel) + params.Printer.Info("Updated project %q\n", projectLabel) return nil }, } diff --git a/internal/cmd/project/update/update_test.go b/internal/cmd/project/update/update_test.go index 836110963..289a3b752 100644 --- a/internal/cmd/project/update/update_test.go +++ b/internal/cmd/project/update/update_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -131,7 +132,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/public-ip/associate/associate.go b/internal/cmd/public-ip/associate/associate.go index 9ce91a954..369efc526 100644 --- a/internal/cmd/public-ip/associate/associate.go +++ b/internal/cmd/public-ip/associate/associate.go @@ -4,6 +4,7 @@ import ( "context" "fmt" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" cliErr "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -30,7 +31,7 @@ type inputModel struct { AssociatedResourceId *string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("associate %s", publicIpIdArg), Short: "Associates a Public IP with a network interface or a virtual IP", @@ -44,20 +45,20 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } publicIpLabel, _, err := iaasUtils.GetPublicIP(ctx, apiClient, model.ProjectId, model.PublicIpId) if err != nil { - p.Debug(print.ErrorLevel, "get public IP: %v", err) + params.Printer.Debug(print.ErrorLevel, "get public IP: %v", err) publicIpLabel = model.PublicIpId } else if publicIpLabel == "" { publicIpLabel = model.PublicIpId @@ -65,7 +66,7 @@ func NewCmd(p *print.Printer) *cobra.Command { if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to associate public IP %q with resource %v?", publicIpLabel, *model.AssociatedResourceId) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -78,7 +79,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("associate public IP: %w", err) } - p.Outputf("Associated public IP %q with resource %v.\n", publicIpLabel, utils.PtrString(resp.GetNetworkInterface())) + params.Printer.Outputf("Associated public IP %q with resource %v.\n", publicIpLabel, utils.PtrString(resp.GetNetworkInterface())) return nil }, } diff --git a/internal/cmd/public-ip/associate/associate_test.go b/internal/cmd/public-ip/associate/associate_test.go index 99b51c3e6..bc2b890e7 100644 --- a/internal/cmd/public-ip/associate/associate_test.go +++ b/internal/cmd/public-ip/associate/associate_test.go @@ -7,6 +7,7 @@ import ( "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" "github.com/google/uuid" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -165,7 +166,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/public-ip/create/create.go b/internal/cmd/public-ip/create/create.go index 0ba5c6a84..e072655db 100644 --- a/internal/cmd/public-ip/create/create.go +++ b/internal/cmd/public-ip/create/create.go @@ -7,6 +7,7 @@ import ( "github.com/goccy/go-yaml" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" cliErr "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -30,7 +31,7 @@ type inputModel struct { Labels *map[string]string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "create", Short: "Creates a Public IP", @@ -52,20 +53,20 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } - projectLabel, err := projectname.GetProjectName(ctx, p, cmd) + projectLabel, err := projectname.GetProjectName(ctx, params.Printer, cmd) if err != nil { - p.Debug(print.ErrorLevel, "get project name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get project name: %v", err) projectLabel = model.ProjectId } else if projectLabel == "" { projectLabel = model.ProjectId @@ -73,7 +74,7 @@ func NewCmd(p *print.Printer) *cobra.Command { if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to create a public IP for project %q?", projectLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -86,7 +87,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("create public IP: %w", err) } - return outputResult(p, model.OutputFormat, projectLabel, *resp) + return outputResult(params.Printer, model.OutputFormat, projectLabel, *resp) }, } configureFlags(cmd) diff --git a/internal/cmd/public-ip/create/create_test.go b/internal/cmd/public-ip/create/create_test.go index b204d561f..be2653938 100644 --- a/internal/cmd/public-ip/create/create_test.go +++ b/internal/cmd/public-ip/create/create_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -142,7 +143,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -232,7 +233,7 @@ func Test_outputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.projectLabel, tt.args.publicIp); (err != nil) != tt.wantErr { diff --git a/internal/cmd/public-ip/delete/delete.go b/internal/cmd/public-ip/delete/delete.go index 57a427dd6..5382526e7 100644 --- a/internal/cmd/public-ip/delete/delete.go +++ b/internal/cmd/public-ip/delete/delete.go @@ -5,6 +5,7 @@ import ( "fmt" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -25,7 +26,7 @@ type inputModel struct { PublicIpId string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("delete %s", publicIpIdArg), Short: "Deletes a Public IP", @@ -42,20 +43,20 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } publicIpLabel, _, err := iaasUtils.GetPublicIP(ctx, apiClient, model.ProjectId, model.PublicIpId) if err != nil { - p.Debug(print.ErrorLevel, "get public IP: %v", err) + params.Printer.Debug(print.ErrorLevel, "get public IP: %v", err) publicIpLabel = model.PublicIpId } else if publicIpLabel == "" { publicIpLabel = model.PublicIpId @@ -63,7 +64,7 @@ func NewCmd(p *print.Printer) *cobra.Command { if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to delete public IP %q? (This cannot be undone)", publicIpLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -76,7 +77,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("delete public IP: %w", err) } - p.Info("Deleted public IP %q\n", publicIpLabel) + params.Printer.Info("Deleted public IP %q\n", publicIpLabel) return nil }, } diff --git a/internal/cmd/public-ip/delete/delete_test.go b/internal/cmd/public-ip/delete/delete_test.go index 1acd8b016..667578fb0 100644 --- a/internal/cmd/public-ip/delete/delete_test.go +++ b/internal/cmd/public-ip/delete/delete_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" @@ -138,7 +139,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/public-ip/describe/describe.go b/internal/cmd/public-ip/describe/describe.go index 820d9ff61..18f96d46d 100644 --- a/internal/cmd/public-ip/describe/describe.go +++ b/internal/cmd/public-ip/describe/describe.go @@ -8,6 +8,7 @@ import ( "github.com/goccy/go-yaml" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -30,7 +31,7 @@ type inputModel struct { PublicIpId string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("describe %s", publicIpIdArg), Short: "Shows details of a Public IP", @@ -48,13 +49,13 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -66,7 +67,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("read public IP: %w", err) } - return outputResult(p, model.OutputFormat, *resp) + return outputResult(params.Printer, model.OutputFormat, *resp) }, } return cmd diff --git a/internal/cmd/public-ip/describe/describe_test.go b/internal/cmd/public-ip/describe/describe_test.go index 44ab876ff..f69ba8dbb 100644 --- a/internal/cmd/public-ip/describe/describe_test.go +++ b/internal/cmd/public-ip/describe/describe_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" @@ -138,7 +139,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -234,7 +235,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.publicIp); (err != nil) != tt.wantErr { diff --git a/internal/cmd/public-ip/disassociate/disassociate.go b/internal/cmd/public-ip/disassociate/disassociate.go index f15bd7efd..cdd427605 100644 --- a/internal/cmd/public-ip/disassociate/disassociate.go +++ b/internal/cmd/public-ip/disassociate/disassociate.go @@ -4,6 +4,7 @@ import ( "context" "fmt" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" cliErr "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -26,7 +27,7 @@ type inputModel struct { PublicIpId string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("disassociate %s", publicIpIdArg), Short: "Disassociates a Public IP from a network interface or a virtual IP", @@ -40,20 +41,20 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } publicIpLabel, associatedResourceId, err := iaasUtils.GetPublicIP(ctx, apiClient, model.ProjectId, model.PublicIpId) if err != nil { - p.Debug(print.ErrorLevel, "get public IP: %v", err) + params.Printer.Debug(print.ErrorLevel, "get public IP: %v", err) publicIpLabel = model.PublicIpId } else if publicIpLabel == "" { publicIpLabel = model.PublicIpId @@ -61,7 +62,7 @@ func NewCmd(p *print.Printer) *cobra.Command { if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to disassociate public IP %q from the associated resource %q?", publicIpLabel, associatedResourceId) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -74,7 +75,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("disassociate public IP: %w", err) } - p.Outputf("Disassociated public IP %q from the associated resource %q.\n", publicIpLabel, associatedResourceId) + params.Printer.Outputf("Disassociated public IP %q from the associated resource %q.\n", publicIpLabel, associatedResourceId) return nil }, } diff --git a/internal/cmd/public-ip/disassociate/disassociate_test.go b/internal/cmd/public-ip/disassociate/disassociate_test.go index e4478b0c7..956be6b23 100644 --- a/internal/cmd/public-ip/disassociate/disassociate_test.go +++ b/internal/cmd/public-ip/disassociate/disassociate_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/google/go-cmp/cmp" @@ -138,7 +139,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/public-ip/list/list.go b/internal/cmd/public-ip/list/list.go index 19c225198..df57f864c 100644 --- a/internal/cmd/public-ip/list/list.go +++ b/internal/cmd/public-ip/list/list.go @@ -6,6 +6,7 @@ import ( "fmt" "github.com/goccy/go-yaml" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -32,7 +33,7 @@ type inputModel struct { LabelSelector *string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "list", Short: "Lists all Public IPs of a project", @@ -58,13 +59,13 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -77,14 +78,14 @@ func NewCmd(p *print.Printer) *cobra.Command { } if resp.Items == nil || len(*resp.Items) == 0 { - projectLabel, err := projectname.GetProjectName(ctx, p, cmd) + projectLabel, err := projectname.GetProjectName(ctx, params.Printer, cmd) if err != nil { - p.Debug(print.ErrorLevel, "get project name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get project name: %v", err) projectLabel = model.ProjectId } else if projectLabel == "" { projectLabel = model.ProjectId } - p.Info("No public IPs found for project %q\n", projectLabel) + params.Printer.Info("No public IPs found for project %q\n", projectLabel) return nil } @@ -94,7 +95,7 @@ func NewCmd(p *print.Printer) *cobra.Command { items = items[:*model.Limit] } - return outputResult(p, model.OutputFormat, items) + return outputResult(params.Printer, model.OutputFormat, items) }, } configureFlags(cmd) diff --git a/internal/cmd/public-ip/list/list_test.go b/internal/cmd/public-ip/list/list_test.go index 0644d83e8..a6fe99a48 100644 --- a/internal/cmd/public-ip/list/list_test.go +++ b/internal/cmd/public-ip/list/list_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -132,7 +133,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -220,7 +221,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.publicIps); (err != nil) != tt.wantErr { diff --git a/internal/cmd/public-ip/public-ip.go b/internal/cmd/public-ip/public-ip.go index 6259759a8..3be1ac1c4 100644 --- a/internal/cmd/public-ip/public-ip.go +++ b/internal/cmd/public-ip/public-ip.go @@ -1,6 +1,7 @@ package publicip import ( + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/cmd/public-ip/associate" "github.com/stackitcloud/stackit-cli/internal/cmd/public-ip/create" "github.com/stackitcloud/stackit-cli/internal/cmd/public-ip/delete" @@ -9,13 +10,12 @@ import ( "github.com/stackitcloud/stackit-cli/internal/cmd/public-ip/list" "github.com/stackitcloud/stackit-cli/internal/cmd/public-ip/update" "github.com/stackitcloud/stackit-cli/internal/pkg/args" - "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" "github.com/spf13/cobra" ) -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "public-ip", Short: "Provides functionality for public IPs", @@ -23,16 +23,16 @@ func NewCmd(p *print.Printer) *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd, p) + addSubcommands(cmd, params) return cmd } -func addSubcommands(cmd *cobra.Command, p *print.Printer) { - cmd.AddCommand(create.NewCmd(p)) - cmd.AddCommand(delete.NewCmd(p)) - cmd.AddCommand(describe.NewCmd(p)) - cmd.AddCommand(list.NewCmd(p)) - cmd.AddCommand(update.NewCmd(p)) - cmd.AddCommand(associate.NewCmd(p)) - cmd.AddCommand(disassociate.NewCmd(p)) +func addSubcommands(cmd *cobra.Command, params *params.CmdParams) { + cmd.AddCommand(create.NewCmd(params)) + cmd.AddCommand(delete.NewCmd(params)) + cmd.AddCommand(describe.NewCmd(params)) + cmd.AddCommand(list.NewCmd(params)) + cmd.AddCommand(update.NewCmd(params)) + cmd.AddCommand(associate.NewCmd(params)) + cmd.AddCommand(disassociate.NewCmd(params)) } diff --git a/internal/cmd/public-ip/update/update.go b/internal/cmd/public-ip/update/update.go index d3bd1a86c..0e9a2180a 100644 --- a/internal/cmd/public-ip/update/update.go +++ b/internal/cmd/public-ip/update/update.go @@ -8,6 +8,7 @@ import ( "github.com/goccy/go-yaml" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" cliErr "github.com/stackitcloud/stackit-cli/internal/pkg/errors" @@ -33,7 +34,7 @@ type inputModel struct { Labels *map[string]string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("update %s", publicIpIdArg), Short: "Updates a Public IP", @@ -51,26 +52,26 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } publicIpLabel, _, err := iaasUtils.GetPublicIP(ctx, apiClient, model.ProjectId, model.PublicIpId) if err != nil { - p.Debug(print.ErrorLevel, "get public IP: %v", err) + params.Printer.Debug(print.ErrorLevel, "get public IP: %v", err) publicIpLabel = model.PublicIpId } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to update public IP %q?", publicIpLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -83,7 +84,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("update public IP: %w", err) } - return outputResult(p, model, publicIpLabel, resp) + return outputResult(params.Printer, model, publicIpLabel, resp) }, } configureFlags(cmd) diff --git a/internal/cmd/public-ip/update/update_test.go b/internal/cmd/public-ip/update/update_test.go index 9590a858a..87c598665 100644 --- a/internal/cmd/public-ip/update/update_test.go +++ b/internal/cmd/public-ip/update/update_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -145,7 +146,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/quota/list/list.go b/internal/cmd/quota/list/list.go index 2e7a307c4..756c10ff5 100644 --- a/internal/cmd/quota/list/list.go +++ b/internal/cmd/quota/list/list.go @@ -8,6 +8,7 @@ import ( "github.com/goccy/go-yaml" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -23,7 +24,7 @@ type inputModel struct { *globalflags.GlobalFlagModel } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "list", Short: "Lists quotas", @@ -37,20 +38,20 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } - projectLabel, err := projectname.GetProjectName(ctx, p, cmd) + projectLabel, err := projectname.GetProjectName(ctx, params.Printer, cmd) if err != nil { - p.Debug(print.ErrorLevel, "get project name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get project name: %v", err) projectLabel = model.ProjectId } else if projectLabel == "" { projectLabel = model.ProjectId @@ -65,9 +66,9 @@ func NewCmd(p *print.Printer) *cobra.Command { } if items := response.Quotas; items == nil { - p.Info("No quotas found for project %q", projectLabel) + params.Printer.Info("No quotas found for project %q", projectLabel) } else { - if err := outputResult(p, model.OutputFormat, items); err != nil { + if err := outputResult(params.Printer, model.OutputFormat, items); err != nil { return fmt.Errorf("output quotas: %w", err) } } diff --git a/internal/cmd/quota/list/list_test.go b/internal/cmd/quota/list/list_test.go index 1daa3fcff..ab2cdd8ea 100644 --- a/internal/cmd/quota/list/list_test.go +++ b/internal/cmd/quota/list/list_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" @@ -95,7 +96,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) if err := globalflags.Configure(cmd.Flags()); err != nil { t.Errorf("cannot configure global flags: %v", err) } @@ -187,7 +188,7 @@ func Test_outputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.quotas); (err != nil) != tt.wantErr { diff --git a/internal/cmd/quota/quota.go b/internal/cmd/quota/quota.go index bd71be405..ff323e97e 100644 --- a/internal/cmd/quota/quota.go +++ b/internal/cmd/quota/quota.go @@ -1,16 +1,16 @@ package quota import ( + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/cmd/quota/list" "github.com/stackitcloud/stackit-cli/internal/pkg/args" - "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" "github.com/spf13/cobra" ) -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "quota", Short: "Manage server quotas", @@ -18,12 +18,12 @@ func NewCmd(p *print.Printer) *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd, p) + addSubcommands(cmd, params) return cmd } -func addSubcommands(cmd *cobra.Command, p *print.Printer) { +func addSubcommands(cmd *cobra.Command, params *params.CmdParams) { cmd.AddCommand( - list.NewCmd(p), + list.NewCmd(params), ) } diff --git a/internal/cmd/rabbitmq/credentials/create/create.go b/internal/cmd/rabbitmq/credentials/create/create.go index bc4e069f4..0cbe75a6f 100644 --- a/internal/cmd/rabbitmq/credentials/create/create.go +++ b/internal/cmd/rabbitmq/credentials/create/create.go @@ -7,6 +7,7 @@ import ( "github.com/goccy/go-yaml" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -30,7 +31,7 @@ type inputModel struct { ShowPassword bool } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "create", Short: "Creates credentials for a RabbitMQ instance", @@ -46,26 +47,26 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } instanceLabel, err := rabbitmqUtils.GetInstanceName(ctx, apiClient, model.ProjectId, model.InstanceId) if err != nil { - p.Debug(print.ErrorLevel, "get instance name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get instance name: %v", err) instanceLabel = model.InstanceId } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to create credentials for instance %q?", instanceLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -78,7 +79,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("create RabbitMQ credentials: %w", err) } - return outputResult(p, *model, instanceLabel, resp) + return outputResult(params.Printer, *model, instanceLabel, resp) }, } configureFlags(cmd) diff --git a/internal/cmd/rabbitmq/credentials/create/create_test.go b/internal/cmd/rabbitmq/credentials/create/create_test.go index 1c053d029..84e65eaa0 100644 --- a/internal/cmd/rabbitmq/credentials/create/create_test.go +++ b/internal/cmd/rabbitmq/credentials/create/create_test.go @@ -7,6 +7,7 @@ import ( "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" "github.com/google/uuid" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-sdk-go/services/rabbitmq" @@ -129,7 +130,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -243,7 +244,7 @@ func Test_outputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.model, tt.args.instanceLabel, tt.args.resp); (err != nil) != tt.wantErr { diff --git a/internal/cmd/rabbitmq/credentials/credentials.go b/internal/cmd/rabbitmq/credentials/credentials.go index 38ec2c552..80c06fb8e 100644 --- a/internal/cmd/rabbitmq/credentials/credentials.go +++ b/internal/cmd/rabbitmq/credentials/credentials.go @@ -1,18 +1,18 @@ package credentials import ( + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/cmd/rabbitmq/credentials/create" "github.com/stackitcloud/stackit-cli/internal/cmd/rabbitmq/credentials/delete" "github.com/stackitcloud/stackit-cli/internal/cmd/rabbitmq/credentials/describe" "github.com/stackitcloud/stackit-cli/internal/cmd/rabbitmq/credentials/list" "github.com/stackitcloud/stackit-cli/internal/pkg/args" - "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" "github.com/spf13/cobra" ) -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "credentials", Short: "Provides functionality for RabbitMQ credentials", @@ -20,13 +20,13 @@ func NewCmd(p *print.Printer) *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd, p) + addSubcommands(cmd, params) return cmd } -func addSubcommands(cmd *cobra.Command, p *print.Printer) { - cmd.AddCommand(create.NewCmd(p)) - cmd.AddCommand(delete.NewCmd(p)) - cmd.AddCommand(describe.NewCmd(p)) - cmd.AddCommand(list.NewCmd(p)) +func addSubcommands(cmd *cobra.Command, params *params.CmdParams) { + cmd.AddCommand(create.NewCmd(params)) + cmd.AddCommand(delete.NewCmd(params)) + cmd.AddCommand(describe.NewCmd(params)) + cmd.AddCommand(list.NewCmd(params)) } diff --git a/internal/cmd/rabbitmq/credentials/delete/delete.go b/internal/cmd/rabbitmq/credentials/delete/delete.go index 682a0eed8..433348260 100644 --- a/internal/cmd/rabbitmq/credentials/delete/delete.go +++ b/internal/cmd/rabbitmq/credentials/delete/delete.go @@ -4,6 +4,7 @@ import ( "context" "fmt" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -30,7 +31,7 @@ type inputModel struct { CredentialsId string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("delete %s", credentialsIdArg), Short: "Deletes credentials of a RabbitMQ instance", @@ -43,32 +44,32 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } instanceLabel, err := rabbitmqUtils.GetInstanceName(ctx, apiClient, model.ProjectId, model.InstanceId) if err != nil { - p.Debug(print.ErrorLevel, "get instance name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get instance name: %v", err) instanceLabel = model.InstanceId } credentialsLabel, err := rabbitmqUtils.GetCredentialsUsername(ctx, apiClient, model.ProjectId, model.InstanceId, model.CredentialsId) if err != nil { - p.Debug(print.ErrorLevel, "get credentials user name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get credentials user name: %v", err) credentialsLabel = model.CredentialsId } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to delete credentials %s of instance %q? (This cannot be undone)", credentialsLabel, instanceLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -81,7 +82,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("delete RabbitMQ credentials: %w", err) } - p.Info("Deleted credentials %s of instance %q\n", credentialsLabel, instanceLabel) + params.Printer.Info("Deleted credentials %s of instance %q\n", credentialsLabel, instanceLabel) return nil }, } diff --git a/internal/cmd/rabbitmq/credentials/delete/delete_test.go b/internal/cmd/rabbitmq/credentials/delete/delete_test.go index 24739b018..e716f7c72 100644 --- a/internal/cmd/rabbitmq/credentials/delete/delete_test.go +++ b/internal/cmd/rabbitmq/credentials/delete/delete_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" @@ -165,7 +166,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/rabbitmq/credentials/describe/describe.go b/internal/cmd/rabbitmq/credentials/describe/describe.go index 85159848d..9ad4dcc9f 100644 --- a/internal/cmd/rabbitmq/credentials/describe/describe.go +++ b/internal/cmd/rabbitmq/credentials/describe/describe.go @@ -6,6 +6,7 @@ import ( "fmt" "github.com/goccy/go-yaml" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -32,7 +33,7 @@ type inputModel struct { CredentialsId string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("describe %s", credentialsIdArg), Short: "Shows details of credentials of a RabbitMQ instance", @@ -48,13 +49,13 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -66,7 +67,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("describe RabbitMQ credentials: %w", err) } - return outputResult(p, model.OutputFormat, resp) + return outputResult(params.Printer, model.OutputFormat, resp) }, } configureFlags(cmd) diff --git a/internal/cmd/rabbitmq/credentials/describe/describe_test.go b/internal/cmd/rabbitmq/credentials/describe/describe_test.go index ef4d23107..a40cd610d 100644 --- a/internal/cmd/rabbitmq/credentials/describe/describe_test.go +++ b/internal/cmd/rabbitmq/credentials/describe/describe_test.go @@ -7,6 +7,7 @@ import ( "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" "github.com/google/uuid" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-sdk-go/services/rabbitmq" @@ -164,7 +165,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -270,7 +271,7 @@ func Test_outputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.credentials); (err != nil) != tt.wantErr { diff --git a/internal/cmd/rabbitmq/credentials/list/list.go b/internal/cmd/rabbitmq/credentials/list/list.go index e9b03d199..6a06c9313 100644 --- a/internal/cmd/rabbitmq/credentials/list/list.go +++ b/internal/cmd/rabbitmq/credentials/list/list.go @@ -7,6 +7,7 @@ import ( "github.com/goccy/go-yaml" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -31,7 +32,7 @@ type inputModel struct { Limit *int64 } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "list", Short: "Lists all credentials' IDs for a RabbitMQ instance", @@ -50,13 +51,13 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -71,10 +72,10 @@ func NewCmd(p *print.Printer) *cobra.Command { if len(credentials) == 0 { instanceLabel, err := rabbitmqUtils.GetInstanceName(ctx, apiClient, model.ProjectId, model.InstanceId) if err != nil { - p.Debug(print.ErrorLevel, "get instance name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get instance name: %v", err) instanceLabel = model.InstanceId } - p.Info("No credentials found for instance %q\n", instanceLabel) + params.Printer.Info("No credentials found for instance %q\n", instanceLabel) return nil } @@ -82,7 +83,7 @@ func NewCmd(p *print.Printer) *cobra.Command { if model.Limit != nil && len(credentials) > int(*model.Limit) { credentials = credentials[:*model.Limit] } - return outputResult(p, model.OutputFormat, credentials) + return outputResult(params.Printer, model.OutputFormat, credentials) }, } configureFlags(cmd) diff --git a/internal/cmd/rabbitmq/credentials/list/list_test.go b/internal/cmd/rabbitmq/credentials/list/list_test.go index 431a51a08..77f7f2f5e 100644 --- a/internal/cmd/rabbitmq/credentials/list/list_test.go +++ b/internal/cmd/rabbitmq/credentials/list/list_test.go @@ -7,6 +7,7 @@ import ( "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" "github.com/google/uuid" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -136,7 +137,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -234,7 +235,7 @@ func Test_outputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.credentials); (err != nil) != tt.wantErr { diff --git a/internal/cmd/rabbitmq/instance/create/create.go b/internal/cmd/rabbitmq/instance/create/create.go index 3ea8bfda1..77f34ed7f 100644 --- a/internal/cmd/rabbitmq/instance/create/create.go +++ b/internal/cmd/rabbitmq/instance/create/create.go @@ -8,6 +8,7 @@ import ( "strings" "github.com/goccy/go-yaml" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" cliErr "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -57,7 +58,7 @@ type inputModel struct { PlanId *string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "create", Short: "Creates a RabbitMQ instance", @@ -76,26 +77,26 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } - projectLabel, err := projectname.GetProjectName(ctx, p, cmd) + projectLabel, err := projectname.GetProjectName(ctx, params.Printer, cmd) if err != nil { - p.Debug(print.ErrorLevel, "get project name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get project name: %v", err) projectLabel = model.ProjectId } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to create an RabbitMQ instance for project %q?", projectLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -118,7 +119,7 @@ func NewCmd(p *print.Printer) *cobra.Command { // Wait for async operation, if async mode not enabled if !model.Async { - s := spinner.New(p) + s := spinner.New(params.Printer) s.Start("Creating instance") _, err = wait.CreateInstanceWaitHandler(ctx, apiClient, model.ProjectId, instanceId).WaitWithContext(ctx) if err != nil { @@ -127,7 +128,7 @@ func NewCmd(p *print.Printer) *cobra.Command { s.Stop() } - return outputResult(p, model, projectLabel, instanceId, resp) + return outputResult(params.Printer, model, projectLabel, instanceId, resp) }, } configureFlags(cmd) diff --git a/internal/cmd/rabbitmq/instance/create/create_test.go b/internal/cmd/rabbitmq/instance/create/create_test.go index 5faeb3257..aa39a62e8 100644 --- a/internal/cmd/rabbitmq/instance/create/create_test.go +++ b/internal/cmd/rabbitmq/instance/create/create_test.go @@ -8,6 +8,7 @@ import ( "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" "github.com/google/uuid" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -276,7 +277,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -521,7 +522,7 @@ func Test_outputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, &tt.args.model, tt.args.projectLabel, tt.args.instanceId, tt.args.resp); (err != nil) != tt.wantErr { diff --git a/internal/cmd/rabbitmq/instance/delete/delete.go b/internal/cmd/rabbitmq/instance/delete/delete.go index 66a7b578e..992526969 100644 --- a/internal/cmd/rabbitmq/instance/delete/delete.go +++ b/internal/cmd/rabbitmq/instance/delete/delete.go @@ -4,6 +4,7 @@ import ( "context" "fmt" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -28,7 +29,7 @@ type inputModel struct { InstanceId string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("delete %s", instanceIdArg), Short: "Deletes a RabbitMQ instance", @@ -41,26 +42,26 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } instanceLabel, err := rabbitmqUtils.GetInstanceName(ctx, apiClient, model.ProjectId, model.InstanceId) if err != nil { - p.Debug(print.ErrorLevel, "get instance name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get instance name: %v", err) instanceLabel = model.InstanceId } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to delete instance %q? (This cannot be undone)", instanceLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -75,7 +76,7 @@ func NewCmd(p *print.Printer) *cobra.Command { // Wait for async operation, if async mode not enabled if !model.Async { - s := spinner.New(p) + s := spinner.New(params.Printer) s.Start("Deleting instance") _, err = wait.DeleteInstanceWaitHandler(ctx, apiClient, model.ProjectId, model.InstanceId).WaitWithContext(ctx) if err != nil { @@ -88,7 +89,7 @@ func NewCmd(p *print.Printer) *cobra.Command { if model.Async { operationState = "Triggered deletion of" } - p.Info("%s instance %q\n", operationState, instanceLabel) + params.Printer.Info("%s instance %q\n", operationState, instanceLabel) return nil }, } diff --git a/internal/cmd/rabbitmq/instance/delete/delete_test.go b/internal/cmd/rabbitmq/instance/delete/delete_test.go index d9b32ef81..8064098ad 100644 --- a/internal/cmd/rabbitmq/instance/delete/delete_test.go +++ b/internal/cmd/rabbitmq/instance/delete/delete_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" @@ -138,7 +139,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/rabbitmq/instance/describe/describe.go b/internal/cmd/rabbitmq/instance/describe/describe.go index c65f67ed3..e79f476fa 100644 --- a/internal/cmd/rabbitmq/instance/describe/describe.go +++ b/internal/cmd/rabbitmq/instance/describe/describe.go @@ -6,6 +6,7 @@ import ( "fmt" "github.com/goccy/go-yaml" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -30,7 +31,7 @@ type inputModel struct { InstanceId string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("describe %s", instanceIdArg), Short: "Shows details of a RabbitMQ instance", @@ -46,12 +47,12 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -63,7 +64,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("read RabbitMQ instance: %w", err) } - return outputResult(p, model.OutputFormat, resp) + return outputResult(params.Printer, model.OutputFormat, resp) }, } return cmd diff --git a/internal/cmd/rabbitmq/instance/describe/describe_test.go b/internal/cmd/rabbitmq/instance/describe/describe_test.go index 4b9420995..8fe1f8288 100644 --- a/internal/cmd/rabbitmq/instance/describe/describe_test.go +++ b/internal/cmd/rabbitmq/instance/describe/describe_test.go @@ -7,6 +7,7 @@ import ( "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" "github.com/google/uuid" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-sdk-go/services/rabbitmq" @@ -137,7 +138,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -250,7 +251,7 @@ func Test_outputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.instance); (err != nil) != tt.wantErr { diff --git a/internal/cmd/rabbitmq/instance/instance.go b/internal/cmd/rabbitmq/instance/instance.go index b9a73a3e0..2ad311846 100644 --- a/internal/cmd/rabbitmq/instance/instance.go +++ b/internal/cmd/rabbitmq/instance/instance.go @@ -1,19 +1,19 @@ package instance import ( + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/cmd/rabbitmq/instance/create" "github.com/stackitcloud/stackit-cli/internal/cmd/rabbitmq/instance/delete" "github.com/stackitcloud/stackit-cli/internal/cmd/rabbitmq/instance/describe" "github.com/stackitcloud/stackit-cli/internal/cmd/rabbitmq/instance/list" "github.com/stackitcloud/stackit-cli/internal/cmd/rabbitmq/instance/update" "github.com/stackitcloud/stackit-cli/internal/pkg/args" - "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" "github.com/spf13/cobra" ) -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "instance", Short: "Provides functionality for RabbitMQ instances", @@ -21,14 +21,14 @@ func NewCmd(p *print.Printer) *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd, p) + addSubcommands(cmd, params) return cmd } -func addSubcommands(cmd *cobra.Command, p *print.Printer) { - cmd.AddCommand(create.NewCmd(p)) - cmd.AddCommand(delete.NewCmd(p)) - cmd.AddCommand(describe.NewCmd(p)) - cmd.AddCommand(list.NewCmd(p)) - cmd.AddCommand(update.NewCmd(p)) +func addSubcommands(cmd *cobra.Command, params *params.CmdParams) { + cmd.AddCommand(create.NewCmd(params)) + cmd.AddCommand(delete.NewCmd(params)) + cmd.AddCommand(describe.NewCmd(params)) + cmd.AddCommand(list.NewCmd(params)) + cmd.AddCommand(update.NewCmd(params)) } diff --git a/internal/cmd/rabbitmq/instance/list/list.go b/internal/cmd/rabbitmq/instance/list/list.go index 3ade03661..cad8afdb3 100644 --- a/internal/cmd/rabbitmq/instance/list/list.go +++ b/internal/cmd/rabbitmq/instance/list/list.go @@ -7,6 +7,7 @@ import ( "github.com/goccy/go-yaml" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -29,7 +30,7 @@ type inputModel struct { Limit *int64 } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "list", Short: "Lists all RabbitMQ instances", @@ -48,13 +49,13 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -67,12 +68,12 @@ func NewCmd(p *print.Printer) *cobra.Command { } instances := *resp.Instances if len(instances) == 0 { - projectLabel, err := projectname.GetProjectName(ctx, p, cmd) + projectLabel, err := projectname.GetProjectName(ctx, params.Printer, cmd) if err != nil { - p.Debug(print.ErrorLevel, "get project name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get project name: %v", err) projectLabel = model.ProjectId } - p.Info("No instances found for project %q\n", projectLabel) + params.Printer.Info("No instances found for project %q\n", projectLabel) return nil } @@ -81,7 +82,7 @@ func NewCmd(p *print.Printer) *cobra.Command { instances = instances[:*model.Limit] } - return outputResult(p, model.OutputFormat, instances) + return outputResult(params.Printer, model.OutputFormat, instances) }, } diff --git a/internal/cmd/rabbitmq/instance/list/list_test.go b/internal/cmd/rabbitmq/instance/list/list_test.go index 50bdcf4bd..bbfefdfff 100644 --- a/internal/cmd/rabbitmq/instance/list/list_test.go +++ b/internal/cmd/rabbitmq/instance/list/list_test.go @@ -8,6 +8,7 @@ import ( "github.com/google/go-cmp/cmp/cmpopts" "github.com/google/uuid" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -213,7 +214,7 @@ func Test_outputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.instances); (err != nil) != tt.wantErr { diff --git a/internal/cmd/rabbitmq/instance/update/update.go b/internal/cmd/rabbitmq/instance/update/update.go index 67144235d..f75c97cac 100644 --- a/internal/cmd/rabbitmq/instance/update/update.go +++ b/internal/cmd/rabbitmq/instance/update/update.go @@ -6,6 +6,7 @@ import ( "fmt" "strings" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" cliErr "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -56,7 +57,7 @@ type inputModel struct { PlanId *string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("update %s", instanceIdArg), Short: "Updates a RabbitMQ instance", @@ -72,26 +73,26 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } instanceLabel, err := rabbitmqUtils.GetInstanceName(ctx, apiClient, model.ProjectId, model.InstanceId) if err != nil { - p.Debug(print.ErrorLevel, "get instance name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get instance name: %v", err) instanceLabel = model.InstanceId } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to update instance %q?", instanceLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -114,7 +115,7 @@ func NewCmd(p *print.Printer) *cobra.Command { // Wait for async operation, if async mode not enabled if !model.Async { - s := spinner.New(p) + s := spinner.New(params.Printer) s.Start("Updating instance") _, err = wait.PartialUpdateInstanceWaitHandler(ctx, apiClient, model.ProjectId, instanceId).WaitWithContext(ctx) if err != nil { @@ -127,7 +128,7 @@ func NewCmd(p *print.Printer) *cobra.Command { if model.Async { operationState = "Triggered update of" } - p.Info("%s instance %q\n", operationState, instanceLabel) + params.Printer.Info("%s instance %q\n", operationState, instanceLabel) return nil }, } diff --git a/internal/cmd/rabbitmq/instance/update/update_test.go b/internal/cmd/rabbitmq/instance/update/update_test.go index c2d92bc6f..50642481c 100644 --- a/internal/cmd/rabbitmq/instance/update/update_test.go +++ b/internal/cmd/rabbitmq/instance/update/update_test.go @@ -5,6 +5,7 @@ import ( "fmt" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -294,7 +295,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/rabbitmq/plans/plans.go b/internal/cmd/rabbitmq/plans/plans.go index 1b84f3029..fc9276a93 100644 --- a/internal/cmd/rabbitmq/plans/plans.go +++ b/internal/cmd/rabbitmq/plans/plans.go @@ -6,6 +6,7 @@ import ( "fmt" "github.com/goccy/go-yaml" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -30,7 +31,7 @@ type inputModel struct { Limit *int64 } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "plans", Short: "Lists all RabbitMQ service plans", @@ -49,13 +50,13 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -68,12 +69,12 @@ func NewCmd(p *print.Printer) *cobra.Command { } plans := *resp.Offerings if len(plans) == 0 { - projectLabel, err := projectname.GetProjectName(ctx, p, cmd) + projectLabel, err := projectname.GetProjectName(ctx, params.Printer, cmd) if err != nil { - p.Debug(print.ErrorLevel, "get project name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get project name: %v", err) projectLabel = model.ProjectId } - p.Info("No plans found for project %q\n", projectLabel) + params.Printer.Info("No plans found for project %q\n", projectLabel) return nil } @@ -82,7 +83,7 @@ func NewCmd(p *print.Printer) *cobra.Command { plans = plans[:*model.Limit] } - return outputResult(p, model.OutputFormat, plans) + return outputResult(params.Printer, model.OutputFormat, plans) }, } diff --git a/internal/cmd/rabbitmq/plans/plans_test.go b/internal/cmd/rabbitmq/plans/plans_test.go index ab8966a20..d5f0f9aee 100644 --- a/internal/cmd/rabbitmq/plans/plans_test.go +++ b/internal/cmd/rabbitmq/plans/plans_test.go @@ -8,6 +8,7 @@ import ( "github.com/google/go-cmp/cmp/cmpopts" "github.com/google/uuid" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -213,7 +214,7 @@ func Test_outputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.plans); (err != nil) != tt.wantErr { diff --git a/internal/cmd/rabbitmq/rabbitmq.go b/internal/cmd/rabbitmq/rabbitmq.go index 26b5db9bb..9dc5b76a3 100644 --- a/internal/cmd/rabbitmq/rabbitmq.go +++ b/internal/cmd/rabbitmq/rabbitmq.go @@ -1,17 +1,17 @@ package rabbitmq import ( + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/cmd/rabbitmq/credentials" "github.com/stackitcloud/stackit-cli/internal/cmd/rabbitmq/instance" "github.com/stackitcloud/stackit-cli/internal/cmd/rabbitmq/plans" "github.com/stackitcloud/stackit-cli/internal/pkg/args" - "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" "github.com/spf13/cobra" ) -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "rabbitmq", Short: "Provides functionality for RabbitMQ", @@ -19,12 +19,12 @@ func NewCmd(p *print.Printer) *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd, p) + addSubcommands(cmd, params) return cmd } -func addSubcommands(cmd *cobra.Command, p *print.Printer) { - cmd.AddCommand(instance.NewCmd(p)) - cmd.AddCommand(plans.NewCmd(p)) - cmd.AddCommand(credentials.NewCmd(p)) +func addSubcommands(cmd *cobra.Command, params *params.CmdParams) { + cmd.AddCommand(instance.NewCmd(params)) + cmd.AddCommand(plans.NewCmd(params)) + cmd.AddCommand(credentials.NewCmd(params)) } diff --git a/internal/cmd/redis/credentials/create/create.go b/internal/cmd/redis/credentials/create/create.go index 9b2098344..17d6e4164 100644 --- a/internal/cmd/redis/credentials/create/create.go +++ b/internal/cmd/redis/credentials/create/create.go @@ -6,6 +6,7 @@ import ( "fmt" "github.com/goccy/go-yaml" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -31,7 +32,7 @@ type inputModel struct { ShowPassword bool } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "create", Short: "Creates credentials for a Redis instance", @@ -47,26 +48,26 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } instanceLabel, err := redisUtils.GetInstanceName(ctx, apiClient, model.ProjectId, model.InstanceId) if err != nil { - p.Debug(print.ErrorLevel, "get instance name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get instance name: %v", err) instanceLabel = model.InstanceId } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to create credentials for instance %q?", instanceLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -79,7 +80,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("create Redis credentials: %w", err) } - return outputResult(p, *model, instanceLabel, resp) + return outputResult(params.Printer, *model, instanceLabel, resp) }, } configureFlags(cmd) diff --git a/internal/cmd/redis/credentials/create/create_test.go b/internal/cmd/redis/credentials/create/create_test.go index 511b066ad..070229478 100644 --- a/internal/cmd/redis/credentials/create/create_test.go +++ b/internal/cmd/redis/credentials/create/create_test.go @@ -7,6 +7,7 @@ import ( "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" "github.com/google/uuid" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-sdk-go/services/redis" @@ -129,7 +130,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -223,7 +224,7 @@ func Test_outputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.model, tt.args.instanceLabel, tt.args.resp); (err != nil) != tt.wantErr { diff --git a/internal/cmd/redis/credentials/credentials.go b/internal/cmd/redis/credentials/credentials.go index 42e6226da..d1d8a4d7a 100644 --- a/internal/cmd/redis/credentials/credentials.go +++ b/internal/cmd/redis/credentials/credentials.go @@ -1,18 +1,18 @@ package credentials import ( + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/cmd/redis/credentials/create" "github.com/stackitcloud/stackit-cli/internal/cmd/redis/credentials/delete" "github.com/stackitcloud/stackit-cli/internal/cmd/redis/credentials/describe" "github.com/stackitcloud/stackit-cli/internal/cmd/redis/credentials/list" "github.com/stackitcloud/stackit-cli/internal/pkg/args" - "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" "github.com/spf13/cobra" ) -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "credentials", Short: "Provides functionality for Redis credentials", @@ -20,13 +20,13 @@ func NewCmd(p *print.Printer) *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd, p) + addSubcommands(cmd, params) return cmd } -func addSubcommands(cmd *cobra.Command, p *print.Printer) { - cmd.AddCommand(create.NewCmd(p)) - cmd.AddCommand(delete.NewCmd(p)) - cmd.AddCommand(describe.NewCmd(p)) - cmd.AddCommand(list.NewCmd(p)) +func addSubcommands(cmd *cobra.Command, params *params.CmdParams) { + cmd.AddCommand(create.NewCmd(params)) + cmd.AddCommand(delete.NewCmd(params)) + cmd.AddCommand(describe.NewCmd(params)) + cmd.AddCommand(list.NewCmd(params)) } diff --git a/internal/cmd/redis/credentials/delete/delete.go b/internal/cmd/redis/credentials/delete/delete.go index 4012c6b3f..147ad731b 100644 --- a/internal/cmd/redis/credentials/delete/delete.go +++ b/internal/cmd/redis/credentials/delete/delete.go @@ -4,6 +4,7 @@ import ( "context" "fmt" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -30,7 +31,7 @@ type inputModel struct { CredentialsId string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("delete %s", credentialsIdArg), Short: "Deletes credentials of a Redis instance", @@ -43,32 +44,32 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } instanceLabel, err := redisUtils.GetInstanceName(ctx, apiClient, model.ProjectId, model.InstanceId) if err != nil { - p.Debug(print.ErrorLevel, "get instance name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get instance name: %v", err) instanceLabel = model.InstanceId } credentialsLabel, err := redisUtils.GetCredentialsUsername(ctx, apiClient, model.ProjectId, model.InstanceId, model.CredentialsId) if err != nil { - p.Debug(print.ErrorLevel, "get credentials user name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get credentials user name: %v", err) credentialsLabel = model.CredentialsId } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to delete credentials %s of instance %q? (This cannot be undone)", credentialsLabel, instanceLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -81,7 +82,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("delete Redis credentials: %w", err) } - p.Info("Deleted credentials %s of instance %q\n", credentialsLabel, instanceLabel) + params.Printer.Info("Deleted credentials %s of instance %q\n", credentialsLabel, instanceLabel) return nil }, } diff --git a/internal/cmd/redis/credentials/delete/delete_test.go b/internal/cmd/redis/credentials/delete/delete_test.go index 716bfedfe..d8b2c4632 100644 --- a/internal/cmd/redis/credentials/delete/delete_test.go +++ b/internal/cmd/redis/credentials/delete/delete_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" @@ -165,7 +166,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/redis/credentials/describe/describe.go b/internal/cmd/redis/credentials/describe/describe.go index 7a6dc78e4..9ab0e9ab4 100644 --- a/internal/cmd/redis/credentials/describe/describe.go +++ b/internal/cmd/redis/credentials/describe/describe.go @@ -6,6 +6,7 @@ import ( "fmt" "github.com/goccy/go-yaml" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -32,7 +33,7 @@ type inputModel struct { CredentialsId string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("describe %s", credentialsIdArg), Short: "Shows details of credentials of a Redis instance", @@ -48,13 +49,13 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -66,7 +67,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("describe Redis credentials: %w", err) } - return outputResult(p, model.OutputFormat, resp) + return outputResult(params.Printer, model.OutputFormat, resp) }, } configureFlags(cmd) diff --git a/internal/cmd/redis/credentials/describe/describe_test.go b/internal/cmd/redis/credentials/describe/describe_test.go index c6f837491..fc8edb1b8 100644 --- a/internal/cmd/redis/credentials/describe/describe_test.go +++ b/internal/cmd/redis/credentials/describe/describe_test.go @@ -7,6 +7,7 @@ import ( "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" "github.com/google/uuid" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-sdk-go/services/redis" @@ -164,7 +165,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -267,7 +268,7 @@ func Test_outputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.credentials); (err != nil) != tt.wantErr { diff --git a/internal/cmd/redis/credentials/list/list.go b/internal/cmd/redis/credentials/list/list.go index a83492451..603dc86ed 100644 --- a/internal/cmd/redis/credentials/list/list.go +++ b/internal/cmd/redis/credentials/list/list.go @@ -7,6 +7,7 @@ import ( "github.com/goccy/go-yaml" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -31,7 +32,7 @@ type inputModel struct { Limit *int64 } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "list", Short: "Lists all credentials' IDs for a Redis instance", @@ -50,13 +51,13 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -71,10 +72,10 @@ func NewCmd(p *print.Printer) *cobra.Command { if len(credentials) == 0 { instanceLabel, err := redisUtils.GetInstanceName(ctx, apiClient, model.ProjectId, model.InstanceId) if err != nil { - p.Debug(print.ErrorLevel, "get instance name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get instance name: %v", err) instanceLabel = model.InstanceId } - p.Info("No credentials found for instance %q\n", instanceLabel) + params.Printer.Info("No credentials found for instance %q\n", instanceLabel) return nil } @@ -82,7 +83,7 @@ func NewCmd(p *print.Printer) *cobra.Command { if model.Limit != nil && len(credentials) > int(*model.Limit) { credentials = credentials[:*model.Limit] } - return outputResult(p, model.OutputFormat, credentials) + return outputResult(params.Printer, model.OutputFormat, credentials) }, } configureFlags(cmd) diff --git a/internal/cmd/redis/credentials/list/list_test.go b/internal/cmd/redis/credentials/list/list_test.go index b771823bf..a37f530c4 100644 --- a/internal/cmd/redis/credentials/list/list_test.go +++ b/internal/cmd/redis/credentials/list/list_test.go @@ -7,6 +7,7 @@ import ( "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" "github.com/google/uuid" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -136,7 +137,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -234,7 +235,7 @@ func Test_outputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.credentials); (err != nil) != tt.wantErr { diff --git a/internal/cmd/redis/instance/create/create.go b/internal/cmd/redis/instance/create/create.go index 87db67741..86fdec837 100644 --- a/internal/cmd/redis/instance/create/create.go +++ b/internal/cmd/redis/instance/create/create.go @@ -8,6 +8,7 @@ import ( "strings" "github.com/goccy/go-yaml" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" cliErr "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -55,7 +56,7 @@ type inputModel struct { PlanId *string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "create", Short: "Creates a Redis instance", @@ -74,26 +75,26 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } - projectLabel, err := projectname.GetProjectName(ctx, p, cmd) + projectLabel, err := projectname.GetProjectName(ctx, params.Printer, cmd) if err != nil { - p.Debug(print.ErrorLevel, "get project name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get project name: %v", err) projectLabel = model.ProjectId } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to create a Redis instance for project %q?", projectLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -116,7 +117,7 @@ func NewCmd(p *print.Printer) *cobra.Command { // Wait for async operation, if async mode not enabled if !model.Async { - s := spinner.New(p) + s := spinner.New(params.Printer) s.Start("Creating instance") _, err = wait.CreateInstanceWaitHandler(ctx, apiClient, model.ProjectId, instanceId).WaitWithContext(ctx) if err != nil { @@ -125,7 +126,7 @@ func NewCmd(p *print.Printer) *cobra.Command { s.Stop() } - return outputResult(p, model, projectLabel, instanceId, resp) + return outputResult(params.Printer, model, projectLabel, instanceId, resp) }, } configureFlags(cmd) diff --git a/internal/cmd/redis/instance/create/create_test.go b/internal/cmd/redis/instance/create/create_test.go index 59ee4d259..814673259 100644 --- a/internal/cmd/redis/instance/create/create_test.go +++ b/internal/cmd/redis/instance/create/create_test.go @@ -8,6 +8,7 @@ import ( "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" "github.com/google/uuid" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -261,7 +262,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -496,7 +497,7 @@ func Test_outputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.model, tt.args.projectLabel, tt.args.instanceId, tt.args.resp); (err != nil) != tt.wantErr { diff --git a/internal/cmd/redis/instance/delete/delete.go b/internal/cmd/redis/instance/delete/delete.go index 902e30ada..bb4efebda 100644 --- a/internal/cmd/redis/instance/delete/delete.go +++ b/internal/cmd/redis/instance/delete/delete.go @@ -4,6 +4,7 @@ import ( "context" "fmt" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -28,7 +29,7 @@ type inputModel struct { InstanceId string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("delete %s", instanceIdArg), Short: "Deletes a Redis instance", @@ -41,26 +42,26 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } instanceLabel, err := redisUtils.GetInstanceName(ctx, apiClient, model.ProjectId, model.InstanceId) if err != nil { - p.Debug(print.ErrorLevel, "get instance name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get instance name: %v", err) instanceLabel = model.InstanceId } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to delete instance %q? (This cannot be undone)", instanceLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -75,7 +76,7 @@ func NewCmd(p *print.Printer) *cobra.Command { // Wait for async operation, if async mode not enabled if !model.Async { - s := spinner.New(p) + s := spinner.New(params.Printer) s.Start("Deleting instance") _, err = wait.DeleteInstanceWaitHandler(ctx, apiClient, model.ProjectId, model.InstanceId).WaitWithContext(ctx) if err != nil { @@ -88,7 +89,7 @@ func NewCmd(p *print.Printer) *cobra.Command { if model.Async { operationState = "Triggered deletion of" } - p.Info("%s instance %q\n", operationState, instanceLabel) + params.Printer.Info("%s instance %q\n", operationState, instanceLabel) return nil }, } diff --git a/internal/cmd/redis/instance/delete/delete_test.go b/internal/cmd/redis/instance/delete/delete_test.go index 60dc78c89..bb0da281f 100644 --- a/internal/cmd/redis/instance/delete/delete_test.go +++ b/internal/cmd/redis/instance/delete/delete_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" @@ -138,7 +139,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/redis/instance/describe/describe.go b/internal/cmd/redis/instance/describe/describe.go index f91b4b1e6..c01d82ddd 100644 --- a/internal/cmd/redis/instance/describe/describe.go +++ b/internal/cmd/redis/instance/describe/describe.go @@ -6,6 +6,7 @@ import ( "fmt" "github.com/goccy/go-yaml" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -30,7 +31,7 @@ type inputModel struct { InstanceId string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("describe %s", instanceIdArg), Short: "Shows details of a Redis instance", @@ -46,12 +47,12 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -63,7 +64,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("read Redis instance: %w", err) } - return outputResult(p, model.OutputFormat, resp) + return outputResult(params.Printer, model.OutputFormat, resp) }, } return cmd diff --git a/internal/cmd/redis/instance/describe/describe_test.go b/internal/cmd/redis/instance/describe/describe_test.go index 1e12d6195..651164b98 100644 --- a/internal/cmd/redis/instance/describe/describe_test.go +++ b/internal/cmd/redis/instance/describe/describe_test.go @@ -7,6 +7,7 @@ import ( "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" "github.com/google/uuid" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-sdk-go/services/redis" @@ -137,7 +138,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -251,7 +252,7 @@ func Test_outputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.instance); (err != nil) != tt.wantErr { diff --git a/internal/cmd/redis/instance/instance.go b/internal/cmd/redis/instance/instance.go index 3518921b5..a45e6bd96 100644 --- a/internal/cmd/redis/instance/instance.go +++ b/internal/cmd/redis/instance/instance.go @@ -1,19 +1,19 @@ package instance import ( + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/cmd/redis/instance/create" "github.com/stackitcloud/stackit-cli/internal/cmd/redis/instance/delete" "github.com/stackitcloud/stackit-cli/internal/cmd/redis/instance/describe" "github.com/stackitcloud/stackit-cli/internal/cmd/redis/instance/list" "github.com/stackitcloud/stackit-cli/internal/cmd/redis/instance/update" "github.com/stackitcloud/stackit-cli/internal/pkg/args" - "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" "github.com/spf13/cobra" ) -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "instance", Short: "Provides functionality for Redis instances", @@ -21,14 +21,14 @@ func NewCmd(p *print.Printer) *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd, p) + addSubcommands(cmd, params) return cmd } -func addSubcommands(cmd *cobra.Command, p *print.Printer) { - cmd.AddCommand(create.NewCmd(p)) - cmd.AddCommand(delete.NewCmd(p)) - cmd.AddCommand(describe.NewCmd(p)) - cmd.AddCommand(list.NewCmd(p)) - cmd.AddCommand(update.NewCmd(p)) +func addSubcommands(cmd *cobra.Command, params *params.CmdParams) { + cmd.AddCommand(create.NewCmd(params)) + cmd.AddCommand(delete.NewCmd(params)) + cmd.AddCommand(describe.NewCmd(params)) + cmd.AddCommand(list.NewCmd(params)) + cmd.AddCommand(update.NewCmd(params)) } diff --git a/internal/cmd/redis/instance/list/list.go b/internal/cmd/redis/instance/list/list.go index 4b49c15fa..87f7b6d3e 100644 --- a/internal/cmd/redis/instance/list/list.go +++ b/internal/cmd/redis/instance/list/list.go @@ -7,6 +7,7 @@ import ( "github.com/goccy/go-yaml" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -29,7 +30,7 @@ type inputModel struct { Limit *int64 } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "list", Short: "Lists all Redis instances", @@ -48,13 +49,13 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -67,12 +68,12 @@ func NewCmd(p *print.Printer) *cobra.Command { } instances := *resp.Instances if len(instances) == 0 { - projectLabel, err := projectname.GetProjectName(ctx, p, cmd) + projectLabel, err := projectname.GetProjectName(ctx, params.Printer, cmd) if err != nil { - p.Debug(print.ErrorLevel, "get project name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get project name: %v", err) projectLabel = model.ProjectId } - p.Info("No instances found for project %q\n", projectLabel) + params.Printer.Info("No instances found for project %q\n", projectLabel) return nil } @@ -81,7 +82,7 @@ func NewCmd(p *print.Printer) *cobra.Command { instances = instances[:*model.Limit] } - return outputResult(p, model.OutputFormat, instances) + return outputResult(params.Printer, model.OutputFormat, instances) }, } diff --git a/internal/cmd/redis/instance/list/list_test.go b/internal/cmd/redis/instance/list/list_test.go index 0338bd8a7..aff5c8020 100644 --- a/internal/cmd/redis/instance/list/list_test.go +++ b/internal/cmd/redis/instance/list/list_test.go @@ -8,6 +8,7 @@ import ( "github.com/google/go-cmp/cmp/cmpopts" "github.com/google/uuid" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -212,7 +213,7 @@ func Test_outputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.instances); (err != nil) != tt.wantErr { diff --git a/internal/cmd/redis/instance/update/update.go b/internal/cmd/redis/instance/update/update.go index 117dbe472..65e92c860 100644 --- a/internal/cmd/redis/instance/update/update.go +++ b/internal/cmd/redis/instance/update/update.go @@ -6,6 +6,7 @@ import ( "fmt" "strings" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" cliErr "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -53,7 +54,7 @@ type inputModel struct { PlanId *string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("update %s", instanceIdArg), Short: "Updates a Redis instance", @@ -69,26 +70,26 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } instanceLabel, err := redisUtils.GetInstanceName(ctx, apiClient, model.ProjectId, model.InstanceId) if err != nil { - p.Debug(print.ErrorLevel, "get instance name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get instance name: %v", err) instanceLabel = model.InstanceId } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to update instance %q?", instanceLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -111,7 +112,7 @@ func NewCmd(p *print.Printer) *cobra.Command { // Wait for async operation, if async mode not enabled if !model.Async { - s := spinner.New(p) + s := spinner.New(params.Printer) s.Start("Updating instance") _, err = wait.PartialUpdateInstanceWaitHandler(ctx, apiClient, model.ProjectId, instanceId).WaitWithContext(ctx) if err != nil { @@ -124,7 +125,7 @@ func NewCmd(p *print.Printer) *cobra.Command { if model.Async { operationState = "Triggered update of" } - p.Info("%s instance %q\n", operationState, instanceLabel) + params.Printer.Info("%s instance %q\n", operationState, instanceLabel) return nil }, } diff --git a/internal/cmd/redis/instance/update/update_test.go b/internal/cmd/redis/instance/update/update_test.go index 30602e593..7c5df24d5 100644 --- a/internal/cmd/redis/instance/update/update_test.go +++ b/internal/cmd/redis/instance/update/update_test.go @@ -5,6 +5,7 @@ import ( "fmt" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -278,7 +279,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/redis/plans/plans.go b/internal/cmd/redis/plans/plans.go index 8ae785da6..7bcb60218 100644 --- a/internal/cmd/redis/plans/plans.go +++ b/internal/cmd/redis/plans/plans.go @@ -7,6 +7,7 @@ import ( "github.com/goccy/go-yaml" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -29,7 +30,7 @@ type inputModel struct { Limit *int64 } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "plans", Short: "Lists all Redis service plans", @@ -48,13 +49,13 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -67,12 +68,12 @@ func NewCmd(p *print.Printer) *cobra.Command { } plans := *resp.Offerings if len(plans) == 0 { - projectLabel, err := projectname.GetProjectName(ctx, p, cmd) + projectLabel, err := projectname.GetProjectName(ctx, params.Printer, cmd) if err != nil { - p.Debug(print.ErrorLevel, "get project name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get project name: %v", err) projectLabel = model.ProjectId } - p.Info("No plans found for project %q\n", projectLabel) + params.Printer.Info("No plans found for project %q\n", projectLabel) return nil } @@ -81,7 +82,7 @@ func NewCmd(p *print.Printer) *cobra.Command { plans = plans[:*model.Limit] } - return outputResult(p, model.OutputFormat, plans) + return outputResult(params.Printer, model.OutputFormat, plans) }, } diff --git a/internal/cmd/redis/plans/plans_test.go b/internal/cmd/redis/plans/plans_test.go index f850de8a1..8ea65f786 100644 --- a/internal/cmd/redis/plans/plans_test.go +++ b/internal/cmd/redis/plans/plans_test.go @@ -8,6 +8,7 @@ import ( "github.com/google/go-cmp/cmp/cmpopts" "github.com/google/uuid" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -212,7 +213,7 @@ func Test_outputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.plans); (err != nil) != tt.wantErr { diff --git a/internal/cmd/redis/redis.go b/internal/cmd/redis/redis.go index 3b07f00c9..4a45e9c33 100644 --- a/internal/cmd/redis/redis.go +++ b/internal/cmd/redis/redis.go @@ -1,17 +1,17 @@ package redis import ( + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/cmd/redis/credentials" "github.com/stackitcloud/stackit-cli/internal/cmd/redis/instance" "github.com/stackitcloud/stackit-cli/internal/cmd/redis/plans" "github.com/stackitcloud/stackit-cli/internal/pkg/args" - "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" "github.com/spf13/cobra" ) -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "redis", Short: "Provides functionality for Redis", @@ -19,12 +19,12 @@ func NewCmd(p *print.Printer) *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd, p) + addSubcommands(cmd, params) return cmd } -func addSubcommands(cmd *cobra.Command, p *print.Printer) { - cmd.AddCommand(instance.NewCmd(p)) - cmd.AddCommand(plans.NewCmd(p)) - cmd.AddCommand(credentials.NewCmd(p)) +func addSubcommands(cmd *cobra.Command, params *params.CmdParams) { + cmd.AddCommand(instance.NewCmd(params)) + cmd.AddCommand(plans.NewCmd(params)) + cmd.AddCommand(credentials.NewCmd(params)) } diff --git a/internal/cmd/root.go b/internal/cmd/root.go index 736c944f5..6da6491d6 100644 --- a/internal/cmd/root.go +++ b/internal/cmd/root.go @@ -6,6 +6,8 @@ import ( "strings" "time" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" + affinityGroups "github.com/stackitcloud/stackit-cli/internal/cmd/affinity-groups" "github.com/stackitcloud/stackit-cli/internal/cmd/auth" "github.com/stackitcloud/stackit-cli/internal/cmd/beta" @@ -114,7 +116,10 @@ func NewRootCmd(version, date string, p *print.Printer) *cobra.Command { err := configureFlags(cmd) cobra.CheckErr(err) - addSubcommands(cmd, p) + addSubcommands(cmd, ¶ms.CmdParams{ + Printer: p, + CliVersion: version, + }) // Cobra creates the help flag with "help for " as the description // We want to override that message by capitalizing the first letter to match the other flag descriptions @@ -154,38 +159,38 @@ func configureFlags(cmd *cobra.Command) error { return nil } -func addSubcommands(cmd *cobra.Command, p *print.Printer) { - cmd.AddCommand(auth.NewCmd(p)) - cmd.AddCommand(configCmd.NewCmd(p)) - cmd.AddCommand(beta.NewCmd(p)) - cmd.AddCommand(curl.NewCmd(p)) - cmd.AddCommand(dns.NewCmd(p)) - cmd.AddCommand(loadbalancer.NewCmd(p)) - cmd.AddCommand(logme.NewCmd(p)) - cmd.AddCommand(mariadb.NewCmd(p)) - cmd.AddCommand(mongodbflex.NewCmd(p)) - cmd.AddCommand(objectstorage.NewCmd(p)) - cmd.AddCommand(observability.NewCmd(p)) - cmd.AddCommand(opensearch.NewCmd(p)) - cmd.AddCommand(organization.NewCmd(p)) - cmd.AddCommand(postgresflex.NewCmd(p)) - cmd.AddCommand(project.NewCmd(p)) - cmd.AddCommand(rabbitmq.NewCmd(p)) - cmd.AddCommand(redis.NewCmd(p)) - cmd.AddCommand(secretsmanager.NewCmd(p)) - cmd.AddCommand(serviceaccount.NewCmd(p)) - cmd.AddCommand(ske.NewCmd(p)) - cmd.AddCommand(server.NewCmd(p)) - cmd.AddCommand(networkArea.NewCmd(p)) - cmd.AddCommand(network.NewCmd(p)) - cmd.AddCommand(volume.NewCmd(p)) - cmd.AddCommand(networkinterface.NewCmd(p)) - cmd.AddCommand(publicip.NewCmd(p)) - cmd.AddCommand(securitygroup.NewCmd(p)) - cmd.AddCommand(keypair.NewCmd(p)) - cmd.AddCommand(image.NewCmd(p)) - cmd.AddCommand(quota.NewCmd(p)) - cmd.AddCommand(affinityGroups.NewCmd(p)) +func addSubcommands(cmd *cobra.Command, params *params.CmdParams) { + cmd.AddCommand(auth.NewCmd(params)) + cmd.AddCommand(configCmd.NewCmd(params)) + cmd.AddCommand(beta.NewCmd(params)) + cmd.AddCommand(curl.NewCmd(params)) + cmd.AddCommand(dns.NewCmd(params)) + cmd.AddCommand(loadbalancer.NewCmd(params)) + cmd.AddCommand(logme.NewCmd(params)) + cmd.AddCommand(mariadb.NewCmd(params)) + cmd.AddCommand(mongodbflex.NewCmd(params)) + cmd.AddCommand(objectstorage.NewCmd(params)) + cmd.AddCommand(observability.NewCmd(params)) + cmd.AddCommand(opensearch.NewCmd(params)) + cmd.AddCommand(organization.NewCmd(params)) + cmd.AddCommand(postgresflex.NewCmd(params)) + cmd.AddCommand(project.NewCmd(params)) + cmd.AddCommand(rabbitmq.NewCmd(params)) + cmd.AddCommand(redis.NewCmd(params)) + cmd.AddCommand(secretsmanager.NewCmd(params)) + cmd.AddCommand(serviceaccount.NewCmd(params)) + cmd.AddCommand(ske.NewCmd(params)) + cmd.AddCommand(server.NewCmd(params)) + cmd.AddCommand(networkArea.NewCmd(params)) + cmd.AddCommand(network.NewCmd(params)) + cmd.AddCommand(volume.NewCmd(params)) + cmd.AddCommand(networkinterface.NewCmd(params)) + cmd.AddCommand(publicip.NewCmd(params)) + cmd.AddCommand(securitygroup.NewCmd(params)) + cmd.AddCommand(keypair.NewCmd(params)) + cmd.AddCommand(image.NewCmd(params)) + cmd.AddCommand(quota.NewCmd(params)) + cmd.AddCommand(affinityGroups.NewCmd(params)) } // traverseCommands calls f for c and all of its children. diff --git a/internal/cmd/secrets-manager/instance/create/create.go b/internal/cmd/secrets-manager/instance/create/create.go index 35c02682c..ad1a8195f 100644 --- a/internal/cmd/secrets-manager/instance/create/create.go +++ b/internal/cmd/secrets-manager/instance/create/create.go @@ -6,6 +6,7 @@ import ( "fmt" "github.com/goccy/go-yaml" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" cliErr "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -32,7 +33,7 @@ type inputModel struct { Acls *[]string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "create", Short: "Creates a Secrets Manager instance", @@ -49,26 +50,26 @@ func NewCmd(p *print.Printer) *cobra.Command { RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } - projectLabel, err := projectname.GetProjectName(ctx, p, cmd) + projectLabel, err := projectname.GetProjectName(ctx, params.Printer, cmd) if err != nil { - p.Debug(print.ErrorLevel, "get project name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get project name: %v", err) projectLabel = model.ProjectId } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to create a Secrets Manager instance for project %q?", projectLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -94,7 +95,7 @@ If you want to retry configuring the ACLs, you can do it via: } } - return outputResult(p, model.OutputFormat, projectLabel, instanceId, resp) + return outputResult(params.Printer, model.OutputFormat, projectLabel, instanceId, resp) }, } configureFlags(cmd) diff --git a/internal/cmd/secrets-manager/instance/create/create_test.go b/internal/cmd/secrets-manager/instance/create/create_test.go index 92b04ba79..02a895c01 100644 --- a/internal/cmd/secrets-manager/instance/create/create_test.go +++ b/internal/cmd/secrets-manager/instance/create/create_test.go @@ -7,6 +7,7 @@ import ( "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" "github.com/google/uuid" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -184,7 +185,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -329,7 +330,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.projectLabel, tt.args.instanceId, tt.args.instance); (err != nil) != tt.wantErr { diff --git a/internal/cmd/secrets-manager/instance/delete/delete.go b/internal/cmd/secrets-manager/instance/delete/delete.go index 960034165..dda64b620 100644 --- a/internal/cmd/secrets-manager/instance/delete/delete.go +++ b/internal/cmd/secrets-manager/instance/delete/delete.go @@ -5,6 +5,7 @@ import ( "fmt" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -25,7 +26,7 @@ type inputModel struct { InstanceId string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("delete %s", instanceIdArg), Short: "Deletes a Secrets Manager instance", @@ -38,26 +39,26 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } instanceLabel, err := secretsmanagerUtils.GetInstanceName(ctx, apiClient, model.ProjectId, model.InstanceId) if err != nil { - p.Debug(print.ErrorLevel, "get instance name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get instance name: %v", err) instanceLabel = model.InstanceId } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to delete instance %q? (This cannot be undone)", instanceLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -70,7 +71,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("delete Secrets Manager instance: %w", err) } - p.Info("Deleted instance %q \n", model.InstanceId) + params.Printer.Info("Deleted instance %q \n", model.InstanceId) return nil }, } diff --git a/internal/cmd/secrets-manager/instance/delete/delete_test.go b/internal/cmd/secrets-manager/instance/delete/delete_test.go index b36eb87b8..fe6eae032 100644 --- a/internal/cmd/secrets-manager/instance/delete/delete_test.go +++ b/internal/cmd/secrets-manager/instance/delete/delete_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" @@ -138,7 +139,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/secrets-manager/instance/describe/describe.go b/internal/cmd/secrets-manager/instance/describe/describe.go index 8551fa8bf..e794a2cce 100644 --- a/internal/cmd/secrets-manager/instance/describe/describe.go +++ b/internal/cmd/secrets-manager/instance/describe/describe.go @@ -7,6 +7,7 @@ import ( "strings" "github.com/goccy/go-yaml" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -29,7 +30,7 @@ type inputModel struct { InstanceId string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("describe %s", instanceIdArg), Short: "Shows details of a Secrets Manager instance", @@ -45,12 +46,12 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -69,7 +70,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("read Secrets Manager instance ACLs: %w", err) } - return outputResult(p, model.OutputFormat, instance, aclList) + return outputResult(params.Printer, model.OutputFormat, instance, aclList) }, } return cmd diff --git a/internal/cmd/secrets-manager/instance/describe/describe_test.go b/internal/cmd/secrets-manager/instance/describe/describe_test.go index 212c825ab..2cadb3be2 100644 --- a/internal/cmd/secrets-manager/instance/describe/describe_test.go +++ b/internal/cmd/secrets-manager/instance/describe/describe_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" @@ -146,7 +147,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -293,7 +294,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.instance, tt.args.aclList); (err != nil) != tt.wantErr { diff --git a/internal/cmd/secrets-manager/instance/instance.go b/internal/cmd/secrets-manager/instance/instance.go index cc38f6ca7..5617aeddc 100644 --- a/internal/cmd/secrets-manager/instance/instance.go +++ b/internal/cmd/secrets-manager/instance/instance.go @@ -1,19 +1,19 @@ package instance import ( + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/cmd/secrets-manager/instance/create" "github.com/stackitcloud/stackit-cli/internal/cmd/secrets-manager/instance/delete" "github.com/stackitcloud/stackit-cli/internal/cmd/secrets-manager/instance/describe" "github.com/stackitcloud/stackit-cli/internal/cmd/secrets-manager/instance/list" "github.com/stackitcloud/stackit-cli/internal/cmd/secrets-manager/instance/update" "github.com/stackitcloud/stackit-cli/internal/pkg/args" - "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" "github.com/spf13/cobra" ) -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "instance", Short: "Provides functionality for Secrets Manager instances", @@ -21,14 +21,14 @@ func NewCmd(p *print.Printer) *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd, p) + addSubcommands(cmd, params) return cmd } -func addSubcommands(cmd *cobra.Command, p *print.Printer) { - cmd.AddCommand(list.NewCmd(p)) - cmd.AddCommand(create.NewCmd(p)) - cmd.AddCommand(delete.NewCmd(p)) - cmd.AddCommand(describe.NewCmd(p)) - cmd.AddCommand(update.NewCmd(p)) +func addSubcommands(cmd *cobra.Command, params *params.CmdParams) { + cmd.AddCommand(list.NewCmd(params)) + cmd.AddCommand(create.NewCmd(params)) + cmd.AddCommand(delete.NewCmd(params)) + cmd.AddCommand(describe.NewCmd(params)) + cmd.AddCommand(update.NewCmd(params)) } diff --git a/internal/cmd/secrets-manager/instance/list/list.go b/internal/cmd/secrets-manager/instance/list/list.go index 019aff8f6..cf6d4c745 100644 --- a/internal/cmd/secrets-manager/instance/list/list.go +++ b/internal/cmd/secrets-manager/instance/list/list.go @@ -7,6 +7,7 @@ import ( "github.com/goccy/go-yaml" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -29,7 +30,7 @@ type inputModel struct { Limit *int64 } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "list", Short: "Lists all Secrets Manager instances", @@ -48,13 +49,13 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -67,12 +68,12 @@ func NewCmd(p *print.Printer) *cobra.Command { } if resp.Instances == nil || len(*resp.Instances) == 0 { - projectLabel, err := projectname.GetProjectName(ctx, p, cmd) + projectLabel, err := projectname.GetProjectName(ctx, params.Printer, cmd) if err != nil { - p.Debug(print.ErrorLevel, "get project name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get project name: %v", err) projectLabel = model.ProjectId } - p.Info("No instances found for project %q\n", projectLabel) + params.Printer.Info("No instances found for project %q\n", projectLabel) return nil } instances := *resp.Instances @@ -82,7 +83,7 @@ func NewCmd(p *print.Printer) *cobra.Command { instances = instances[:*model.Limit] } - return outputResult(p, model.OutputFormat, instances) + return outputResult(params.Printer, model.OutputFormat, instances) }, } diff --git a/internal/cmd/secrets-manager/instance/list/list_test.go b/internal/cmd/secrets-manager/instance/list/list_test.go index 3903b0210..6ea677137 100644 --- a/internal/cmd/secrets-manager/instance/list/list_test.go +++ b/internal/cmd/secrets-manager/instance/list/list_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -228,7 +229,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.instances); (err != nil) != tt.wantErr { diff --git a/internal/cmd/secrets-manager/instance/update/update.go b/internal/cmd/secrets-manager/instance/update/update.go index e013c1e73..371711e55 100644 --- a/internal/cmd/secrets-manager/instance/update/update.go +++ b/internal/cmd/secrets-manager/instance/update/update.go @@ -4,6 +4,7 @@ import ( "context" "fmt" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" cliErr "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -32,7 +33,7 @@ type inputModel struct { Acls *[]string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("update %s", instanceIdArg), Short: "Updates a Secrets Manager instance", @@ -45,26 +46,26 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } instanceLabel, err := secretsManagerUtils.GetInstanceName(ctx, apiClient, model.ProjectId, model.InstanceId) if err != nil { - p.Debug(print.ErrorLevel, "get instance name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get instance name: %v", err) instanceLabel = model.InstanceId } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to update instance %q?", instanceLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -77,7 +78,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("update Secrets Manager instance: %w", err) } - p.Info("Updated instance %q\n", instanceLabel) + params.Printer.Info("Updated instance %q\n", instanceLabel) return nil }, } diff --git a/internal/cmd/secrets-manager/instance/update/update_test.go b/internal/cmd/secrets-manager/instance/update/update_test.go index 8668d9ca1..1a60db361 100644 --- a/internal/cmd/secrets-manager/instance/update/update_test.go +++ b/internal/cmd/secrets-manager/instance/update/update_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -198,7 +199,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/secrets-manager/secrets_manager.go b/internal/cmd/secrets-manager/secrets_manager.go index d9c78f035..35745dfde 100644 --- a/internal/cmd/secrets-manager/secrets_manager.go +++ b/internal/cmd/secrets-manager/secrets_manager.go @@ -1,16 +1,16 @@ package secretsmanager import ( + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/cmd/secrets-manager/instance" "github.com/stackitcloud/stackit-cli/internal/cmd/secrets-manager/user" "github.com/stackitcloud/stackit-cli/internal/pkg/args" - "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" "github.com/spf13/cobra" ) -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "secrets-manager", Short: "Provides functionality for Secrets Manager", @@ -18,11 +18,11 @@ func NewCmd(p *print.Printer) *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd, p) + addSubcommands(cmd, params) return cmd } -func addSubcommands(cmd *cobra.Command, p *print.Printer) { - cmd.AddCommand(instance.NewCmd(p)) - cmd.AddCommand(user.NewCmd(p)) +func addSubcommands(cmd *cobra.Command, params *params.CmdParams) { + cmd.AddCommand(instance.NewCmd(params)) + cmd.AddCommand(user.NewCmd(params)) } diff --git a/internal/cmd/secrets-manager/user/create/create.go b/internal/cmd/secrets-manager/user/create/create.go index 12a767cf4..62d54d9d7 100644 --- a/internal/cmd/secrets-manager/user/create/create.go +++ b/internal/cmd/secrets-manager/user/create/create.go @@ -6,6 +6,7 @@ import ( "fmt" "github.com/goccy/go-yaml" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -34,7 +35,7 @@ type inputModel struct { Write *bool } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "create", Short: "Creates a Secrets Manager user", @@ -54,26 +55,26 @@ func NewCmd(p *print.Printer) *cobra.Command { Args: args.NoArgs, RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } instanceLabel, err := secretsManagerUtils.GetInstanceName(ctx, apiClient, model.ProjectId, model.InstanceId) if err != nil { - p.Debug(print.ErrorLevel, "get instance name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get instance name: %v", err) instanceLabel = model.InstanceId } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to create a user for instance %q?", instanceLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -86,7 +87,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("create Secrets Manager user: %w", err) } - return outputResult(p, model.OutputFormat, instanceLabel, resp) + return outputResult(params.Printer, model.OutputFormat, instanceLabel, resp) }, } diff --git a/internal/cmd/secrets-manager/user/create/create_test.go b/internal/cmd/secrets-manager/user/create/create_test.go index be5ae1a37..a64b826ba 100644 --- a/internal/cmd/secrets-manager/user/create/create_test.go +++ b/internal/cmd/secrets-manager/user/create/create_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -251,7 +252,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.instanceLabel, tt.args.user); (err != nil) != tt.wantErr { diff --git a/internal/cmd/secrets-manager/user/delete/delete.go b/internal/cmd/secrets-manager/user/delete/delete.go index 6ed848d0e..fc3272435 100644 --- a/internal/cmd/secrets-manager/user/delete/delete.go +++ b/internal/cmd/secrets-manager/user/delete/delete.go @@ -4,6 +4,7 @@ import ( "context" "fmt" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -31,7 +32,7 @@ type inputModel struct { UserId string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("delete %s", userIdArg), Short: "Deletes a Secrets Manager user", @@ -47,32 +48,32 @@ func NewCmd(p *print.Printer) *cobra.Command { Args: args.SingleArg(userIdArg, utils.ValidateUUID), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } instanceLabel, err := secretsManagerUtils.GetInstanceName(ctx, apiClient, model.ProjectId, model.InstanceId) if err != nil { - p.Debug(print.ErrorLevel, "get instance name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get instance name: %v", err) instanceLabel = model.InstanceId } userLabel, err := secretsManagerUtils.GetUserLabel(ctx, apiClient, model.ProjectId, model.InstanceId, model.UserId) if err != nil { - p.Debug(print.ErrorLevel, "get user label: %v", err) + params.Printer.Debug(print.ErrorLevel, "get user label: %v", err) userLabel = fmt.Sprintf("%q", model.UserId) } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to delete user %s of instance %q? (This cannot be undone)", userLabel, instanceLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -85,7 +86,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("delete Secrets Manager user: %w", err) } - p.Info("Deleted user %s of instance %q\n", userLabel, instanceLabel) + params.Printer.Info("Deleted user %s of instance %q\n", userLabel, instanceLabel) return nil }, } diff --git a/internal/cmd/secrets-manager/user/delete/delete_test.go b/internal/cmd/secrets-manager/user/delete/delete_test.go index 8ad0a2bf2..0827ebf88 100644 --- a/internal/cmd/secrets-manager/user/delete/delete_test.go +++ b/internal/cmd/secrets-manager/user/delete/delete_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" @@ -153,7 +154,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/secrets-manager/user/describe/describe.go b/internal/cmd/secrets-manager/user/describe/describe.go index 2bfb18b08..c08e0ac32 100644 --- a/internal/cmd/secrets-manager/user/describe/describe.go +++ b/internal/cmd/secrets-manager/user/describe/describe.go @@ -6,6 +6,7 @@ import ( "fmt" "github.com/goccy/go-yaml" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -33,7 +34,7 @@ type inputModel struct { UserId string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("describe %s", userIdArg), Short: "Shows details of a Secrets Manager user", @@ -49,13 +50,13 @@ func NewCmd(p *print.Printer) *cobra.Command { Args: args.SingleArg(userIdArg, utils.ValidateUUID), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -67,7 +68,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("get Secrets Manager user: %w", err) } - return outputResult(p, model.OutputFormat, *resp) + return outputResult(params.Printer, model.OutputFormat, *resp) }, } diff --git a/internal/cmd/secrets-manager/user/describe/describe_test.go b/internal/cmd/secrets-manager/user/describe/describe_test.go index 46cd4b63e..53c14c586 100644 --- a/internal/cmd/secrets-manager/user/describe/describe_test.go +++ b/internal/cmd/secrets-manager/user/describe/describe_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" @@ -165,7 +166,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -268,7 +269,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.user); (err != nil) != tt.wantErr { diff --git a/internal/cmd/secrets-manager/user/list/list.go b/internal/cmd/secrets-manager/user/list/list.go index 19ff7f8ea..b414e2cd9 100644 --- a/internal/cmd/secrets-manager/user/list/list.go +++ b/internal/cmd/secrets-manager/user/list/list.go @@ -7,6 +7,7 @@ import ( "github.com/goccy/go-yaml" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -32,7 +33,7 @@ type inputModel struct { Limit *int64 } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "list", Short: "Lists all Secrets Manager users", @@ -51,13 +52,13 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -71,10 +72,10 @@ func NewCmd(p *print.Printer) *cobra.Command { if resp.Users == nil || len(*resp.Users) == 0 { instanceLabel, err := secretsManagerUtils.GetInstanceName(ctx, apiClient, model.ProjectId, *model.InstanceId) if err != nil { - p.Debug(print.ErrorLevel, "get instance name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get instance name: %v", err) instanceLabel = *model.InstanceId } - p.Info("No users found for instance %q\n", instanceLabel) + params.Printer.Info("No users found for instance %q\n", instanceLabel) return nil } users := *resp.Users @@ -84,7 +85,7 @@ func NewCmd(p *print.Printer) *cobra.Command { users = users[:*model.Limit] } - return outputResult(p, model.OutputFormat, users) + return outputResult(params.Printer, model.OutputFormat, users) }, } diff --git a/internal/cmd/secrets-manager/user/list/list_test.go b/internal/cmd/secrets-manager/user/list/list_test.go index ac4c830a7..d2de82297 100644 --- a/internal/cmd/secrets-manager/user/list/list_test.go +++ b/internal/cmd/secrets-manager/user/list/list_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -235,7 +236,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.users); (err != nil) != tt.wantErr { diff --git a/internal/cmd/secrets-manager/user/update/update.go b/internal/cmd/secrets-manager/user/update/update.go index 573076eb4..f0b6e35b9 100644 --- a/internal/cmd/secrets-manager/user/update/update.go +++ b/internal/cmd/secrets-manager/user/update/update.go @@ -4,6 +4,7 @@ import ( "context" "fmt" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -35,7 +36,7 @@ type inputModel struct { DisableWrite *bool } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("update %s", userIdArg), Short: "Updates the write privileges Secrets Manager user", @@ -51,32 +52,32 @@ func NewCmd(p *print.Printer) *cobra.Command { Args: args.SingleArg(userIdArg, utils.ValidateUUID), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } instanceLabel, err := secretsManagerUtils.GetInstanceName(ctx, apiClient, model.ProjectId, model.InstanceId) if err != nil { - p.Debug(print.ErrorLevel, "get instance name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get instance name: %v", err) instanceLabel = model.InstanceId } userLabel, err := secretsManagerUtils.GetUserLabel(ctx, apiClient, model.ProjectId, model.InstanceId, model.UserId) if err != nil { - p.Debug(print.ErrorLevel, "get user label: %v", err) + params.Printer.Debug(print.ErrorLevel, "get user label: %v", err) userLabel = fmt.Sprintf("%q", model.UserId) } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to update user %s of instance %q?", userLabel, instanceLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -93,7 +94,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("update Secrets Manager user: %w", err) } - p.Info("Updated user %s of instance %q\n", userLabel, instanceLabel) + params.Printer.Info("Updated user %s of instance %q\n", userLabel, instanceLabel) return nil }, } diff --git a/internal/cmd/secrets-manager/user/update/update_test.go b/internal/cmd/secrets-manager/user/update/update_test.go index b3b8a67b0..5ae24676b 100644 --- a/internal/cmd/secrets-manager/user/update/update_test.go +++ b/internal/cmd/secrets-manager/user/update/update_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -189,7 +190,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/secrets-manager/user/user.go b/internal/cmd/secrets-manager/user/user.go index 8dcd68410..ae4e1c90f 100644 --- a/internal/cmd/secrets-manager/user/user.go +++ b/internal/cmd/secrets-manager/user/user.go @@ -2,10 +2,10 @@ package user import ( "github.com/stackitcloud/stackit-cli/internal/pkg/args" - "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/cmd/secrets-manager/user/create" "github.com/stackitcloud/stackit-cli/internal/cmd/secrets-manager/user/delete" "github.com/stackitcloud/stackit-cli/internal/cmd/secrets-manager/user/describe" @@ -13,7 +13,7 @@ import ( "github.com/stackitcloud/stackit-cli/internal/cmd/secrets-manager/user/update" ) -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "user", Short: "Provides functionality for Secrets Manager users", @@ -21,14 +21,14 @@ func NewCmd(p *print.Printer) *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd, p) + addSubcommands(cmd, params) return cmd } -func addSubcommands(cmd *cobra.Command, p *print.Printer) { - cmd.AddCommand(list.NewCmd(p)) - cmd.AddCommand(create.NewCmd(p)) - cmd.AddCommand(delete.NewCmd(p)) - cmd.AddCommand(describe.NewCmd(p)) - cmd.AddCommand(update.NewCmd(p)) +func addSubcommands(cmd *cobra.Command, params *params.CmdParams) { + cmd.AddCommand(list.NewCmd(params)) + cmd.AddCommand(create.NewCmd(params)) + cmd.AddCommand(delete.NewCmd(params)) + cmd.AddCommand(describe.NewCmd(params)) + cmd.AddCommand(update.NewCmd(params)) } diff --git a/internal/cmd/security-group/create/create.go b/internal/cmd/security-group/create/create.go index 24f3da09c..891abccc9 100644 --- a/internal/cmd/security-group/create/create.go +++ b/internal/cmd/security-group/create/create.go @@ -7,6 +7,7 @@ import ( "github.com/goccy/go-yaml" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -33,7 +34,7 @@ type inputModel struct { Stateful *bool } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "create", Short: "Creates security groups", @@ -45,20 +46,20 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to create the security group %q?", *model.Name) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -72,7 +73,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("create security group: %w", err) } - if err := outputResult(p, model.OutputFormat, *model.Name, *group); err != nil { + if err := outputResult(params.Printer, model.OutputFormat, *model.Name, *group); err != nil { return err } diff --git a/internal/cmd/security-group/create/create_test.go b/internal/cmd/security-group/create/create_test.go index 6a6a02898..9b2561489 100644 --- a/internal/cmd/security-group/create/create_test.go +++ b/internal/cmd/security-group/create/create_test.go @@ -7,6 +7,7 @@ import ( "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" "github.com/google/uuid" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -168,7 +169,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) if err := globalflags.Configure(cmd.Flags()); err != nil { t.Errorf("cannot configure global flags: %v", err) } @@ -282,7 +283,7 @@ func TestOutputResult(t *testing.T) { } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.name, tt.args.resp); (err != nil) != tt.wantErr { diff --git a/internal/cmd/security-group/delete/delete.go b/internal/cmd/security-group/delete/delete.go index 85781ae13..0d116da7e 100644 --- a/internal/cmd/security-group/delete/delete.go +++ b/internal/cmd/security-group/delete/delete.go @@ -5,6 +5,7 @@ import ( "fmt" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -24,7 +25,7 @@ type inputModel struct { const groupIdArg = "GROUP_ID" -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("delete %s", groupIdArg), Short: "Deletes a security group", @@ -35,32 +36,32 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } - projectLabel, err := projectname.GetProjectName(ctx, p, cmd) + projectLabel, err := projectname.GetProjectName(ctx, params.Printer, cmd) if err != nil { - p.Debug(print.ErrorLevel, "get project name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get project name: %v", err) projectLabel = model.ProjectId } groupLabel, err := iaasUtils.GetSecurityGroupName(ctx, apiClient, model.ProjectId, model.SecurityGroupId) if err != nil { - p.Warn("get security group name: %v", err) + params.Printer.Warn("get security group name: %v", err) groupLabel = model.SecurityGroupId } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to delete the security group %q for %q?", groupLabel, projectLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -72,7 +73,7 @@ func NewCmd(p *print.Printer) *cobra.Command { if err := request.Execute(); err != nil { return fmt.Errorf("delete security group: %w", err) } - p.Info("Deleted security group %q for %q\n", groupLabel, projectLabel) + params.Printer.Info("Deleted security group %q for %q\n", groupLabel, projectLabel) return nil }, diff --git a/internal/cmd/security-group/delete/delete_test.go b/internal/cmd/security-group/delete/delete_test.go index 7666e1585..ec7143cbf 100644 --- a/internal/cmd/security-group/delete/delete_test.go +++ b/internal/cmd/security-group/delete/delete_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" @@ -105,7 +106,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/security-group/describe/describe.go b/internal/cmd/security-group/describe/describe.go index 01d86df03..bde9cbc9f 100644 --- a/internal/cmd/security-group/describe/describe.go +++ b/internal/cmd/security-group/describe/describe.go @@ -6,6 +6,7 @@ import ( "fmt" "strings" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -27,7 +28,7 @@ type inputModel struct { const groupIdArg = "GROUP_ID" -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("describe %s", groupIdArg), Short: "Describes security groups", @@ -38,13 +39,13 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -57,7 +58,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("get security group: %w", err) } - if err := outputResult(p, model.OutputFormat, group); err != nil { + if err := outputResult(params.Printer, model.OutputFormat, group); err != nil { return err } diff --git a/internal/cmd/security-group/describe/describe_test.go b/internal/cmd/security-group/describe/describe_test.go index 6deaf042f..f9b5f7fba 100644 --- a/internal/cmd/security-group/describe/describe_test.go +++ b/internal/cmd/security-group/describe/describe_test.go @@ -7,6 +7,7 @@ import ( "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" "github.com/google/uuid" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-sdk-go/services/iaas" @@ -119,7 +120,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) if err := globalflags.Configure(cmd.Flags()); err != nil { t.Errorf("cannot configure global flags: %v", err) } @@ -216,7 +217,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.resp); (err != nil) != tt.wantErr { diff --git a/internal/cmd/security-group/list/list.go b/internal/cmd/security-group/list/list.go index 48caeddb3..592c65408 100644 --- a/internal/cmd/security-group/list/list.go +++ b/internal/cmd/security-group/list/list.go @@ -8,6 +8,7 @@ import ( "github.com/goccy/go-yaml" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -30,7 +31,7 @@ const ( labelSelectorFlag = "label-selector" ) -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "list", Short: "Lists security groups", @@ -42,20 +43,20 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } - projectLabel, err := projectname.GetProjectName(ctx, p, cmd) + projectLabel, err := projectname.GetProjectName(ctx, params.Printer, cmd) if err != nil { - p.Debug(print.ErrorLevel, "get project name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get project name: %v", err) projectLabel = model.ProjectId } @@ -68,9 +69,9 @@ func NewCmd(p *print.Printer) *cobra.Command { } if items := response.GetItems(); len(items) == 0 { - p.Info("No security groups found for project %q", projectLabel) + params.Printer.Info("No security groups found for project %q", projectLabel) } else { - if err := outputResult(p, model.OutputFormat, items); err != nil { + if err := outputResult(params.Printer, model.OutputFormat, items); err != nil { return fmt.Errorf("output security groups: %w", err) } } diff --git a/internal/cmd/security-group/list/list_test.go b/internal/cmd/security-group/list/list_test.go index d19adf3a0..a78e3501a 100644 --- a/internal/cmd/security-group/list/list_test.go +++ b/internal/cmd/security-group/list/list_test.go @@ -7,6 +7,7 @@ import ( "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" "github.com/google/uuid" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -119,7 +120,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) if err := globalflags.Configure(cmd.Flags()); err != nil { t.Errorf("cannot configure global flags: %v", err) } @@ -222,7 +223,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.items); (err != nil) != tt.wantErr { diff --git a/internal/cmd/security-group/rule/create/create.go b/internal/cmd/security-group/rule/create/create.go index a330e98eb..a5b4bbc9d 100644 --- a/internal/cmd/security-group/rule/create/create.go +++ b/internal/cmd/security-group/rule/create/create.go @@ -6,6 +6,7 @@ import ( "fmt" "github.com/goccy/go-yaml" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" cliErr "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -52,7 +53,7 @@ type inputModel struct { ProtocolName *string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "create", Short: "Creates a security group rule", @@ -78,32 +79,32 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } - projectLabel, err := projectname.GetProjectName(ctx, p, cmd) + projectLabel, err := projectname.GetProjectName(ctx, params.Printer, cmd) if err != nil { - p.Debug(print.ErrorLevel, "get project name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get project name: %v", err) projectLabel = model.ProjectId } securityGroupLabel, err := iaasUtils.GetSecurityGroupName(ctx, apiClient, model.ProjectId, model.SecurityGroupId) if err != nil { - p.Debug(print.ErrorLevel, "get security group name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get security group name: %v", err) securityGroupLabel = model.SecurityGroupId } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to create a security group rule for security group %q for project %q?", securityGroupLabel, projectLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -116,7 +117,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("create security group rule : %w", err) } - return outputResult(p, model, projectLabel, securityGroupLabel, resp) + return outputResult(params.Printer, model, projectLabel, securityGroupLabel, resp) }, } configureFlags(cmd) diff --git a/internal/cmd/security-group/rule/create/create_test.go b/internal/cmd/security-group/rule/create/create_test.go index 1a0024129..6c1521899 100644 --- a/internal/cmd/security-group/rule/create/create_test.go +++ b/internal/cmd/security-group/rule/create/create_test.go @@ -7,6 +7,7 @@ import ( "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" "github.com/google/uuid" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -243,7 +244,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -360,7 +361,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.model, tt.args.projectLabel, tt.args.securityGroupName, tt.args.securityGroupRule); (err != nil) != tt.wantErr { diff --git a/internal/cmd/security-group/rule/delete/delete.go b/internal/cmd/security-group/rule/delete/delete.go index 247caed8a..adca9d3eb 100644 --- a/internal/cmd/security-group/rule/delete/delete.go +++ b/internal/cmd/security-group/rule/delete/delete.go @@ -5,6 +5,7 @@ import ( "fmt" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -29,7 +30,7 @@ type inputModel struct { SecurityGroupId *string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("delete %s", securityGroupRuleIdArg), Short: "Deletes a security group rule", @@ -46,32 +47,32 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } securityGroupLabel, err := iaasUtils.GetSecurityGroupName(ctx, apiClient, model.ProjectId, *model.SecurityGroupId) if err != nil { - p.Debug(print.ErrorLevel, "get security group name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get security group name: %v", err) securityGroupLabel = *model.SecurityGroupId } securityGroupRuleLabel, err := iaasUtils.GetSecurityGroupRuleName(ctx, apiClient, model.ProjectId, model.SecurityGroupRuleId, *model.SecurityGroupId) if err != nil { - p.Debug(print.ErrorLevel, "get security group rule name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get security group rule name: %v", err) securityGroupRuleLabel = model.SecurityGroupRuleId } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to delete security group rule %q from security group %q?", securityGroupRuleLabel, securityGroupLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -84,7 +85,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("delete security group rule: %w", err) } - p.Info("Deleted security group rule %q from security group %q\n", securityGroupRuleLabel, securityGroupLabel) + params.Printer.Info("Deleted security group rule %q from security group %q\n", securityGroupRuleLabel, securityGroupLabel) return nil }, } diff --git a/internal/cmd/security-group/rule/delete/delete_test.go b/internal/cmd/security-group/rule/delete/delete_test.go index e8d36d7f6..ebf83035c 100644 --- a/internal/cmd/security-group/rule/delete/delete_test.go +++ b/internal/cmd/security-group/rule/delete/delete_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -155,7 +156,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/security-group/rule/describe/describe.go b/internal/cmd/security-group/rule/describe/describe.go index 8e8bd0508..feefb5d54 100644 --- a/internal/cmd/security-group/rule/describe/describe.go +++ b/internal/cmd/security-group/rule/describe/describe.go @@ -6,6 +6,7 @@ import ( "fmt" "github.com/goccy/go-yaml" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -32,7 +33,7 @@ type inputModel struct { SecurityGroupId *string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("describe %s", securityGroupRuleIdArg), Short: "Shows details of a security group rule", @@ -50,13 +51,13 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -68,7 +69,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("read security group rule: %w", err) } - return outputResult(p, model.OutputFormat, resp) + return outputResult(params.Printer, model.OutputFormat, resp) }, } configureFlags(cmd) diff --git a/internal/cmd/security-group/rule/describe/describe_test.go b/internal/cmd/security-group/rule/describe/describe_test.go index 03060fcb2..66b536f5c 100644 --- a/internal/cmd/security-group/rule/describe/describe_test.go +++ b/internal/cmd/security-group/rule/describe/describe_test.go @@ -7,6 +7,7 @@ import ( "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" "github.com/google/uuid" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -165,7 +166,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -268,7 +269,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.securityGroupRule); (err != nil) != tt.wantErr { diff --git a/internal/cmd/security-group/rule/list/list.go b/internal/cmd/security-group/rule/list/list.go index 84c83ece4..50304378e 100644 --- a/internal/cmd/security-group/rule/list/list.go +++ b/internal/cmd/security-group/rule/list/list.go @@ -5,6 +5,7 @@ import ( "encoding/json" "fmt" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -34,7 +35,7 @@ type inputModel struct { SecurityGroupId *string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "list", Short: "Lists all security group rules in a security group of a project", @@ -56,13 +57,13 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -77,16 +78,16 @@ func NewCmd(p *print.Printer) *cobra.Command { if resp.Items == nil || len(*resp.Items) == 0 { securityGroupLabel, err := iaasUtils.GetSecurityGroupName(ctx, apiClient, model.ProjectId, *model.SecurityGroupId) if err != nil { - p.Debug(print.ErrorLevel, "get security group name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get security group name: %v", err) securityGroupLabel = *model.SecurityGroupId } - projectLabel, err := projectname.GetProjectName(ctx, p, cmd) + projectLabel, err := projectname.GetProjectName(ctx, params.Printer, cmd) if err != nil { - p.Debug(print.ErrorLevel, "get project name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get project name: %v", err) projectLabel = model.ProjectId } - p.Info("No rules found in security group %q for project %q\n", securityGroupLabel, projectLabel) + params.Printer.Info("No rules found in security group %q for project %q\n", securityGroupLabel, projectLabel) return nil } @@ -96,7 +97,7 @@ func NewCmd(p *print.Printer) *cobra.Command { items = items[:*model.Limit] } - return outputResult(p, model.OutputFormat, items) + return outputResult(params.Printer, model.OutputFormat, items) }, } configureFlags(cmd) diff --git a/internal/cmd/security-group/rule/list/list_test.go b/internal/cmd/security-group/rule/list/list_test.go index f72f89897..0b7a49b25 100644 --- a/internal/cmd/security-group/rule/list/list_test.go +++ b/internal/cmd/security-group/rule/list/list_test.go @@ -7,6 +7,7 @@ import ( "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" "github.com/google/uuid" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -141,7 +142,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -229,7 +230,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.securityGroupRules); (err != nil) != tt.wantErr { diff --git a/internal/cmd/security-group/rule/security_group_rule.go b/internal/cmd/security-group/rule/security_group_rule.go index c1e133841..75f394384 100644 --- a/internal/cmd/security-group/rule/security_group_rule.go +++ b/internal/cmd/security-group/rule/security_group_rule.go @@ -1,18 +1,18 @@ package rule import ( + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/cmd/security-group/rule/create" "github.com/stackitcloud/stackit-cli/internal/cmd/security-group/rule/delete" "github.com/stackitcloud/stackit-cli/internal/cmd/security-group/rule/describe" "github.com/stackitcloud/stackit-cli/internal/cmd/security-group/rule/list" "github.com/stackitcloud/stackit-cli/internal/pkg/args" - "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" "github.com/spf13/cobra" ) -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "rule", Short: "Provides functionality for security group rules", @@ -20,13 +20,13 @@ func NewCmd(p *print.Printer) *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd, p) + addSubcommands(cmd, params) return cmd } -func addSubcommands(cmd *cobra.Command, p *print.Printer) { - cmd.AddCommand(create.NewCmd(p)) - cmd.AddCommand(delete.NewCmd(p)) - cmd.AddCommand(describe.NewCmd(p)) - cmd.AddCommand(list.NewCmd(p)) +func addSubcommands(cmd *cobra.Command, params *params.CmdParams) { + cmd.AddCommand(create.NewCmd(params)) + cmd.AddCommand(delete.NewCmd(params)) + cmd.AddCommand(describe.NewCmd(params)) + cmd.AddCommand(list.NewCmd(params)) } diff --git a/internal/cmd/security-group/security_group.go b/internal/cmd/security-group/security_group.go index f20679d57..952da976c 100644 --- a/internal/cmd/security-group/security_group.go +++ b/internal/cmd/security-group/security_group.go @@ -1,6 +1,7 @@ package security_group import ( + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/cmd/security-group/create" "github.com/stackitcloud/stackit-cli/internal/cmd/security-group/delete" "github.com/stackitcloud/stackit-cli/internal/cmd/security-group/describe" @@ -8,14 +9,13 @@ import ( "github.com/stackitcloud/stackit-cli/internal/cmd/security-group/rule" "github.com/stackitcloud/stackit-cli/internal/cmd/security-group/update" "github.com/stackitcloud/stackit-cli/internal/pkg/args" - "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" "github.com/spf13/cobra" ) -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "security-group", Short: "Manage security groups", @@ -23,17 +23,17 @@ func NewCmd(p *print.Printer) *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd, p) + addSubcommands(cmd, params) return cmd } -func addSubcommands(cmd *cobra.Command, p *print.Printer) { +func addSubcommands(cmd *cobra.Command, params *params.CmdParams) { cmd.AddCommand( - rule.NewCmd(p), - create.NewCmd(p), - delete.NewCmd(p), - describe.NewCmd(p), - list.NewCmd(p), - update.NewCmd(p), + rule.NewCmd(params), + create.NewCmd(params), + delete.NewCmd(params), + describe.NewCmd(params), + list.NewCmd(params), + update.NewCmd(params), ) } diff --git a/internal/cmd/security-group/update/update.go b/internal/cmd/security-group/update/update.go index ddff90e23..a91c07971 100644 --- a/internal/cmd/security-group/update/update.go +++ b/internal/cmd/security-group/update/update.go @@ -5,6 +5,7 @@ import ( "fmt" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -34,7 +35,7 @@ const ( labelsArg = "labels" ) -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("update %s", groupNameArg), Short: "Updates a security group", @@ -46,32 +47,32 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } - projectLabel, err := projectname.GetProjectName(ctx, p, cmd) + projectLabel, err := projectname.GetProjectName(ctx, params.Printer, cmd) if err != nil { - p.Debug(print.ErrorLevel, "get project name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get project name: %v", err) projectLabel = model.ProjectId } groupLabel, err := iaasUtils.GetSecurityGroupName(ctx, apiClient, model.ProjectId, model.SecurityGroupId) if err != nil { - p.Warn("cannot retrieve groupname: %v", err) + params.Printer.Warn("cannot retrieve groupname: %v", err) groupLabel = model.SecurityGroupId } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to update the security group %q?", groupLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -84,7 +85,7 @@ func NewCmd(p *print.Printer) *cobra.Command { if err != nil { return fmt.Errorf("update security group: %w", err) } - p.Info("Updated security group \"%v\" for %q\n", utils.PtrString(resp.Name), projectLabel) + params.Printer.Info("Updated security group \"%v\" for %q\n", utils.PtrString(resp.Name), projectLabel) return nil }, diff --git a/internal/cmd/security-group/update/update_test.go b/internal/cmd/security-group/update/update_test.go index f27cbfc25..3bc10055a 100644 --- a/internal/cmd/security-group/update/update_test.go +++ b/internal/cmd/security-group/update/update_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -204,7 +205,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) if err := globalflags.Configure(cmd.Flags()); err != nil { t.Errorf("cannot configure global flags: %v", err) } diff --git a/internal/cmd/server/backup/backup.go b/internal/cmd/server/backup/backup.go index 8936ae65f..e29887143 100644 --- a/internal/cmd/server/backup/backup.go +++ b/internal/cmd/server/backup/backup.go @@ -1,6 +1,7 @@ package backup import ( + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/cmd/server/backup/create" del "github.com/stackitcloud/stackit-cli/internal/cmd/server/backup/delete" "github.com/stackitcloud/stackit-cli/internal/cmd/server/backup/describe" @@ -11,13 +12,12 @@ import ( "github.com/stackitcloud/stackit-cli/internal/cmd/server/backup/schedule" volumebackup "github.com/stackitcloud/stackit-cli/internal/cmd/server/backup/volume-backup" "github.com/stackitcloud/stackit-cli/internal/pkg/args" - "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" "github.com/spf13/cobra" ) -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "backup", Short: "Provides functionality for server backups", @@ -25,18 +25,18 @@ func NewCmd(p *print.Printer) *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd, p) + addSubcommands(cmd, params) return cmd } -func addSubcommands(cmd *cobra.Command, p *print.Printer) { - cmd.AddCommand(enable.NewCmd(p)) - cmd.AddCommand(disable.NewCmd(p)) - cmd.AddCommand(describe.NewCmd(p)) - cmd.AddCommand(list.NewCmd(p)) - cmd.AddCommand(schedule.NewCmd(p)) - cmd.AddCommand(create.NewCmd(p)) - cmd.AddCommand(restore.NewCmd(p)) - cmd.AddCommand(del.NewCmd(p)) - cmd.AddCommand(volumebackup.NewCmd(p)) +func addSubcommands(cmd *cobra.Command, params *params.CmdParams) { + cmd.AddCommand(enable.NewCmd(params)) + cmd.AddCommand(disable.NewCmd(params)) + cmd.AddCommand(describe.NewCmd(params)) + cmd.AddCommand(list.NewCmd(params)) + cmd.AddCommand(schedule.NewCmd(params)) + cmd.AddCommand(create.NewCmd(params)) + cmd.AddCommand(restore.NewCmd(params)) + cmd.AddCommand(del.NewCmd(params)) + cmd.AddCommand(volumebackup.NewCmd(params)) } diff --git a/internal/cmd/server/backup/create/create.go b/internal/cmd/server/backup/create/create.go index 28243cdc8..ba2aa95c5 100644 --- a/internal/cmd/server/backup/create/create.go +++ b/internal/cmd/server/backup/create/create.go @@ -6,6 +6,7 @@ import ( "fmt" "github.com/goccy/go-yaml" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" cliErr "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -39,7 +40,7 @@ type inputModel struct { BackupVolumeIds []string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "create", Short: "Creates a Server Backup.", @@ -56,23 +57,23 @@ func NewCmd(p *print.Printer) *cobra.Command { RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } serverLabel := model.ServerId // Get server name - if iaasApiClient, err := iaasClient.ConfigureClient(p); err == nil { + if iaasApiClient, err := iaasClient.ConfigureClient(params.Printer); err == nil { serverName, err := iaasUtils.GetServerName(ctx, iaasApiClient, model.ProjectId, model.ServerId) if err != nil { - p.Debug(print.ErrorLevel, "get server name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get server name: %v", err) } else if serverName != "" { serverLabel = serverName } @@ -80,7 +81,7 @@ func NewCmd(p *print.Printer) *cobra.Command { if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to create a Backup for server %s?", model.ServerId) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -96,7 +97,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("create Server Backup: %w", err) } - return outputResult(p, model.OutputFormat, serverLabel, *resp) + return outputResult(params.Printer, model.OutputFormat, serverLabel, *resp) }, } configureFlags(cmd) diff --git a/internal/cmd/server/backup/create/create_test.go b/internal/cmd/server/backup/create/create_test.go index b0d547f44..71a50ff6b 100644 --- a/internal/cmd/server/backup/create/create_test.go +++ b/internal/cmd/server/backup/create/create_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -131,7 +132,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -228,7 +229,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.serverLabel, tt.args.resp); (err != nil) != tt.wantErr { diff --git a/internal/cmd/server/backup/delete/delete.go b/internal/cmd/server/backup/delete/delete.go index 7805d20c0..a30dec646 100644 --- a/internal/cmd/server/backup/delete/delete.go +++ b/internal/cmd/server/backup/delete/delete.go @@ -4,6 +4,7 @@ import ( "context" "fmt" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -28,7 +29,7 @@ type inputModel struct { ServerId string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("delete %s", backupIdArg), Short: "Deletes a Server Backup.", @@ -41,20 +42,20 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to delete server backup %q? (This cannot be undone)", model.BackupId) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -67,7 +68,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("delete Server Backup: %w", err) } - p.Info("Triggered deletion of server backup %q\n", model.BackupId) + params.Printer.Info("Triggered deletion of server backup %q\n", model.BackupId) return nil }, } diff --git a/internal/cmd/server/backup/delete/delete_test.go b/internal/cmd/server/backup/delete/delete_test.go index d1f455b3d..d1de3562f 100644 --- a/internal/cmd/server/backup/delete/delete_test.go +++ b/internal/cmd/server/backup/delete/delete_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" @@ -130,7 +131,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/server/backup/describe/describe.go b/internal/cmd/server/backup/describe/describe.go index b1fe36663..9b3ad9247 100644 --- a/internal/cmd/server/backup/describe/describe.go +++ b/internal/cmd/server/backup/describe/describe.go @@ -7,6 +7,7 @@ import ( "strconv" "github.com/goccy/go-yaml" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -32,7 +33,7 @@ type inputModel struct { BackupId string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("describe %s", backupIdArg), Short: "Shows details of a Server Backup", @@ -48,12 +49,12 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -65,7 +66,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("read server backup: %w", err) } - return outputResult(p, model.OutputFormat, *resp) + return outputResult(params.Printer, model.OutputFormat, *resp) }, } configureFlags(cmd) diff --git a/internal/cmd/server/backup/describe/describe_test.go b/internal/cmd/server/backup/describe/describe_test.go index 2760e7e7c..c706734f1 100644 --- a/internal/cmd/server/backup/describe/describe_test.go +++ b/internal/cmd/server/backup/describe/describe_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" @@ -130,7 +131,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -236,7 +237,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.backup); (err != nil) != tt.wantErr { diff --git a/internal/cmd/server/backup/disable/disable.go b/internal/cmd/server/backup/disable/disable.go index 0a76151a5..a61dce696 100644 --- a/internal/cmd/server/backup/disable/disable.go +++ b/internal/cmd/server/backup/disable/disable.go @@ -4,6 +4,7 @@ import ( "context" "fmt" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -28,7 +29,7 @@ type inputModel struct { ServerId string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "disable", Short: "Disables Server Backup service", @@ -41,23 +42,23 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } serverLabel := model.ServerId // Get server name - if iaasApiClient, err := iaasClient.ConfigureClient(p); err == nil { + if iaasApiClient, err := iaasClient.ConfigureClient(params.Printer); err == nil { serverName, err := iaasUtils.GetServerName(ctx, iaasApiClient, model.ProjectId, model.ServerId) if err != nil { - p.Debug(print.ErrorLevel, "get server name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get server name: %v", err) } else if serverName != "" { serverLabel = serverName } @@ -68,13 +69,13 @@ func NewCmd(p *print.Printer) *cobra.Command { return err } if !canDisable { - p.Info("Cannot disable backup service for server %s - existing backups or existing backup schedules found\n", serverLabel) + params.Printer.Info("Cannot disable backup service for server %s - existing backups or existing backup schedules found\n", serverLabel) return nil } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to disable the backup service for server %s?", serverLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -87,7 +88,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("disable server backup service: %w", err) } - p.Info("Disabled Server Backup service for server %s\n", serverLabel) + params.Printer.Info("Disabled Server Backup service for server %s\n", serverLabel) return nil }, } diff --git a/internal/cmd/server/backup/enable/enable.go b/internal/cmd/server/backup/enable/enable.go index 9f0f48e1b..89265e748 100644 --- a/internal/cmd/server/backup/enable/enable.go +++ b/internal/cmd/server/backup/enable/enable.go @@ -5,6 +5,7 @@ import ( "fmt" "strings" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -28,7 +29,7 @@ type inputModel struct { ServerId string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "enable", Short: "Enables Server Backup service", @@ -41,23 +42,23 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } serverLabel := model.ServerId // Get server name - if iaasApiClient, err := iaasClient.ConfigureClient(p); err == nil { + if iaasApiClient, err := iaasClient.ConfigureClient(params.Printer); err == nil { serverName, err := iaasUtils.GetServerName(ctx, iaasApiClient, model.ProjectId, model.ServerId) if err != nil { - p.Debug(print.ErrorLevel, "get server name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get server name: %v", err) } else if serverName != "" { serverLabel = serverName } @@ -65,7 +66,7 @@ func NewCmd(p *print.Printer) *cobra.Command { if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to enable the Server Backup service for server %s?", serverLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -80,7 +81,7 @@ func NewCmd(p *print.Printer) *cobra.Command { } } - p.Info("Enabled backup service for server %s\n", serverLabel) + params.Printer.Info("Enabled backup service for server %s\n", serverLabel) return nil }, } diff --git a/internal/cmd/server/backup/list/list.go b/internal/cmd/server/backup/list/list.go index d7cbfeb6f..ccd29a624 100644 --- a/internal/cmd/server/backup/list/list.go +++ b/internal/cmd/server/backup/list/list.go @@ -7,6 +7,7 @@ import ( "github.com/goccy/go-yaml" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -32,7 +33,7 @@ type inputModel struct { Limit *int64 } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "list", Short: "Lists all server backups", @@ -48,13 +49,13 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -69,15 +70,15 @@ func NewCmd(p *print.Printer) *cobra.Command { if len(backups) == 0 { serverLabel := model.ServerId // Get server name - if iaasApiClient, err := iaasClient.ConfigureClient(p); err == nil { + if iaasApiClient, err := iaasClient.ConfigureClient(params.Printer); err == nil { serverName, err := iaasUtils.GetServerName(ctx, iaasApiClient, model.ProjectId, model.ServerId) if err != nil { - p.Debug(print.ErrorLevel, "get server name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get server name: %v", err) } else if serverName != "" { serverLabel = serverName } } - p.Info("No backups found for server %s\n", serverLabel) + params.Printer.Info("No backups found for server %s\n", serverLabel) return nil } @@ -85,7 +86,7 @@ func NewCmd(p *print.Printer) *cobra.Command { if model.Limit != nil && len(backups) > int(*model.Limit) { backups = backups[:*model.Limit] } - return outputResult(p, model.OutputFormat, backups) + return outputResult(params.Printer, model.OutputFormat, backups) }, } configureFlags(cmd) diff --git a/internal/cmd/server/backup/list/list_test.go b/internal/cmd/server/backup/list/list_test.go index c9f5b9c2e..218a17c6e 100644 --- a/internal/cmd/server/backup/list/list_test.go +++ b/internal/cmd/server/backup/list/list_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -117,7 +118,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -214,7 +215,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.backups); (err != nil) != tt.wantErr { diff --git a/internal/cmd/server/backup/restore/restore.go b/internal/cmd/server/backup/restore/restore.go index 67bbff395..445680053 100644 --- a/internal/cmd/server/backup/restore/restore.go +++ b/internal/cmd/server/backup/restore/restore.go @@ -4,6 +4,7 @@ import ( "context" "fmt" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -34,7 +35,7 @@ type inputModel struct { BackupVolumeIds []string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("restore %s", backupIdArg), Short: "Restores a Server Backup.", @@ -50,20 +51,20 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to restore server backup %q? (This cannot be undone)", model.BackupId) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -76,7 +77,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("restore Server Backup: %w", err) } - p.Info("Triggered restoring of server backup %q\n", model.BackupId) + params.Printer.Info("Triggered restoring of server backup %q\n", model.BackupId) return nil }, } diff --git a/internal/cmd/server/backup/restore/restore_test.go b/internal/cmd/server/backup/restore/restore_test.go index c352d56cd..ff14455c2 100644 --- a/internal/cmd/server/backup/restore/restore_test.go +++ b/internal/cmd/server/backup/restore/restore_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" @@ -132,7 +133,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/server/backup/schedule/create/create.go b/internal/cmd/server/backup/schedule/create/create.go index 716e0d5c0..09b834c50 100644 --- a/internal/cmd/server/backup/schedule/create/create.go +++ b/internal/cmd/server/backup/schedule/create/create.go @@ -6,6 +6,7 @@ import ( "fmt" "github.com/goccy/go-yaml" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" cliErr "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -47,7 +48,7 @@ type inputModel struct { BackupVolumeIds []string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "create", Short: "Creates a Server Backup Schedule", @@ -64,23 +65,23 @@ func NewCmd(p *print.Printer) *cobra.Command { RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } serverLabel := model.ServerId // Get server name - if iaasApiClient, err := iaasClient.ConfigureClient(p); err == nil { + if iaasApiClient, err := iaasClient.ConfigureClient(params.Printer); err == nil { serverName, err := iaasUtils.GetServerName(ctx, iaasApiClient, model.ProjectId, model.ServerId) if err != nil { - p.Debug(print.ErrorLevel, "get server name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get server name: %v", err) } else if serverName != "" { serverLabel = serverName } @@ -88,7 +89,7 @@ func NewCmd(p *print.Printer) *cobra.Command { if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to create a Backup Schedule for server %s?", serverLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -104,7 +105,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("create Server Backup Schedule: %w", err) } - return outputResult(p, model.OutputFormat, serverLabel, *resp) + return outputResult(params.Printer, model.OutputFormat, serverLabel, *resp) }, } configureFlags(cmd) diff --git a/internal/cmd/server/backup/schedule/create/create_test.go b/internal/cmd/server/backup/schedule/create/create_test.go index 00ec85560..ceb92188b 100644 --- a/internal/cmd/server/backup/schedule/create/create_test.go +++ b/internal/cmd/server/backup/schedule/create/create_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -142,7 +143,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -238,7 +239,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.serverLabel, tt.args.resp); (err != nil) != tt.wantErr { diff --git a/internal/cmd/server/backup/schedule/delete/delete.go b/internal/cmd/server/backup/schedule/delete/delete.go index e2dcaa7a0..69dc81579 100644 --- a/internal/cmd/server/backup/schedule/delete/delete.go +++ b/internal/cmd/server/backup/schedule/delete/delete.go @@ -4,6 +4,7 @@ import ( "context" "fmt" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -29,7 +30,7 @@ type inputModel struct { ServerId string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("delete %s", scheduleIdArg), Short: "Deletes a Server Backup Schedule", @@ -42,23 +43,23 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } serverLabel := model.ServerId // Get server name - if iaasApiClient, err := iaasClient.ConfigureClient(p); err == nil { + if iaasApiClient, err := iaasClient.ConfigureClient(params.Printer); err == nil { serverName, err := iaasUtils.GetServerName(ctx, iaasApiClient, model.ProjectId, model.ServerId) if err != nil { - p.Debug(print.ErrorLevel, "get server name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get server name: %v", err) } else if serverName != "" { serverLabel = serverName } @@ -66,7 +67,7 @@ func NewCmd(p *print.Printer) *cobra.Command { if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to delete server backup schedule %q? (This cannot be undone)", serverLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -79,7 +80,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("delete Server Backup Schedule: %w", err) } - p.Info("Deleted server backup schedule %q\n", model.ScheduleId) + params.Printer.Info("Deleted server backup schedule %q\n", model.ScheduleId) return nil }, } diff --git a/internal/cmd/server/backup/schedule/delete/delete_test.go b/internal/cmd/server/backup/schedule/delete/delete_test.go index 7915f1c07..26c218485 100644 --- a/internal/cmd/server/backup/schedule/delete/delete_test.go +++ b/internal/cmd/server/backup/schedule/delete/delete_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" @@ -130,7 +131,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/server/backup/schedule/describe/describe.go b/internal/cmd/server/backup/schedule/describe/describe.go index 990032499..cc838e0a8 100644 --- a/internal/cmd/server/backup/schedule/describe/describe.go +++ b/internal/cmd/server/backup/schedule/describe/describe.go @@ -6,6 +6,7 @@ import ( "fmt" "github.com/goccy/go-yaml" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -31,7 +32,7 @@ type inputModel struct { BackupScheduleId string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("describe %s", backupScheduleIdArg), Short: "Shows details of a Server Backup Schedule", @@ -47,12 +48,12 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -64,7 +65,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("read server backup schedule: %w", err) } - return outputResult(p, model.OutputFormat, *resp) + return outputResult(params.Printer, model.OutputFormat, *resp) }, } configureFlags(cmd) diff --git a/internal/cmd/server/backup/schedule/describe/describe_test.go b/internal/cmd/server/backup/schedule/describe/describe_test.go index f94627dc3..edc952d18 100644 --- a/internal/cmd/server/backup/schedule/describe/describe_test.go +++ b/internal/cmd/server/backup/schedule/describe/describe_test.go @@ -7,6 +7,7 @@ import ( "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" "github.com/google/uuid" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-sdk-go/services/serverbackup" @@ -129,7 +130,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -263,7 +264,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.schedule); (err != nil) != tt.wantErr { diff --git a/internal/cmd/server/backup/schedule/list/list.go b/internal/cmd/server/backup/schedule/list/list.go index 2920e9853..3ad1c840e 100644 --- a/internal/cmd/server/backup/schedule/list/list.go +++ b/internal/cmd/server/backup/schedule/list/list.go @@ -7,6 +7,7 @@ import ( "github.com/goccy/go-yaml" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -32,7 +33,7 @@ type inputModel struct { Limit *int64 } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "list", Short: "Lists all server backup schedules", @@ -48,23 +49,23 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } serverLabel := model.ServerId // Get server name - if iaasApiClient, err := iaasClient.ConfigureClient(p); err == nil { + if iaasApiClient, err := iaasClient.ConfigureClient(params.Printer); err == nil { serverName, err := iaasUtils.GetServerName(ctx, iaasApiClient, model.ProjectId, model.ServerId) if err != nil { - p.Debug(print.ErrorLevel, "get server name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get server name: %v", err) } else if serverName != "" { serverLabel = serverName } @@ -78,7 +79,7 @@ func NewCmd(p *print.Printer) *cobra.Command { } schedules := *resp.Items if len(schedules) == 0 { - p.Info("No backup schedules found for server %s\n", serverLabel) + params.Printer.Info("No backup schedules found for server %s\n", serverLabel) return nil } @@ -86,7 +87,7 @@ func NewCmd(p *print.Printer) *cobra.Command { if model.Limit != nil && len(schedules) > int(*model.Limit) { schedules = schedules[:*model.Limit] } - return outputResult(p, model.OutputFormat, schedules) + return outputResult(params.Printer, model.OutputFormat, schedules) }, } configureFlags(cmd) diff --git a/internal/cmd/server/backup/schedule/list/list_test.go b/internal/cmd/server/backup/schedule/list/list_test.go index 6c055bcef..c88ec459e 100644 --- a/internal/cmd/server/backup/schedule/list/list_test.go +++ b/internal/cmd/server/backup/schedule/list/list_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -117,7 +118,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -225,7 +226,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.schedules); (err != nil) != tt.wantErr { diff --git a/internal/cmd/server/backup/schedule/schedule.go b/internal/cmd/server/backup/schedule/schedule.go index 423486fde..1ce797c4f 100644 --- a/internal/cmd/server/backup/schedule/schedule.go +++ b/internal/cmd/server/backup/schedule/schedule.go @@ -1,19 +1,19 @@ package schedule import ( + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/cmd/server/backup/schedule/create" del "github.com/stackitcloud/stackit-cli/internal/cmd/server/backup/schedule/delete" "github.com/stackitcloud/stackit-cli/internal/cmd/server/backup/schedule/describe" "github.com/stackitcloud/stackit-cli/internal/cmd/server/backup/schedule/list" "github.com/stackitcloud/stackit-cli/internal/cmd/server/backup/schedule/update" "github.com/stackitcloud/stackit-cli/internal/pkg/args" - "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" "github.com/spf13/cobra" ) -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "schedule", Short: "Provides functionality for Server Backup Schedule", @@ -21,14 +21,14 @@ func NewCmd(p *print.Printer) *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd, p) + addSubcommands(cmd, params) return cmd } -func addSubcommands(cmd *cobra.Command, p *print.Printer) { - cmd.AddCommand(list.NewCmd(p)) - cmd.AddCommand(describe.NewCmd(p)) - cmd.AddCommand(create.NewCmd(p)) - cmd.AddCommand(del.NewCmd(p)) - cmd.AddCommand(update.NewCmd(p)) +func addSubcommands(cmd *cobra.Command, params *params.CmdParams) { + cmd.AddCommand(list.NewCmd(params)) + cmd.AddCommand(describe.NewCmd(params)) + cmd.AddCommand(create.NewCmd(params)) + cmd.AddCommand(del.NewCmd(params)) + cmd.AddCommand(update.NewCmd(params)) } diff --git a/internal/cmd/server/backup/schedule/update/update.go b/internal/cmd/server/backup/schedule/update/update.go index 9885cc2a1..4bc895fc0 100644 --- a/internal/cmd/server/backup/schedule/update/update.go +++ b/internal/cmd/server/backup/schedule/update/update.go @@ -6,6 +6,7 @@ import ( "fmt" "github.com/goccy/go-yaml" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" cliErr "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -48,7 +49,7 @@ type inputModel struct { BackupVolumeIds []string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("update %s", scheduleIdArg), Short: "Updates a Server Backup Schedule", @@ -65,26 +66,26 @@ func NewCmd(p *print.Printer) *cobra.Command { RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } currentBackupSchedule, err := apiClient.GetBackupScheduleExecute(ctx, model.ProjectId, model.ServerId, model.Region, model.BackupScheduleId) if err != nil { - p.Debug(print.ErrorLevel, "get current server backup schedule: %v", err) + params.Printer.Debug(print.ErrorLevel, "get current server backup schedule: %v", err) return err } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to update Server Backup Schedule %q?", model.BackupScheduleId) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -100,7 +101,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("update Server Backup Schedule: %w", err) } - return outputResult(p, model.OutputFormat, *resp) + return outputResult(params.Printer, model.OutputFormat, *resp) }, } configureFlags(cmd) diff --git a/internal/cmd/server/backup/schedule/update/update_test.go b/internal/cmd/server/backup/schedule/update/update_test.go index 8b127b235..263a9188a 100644 --- a/internal/cmd/server/backup/schedule/update/update_test.go +++ b/internal/cmd/server/backup/schedule/update/update_test.go @@ -5,6 +5,7 @@ import ( "strconv" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -183,7 +184,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -295,7 +296,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.resp); (err != nil) != tt.wantErr { diff --git a/internal/cmd/server/backup/volume-backup/delete/delete.go b/internal/cmd/server/backup/volume-backup/delete/delete.go index 9e3de47a8..1b924fe85 100644 --- a/internal/cmd/server/backup/volume-backup/delete/delete.go +++ b/internal/cmd/server/backup/volume-backup/delete/delete.go @@ -4,6 +4,7 @@ import ( "context" "fmt" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -30,7 +31,7 @@ type inputModel struct { ServerId string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("delete %s", volumeBackupIdArg), Short: "Deletes a Server Volume Backup.", @@ -43,20 +44,20 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to delete server volume backup %q? (This cannot be undone)", model.VolumeId) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -69,7 +70,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("delete Server Volume Backup: %w", err) } - p.Info("Triggered deletion of server volume backup %q\n", model.VolumeId) + params.Printer.Info("Triggered deletion of server volume backup %q\n", model.VolumeId) return nil }, } diff --git a/internal/cmd/server/backup/volume-backup/delete/delete_test.go b/internal/cmd/server/backup/volume-backup/delete/delete_test.go index 59af68770..34859934a 100644 --- a/internal/cmd/server/backup/volume-backup/delete/delete_test.go +++ b/internal/cmd/server/backup/volume-backup/delete/delete_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" @@ -133,7 +134,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/server/backup/volume-backup/restore/restore.go b/internal/cmd/server/backup/volume-backup/restore/restore.go index 8e0881d1f..db7c6b543 100644 --- a/internal/cmd/server/backup/volume-backup/restore/restore.go +++ b/internal/cmd/server/backup/volume-backup/restore/restore.go @@ -4,6 +4,7 @@ import ( "context" "fmt" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -32,7 +33,7 @@ type inputModel struct { RestoreVolumeId string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("restore %s", volumeBackupIdArg), Short: "Restore a Server Volume Backup to a volume.", @@ -45,20 +46,20 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to restore volume backup %q? (This cannot be undone)", model.VolumeBackupId) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -71,7 +72,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("restore Server Volume Backup: %w", err) } - p.Info("Triggered restoring of server volume backup %q\n", model.VolumeBackupId) + params.Printer.Info("Triggered restoring of server volume backup %q\n", model.VolumeBackupId) return nil }, } diff --git a/internal/cmd/server/backup/volume-backup/restore/restore_test.go b/internal/cmd/server/backup/volume-backup/restore/restore_test.go index cb3701baa..21da9b128 100644 --- a/internal/cmd/server/backup/volume-backup/restore/restore_test.go +++ b/internal/cmd/server/backup/volume-backup/restore/restore_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" @@ -139,7 +140,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/server/backup/volume-backup/volumebackup.go b/internal/cmd/server/backup/volume-backup/volumebackup.go index 7a4024eb4..e0d3b6d62 100644 --- a/internal/cmd/server/backup/volume-backup/volumebackup.go +++ b/internal/cmd/server/backup/volume-backup/volumebackup.go @@ -1,16 +1,16 @@ package volumebackup import ( + "github.com/stackitcloud/stackit-cli/internal/cmd/params" del "github.com/stackitcloud/stackit-cli/internal/cmd/server/backup/volume-backup/delete" "github.com/stackitcloud/stackit-cli/internal/cmd/server/backup/volume-backup/restore" "github.com/stackitcloud/stackit-cli/internal/pkg/args" - "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" "github.com/spf13/cobra" ) -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "volume-backup", Short: "Provides functionality for Server Backup Volume Backups", @@ -18,11 +18,11 @@ func NewCmd(p *print.Printer) *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd, p) + addSubcommands(cmd, params) return cmd } -func addSubcommands(cmd *cobra.Command, p *print.Printer) { - cmd.AddCommand(del.NewCmd(p)) - cmd.AddCommand(restore.NewCmd(p)) +func addSubcommands(cmd *cobra.Command, params *params.CmdParams) { + cmd.AddCommand(del.NewCmd(params)) + cmd.AddCommand(restore.NewCmd(params)) } diff --git a/internal/cmd/server/command/command.go b/internal/cmd/server/command/command.go index 9347fb111..e6c2d79a8 100644 --- a/internal/cmd/server/command/command.go +++ b/internal/cmd/server/command/command.go @@ -1,18 +1,18 @@ package command import ( + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/cmd/server/command/create" "github.com/stackitcloud/stackit-cli/internal/cmd/server/command/describe" "github.com/stackitcloud/stackit-cli/internal/cmd/server/command/list" "github.com/stackitcloud/stackit-cli/internal/cmd/server/command/template" "github.com/stackitcloud/stackit-cli/internal/pkg/args" - "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" "github.com/spf13/cobra" ) -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "command", Short: "Provides functionality for Server Command", @@ -20,13 +20,13 @@ func NewCmd(p *print.Printer) *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd, p) + addSubcommands(cmd, params) return cmd } -func addSubcommands(cmd *cobra.Command, p *print.Printer) { - cmd.AddCommand(create.NewCmd(p)) - cmd.AddCommand(describe.NewCmd(p)) - cmd.AddCommand(list.NewCmd(p)) - cmd.AddCommand(template.NewCmd(p)) +func addSubcommands(cmd *cobra.Command, params *params.CmdParams) { + cmd.AddCommand(create.NewCmd(params)) + cmd.AddCommand(describe.NewCmd(params)) + cmd.AddCommand(list.NewCmd(params)) + cmd.AddCommand(template.NewCmd(params)) } diff --git a/internal/cmd/server/command/create/create.go b/internal/cmd/server/command/create/create.go index ed65528ac..748d50b9b 100644 --- a/internal/cmd/server/command/create/create.go +++ b/internal/cmd/server/command/create/create.go @@ -7,6 +7,7 @@ import ( "github.com/goccy/go-yaml" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" cliErr "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -35,7 +36,7 @@ type inputModel struct { Params *map[string]string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "create", Short: "Creates a Server Command", @@ -52,23 +53,23 @@ func NewCmd(p *print.Printer) *cobra.Command { RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } serverLabel := model.ServerId // Get server name - if iaasApiClient, err := iaasClient.ConfigureClient(p); err == nil { + if iaasApiClient, err := iaasClient.ConfigureClient(params.Printer); err == nil { serverName, err := iaasUtils.GetServerName(ctx, iaasApiClient, model.ProjectId, model.ServerId) if err != nil { - p.Debug(print.ErrorLevel, "get server name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get server name: %v", err) } else if serverName != "" { serverLabel = serverName } @@ -76,7 +77,7 @@ func NewCmd(p *print.Printer) *cobra.Command { if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to create a Command for server %s?", serverLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -92,7 +93,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("create Server Command: %w", err) } - return outputResult(p, model.OutputFormat, serverLabel, *resp) + return outputResult(params.Printer, model.OutputFormat, serverLabel, *resp) }, } configureFlags(cmd) diff --git a/internal/cmd/server/command/create/create_test.go b/internal/cmd/server/command/create/create_test.go index b7b3657ca..eed029584 100644 --- a/internal/cmd/server/command/create/create_test.go +++ b/internal/cmd/server/command/create/create_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -127,7 +128,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -223,7 +224,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.serverLabel, tt.args.resp); (err != nil) != tt.wantErr { diff --git a/internal/cmd/server/command/describe/describe.go b/internal/cmd/server/command/describe/describe.go index 6eb4359c7..a93d66988 100644 --- a/internal/cmd/server/command/describe/describe.go +++ b/internal/cmd/server/command/describe/describe.go @@ -7,6 +7,7 @@ import ( "github.com/goccy/go-yaml" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -30,7 +31,7 @@ type inputModel struct { CommandId string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("describe %s", commandIdArg), Short: "Shows details of a Server Command", @@ -46,12 +47,12 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -63,7 +64,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("read server command: %w", err) } - return outputResult(p, model.OutputFormat, *resp) + return outputResult(params.Printer, model.OutputFormat, *resp) }, } configureFlags(cmd) diff --git a/internal/cmd/server/command/describe/describe_test.go b/internal/cmd/server/command/describe/describe_test.go index c9d1f3d86..59363f0e7 100644 --- a/internal/cmd/server/command/describe/describe_test.go +++ b/internal/cmd/server/command/describe/describe_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" @@ -163,7 +164,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -261,7 +262,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.command); (err != nil) != tt.wantErr { diff --git a/internal/cmd/server/command/list/list.go b/internal/cmd/server/command/list/list.go index 4a00abf57..f5deb62d1 100644 --- a/internal/cmd/server/command/list/list.go +++ b/internal/cmd/server/command/list/list.go @@ -7,6 +7,7 @@ import ( "github.com/goccy/go-yaml" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -32,7 +33,7 @@ type inputModel struct { Limit *int64 } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "list", Short: "Lists all server commands", @@ -48,23 +49,23 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } serverLabel := model.ServerId // Get server name - if iaasApiClient, err := iaasClient.ConfigureClient(p); err == nil { + if iaasApiClient, err := iaasClient.ConfigureClient(params.Printer); err == nil { serverName, err := iaasUtils.GetServerName(ctx, iaasApiClient, model.ProjectId, model.ServerId) if err != nil { - p.Debug(print.ErrorLevel, "get server name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get server name: %v", err) } else if serverName != "" { serverLabel = serverName } @@ -77,7 +78,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("list server commands: %w", err) } if commands := resp.Items; commands == nil || len(*commands) == 0 { - p.Info("No commands found for server %s\n", serverLabel) + params.Printer.Info("No commands found for server %s\n", serverLabel) return nil } commands := *resp.Items @@ -85,7 +86,7 @@ func NewCmd(p *print.Printer) *cobra.Command { if model.Limit != nil && len(commands) > int(*model.Limit) { commands = commands[:*model.Limit] } - return outputResult(p, model.OutputFormat, commands) + return outputResult(params.Printer, model.OutputFormat, commands) }, } configureFlags(cmd) diff --git a/internal/cmd/server/command/list/list_test.go b/internal/cmd/server/command/list/list_test.go index 6740fac38..7d6ce40fb 100644 --- a/internal/cmd/server/command/list/list_test.go +++ b/internal/cmd/server/command/list/list_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -120,7 +121,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -208,7 +209,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.commands); (err != nil) != tt.wantErr { diff --git a/internal/cmd/server/command/template/describe/describe.go b/internal/cmd/server/command/template/describe/describe.go index 816bfa8be..a694b9cad 100644 --- a/internal/cmd/server/command/template/describe/describe.go +++ b/internal/cmd/server/command/template/describe/describe.go @@ -7,6 +7,7 @@ import ( "github.com/goccy/go-yaml" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -30,7 +31,7 @@ type inputModel struct { CommandTemplateName string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("describe %s", commandTemplateNameArg), Short: "Shows details of a Server Command Template", @@ -46,12 +47,12 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -63,7 +64,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("read server command template: %w", err) } - return outputResult(p, model.OutputFormat, *resp) + return outputResult(params.Printer, model.OutputFormat, *resp) }, } configureFlags(cmd) diff --git a/internal/cmd/server/command/template/describe/describe_test.go b/internal/cmd/server/command/template/describe/describe_test.go index 85b97a8ea..9fa7ba208 100644 --- a/internal/cmd/server/command/template/describe/describe_test.go +++ b/internal/cmd/server/command/template/describe/describe_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" @@ -163,7 +164,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -261,7 +262,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.commandTemplate); (err != nil) != tt.wantErr { diff --git a/internal/cmd/server/command/template/list/list.go b/internal/cmd/server/command/template/list/list.go index 079c4f5f7..439df690d 100644 --- a/internal/cmd/server/command/template/list/list.go +++ b/internal/cmd/server/command/template/list/list.go @@ -7,6 +7,7 @@ import ( "github.com/goccy/go-yaml" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -28,7 +29,7 @@ type inputModel struct { Limit *int64 } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "list", Short: "Lists all server command templates", @@ -44,13 +45,13 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -62,7 +63,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("list server command templates: %w", err) } if templates := resp.Items; templates == nil || len(*templates) == 0 { - p.Info("No commands templates found\n") + params.Printer.Info("No commands templates found\n") return nil } templates := *resp.Items @@ -71,7 +72,7 @@ func NewCmd(p *print.Printer) *cobra.Command { if model.Limit != nil && len(templates) > int(*model.Limit) { templates = templates[:*model.Limit] } - return outputResult(p, model.OutputFormat, templates) + return outputResult(params.Printer, model.OutputFormat, templates) }, } configureFlags(cmd) diff --git a/internal/cmd/server/command/template/list/list_test.go b/internal/cmd/server/command/template/list/list_test.go index 309716c16..0ff491e5d 100644 --- a/internal/cmd/server/command/template/list/list_test.go +++ b/internal/cmd/server/command/template/list/list_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -111,7 +112,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -207,7 +208,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.templates); (err != nil) != tt.wantErr { diff --git a/internal/cmd/server/command/template/template.go b/internal/cmd/server/command/template/template.go index 03f9b13da..6b41f434b 100644 --- a/internal/cmd/server/command/template/template.go +++ b/internal/cmd/server/command/template/template.go @@ -1,16 +1,16 @@ package template import ( + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/cmd/server/command/template/describe" "github.com/stackitcloud/stackit-cli/internal/cmd/server/command/template/list" "github.com/stackitcloud/stackit-cli/internal/pkg/args" - "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" "github.com/spf13/cobra" ) -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "template", Short: "Provides functionality for Server Command Template", @@ -18,11 +18,11 @@ func NewCmd(p *print.Printer) *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd, p) + addSubcommands(cmd, params) return cmd } -func addSubcommands(cmd *cobra.Command, p *print.Printer) { - cmd.AddCommand(describe.NewCmd(p)) - cmd.AddCommand(list.NewCmd(p)) +func addSubcommands(cmd *cobra.Command, params *params.CmdParams) { + cmd.AddCommand(describe.NewCmd(params)) + cmd.AddCommand(list.NewCmd(params)) } diff --git a/internal/cmd/server/console/console.go b/internal/cmd/server/console/console.go index a9964619c..35e5f814a 100644 --- a/internal/cmd/server/console/console.go +++ b/internal/cmd/server/console/console.go @@ -7,6 +7,7 @@ import ( "net/url" "github.com/goccy/go-yaml" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -29,7 +30,7 @@ type inputModel struct { ServerId string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("console %s", serverIdArg), Short: "Gets a URL for server remote console", @@ -47,20 +48,20 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } serverLabel, err := iaasUtils.GetServerName(ctx, apiClient, model.ProjectId, model.ServerId) if err != nil { - p.Debug(print.ErrorLevel, "get server name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get server name: %v", err) serverLabel = model.ServerId } else if serverLabel == "" { serverLabel = model.ServerId @@ -73,7 +74,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("server console: %w", err) } - return outputResult(p, model.OutputFormat, serverLabel, *resp) + return outputResult(params.Printer, model.OutputFormat, serverLabel, *resp) }, } return cmd diff --git a/internal/cmd/server/console/console_test.go b/internal/cmd/server/console/console_test.go index 4d9b9e6c1..9b6c0413c 100644 --- a/internal/cmd/server/console/console_test.go +++ b/internal/cmd/server/console/console_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -129,7 +130,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -234,7 +235,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.serverLabel, tt.args.serverUrl); (err != nil) != tt.wantErr { diff --git a/internal/cmd/server/create/create.go b/internal/cmd/server/create/create.go index 69cfc627b..25544b3c8 100644 --- a/internal/cmd/server/create/create.go +++ b/internal/cmd/server/create/create.go @@ -6,6 +6,7 @@ import ( "fmt" "github.com/goccy/go-yaml" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" cliErr "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -65,7 +66,7 @@ type inputModel struct { Volumes *[]string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "create", Short: "Creates a server", @@ -111,26 +112,26 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } - projectLabel, err := projectname.GetProjectName(ctx, p, cmd) + projectLabel, err := projectname.GetProjectName(ctx, params.Printer, cmd) if err != nil { - p.Debug(print.ErrorLevel, "get project name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get project name: %v", err) projectLabel = model.ProjectId } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to create a server for project %q?", projectLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -146,7 +147,7 @@ func NewCmd(p *print.Printer) *cobra.Command { // Wait for async operation, if async mode not enabled if !model.Async { - s := spinner.New(p) + s := spinner.New(params.Printer) s.Start("Creating server") _, err = wait.CreateServerWaitHandler(ctx, apiClient, model.ProjectId, serverId).WaitWithContext(ctx) if err != nil { @@ -155,7 +156,7 @@ func NewCmd(p *print.Printer) *cobra.Command { s.Stop() } - return outputResult(p, model.OutputFormat, projectLabel, resp) + return outputResult(params.Printer, model.OutputFormat, projectLabel, resp) }, } configureFlags(cmd) diff --git a/internal/cmd/server/create/create_test.go b/internal/cmd/server/create/create_test.go index bf8f815d5..1548691d4 100644 --- a/internal/cmd/server/create/create_test.go +++ b/internal/cmd/server/create/create_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -326,7 +327,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -434,7 +435,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.projectLabel, tt.args.server); (err != nil) != tt.wantErr { diff --git a/internal/cmd/server/deallocate/deallocate.go b/internal/cmd/server/deallocate/deallocate.go index 53a01de69..f51fab931 100644 --- a/internal/cmd/server/deallocate/deallocate.go +++ b/internal/cmd/server/deallocate/deallocate.go @@ -4,6 +4,7 @@ import ( "context" "fmt" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -28,7 +29,7 @@ type inputModel struct { ServerId string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("deallocate %s", serverIdArg), Short: "Deallocates an existing server", @@ -42,20 +43,20 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } serverLabel, err := iaasUtils.GetServerName(ctx, apiClient, model.ProjectId, model.ServerId) if err != nil { - p.Debug(print.ErrorLevel, "get server name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get server name: %v", err) serverLabel = model.ServerId } else if serverLabel == "" { serverLabel = model.ServerId @@ -63,7 +64,7 @@ func NewCmd(p *print.Printer) *cobra.Command { if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to deallocate server %q?", serverLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -78,7 +79,7 @@ func NewCmd(p *print.Printer) *cobra.Command { // Wait for async operation, if async mode not enabled if !model.Async { - s := spinner.New(p) + s := spinner.New(params.Printer) s.Start("Deallocating server") _, err = wait.DeallocateServerWaitHandler(ctx, apiClient, model.ProjectId, model.ServerId).WaitWithContext(ctx) if err != nil { @@ -91,7 +92,7 @@ func NewCmd(p *print.Printer) *cobra.Command { if model.Async { operationState = "Triggered deallocation of" } - p.Info("%s server %q\n", operationState, serverLabel) + params.Printer.Info("%s server %q\n", operationState, serverLabel) return nil }, diff --git a/internal/cmd/server/deallocate/deallocate_test.go b/internal/cmd/server/deallocate/deallocate_test.go index 6ededf983..379fc3a58 100644 --- a/internal/cmd/server/deallocate/deallocate_test.go +++ b/internal/cmd/server/deallocate/deallocate_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" @@ -128,7 +129,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/server/delete/delete.go b/internal/cmd/server/delete/delete.go index 77ad35479..d9a366c2e 100644 --- a/internal/cmd/server/delete/delete.go +++ b/internal/cmd/server/delete/delete.go @@ -4,6 +4,7 @@ import ( "context" "fmt" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -28,7 +29,7 @@ type inputModel struct { ServerId string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("delete %s", serverIdArg), Short: "Deletes a server", @@ -45,20 +46,20 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } serverLabel, err := iaasUtils.GetServerName(ctx, apiClient, model.ProjectId, model.ServerId) if err != nil { - p.Debug(print.ErrorLevel, "get server name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get server name: %v", err) serverLabel = model.ServerId } else if serverLabel == "" { serverLabel = model.ProjectId @@ -66,7 +67,7 @@ func NewCmd(p *print.Printer) *cobra.Command { if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to delete server %q?", serverLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -81,7 +82,7 @@ func NewCmd(p *print.Printer) *cobra.Command { // Wait for async operation, if async mode not enabled if !model.Async { - s := spinner.New(p) + s := spinner.New(params.Printer) s.Start("Deleting server") _, err = wait.DeleteServerWaitHandler(ctx, apiClient, model.ProjectId, model.ServerId).WaitWithContext(ctx) if err != nil { @@ -94,7 +95,7 @@ func NewCmd(p *print.Printer) *cobra.Command { if model.Async { operationState = "Triggered deletion of" } - p.Info("%s server %q\n", operationState, serverLabel) + params.Printer.Info("%s server %q\n", operationState, serverLabel) return nil }, } diff --git a/internal/cmd/server/delete/delete_test.go b/internal/cmd/server/delete/delete_test.go index 3b7c0ba31..1c04e26cb 100644 --- a/internal/cmd/server/delete/delete_test.go +++ b/internal/cmd/server/delete/delete_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" @@ -138,7 +139,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/server/describe/describe.go b/internal/cmd/server/describe/describe.go index d45086fb2..4873e688c 100644 --- a/internal/cmd/server/describe/describe.go +++ b/internal/cmd/server/describe/describe.go @@ -6,6 +6,7 @@ import ( "fmt" "strings" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -29,7 +30,7 @@ type inputModel struct { ServerId string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("describe %s", serverIdArg), Short: "Shows details of a server", @@ -47,13 +48,13 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -65,7 +66,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("read server: %w", err) } - return outputResult(p, model.OutputFormat, resp) + return outputResult(params.Printer, model.OutputFormat, resp) }, } return cmd diff --git a/internal/cmd/server/describe/describe_test.go b/internal/cmd/server/describe/describe_test.go index d16a3ec04..86052e8b7 100644 --- a/internal/cmd/server/describe/describe_test.go +++ b/internal/cmd/server/describe/describe_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" @@ -139,7 +140,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -243,7 +244,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.server); (err != nil) != tt.wantErr { diff --git a/internal/cmd/server/list/list.go b/internal/cmd/server/list/list.go index db72cdb26..867a0e233 100644 --- a/internal/cmd/server/list/list.go +++ b/internal/cmd/server/list/list.go @@ -5,6 +5,7 @@ import ( "encoding/json" "fmt" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -32,7 +33,7 @@ type inputModel struct { LabelSelector *string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "list", Short: "Lists all servers of a project", @@ -58,13 +59,13 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -77,12 +78,12 @@ func NewCmd(p *print.Printer) *cobra.Command { } if resp.Items == nil || len(*resp.Items) == 0 { - projectLabel, err := projectname.GetProjectName(ctx, p, cmd) + projectLabel, err := projectname.GetProjectName(ctx, params.Printer, cmd) if err != nil { - p.Debug(print.ErrorLevel, "get project name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get project name: %v", err) projectLabel = model.ProjectId } - p.Info("No servers found for project %q\n", projectLabel) + params.Printer.Info("No servers found for project %q\n", projectLabel) return nil } @@ -92,7 +93,7 @@ func NewCmd(p *print.Printer) *cobra.Command { items = items[:*model.Limit] } - return outputResult(p, model.OutputFormat, items) + return outputResult(params.Printer, model.OutputFormat, items) }, } configureFlags(cmd) diff --git a/internal/cmd/server/list/list_test.go b/internal/cmd/server/list/list_test.go index a8dfe0e1e..64fe0ddb4 100644 --- a/internal/cmd/server/list/list_test.go +++ b/internal/cmd/server/list/list_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -133,7 +134,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -221,7 +222,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.servers); (err != nil) != tt.wantErr { diff --git a/internal/cmd/server/log/log.go b/internal/cmd/server/log/log.go index 646417b42..76e80cc67 100644 --- a/internal/cmd/server/log/log.go +++ b/internal/cmd/server/log/log.go @@ -7,6 +7,7 @@ import ( "strings" "github.com/goccy/go-yaml" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -34,7 +35,7 @@ type inputModel struct { Length *int64 } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("log %s", serverIdArg), Short: "Gets server console log", @@ -56,20 +57,20 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } serverLabel, err := iaasUtils.GetServerName(ctx, apiClient, model.ProjectId, model.ServerId) if err != nil { - p.Debug(print.ErrorLevel, "get server name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get server name: %v", err) serverLabel = model.ServerId } else if serverLabel == "" { serverLabel = model.ServerId @@ -88,10 +89,10 @@ func NewCmd(p *print.Printer) *cobra.Command { if len(lines) > int(*model.Length) { // Truncate output and show most recent logs start := len(lines) - int(*model.Length) - return outputResult(p, model.OutputFormat, serverLabel, strings.Join(lines[start:], "\n")) + return outputResult(params.Printer, model.OutputFormat, serverLabel, strings.Join(lines[start:], "\n")) } - return outputResult(p, model.OutputFormat, serverLabel, log) + return outputResult(params.Printer, model.OutputFormat, serverLabel, log) }, } configureFlags(cmd) diff --git a/internal/cmd/server/log/log_test.go b/internal/cmd/server/log/log_test.go index 50ce7c723..0ebc57bdc 100644 --- a/internal/cmd/server/log/log_test.go +++ b/internal/cmd/server/log/log_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -142,7 +143,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -239,7 +240,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.serverLabel, tt.args.log); (err != nil) != tt.wantErr { diff --git a/internal/cmd/server/machine-type/describe/describe.go b/internal/cmd/server/machine-type/describe/describe.go index 8c5d99e38..018bf0ad7 100644 --- a/internal/cmd/server/machine-type/describe/describe.go +++ b/internal/cmd/server/machine-type/describe/describe.go @@ -7,6 +7,7 @@ import ( "github.com/goccy/go-yaml" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -29,7 +30,7 @@ type inputModel struct { MachineType string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("describe %s", machineTypeArg), Short: "Shows details of a server machine type", @@ -47,13 +48,13 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -65,7 +66,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("read server machine type: %w", err) } - return outputResult(p, model.OutputFormat, resp) + return outputResult(params.Printer, model.OutputFormat, resp) }, } return cmd diff --git a/internal/cmd/server/machine-type/describe/describe_test.go b/internal/cmd/server/machine-type/describe/describe_test.go index 32f6d9a0b..20e47affc 100644 --- a/internal/cmd/server/machine-type/describe/describe_test.go +++ b/internal/cmd/server/machine-type/describe/describe_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" @@ -126,7 +127,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -222,7 +223,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.machineType); (err != nil) != tt.wantErr { diff --git a/internal/cmd/server/machine-type/list/list.go b/internal/cmd/server/machine-type/list/list.go index 19be072d3..2e5a20bde 100644 --- a/internal/cmd/server/machine-type/list/list.go +++ b/internal/cmd/server/machine-type/list/list.go @@ -6,6 +6,7 @@ import ( "fmt" "github.com/goccy/go-yaml" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -30,7 +31,7 @@ const ( limitFlag = "limit" ) -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "list", Short: "Get list of all machine types available in a project", @@ -52,13 +53,13 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -71,12 +72,12 @@ func NewCmd(p *print.Printer) *cobra.Command { } if resp.Items == nil || len(*resp.Items) == 0 { - projectLabel, err := projectname.GetProjectName(ctx, p, cmd) + projectLabel, err := projectname.GetProjectName(ctx, params.Printer, cmd) if err != nil { - p.Debug(print.ErrorLevel, "get project name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get project name: %v", err) projectLabel = model.ProjectId } - p.Info("No machine-types found for project %q\n", projectLabel) + params.Printer.Info("No machine-types found for project %q\n", projectLabel) return nil } @@ -85,7 +86,7 @@ func NewCmd(p *print.Printer) *cobra.Command { *resp.Items = (*resp.Items)[:*model.Limit] } - return outputResult(p, model.OutputFormat, *resp) + return outputResult(params.Printer, model.OutputFormat, *resp) }, } diff --git a/internal/cmd/server/machine-type/list/list_test.go b/internal/cmd/server/machine-type/list/list_test.go index a38d4f386..8472b596d 100644 --- a/internal/cmd/server/machine-type/list/list_test.go +++ b/internal/cmd/server/machine-type/list/list_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -118,7 +119,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -206,7 +207,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.machineTypes); (err != nil) != tt.wantErr { diff --git a/internal/cmd/server/machine-type/machine-type.go b/internal/cmd/server/machine-type/machine-type.go index 51c33d236..9482a6143 100644 --- a/internal/cmd/server/machine-type/machine-type.go +++ b/internal/cmd/server/machine-type/machine-type.go @@ -1,16 +1,16 @@ package machinetype import ( + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/cmd/server/machine-type/describe" "github.com/stackitcloud/stackit-cli/internal/cmd/server/machine-type/list" "github.com/stackitcloud/stackit-cli/internal/pkg/args" - "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" "github.com/spf13/cobra" ) -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "machine-type", Short: "Provides functionality for server machine types available inside a project", @@ -18,11 +18,11 @@ func NewCmd(p *print.Printer) *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd, p) + addSubcommands(cmd, params) return cmd } -func addSubcommands(cmd *cobra.Command, p *print.Printer) { - cmd.AddCommand(describe.NewCmd(p)) - cmd.AddCommand(list.NewCmd(p)) +func addSubcommands(cmd *cobra.Command, params *params.CmdParams) { + cmd.AddCommand(describe.NewCmd(params)) + cmd.AddCommand(list.NewCmd(params)) } diff --git a/internal/cmd/server/network-interface/attach/attach.go b/internal/cmd/server/network-interface/attach/attach.go index b2f987f27..68e670159 100644 --- a/internal/cmd/server/network-interface/attach/attach.go +++ b/internal/cmd/server/network-interface/attach/attach.go @@ -5,6 +5,7 @@ import ( "fmt" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" cliErr "github.com/stackitcloud/stackit-cli/internal/pkg/errors" @@ -35,7 +36,7 @@ type inputModel struct { Create *bool } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "attach", Short: "Attaches a network interface to a server", @@ -53,20 +54,20 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } serverLabel, err := iaasUtils.GetServerName(ctx, apiClient, model.ProjectId, *model.ServerId) if err != nil { - p.Debug(print.ErrorLevel, "get server name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get server name: %v", err) serverLabel = *model.ServerId } @@ -74,12 +75,12 @@ func NewCmd(p *print.Printer) *cobra.Command { if model.Create != nil && *model.Create { networkLabel, err := iaasUtils.GetNetworkName(ctx, apiClient, model.ProjectId, *model.NetworkId) if err != nil { - p.Debug(print.ErrorLevel, "get network name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get network name: %v", err) networkLabel = *model.NetworkId } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to create a network interface for network %q and attach it to server %q?", networkLabel, serverLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -90,13 +91,13 @@ func NewCmd(p *print.Printer) *cobra.Command { if err != nil { return fmt.Errorf("create and attach network interface: %w", err) } - p.Info("Created a network interface for network %q and attached it to server %q\n", networkLabel, serverLabel) + params.Printer.Info("Created a network interface for network %q and attached it to server %q\n", networkLabel, serverLabel) return nil } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to attach network interface %q to server %q?", *model.NicId, serverLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -107,7 +108,7 @@ func NewCmd(p *print.Printer) *cobra.Command { if err != nil { return fmt.Errorf("attach network interface: %w", err) } - p.Info("Attached network interface %q to server %q\n", utils.PtrString(model.NicId), serverLabel) + params.Printer.Info("Attached network interface %q to server %q\n", utils.PtrString(model.NicId), serverLabel) return nil }, diff --git a/internal/cmd/server/network-interface/attach/attach_test.go b/internal/cmd/server/network-interface/attach/attach_test.go index 2adeff447..6e5a00011 100644 --- a/internal/cmd/server/network-interface/attach/attach_test.go +++ b/internal/cmd/server/network-interface/attach/attach_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -199,7 +200,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/server/network-interface/detach/detach.go b/internal/cmd/server/network-interface/detach/detach.go index f0c735cb1..6a2517e50 100644 --- a/internal/cmd/server/network-interface/detach/detach.go +++ b/internal/cmd/server/network-interface/detach/detach.go @@ -5,6 +5,7 @@ import ( "fmt" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" cliErr "github.com/stackitcloud/stackit-cli/internal/pkg/errors" @@ -35,7 +36,7 @@ type inputModel struct { Delete *bool } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "detach", Short: "Detaches a network interface from a server", @@ -53,20 +54,20 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } serverLabel, err := iaasUtils.GetServerName(ctx, apiClient, model.ProjectId, *model.ServerId) if err != nil { - p.Debug(print.ErrorLevel, "get server name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get server name: %v", err) serverLabel = *model.ServerId } else if serverLabel == "" { serverLabel = *model.ServerId @@ -76,12 +77,12 @@ func NewCmd(p *print.Printer) *cobra.Command { if model.Delete != nil && *model.Delete { networkLabel, err := iaasUtils.GetNetworkName(ctx, apiClient, model.ProjectId, *model.NetworkId) if err != nil { - p.Debug(print.ErrorLevel, "get network name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get network name: %v", err) networkLabel = *model.NetworkId } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to detach and delete all network interfaces of network %q from server %q? (This cannot be undone)", networkLabel, serverLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -92,13 +93,13 @@ func NewCmd(p *print.Printer) *cobra.Command { if err != nil { return fmt.Errorf("detach and delete network interfaces: %w", err) } - p.Info("Detached and deleted all network interfaces of network %q from server %q\n", networkLabel, serverLabel) + params.Printer.Info("Detached and deleted all network interfaces of network %q from server %q\n", networkLabel, serverLabel) return nil } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to detach network interface %q from server %q?", *model.NicId, serverLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -109,7 +110,7 @@ func NewCmd(p *print.Printer) *cobra.Command { if err != nil { return fmt.Errorf("detach network interface: %w", err) } - p.Info("Detached network interface %q from server %q\n", utils.PtrString(model.NicId), serverLabel) + params.Printer.Info("Detached network interface %q from server %q\n", utils.PtrString(model.NicId), serverLabel) return nil }, diff --git a/internal/cmd/server/network-interface/detach/detach_test.go b/internal/cmd/server/network-interface/detach/detach_test.go index df408bbb8..cc60c9912 100644 --- a/internal/cmd/server/network-interface/detach/detach_test.go +++ b/internal/cmd/server/network-interface/detach/detach_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -199,7 +200,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/server/network-interface/list/list.go b/internal/cmd/server/network-interface/list/list.go index eb3210cf4..62d4f6c8b 100644 --- a/internal/cmd/server/network-interface/list/list.go +++ b/internal/cmd/server/network-interface/list/list.go @@ -7,6 +7,7 @@ import ( "github.com/goccy/go-yaml" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -31,7 +32,7 @@ type inputModel struct { Limit *int64 } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "list", Short: "Lists all attached network interfaces of a server", @@ -53,13 +54,13 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -74,12 +75,12 @@ func NewCmd(p *print.Printer) *cobra.Command { if resp.Items == nil || len(*resp.Items) == 0 { serverLabel, err := iaasUtils.GetServerName(ctx, apiClient, model.ProjectId, *model.ServerId) if err != nil { - p.Debug(print.ErrorLevel, "get server name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get server name: %v", err) serverLabel = *model.ServerId } else if serverLabel == "" { serverLabel = *model.ServerId } - p.Info("No attached network interfaces found for server %q\n", serverLabel) + params.Printer.Info("No attached network interfaces found for server %q\n", serverLabel) return nil } @@ -89,7 +90,7 @@ func NewCmd(p *print.Printer) *cobra.Command { items = items[:*model.Limit] } - return outputResult(p, model.OutputFormat, *model.ServerId, items) + return outputResult(params.Printer, model.OutputFormat, *model.ServerId, items) }, } configureFlags(cmd) diff --git a/internal/cmd/server/network-interface/list/list_test.go b/internal/cmd/server/network-interface/list/list_test.go index f627b1d19..8d2c90f86 100644 --- a/internal/cmd/server/network-interface/list/list_test.go +++ b/internal/cmd/server/network-interface/list/list_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -137,7 +138,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -235,7 +236,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.serverId, tt.args.serverNics); (err != nil) != tt.wantErr { diff --git a/internal/cmd/server/network-interface/network-interface.go b/internal/cmd/server/network-interface/network-interface.go index 703e48eba..998ee07d3 100644 --- a/internal/cmd/server/network-interface/network-interface.go +++ b/internal/cmd/server/network-interface/network-interface.go @@ -1,17 +1,17 @@ package networkinterface import ( + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/cmd/server/network-interface/attach" "github.com/stackitcloud/stackit-cli/internal/cmd/server/network-interface/detach" "github.com/stackitcloud/stackit-cli/internal/cmd/server/network-interface/list" "github.com/stackitcloud/stackit-cli/internal/pkg/args" - "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" "github.com/spf13/cobra" ) -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "network-interface", Short: "Allows attaching/detaching network interfaces to servers", @@ -19,12 +19,12 @@ func NewCmd(p *print.Printer) *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd, p) + addSubcommands(cmd, params) return cmd } -func addSubcommands(cmd *cobra.Command, p *print.Printer) { - cmd.AddCommand(attach.NewCmd(p)) - cmd.AddCommand(list.NewCmd(p)) - cmd.AddCommand(detach.NewCmd(p)) +func addSubcommands(cmd *cobra.Command, params *params.CmdParams) { + cmd.AddCommand(attach.NewCmd(params)) + cmd.AddCommand(list.NewCmd(params)) + cmd.AddCommand(detach.NewCmd(params)) } diff --git a/internal/cmd/server/os-update/create/create.go b/internal/cmd/server/os-update/create/create.go index 71845bc2a..0bb65907d 100644 --- a/internal/cmd/server/os-update/create/create.go +++ b/internal/cmd/server/os-update/create/create.go @@ -6,6 +6,7 @@ import ( "fmt" "github.com/goccy/go-yaml" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" cliErr "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -34,7 +35,7 @@ type inputModel struct { MaintenanceWindow int64 } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "create", Short: "Creates a Server os-update.", @@ -51,23 +52,23 @@ func NewCmd(p *print.Printer) *cobra.Command { RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } serverLabel := model.ServerId // Get server name - if iaasApiClient, err := iaasClient.ConfigureClient(p); err == nil { + if iaasApiClient, err := iaasClient.ConfigureClient(params.Printer); err == nil { serverName, err := iaasUtils.GetServerName(ctx, iaasApiClient, model.ProjectId, model.ServerId) if err != nil { - p.Debug(print.ErrorLevel, "get server name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get server name: %v", err) } else if serverName != "" { serverLabel = serverName } @@ -75,7 +76,7 @@ func NewCmd(p *print.Printer) *cobra.Command { if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to create a os-update for server %s?", serverLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -91,7 +92,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("create Server os-update: %w", err) } - return outputResult(p, model.OutputFormat, serverLabel, *resp) + return outputResult(params.Printer, model.OutputFormat, serverLabel, *resp) }, } configureFlags(cmd) diff --git a/internal/cmd/server/os-update/create/create_test.go b/internal/cmd/server/os-update/create/create_test.go index 9dd635e70..78e914882 100644 --- a/internal/cmd/server/os-update/create/create_test.go +++ b/internal/cmd/server/os-update/create/create_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -129,7 +130,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -226,7 +227,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.serverLabel, tt.args.resp); (err != nil) != tt.wantErr { diff --git a/internal/cmd/server/os-update/describe/describe.go b/internal/cmd/server/os-update/describe/describe.go index 7260131ff..e722e3994 100644 --- a/internal/cmd/server/os-update/describe/describe.go +++ b/internal/cmd/server/os-update/describe/describe.go @@ -7,6 +7,7 @@ import ( "github.com/goccy/go-yaml" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -30,7 +31,7 @@ type inputModel struct { UpdateId string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("describe %s", updateIdArg), Short: "Shows details of a Server os-update", @@ -46,12 +47,12 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -63,7 +64,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("read server os-update: %w", err) } - return outputResult(p, model.OutputFormat, *resp) + return outputResult(params.Printer, model.OutputFormat, *resp) }, } configureFlags(cmd) diff --git a/internal/cmd/server/os-update/describe/describe_test.go b/internal/cmd/server/os-update/describe/describe_test.go index 741bbe6dc..0a859c415 100644 --- a/internal/cmd/server/os-update/describe/describe_test.go +++ b/internal/cmd/server/os-update/describe/describe_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" @@ -133,7 +134,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -231,7 +232,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.update); (err != nil) != tt.wantErr { diff --git a/internal/cmd/server/os-update/disable/disable.go b/internal/cmd/server/os-update/disable/disable.go index f0270b0cd..153754db3 100644 --- a/internal/cmd/server/os-update/disable/disable.go +++ b/internal/cmd/server/os-update/disable/disable.go @@ -4,6 +4,7 @@ import ( "context" "fmt" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -27,7 +28,7 @@ type inputModel struct { ServerId string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "disable", Short: "Disables server os-update service", @@ -40,23 +41,23 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } serverLabel := model.ServerId // Get server name - if iaasApiClient, err := iaasClient.ConfigureClient(p); err == nil { + if iaasApiClient, err := iaasClient.ConfigureClient(params.Printer); err == nil { serverName, err := iaasUtils.GetServerName(ctx, iaasApiClient, model.ProjectId, model.ServerId) if err != nil { - p.Debug(print.ErrorLevel, "get server name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get server name: %v", err) } else if serverName != "" { serverLabel = serverName } @@ -64,7 +65,7 @@ func NewCmd(p *print.Printer) *cobra.Command { if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to disable the os-update service for server %s?", serverLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -77,7 +78,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("disable server os-update service: %w", err) } - p.Info("Disabled Server os-update service for server %s\n", serverLabel) + params.Printer.Info("Disabled Server os-update service for server %s\n", serverLabel) return nil }, } diff --git a/internal/cmd/server/os-update/enable/enable.go b/internal/cmd/server/os-update/enable/enable.go index c255c778e..ad0467bb5 100644 --- a/internal/cmd/server/os-update/enable/enable.go +++ b/internal/cmd/server/os-update/enable/enable.go @@ -5,6 +5,7 @@ import ( "fmt" "strings" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -28,7 +29,7 @@ type inputModel struct { ServerId string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "enable", Short: "Enables Server os-update service", @@ -41,23 +42,23 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } serverLabel := model.ServerId // Get server name - if iaasApiClient, err := iaasClient.ConfigureClient(p); err == nil { + if iaasApiClient, err := iaasClient.ConfigureClient(params.Printer); err == nil { serverName, err := iaasUtils.GetServerName(ctx, iaasApiClient, model.ProjectId, model.ServerId) if err != nil { - p.Debug(print.ErrorLevel, "get server name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get server name: %v", err) } else if serverName != "" { serverLabel = serverName } @@ -65,7 +66,7 @@ func NewCmd(p *print.Printer) *cobra.Command { if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to enable the server os-update service for server %s?", serverLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -80,7 +81,7 @@ func NewCmd(p *print.Printer) *cobra.Command { } } - p.Info("Enabled os-update service for server %s\n", serverLabel) + params.Printer.Info("Enabled os-update service for server %s\n", serverLabel) return nil }, } diff --git a/internal/cmd/server/os-update/list/list.go b/internal/cmd/server/os-update/list/list.go index e1435badc..ed7f183b7 100644 --- a/internal/cmd/server/os-update/list/list.go +++ b/internal/cmd/server/os-update/list/list.go @@ -6,6 +6,7 @@ import ( "fmt" "strconv" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -34,7 +35,7 @@ type inputModel struct { Limit *int64 } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "list", Short: "Lists all server os-updates", @@ -50,13 +51,13 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -71,15 +72,15 @@ func NewCmd(p *print.Printer) *cobra.Command { if len(updates) == 0 { serverLabel := model.ServerId // Get server name - if iaasApiClient, err := iaasClient.ConfigureClient(p); err == nil { + if iaasApiClient, err := iaasClient.ConfigureClient(params.Printer); err == nil { serverName, err := iaasUtils.GetServerName(ctx, iaasApiClient, model.ProjectId, model.ServerId) if err != nil { - p.Debug(print.ErrorLevel, "get server name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get server name: %v", err) } else if serverName != "" { serverLabel = serverName } } - p.Info("No os-updates found for server %s\n", serverLabel) + params.Printer.Info("No os-updates found for server %s\n", serverLabel) return nil } @@ -87,7 +88,7 @@ func NewCmd(p *print.Printer) *cobra.Command { if model.Limit != nil && len(updates) > int(*model.Limit) { updates = updates[:*model.Limit] } - return outputResult(p, model.OutputFormat, updates) + return outputResult(params.Printer, model.OutputFormat, updates) }, } configureFlags(cmd) diff --git a/internal/cmd/server/os-update/list/list_test.go b/internal/cmd/server/os-update/list/list_test.go index 36205f88d..078f7ca0a 100644 --- a/internal/cmd/server/os-update/list/list_test.go +++ b/internal/cmd/server/os-update/list/list_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -120,7 +121,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -208,7 +209,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.updates); (err != nil) != tt.wantErr { diff --git a/internal/cmd/server/os-update/os-update.go b/internal/cmd/server/os-update/os-update.go index a9d3ec8fc..516d7ce06 100644 --- a/internal/cmd/server/os-update/os-update.go +++ b/internal/cmd/server/os-update/os-update.go @@ -1,6 +1,7 @@ package osupdate import ( + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/cmd/server/os-update/create" "github.com/stackitcloud/stackit-cli/internal/cmd/server/os-update/describe" "github.com/stackitcloud/stackit-cli/internal/cmd/server/os-update/disable" @@ -8,13 +9,12 @@ import ( "github.com/stackitcloud/stackit-cli/internal/cmd/server/os-update/list" "github.com/stackitcloud/stackit-cli/internal/cmd/server/os-update/schedule" "github.com/stackitcloud/stackit-cli/internal/pkg/args" - "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" "github.com/spf13/cobra" ) -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "os-update", Short: "Provides functionality for managed server updates", @@ -22,15 +22,15 @@ func NewCmd(p *print.Printer) *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd, p) + addSubcommands(cmd, params) return cmd } -func addSubcommands(cmd *cobra.Command, p *print.Printer) { - cmd.AddCommand(create.NewCmd(p)) - cmd.AddCommand(describe.NewCmd(p)) - cmd.AddCommand(list.NewCmd(p)) - cmd.AddCommand(enable.NewCmd(p)) - cmd.AddCommand(disable.NewCmd(p)) - cmd.AddCommand(schedule.NewCmd(p)) +func addSubcommands(cmd *cobra.Command, params *params.CmdParams) { + cmd.AddCommand(create.NewCmd(params)) + cmd.AddCommand(describe.NewCmd(params)) + cmd.AddCommand(list.NewCmd(params)) + cmd.AddCommand(enable.NewCmd(params)) + cmd.AddCommand(disable.NewCmd(params)) + cmd.AddCommand(schedule.NewCmd(params)) } diff --git a/internal/cmd/server/os-update/schedule/create/create.go b/internal/cmd/server/os-update/schedule/create/create.go index 2f30e876f..f64b10728 100644 --- a/internal/cmd/server/os-update/schedule/create/create.go +++ b/internal/cmd/server/os-update/schedule/create/create.go @@ -6,6 +6,7 @@ import ( "fmt" "github.com/goccy/go-yaml" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" cliErr "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -43,7 +44,7 @@ type inputModel struct { MaintenanceWindow int64 } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "create", Short: "Creates a Server os-update Schedule", @@ -60,23 +61,23 @@ func NewCmd(p *print.Printer) *cobra.Command { RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } serverLabel := model.ServerId // Get server name - if iaasApiClient, err := iaasClient.ConfigureClient(p); err == nil { + if iaasApiClient, err := iaasClient.ConfigureClient(params.Printer); err == nil { serverName, err := iaasUtils.GetServerName(ctx, iaasApiClient, model.ProjectId, model.ServerId) if err != nil { - p.Debug(print.ErrorLevel, "get server name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get server name: %v", err) } else if serverName != "" { serverLabel = serverName } @@ -84,7 +85,7 @@ func NewCmd(p *print.Printer) *cobra.Command { if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to create a os-update Schedule for server %s?", serverLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -100,7 +101,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("create Server os-update Schedule: %w", err) } - return outputResult(p, model.OutputFormat, serverLabel, *resp) + return outputResult(params.Printer, model.OutputFormat, serverLabel, *resp) }, } configureFlags(cmd) diff --git a/internal/cmd/server/os-update/schedule/create/create_test.go b/internal/cmd/server/os-update/schedule/create/create_test.go index 7d084516e..2cdb00f75 100644 --- a/internal/cmd/server/os-update/schedule/create/create_test.go +++ b/internal/cmd/server/os-update/schedule/create/create_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -136,7 +137,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -233,7 +234,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.serverLabel, tt.args.resp); (err != nil) != tt.wantErr { diff --git a/internal/cmd/server/os-update/schedule/delete/delete.go b/internal/cmd/server/os-update/schedule/delete/delete.go index 855690fa4..a439bf76a 100644 --- a/internal/cmd/server/os-update/schedule/delete/delete.go +++ b/internal/cmd/server/os-update/schedule/delete/delete.go @@ -4,6 +4,7 @@ import ( "context" "fmt" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -27,7 +28,7 @@ type inputModel struct { ServerId string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("delete %s", scheduleIdArg), Short: "Deletes a Server os-update Schedule", @@ -40,20 +41,20 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to delete server os-update schedule %q? (This cannot be undone)", model.ScheduleId) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -66,7 +67,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("delete Server os-update Schedule: %w", err) } - p.Info("Deleted server os-update schedule %q\n", model.ScheduleId) + params.Printer.Info("Deleted server os-update schedule %q\n", model.ScheduleId) return nil }, } diff --git a/internal/cmd/server/os-update/schedule/delete/delete_test.go b/internal/cmd/server/os-update/schedule/delete/delete_test.go index 040aa61f7..eb005e164 100644 --- a/internal/cmd/server/os-update/schedule/delete/delete_test.go +++ b/internal/cmd/server/os-update/schedule/delete/delete_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" @@ -133,7 +134,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/server/os-update/schedule/describe/describe.go b/internal/cmd/server/os-update/schedule/describe/describe.go index 60aa9431c..f66c9b5d2 100644 --- a/internal/cmd/server/os-update/schedule/describe/describe.go +++ b/internal/cmd/server/os-update/schedule/describe/describe.go @@ -7,6 +7,7 @@ import ( "github.com/goccy/go-yaml" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -30,7 +31,7 @@ type inputModel struct { ScheduleId string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("describe %s", scheduleIdArg), Short: "Shows details of a Server os-update Schedule", @@ -46,12 +47,12 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -63,7 +64,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("read server os-update schedule: %w", err) } - return outputResult(p, model.OutputFormat, *resp) + return outputResult(params.Printer, model.OutputFormat, *resp) }, } configureFlags(cmd) diff --git a/internal/cmd/server/os-update/schedule/describe/describe_test.go b/internal/cmd/server/os-update/schedule/describe/describe_test.go index 5928e712b..178585383 100644 --- a/internal/cmd/server/os-update/schedule/describe/describe_test.go +++ b/internal/cmd/server/os-update/schedule/describe/describe_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" @@ -133,7 +134,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -231,7 +232,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.schedule); (err != nil) != tt.wantErr { diff --git a/internal/cmd/server/os-update/schedule/list/list.go b/internal/cmd/server/os-update/schedule/list/list.go index 562b30665..b14299303 100644 --- a/internal/cmd/server/os-update/schedule/list/list.go +++ b/internal/cmd/server/os-update/schedule/list/list.go @@ -7,6 +7,7 @@ import ( "github.com/goccy/go-yaml" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -32,7 +33,7 @@ type inputModel struct { Limit *int64 } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "list", Short: "Lists all server os-update schedules", @@ -48,13 +49,13 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -69,15 +70,15 @@ func NewCmd(p *print.Printer) *cobra.Command { if len(schedules) == 0 { serverLabel := model.ServerId // Get server name - if iaasApiClient, err := iaasClient.ConfigureClient(p); err == nil { + if iaasApiClient, err := iaasClient.ConfigureClient(params.Printer); err == nil { serverName, err := iaasUtils.GetServerName(ctx, iaasApiClient, model.ProjectId, model.ServerId) if err != nil { - p.Debug(print.ErrorLevel, "get server name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get server name: %v", err) } else if serverName != "" { serverLabel = serverName } } - p.Info("No os-update schedules found for server %s\n", serverLabel) + params.Printer.Info("No os-update schedules found for server %s\n", serverLabel) return nil } @@ -85,7 +86,7 @@ func NewCmd(p *print.Printer) *cobra.Command { if model.Limit != nil && len(schedules) > int(*model.Limit) { schedules = schedules[:*model.Limit] } - return outputResult(p, model.OutputFormat, schedules) + return outputResult(params.Printer, model.OutputFormat, schedules) }, } configureFlags(cmd) diff --git a/internal/cmd/server/os-update/schedule/list/list_test.go b/internal/cmd/server/os-update/schedule/list/list_test.go index d29d3dd5f..57aa1c0dc 100644 --- a/internal/cmd/server/os-update/schedule/list/list_test.go +++ b/internal/cmd/server/os-update/schedule/list/list_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -120,7 +121,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -208,7 +209,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.schedules); (err != nil) != tt.wantErr { diff --git a/internal/cmd/server/os-update/schedule/schedule.go b/internal/cmd/server/os-update/schedule/schedule.go index 9f051ced0..3ffeb36ea 100644 --- a/internal/cmd/server/os-update/schedule/schedule.go +++ b/internal/cmd/server/os-update/schedule/schedule.go @@ -1,19 +1,19 @@ package schedule import ( + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/cmd/server/os-update/schedule/create" del "github.com/stackitcloud/stackit-cli/internal/cmd/server/os-update/schedule/delete" "github.com/stackitcloud/stackit-cli/internal/cmd/server/os-update/schedule/describe" "github.com/stackitcloud/stackit-cli/internal/cmd/server/os-update/schedule/list" "github.com/stackitcloud/stackit-cli/internal/cmd/server/os-update/schedule/update" "github.com/stackitcloud/stackit-cli/internal/pkg/args" - "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" "github.com/spf13/cobra" ) -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "schedule", Short: "Provides functionality for Server os-update Schedule", @@ -21,14 +21,14 @@ func NewCmd(p *print.Printer) *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd, p) + addSubcommands(cmd, params) return cmd } -func addSubcommands(cmd *cobra.Command, p *print.Printer) { - cmd.AddCommand(list.NewCmd(p)) - cmd.AddCommand(describe.NewCmd(p)) - cmd.AddCommand(create.NewCmd(p)) - cmd.AddCommand(del.NewCmd(p)) - cmd.AddCommand(update.NewCmd(p)) +func addSubcommands(cmd *cobra.Command, params *params.CmdParams) { + cmd.AddCommand(list.NewCmd(params)) + cmd.AddCommand(describe.NewCmd(params)) + cmd.AddCommand(create.NewCmd(params)) + cmd.AddCommand(del.NewCmd(params)) + cmd.AddCommand(update.NewCmd(params)) } diff --git a/internal/cmd/server/os-update/schedule/update/update.go b/internal/cmd/server/os-update/schedule/update/update.go index e698f21f7..bae09473e 100644 --- a/internal/cmd/server/os-update/schedule/update/update.go +++ b/internal/cmd/server/os-update/schedule/update/update.go @@ -7,6 +7,7 @@ import ( "github.com/goccy/go-yaml" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" cliErr "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -43,7 +44,7 @@ type inputModel struct { MaintenanceWindow *int64 } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("update %s", scheduleIdArg), Short: "Updates a Server os-update Schedule", @@ -57,26 +58,26 @@ func NewCmd(p *print.Printer) *cobra.Command { RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } currentSchedule, err := apiClient.GetUpdateScheduleExecute(ctx, model.ProjectId, model.ServerId, model.ScheduleId, model.Region) if err != nil { - p.Debug(print.ErrorLevel, "get current server os-update schedule: %v", err) + params.Printer.Debug(print.ErrorLevel, "get current server os-update schedule: %v", err) return err } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to update Server os-update Schedule %q?", model.ScheduleId) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -92,7 +93,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("update Server os-update Schedule: %w", err) } - return outputResult(p, model.OutputFormat, *resp) + return outputResult(params.Printer, model.OutputFormat, *resp) }, } configureFlags(cmd) diff --git a/internal/cmd/server/os-update/schedule/update/update_test.go b/internal/cmd/server/os-update/schedule/update/update_test.go index 63ab21239..a19c77958 100644 --- a/internal/cmd/server/os-update/schedule/update/update_test.go +++ b/internal/cmd/server/os-update/schedule/update/update_test.go @@ -5,6 +5,7 @@ import ( "strconv" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -173,7 +174,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -285,7 +286,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.resp); (err != nil) != tt.wantErr { diff --git a/internal/cmd/server/public-ip/attach/attach.go b/internal/cmd/server/public-ip/attach/attach.go index 848a913b4..8caecd342 100644 --- a/internal/cmd/server/public-ip/attach/attach.go +++ b/internal/cmd/server/public-ip/attach/attach.go @@ -5,6 +5,7 @@ import ( "fmt" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -29,7 +30,7 @@ type inputModel struct { PublicIpId string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("attach %s", publicIpIdArg), Short: "Attaches a public IP to a server", @@ -42,20 +43,20 @@ func NewCmd(p *print.Printer) *cobra.Command { )), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } publicIpLabel, _, err := iaasUtils.GetPublicIP(ctx, apiClient, model.ProjectId, model.PublicIpId) if err != nil { - p.Debug(print.ErrorLevel, "get public ip name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get public ip name: %v", err) publicIpLabel = model.PublicIpId } else if publicIpLabel == "" { publicIpLabel = model.PublicIpId @@ -63,7 +64,7 @@ func NewCmd(p *print.Printer) *cobra.Command { serverLabel, err := iaasUtils.GetServerName(ctx, apiClient, model.ProjectId, *model.ServerId) if err != nil { - p.Debug(print.ErrorLevel, "get server name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get server name: %v", err) serverLabel = *model.ServerId } else if serverLabel == "" { serverLabel = *model.ServerId @@ -71,7 +72,7 @@ func NewCmd(p *print.Printer) *cobra.Command { if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to attach public IP %q to server %q?", publicIpLabel, serverLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -84,7 +85,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("attach server to public ip: %w", err) } - p.Info("Attached public IP %q to server %q\n", publicIpLabel, serverLabel) + params.Printer.Info("Attached public IP %q to server %q\n", publicIpLabel, serverLabel) return nil }, } diff --git a/internal/cmd/server/public-ip/attach/attach_test.go b/internal/cmd/server/public-ip/attach/attach_test.go index b8a80d6b2..c58f7b280 100644 --- a/internal/cmd/server/public-ip/attach/attach_test.go +++ b/internal/cmd/server/public-ip/attach/attach_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -147,7 +148,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/server/public-ip/detach/detach.go b/internal/cmd/server/public-ip/detach/detach.go index 809b76759..c8066db67 100644 --- a/internal/cmd/server/public-ip/detach/detach.go +++ b/internal/cmd/server/public-ip/detach/detach.go @@ -5,6 +5,7 @@ import ( "fmt" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -29,7 +30,7 @@ type inputModel struct { PublicIpId string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("detach %s", publicIpIdArg), Short: "Detaches a public IP from a server", @@ -43,20 +44,20 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } publicIpLabel, _, err := iaasUtils.GetPublicIP(ctx, apiClient, model.ProjectId, model.PublicIpId) if err != nil { - p.Debug(print.ErrorLevel, "get public ip: %v", err) + params.Printer.Debug(print.ErrorLevel, "get public ip: %v", err) publicIpLabel = model.PublicIpId } else if publicIpLabel == "" { publicIpLabel = model.PublicIpId @@ -64,7 +65,7 @@ func NewCmd(p *print.Printer) *cobra.Command { serverLabel, err := iaasUtils.GetServerName(ctx, apiClient, model.ProjectId, *model.ServerId) if err != nil { - p.Debug(print.ErrorLevel, "get server name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get server name: %v", err) serverLabel = *model.ServerId } else if serverLabel == "" { serverLabel = *model.ServerId @@ -72,7 +73,7 @@ func NewCmd(p *print.Printer) *cobra.Command { if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to detach public IP %q from server %q?", publicIpLabel, serverLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -84,7 +85,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("detach public ip from server: %w", err) } - p.Info("Detached public IP %q from server %q\n", publicIpLabel, serverLabel) + params.Printer.Info("Detached public IP %q from server %q\n", publicIpLabel, serverLabel) return nil }, diff --git a/internal/cmd/server/public-ip/detach/detach_test.go b/internal/cmd/server/public-ip/detach/detach_test.go index b7ed5c286..8a46591a7 100644 --- a/internal/cmd/server/public-ip/detach/detach_test.go +++ b/internal/cmd/server/public-ip/detach/detach_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -146,7 +147,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/server/public-ip/public_ip.go b/internal/cmd/server/public-ip/public_ip.go index 0bfafbcbd..494993ce8 100644 --- a/internal/cmd/server/public-ip/public_ip.go +++ b/internal/cmd/server/public-ip/public_ip.go @@ -1,16 +1,16 @@ package publicip import ( + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/cmd/server/public-ip/attach" "github.com/stackitcloud/stackit-cli/internal/cmd/server/public-ip/detach" "github.com/stackitcloud/stackit-cli/internal/pkg/args" - "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" "github.com/spf13/cobra" ) -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "public-ip", Short: "Allows attaching/detaching public IPs to servers", @@ -18,11 +18,11 @@ func NewCmd(p *print.Printer) *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd, p) + addSubcommands(cmd, params) return cmd } -func addSubcommands(cmd *cobra.Command, p *print.Printer) { - cmd.AddCommand(attach.NewCmd(p)) - cmd.AddCommand(detach.NewCmd(p)) +func addSubcommands(cmd *cobra.Command, params *params.CmdParams) { + cmd.AddCommand(attach.NewCmd(params)) + cmd.AddCommand(detach.NewCmd(params)) } diff --git a/internal/cmd/server/reboot/reboot.go b/internal/cmd/server/reboot/reboot.go index 80aed47b3..465adaaa1 100644 --- a/internal/cmd/server/reboot/reboot.go +++ b/internal/cmd/server/reboot/reboot.go @@ -4,6 +4,7 @@ import ( "context" "fmt" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -32,7 +33,7 @@ type inputModel struct { HardReboot bool } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("reboot %s", serverIdArg), Short: "Reboots a server", @@ -50,27 +51,27 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } serverLabel, err := iaasUtils.GetServerName(ctx, apiClient, model.ProjectId, model.ServerId) if err != nil { - p.Debug(print.ErrorLevel, "get server name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get server name: %v", err) serverLabel = model.ServerId } else if serverLabel == "" { serverLabel = model.ServerId } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to reboot server %q?", serverLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -83,7 +84,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("server reboot: %w", err) } - p.Info("Server %q rebooted\n", serverLabel) + params.Printer.Info("Server %q rebooted\n", serverLabel) return nil }, diff --git a/internal/cmd/server/reboot/reboot_test.go b/internal/cmd/server/reboot/reboot_test.go index c4444fa85..77e99f506 100644 --- a/internal/cmd/server/reboot/reboot_test.go +++ b/internal/cmd/server/reboot/reboot_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" @@ -130,7 +131,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/server/rescue/rescue.go b/internal/cmd/server/rescue/rescue.go index 81eabcab2..73aeae948 100644 --- a/internal/cmd/server/rescue/rescue.go +++ b/internal/cmd/server/rescue/rescue.go @@ -4,6 +4,7 @@ import ( "context" "fmt" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -32,7 +33,7 @@ type inputModel struct { ImageId *string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("rescue %s", serverIdArg), Short: "Rescues an existing server", @@ -46,20 +47,20 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } serverLabel, err := iaasUtils.GetServerName(ctx, apiClient, model.ProjectId, model.ServerId) if err != nil { - p.Debug(print.ErrorLevel, "get server name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get server name: %v", err) serverLabel = model.ServerId } else if serverLabel == "" { serverLabel = model.ServerId @@ -67,7 +68,7 @@ func NewCmd(p *print.Printer) *cobra.Command { if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to rescue server %q?", serverLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -82,7 +83,7 @@ func NewCmd(p *print.Printer) *cobra.Command { // Wait for async operation, if async mode not enabled if !model.Async { - s := spinner.New(p) + s := spinner.New(params.Printer) s.Start("Rescuing server") _, err = wait.RescueServerWaitHandler(ctx, apiClient, model.ProjectId, model.ServerId).WaitWithContext(ctx) if err != nil { @@ -95,7 +96,7 @@ func NewCmd(p *print.Printer) *cobra.Command { if model.Async { operationState = "Triggered rescue of" } - p.Info("%s server %q. Image %q is used as temporary boot image\n", operationState, serverLabel, utils.PtrString(model.ImageId)) + params.Printer.Info("%s server %q. Image %q is used as temporary boot image\n", operationState, serverLabel, utils.PtrString(model.ImageId)) return nil }, diff --git a/internal/cmd/server/rescue/rescue_test.go b/internal/cmd/server/rescue/rescue_test.go index 9d17daf78..8a5695fd9 100644 --- a/internal/cmd/server/rescue/rescue_test.go +++ b/internal/cmd/server/rescue/rescue_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -145,7 +146,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/server/resize/resize.go b/internal/cmd/server/resize/resize.go index d0112b723..8d3f19b3e 100644 --- a/internal/cmd/server/resize/resize.go +++ b/internal/cmd/server/resize/resize.go @@ -4,6 +4,7 @@ import ( "context" "fmt" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -32,7 +33,7 @@ type inputModel struct { MachineType *string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("resize %s", serverIdArg), Short: "Resizes the server to the given machine type", @@ -46,20 +47,20 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } serverLabel, err := iaasUtils.GetServerName(ctx, apiClient, model.ProjectId, model.ServerId) if err != nil { - p.Debug(print.ErrorLevel, "get server name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get server name: %v", err) serverLabel = model.ServerId } else if serverLabel == "" { serverLabel = model.ServerId @@ -67,7 +68,7 @@ func NewCmd(p *print.Printer) *cobra.Command { if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to resize server %q to machine type %q?", serverLabel, *model.MachineType) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -82,7 +83,7 @@ func NewCmd(p *print.Printer) *cobra.Command { // Wait for async operation, if async mode not enabled if !model.Async { - s := spinner.New(p) + s := spinner.New(params.Printer) s.Start("Resizing server") _, err = wait.ResizeServerWaitHandler(ctx, apiClient, model.ProjectId, model.ServerId).WaitWithContext(ctx) if err != nil { @@ -95,7 +96,7 @@ func NewCmd(p *print.Printer) *cobra.Command { if model.Async { operationState = "Triggered resize of" } - p.Info("%s server %q\n", operationState, serverLabel) + params.Printer.Info("%s server %q\n", operationState, serverLabel) return nil }, diff --git a/internal/cmd/server/resize/resize_test.go b/internal/cmd/server/resize/resize_test.go index a93d8fdc1..ee94cfe6e 100644 --- a/internal/cmd/server/resize/resize_test.go +++ b/internal/cmd/server/resize/resize_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -144,7 +145,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/server/server.go b/internal/cmd/server/server.go index 209b19e1b..ac3101b41 100644 --- a/internal/cmd/server/server.go +++ b/internal/cmd/server/server.go @@ -1,6 +1,7 @@ package server import ( + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/cmd/server/backup" "github.com/stackitcloud/stackit-cli/internal/cmd/server/command" "github.com/stackitcloud/stackit-cli/internal/cmd/server/console" @@ -25,13 +26,12 @@ import ( "github.com/stackitcloud/stackit-cli/internal/cmd/server/volume" "github.com/stackitcloud/stackit-cli/internal/pkg/args" - "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" "github.com/spf13/cobra" ) -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "server", Short: "Provides functionality for servers", @@ -39,31 +39,31 @@ func NewCmd(p *print.Printer) *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd, p) + addSubcommands(cmd, params) return cmd } -func addSubcommands(cmd *cobra.Command, p *print.Printer) { - cmd.AddCommand(backup.NewCmd(p)) - cmd.AddCommand(command.NewCmd(p)) - cmd.AddCommand(create.NewCmd(p)) - cmd.AddCommand(delete.NewCmd(p)) - cmd.AddCommand(describe.NewCmd(p)) - cmd.AddCommand(list.NewCmd(p)) - cmd.AddCommand(publicip.NewCmd(p)) - cmd.AddCommand(serviceaccount.NewCmd(p)) - cmd.AddCommand(update.NewCmd(p)) - cmd.AddCommand(volume.NewCmd(p)) - cmd.AddCommand(networkinterface.NewCmd(p)) - cmd.AddCommand(console.NewCmd(p)) - cmd.AddCommand(log.NewCmd(p)) - cmd.AddCommand(start.NewCmd(p)) - cmd.AddCommand(stop.NewCmd(p)) - cmd.AddCommand(reboot.NewCmd(p)) - cmd.AddCommand(deallocate.NewCmd(p)) - cmd.AddCommand(resize.NewCmd(p)) - cmd.AddCommand(rescue.NewCmd(p)) - cmd.AddCommand(unrescue.NewCmd(p)) - cmd.AddCommand(osUpdate.NewCmd(p)) - cmd.AddCommand(machinetype.NewCmd(p)) +func addSubcommands(cmd *cobra.Command, params *params.CmdParams) { + cmd.AddCommand(backup.NewCmd(params)) + cmd.AddCommand(command.NewCmd(params)) + cmd.AddCommand(create.NewCmd(params)) + cmd.AddCommand(delete.NewCmd(params)) + cmd.AddCommand(describe.NewCmd(params)) + cmd.AddCommand(list.NewCmd(params)) + cmd.AddCommand(publicip.NewCmd(params)) + cmd.AddCommand(serviceaccount.NewCmd(params)) + cmd.AddCommand(update.NewCmd(params)) + cmd.AddCommand(volume.NewCmd(params)) + cmd.AddCommand(networkinterface.NewCmd(params)) + cmd.AddCommand(console.NewCmd(params)) + cmd.AddCommand(log.NewCmd(params)) + cmd.AddCommand(start.NewCmd(params)) + cmd.AddCommand(stop.NewCmd(params)) + cmd.AddCommand(reboot.NewCmd(params)) + cmd.AddCommand(deallocate.NewCmd(params)) + cmd.AddCommand(resize.NewCmd(params)) + cmd.AddCommand(rescue.NewCmd(params)) + cmd.AddCommand(unrescue.NewCmd(params)) + cmd.AddCommand(osUpdate.NewCmd(params)) + cmd.AddCommand(machinetype.NewCmd(params)) } diff --git a/internal/cmd/server/service-account/attach/attach.go b/internal/cmd/server/service-account/attach/attach.go index 49f7a1aa4..e4e29042c 100644 --- a/internal/cmd/server/service-account/attach/attach.go +++ b/internal/cmd/server/service-account/attach/attach.go @@ -5,6 +5,7 @@ import ( "encoding/json" "fmt" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -31,7 +32,7 @@ type inputModel struct { ServiceAccMail string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("attach %s", serviceAccMailArg), Short: "Attach a service account to a server", @@ -45,19 +46,19 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } serverLabel, err := iaasUtils.GetServerName(ctx, apiClient, model.ProjectId, *model.ServerId) if err != nil { - p.Debug(print.ErrorLevel, "get server name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get server name: %v", err) serverLabel = *model.ServerId } else if serverLabel == "" { serverLabel = *model.ServerId @@ -65,7 +66,7 @@ func NewCmd(p *print.Printer) *cobra.Command { if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to attach service account %q to server %q?", model.ServiceAccMail, serverLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -78,7 +79,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("attach service account to server: %w", err) } - return outputResult(p, model.OutputFormat, model.ServiceAccMail, serverLabel, *resp) + return outputResult(params.Printer, model.OutputFormat, model.ServiceAccMail, serverLabel, *resp) }, } configureFlags(cmd) diff --git a/internal/cmd/server/service-account/attach/attach_test.go b/internal/cmd/server/service-account/attach/attach_test.go index fddde577b..96ed151ce 100644 --- a/internal/cmd/server/service-account/attach/attach_test.go +++ b/internal/cmd/server/service-account/attach/attach_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -146,7 +147,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -244,7 +245,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.serviceAccMail, tt.args.serverLabel, tt.args.serviceAccounts); (err != nil) != tt.wantErr { diff --git a/internal/cmd/server/service-account/detach/detach.go b/internal/cmd/server/service-account/detach/detach.go index aa3983c58..6b79c6998 100644 --- a/internal/cmd/server/service-account/detach/detach.go +++ b/internal/cmd/server/service-account/detach/detach.go @@ -5,6 +5,7 @@ import ( "encoding/json" "fmt" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -31,7 +32,7 @@ type inputModel struct { ServiceAccMail string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("detach %s", serviceAccMailArg), Short: "Detach a service account from a server", @@ -45,19 +46,19 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } serverLabel, err := iaasUtils.GetServerName(ctx, apiClient, model.ProjectId, *model.ServerId) if err != nil { - p.Debug(print.ErrorLevel, "get server name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get server name: %v", err) serverLabel = *model.ServerId } else if serverLabel == "" { serverLabel = *model.ServerId @@ -65,7 +66,7 @@ func NewCmd(p *print.Printer) *cobra.Command { if !model.AssumeYes { prompt := fmt.Sprintf("Are your sure you want to detach service account %q from a server %q?", model.ServiceAccMail, serverLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -78,7 +79,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("detach service account request: %w", err) } - return outputResult(p, model.OutputFormat, model.ServiceAccMail, serverLabel, *resp) + return outputResult(params.Printer, model.OutputFormat, model.ServiceAccMail, serverLabel, *resp) }, } configureFlags(cmd) diff --git a/internal/cmd/server/service-account/detach/detach_test.go b/internal/cmd/server/service-account/detach/detach_test.go index ffee083d4..acfd02802 100644 --- a/internal/cmd/server/service-account/detach/detach_test.go +++ b/internal/cmd/server/service-account/detach/detach_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -146,7 +147,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -244,7 +245,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.serviceAccMail, tt.args.serverLabel, tt.args.service); (err != nil) != tt.wantErr { diff --git a/internal/cmd/server/service-account/list/list.go b/internal/cmd/server/service-account/list/list.go index d9288a246..5c90714fd 100644 --- a/internal/cmd/server/service-account/list/list.go +++ b/internal/cmd/server/service-account/list/list.go @@ -5,6 +5,7 @@ import ( "encoding/json" "fmt" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" "github.com/stackitcloud/stackit-cli/internal/pkg/flags" @@ -30,7 +31,7 @@ type inputModel struct { ServerId *string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "list", Short: "List all attached service accounts for a server", @@ -52,20 +53,20 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } serverName, err := iaasUtils.GetServerName(ctx, apiClient, model.ProjectId, *model.ServerId) if err != nil { - p.Debug(print.ErrorLevel, "get server name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get server name: %v", err) serverName = *model.ServerId } else if serverName == "" { serverName = *model.ServerId @@ -79,7 +80,7 @@ func NewCmd(p *print.Printer) *cobra.Command { } serviceAccounts := *resp.Items if len(serviceAccounts) == 0 { - p.Info("No service accounts found for server %s\n", serverName) + params.Printer.Info("No service accounts found for server %s\n", serverName) return nil } @@ -87,7 +88,7 @@ func NewCmd(p *print.Printer) *cobra.Command { serviceAccounts = serviceAccounts[:int(*model.Limit)] } - return outputResult(p, model.OutputFormat, *model.ServerId, serverName, serviceAccounts) + return outputResult(params.Printer, model.OutputFormat, *model.ServerId, serverName, serviceAccounts) }, } configureFlags(cmd) diff --git a/internal/cmd/server/service-account/list/list_test.go b/internal/cmd/server/service-account/list/list_test.go index 961ba2a0a..9ba1d281a 100644 --- a/internal/cmd/server/service-account/list/list_test.go +++ b/internal/cmd/server/service-account/list/list_test.go @@ -5,6 +5,7 @@ import ( "strconv" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -149,7 +150,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -248,7 +249,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.serverId, tt.args.serverName, tt.args.serviceAccounts); (err != nil) != tt.wantErr { diff --git a/internal/cmd/server/service-account/service-account.go b/internal/cmd/server/service-account/service-account.go index 0739d29a7..b9455c563 100644 --- a/internal/cmd/server/service-account/service-account.go +++ b/internal/cmd/server/service-account/service-account.go @@ -3,15 +3,15 @@ package serviceaccount import ( "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/cmd/server/service-account/attach" "github.com/stackitcloud/stackit-cli/internal/cmd/server/service-account/detach" "github.com/stackitcloud/stackit-cli/internal/cmd/server/service-account/list" - "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" ) -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "service-account", Short: "Allows attaching/detaching service accounts to servers", @@ -19,12 +19,12 @@ func NewCmd(p *print.Printer) *cobra.Command { Args: cobra.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd, p) + addSubcommands(cmd, params) return cmd } -func addSubcommands(cmd *cobra.Command, p *print.Printer) { - cmd.AddCommand(attach.NewCmd(p)) - cmd.AddCommand(detach.NewCmd(p)) - cmd.AddCommand(list.NewCmd(p)) +func addSubcommands(cmd *cobra.Command, params *params.CmdParams) { + cmd.AddCommand(attach.NewCmd(params)) + cmd.AddCommand(detach.NewCmd(params)) + cmd.AddCommand(list.NewCmd(params)) } diff --git a/internal/cmd/server/start/start.go b/internal/cmd/server/start/start.go index 7daec696f..918bab2a3 100644 --- a/internal/cmd/server/start/start.go +++ b/internal/cmd/server/start/start.go @@ -4,6 +4,7 @@ import ( "context" "fmt" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -28,7 +29,7 @@ type inputModel struct { ServerId string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("start %s", serverIdArg), Short: "Starts an existing server or allocates the server if deallocated", @@ -42,20 +43,20 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } serverLabel, err := iaasUtils.GetServerName(ctx, apiClient, model.ProjectId, model.ServerId) if err != nil { - p.Debug(print.ErrorLevel, "get server name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get server name: %v", err) serverLabel = model.ServerId } else if serverLabel == "" { serverLabel = model.ServerId @@ -70,7 +71,7 @@ func NewCmd(p *print.Printer) *cobra.Command { // Wait for async operation, if async mode not enabled if !model.Async { - s := spinner.New(p) + s := spinner.New(params.Printer) s.Start("Starting server") _, err = wait.StartServerWaitHandler(ctx, apiClient, model.ProjectId, model.ServerId).WaitWithContext(ctx) if err != nil { @@ -83,7 +84,7 @@ func NewCmd(p *print.Printer) *cobra.Command { if model.Async { operationState = "Triggered start of" } - p.Info("%s server %q\n", operationState, serverLabel) + params.Printer.Info("%s server %q\n", operationState, serverLabel) return nil }, diff --git a/internal/cmd/server/start/start_test.go b/internal/cmd/server/start/start_test.go index f90b6048d..2776c77e7 100644 --- a/internal/cmd/server/start/start_test.go +++ b/internal/cmd/server/start/start_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" @@ -128,7 +129,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/server/stop/stop.go b/internal/cmd/server/stop/stop.go index 1201c778b..ccb21bedc 100644 --- a/internal/cmd/server/stop/stop.go +++ b/internal/cmd/server/stop/stop.go @@ -4,6 +4,7 @@ import ( "context" "fmt" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -28,7 +29,7 @@ type inputModel struct { ServerId string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("stop %s", serverIdArg), Short: "Stops an existing server", @@ -42,20 +43,20 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } serverLabel, err := iaasUtils.GetServerName(ctx, apiClient, model.ProjectId, model.ServerId) if err != nil { - p.Debug(print.ErrorLevel, "get server name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get server name: %v", err) serverLabel = model.ServerId } else if serverLabel == "" { serverLabel = model.ServerId @@ -63,7 +64,7 @@ func NewCmd(p *print.Printer) *cobra.Command { if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to stop server %q?", serverLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -78,7 +79,7 @@ func NewCmd(p *print.Printer) *cobra.Command { // Wait for async operation, if async mode not enabled if !model.Async { - s := spinner.New(p) + s := spinner.New(params.Printer) s.Start("Stopping server") _, err = wait.StopServerWaitHandler(ctx, apiClient, model.ProjectId, model.ServerId).WaitWithContext(ctx) if err != nil { @@ -91,7 +92,7 @@ func NewCmd(p *print.Printer) *cobra.Command { if model.Async { operationState = "Triggered stop of" } - p.Info("%s server %q\n", operationState, serverLabel) + params.Printer.Info("%s server %q\n", operationState, serverLabel) return nil }, diff --git a/internal/cmd/server/stop/stop_test.go b/internal/cmd/server/stop/stop_test.go index a4e889181..bbaefddba 100644 --- a/internal/cmd/server/stop/stop_test.go +++ b/internal/cmd/server/stop/stop_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" @@ -128,7 +129,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/server/unrescue/unrescue.go b/internal/cmd/server/unrescue/unrescue.go index 5fc2d7c42..181ab88bc 100644 --- a/internal/cmd/server/unrescue/unrescue.go +++ b/internal/cmd/server/unrescue/unrescue.go @@ -4,6 +4,7 @@ import ( "context" "fmt" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -28,7 +29,7 @@ type inputModel struct { ServerId string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("unrescue %s", serverIdArg), Short: "Unrescues an existing server", @@ -42,20 +43,20 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } serverLabel, err := iaasUtils.GetServerName(ctx, apiClient, model.ProjectId, model.ServerId) if err != nil { - p.Debug(print.ErrorLevel, "get server name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get server name: %v", err) serverLabel = model.ServerId } else if serverLabel == "" { serverLabel = model.ServerId @@ -63,7 +64,7 @@ func NewCmd(p *print.Printer) *cobra.Command { if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to unrescue server %q?", serverLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -78,7 +79,7 @@ func NewCmd(p *print.Printer) *cobra.Command { // Wait for async operation, if async mode not enabled if !model.Async { - s := spinner.New(p) + s := spinner.New(params.Printer) s.Start("Unrescuing server") _, err = wait.UnrescueServerWaitHandler(ctx, apiClient, model.ProjectId, model.ServerId).WaitWithContext(ctx) if err != nil { @@ -91,7 +92,7 @@ func NewCmd(p *print.Printer) *cobra.Command { if model.Async { operationState = "Triggered unrescue of" } - p.Info("%s server %q\n", operationState, serverLabel) + params.Printer.Info("%s server %q\n", operationState, serverLabel) return nil }, diff --git a/internal/cmd/server/unrescue/unrescue_test.go b/internal/cmd/server/unrescue/unrescue_test.go index 82f75a370..1f2d95712 100644 --- a/internal/cmd/server/unrescue/unrescue_test.go +++ b/internal/cmd/server/unrescue/unrescue_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" @@ -128,7 +129,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/server/update/update.go b/internal/cmd/server/update/update.go index d7e06d95a..2b8b35985 100644 --- a/internal/cmd/server/update/update.go +++ b/internal/cmd/server/update/update.go @@ -8,6 +8,7 @@ import ( "github.com/goccy/go-yaml" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" cliErr "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -34,7 +35,7 @@ type inputModel struct { Labels *map[string]string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("update %s", serverIdArg), Short: "Updates a server", @@ -52,20 +53,20 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } serverLabel, err := iaasUtils.GetServerName(ctx, apiClient, model.ProjectId, model.ServerId) if err != nil { - p.Debug(print.ErrorLevel, "get server name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get server name: %v", err) serverLabel = model.ServerId } else if serverLabel == "" { serverLabel = model.ServerId @@ -73,7 +74,7 @@ func NewCmd(p *print.Printer) *cobra.Command { if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to update server %q?", serverLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -86,7 +87,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("update server: %w", err) } - return outputResult(p, model.OutputFormat, serverLabel, resp) + return outputResult(params.Printer, model.OutputFormat, serverLabel, resp) }, } configureFlags(cmd) diff --git a/internal/cmd/server/update/update_test.go b/internal/cmd/server/update/update_test.go index 5286eb70f..63398311e 100644 --- a/internal/cmd/server/update/update_test.go +++ b/internal/cmd/server/update/update_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -172,7 +173,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -269,7 +270,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.serverLabel, tt.args.server); (err != nil) != tt.wantErr { diff --git a/internal/cmd/server/volume/attach/attach.go b/internal/cmd/server/volume/attach/attach.go index 61eab4ba0..fee7c9097 100644 --- a/internal/cmd/server/volume/attach/attach.go +++ b/internal/cmd/server/volume/attach/attach.go @@ -7,6 +7,7 @@ import ( "github.com/goccy/go-yaml" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -35,7 +36,7 @@ type inputModel struct { DeleteOnTermination *bool } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("attach %s", volumeIdArg), Short: "Attaches a volume to a server", @@ -53,20 +54,20 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } volumeLabel, err := iaasUtils.GetVolumeName(ctx, apiClient, model.ProjectId, model.VolumeId) if err != nil { - p.Debug(print.ErrorLevel, "get volume name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get volume name: %v", err) volumeLabel = model.VolumeId } else if volumeLabel == "" { volumeLabel = model.VolumeId @@ -74,7 +75,7 @@ func NewCmd(p *print.Printer) *cobra.Command { serverLabel, err := iaasUtils.GetServerName(ctx, apiClient, model.ProjectId, *model.ServerId) if err != nil { - p.Debug(print.ErrorLevel, "get server name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get server name: %v", err) serverLabel = *model.ServerId } else if serverLabel == "" { serverLabel = *model.ServerId @@ -82,7 +83,7 @@ func NewCmd(p *print.Printer) *cobra.Command { if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to attach volume %q to server %q?", volumeLabel, serverLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -95,7 +96,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("attach server volume: %w", err) } - return outputResult(p, model.OutputFormat, volumeLabel, serverLabel, *resp) + return outputResult(params.Printer, model.OutputFormat, volumeLabel, serverLabel, *resp) }, } configureFlags(cmd) diff --git a/internal/cmd/server/volume/attach/attach_test.go b/internal/cmd/server/volume/attach/attach_test.go index 29dd0ff1f..9a180a4c0 100644 --- a/internal/cmd/server/volume/attach/attach_test.go +++ b/internal/cmd/server/volume/attach/attach_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -171,7 +172,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -269,7 +270,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.volumeLabel, tt.args.serverLabel, tt.args.volume); (err != nil) != tt.wantErr { diff --git a/internal/cmd/server/volume/describe/describe.go b/internal/cmd/server/volume/describe/describe.go index 9336183a5..87ebac142 100644 --- a/internal/cmd/server/volume/describe/describe.go +++ b/internal/cmd/server/volume/describe/describe.go @@ -7,6 +7,7 @@ import ( "github.com/goccy/go-yaml" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -31,7 +32,7 @@ type inputModel struct { VolumeId string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("describe %s", volumeIdArg), Short: "Describes a server volume attachment", @@ -53,20 +54,20 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } volumeLabel, err := iaasUtils.GetVolumeName(ctx, apiClient, model.ProjectId, model.VolumeId) if err != nil { - p.Debug(print.ErrorLevel, "get volume name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get volume name: %v", err) volumeLabel = model.VolumeId } else if volumeLabel == "" { volumeLabel = model.VolumeId @@ -74,7 +75,7 @@ func NewCmd(p *print.Printer) *cobra.Command { serverLabel, err := iaasUtils.GetServerName(ctx, apiClient, model.ProjectId, *model.ServerId) if err != nil { - p.Debug(print.ErrorLevel, "get server name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get server name: %v", err) serverLabel = *model.ServerId } else if serverLabel == "" { serverLabel = *model.ServerId @@ -87,7 +88,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("describe server volume: %w", err) } - return outputResult(p, model.OutputFormat, serverLabel, volumeLabel, *resp) + return outputResult(params.Printer, model.OutputFormat, serverLabel, volumeLabel, *resp) }, } configureFlags(cmd) diff --git a/internal/cmd/server/volume/describe/describe_test.go b/internal/cmd/server/volume/describe/describe_test.go index c86b57259..f2182f04c 100644 --- a/internal/cmd/server/volume/describe/describe_test.go +++ b/internal/cmd/server/volume/describe/describe_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -147,7 +148,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -245,7 +246,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.serverLabel, tt.args.volumeLabel, tt.args.volume); (err != nil) != tt.wantErr { diff --git a/internal/cmd/server/volume/detach/detach.go b/internal/cmd/server/volume/detach/detach.go index 826f1881c..e557fc383 100644 --- a/internal/cmd/server/volume/detach/detach.go +++ b/internal/cmd/server/volume/detach/detach.go @@ -5,6 +5,7 @@ import ( "fmt" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -29,7 +30,7 @@ type inputModel struct { VolumeId string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("detach %s", volumeIdArg), Short: "Detaches a volume from a server", @@ -43,20 +44,20 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } volumeLabel, err := iaasUtils.GetVolumeName(ctx, apiClient, model.ProjectId, model.VolumeId) if err != nil { - p.Debug(print.ErrorLevel, "get volume name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get volume name: %v", err) volumeLabel = model.VolumeId } else if volumeLabel == "" { volumeLabel = model.VolumeId @@ -64,7 +65,7 @@ func NewCmd(p *print.Printer) *cobra.Command { serverLabel, err := iaasUtils.GetServerName(ctx, apiClient, model.ProjectId, *model.ServerId) if err != nil { - p.Debug(print.ErrorLevel, "get server name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get server name: %v", err) serverLabel = *model.ServerId } else if serverLabel == "" { serverLabel = *model.ServerId @@ -72,7 +73,7 @@ func NewCmd(p *print.Printer) *cobra.Command { if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to detach volume %q from server %q?", volumeLabel, serverLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -84,7 +85,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("detach server volume: %w", err) } - p.Info("Detached volume %q from server %q\n", volumeLabel, serverLabel) + params.Printer.Info("Detached volume %q from server %q\n", volumeLabel, serverLabel) return nil }, diff --git a/internal/cmd/server/volume/detach/detach_test.go b/internal/cmd/server/volume/detach/detach_test.go index dd2032277..4934e778d 100644 --- a/internal/cmd/server/volume/detach/detach_test.go +++ b/internal/cmd/server/volume/detach/detach_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -146,7 +147,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/server/volume/list/list.go b/internal/cmd/server/volume/list/list.go index dd7509d7f..f3ac90101 100644 --- a/internal/cmd/server/volume/list/list.go +++ b/internal/cmd/server/volume/list/list.go @@ -7,6 +7,7 @@ import ( "github.com/goccy/go-yaml" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -29,7 +30,7 @@ type inputModel struct { ServerId *string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "list", Short: "Lists all server volumes", @@ -45,20 +46,20 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } serverLabel, err := iaasUtils.GetServerName(ctx, apiClient, model.ProjectId, *model.ServerId) if err != nil { - p.Debug(print.ErrorLevel, "get server name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get server name: %v", err) serverLabel = *model.ServerId } else if serverLabel == "" { serverLabel = *model.ServerId @@ -72,7 +73,7 @@ func NewCmd(p *print.Printer) *cobra.Command { } volumes := *resp.Items if len(volumes) == 0 { - p.Info("No volumes found for server %s\n", serverLabel) + params.Printer.Info("No volumes found for server %s\n", serverLabel) return nil } @@ -81,12 +82,12 @@ func NewCmd(p *print.Printer) *cobra.Command { for i := range volumes { volumeLabel, err := iaasUtils.GetVolumeName(ctx, apiClient, model.ProjectId, *volumes[i].VolumeId) if err != nil { - p.Debug(print.ErrorLevel, "get volume name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get volume name: %v", err) } volumeNames = append(volumeNames, volumeLabel) } - return outputResult(p, model.OutputFormat, serverLabel, volumeNames, volumes) + return outputResult(params.Printer, model.OutputFormat, serverLabel, volumeNames, volumes) }, } configureFlags(cmd) diff --git a/internal/cmd/server/volume/list/list_test.go b/internal/cmd/server/volume/list/list_test.go index b9c37814f..a337fc247 100644 --- a/internal/cmd/server/volume/list/list_test.go +++ b/internal/cmd/server/volume/list/list_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -121,7 +122,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -220,7 +221,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.serverLabel, tt.args.volumeNames, tt.args.volumes); (err != nil) != tt.wantErr { diff --git a/internal/cmd/server/volume/update/update.go b/internal/cmd/server/volume/update/update.go index 5722808ef..e4795996d 100644 --- a/internal/cmd/server/volume/update/update.go +++ b/internal/cmd/server/volume/update/update.go @@ -7,6 +7,7 @@ import ( "github.com/goccy/go-yaml" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -35,7 +36,7 @@ type inputModel struct { DeleteOnTermination *bool } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("update %s", volumeIdArg), Short: "Updates an attached volume of a server", @@ -49,20 +50,20 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } volumeLabel, err := iaasUtils.GetVolumeName(ctx, apiClient, model.ProjectId, model.VolumeId) if err != nil { - p.Debug(print.ErrorLevel, "get volume name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get volume name: %v", err) volumeLabel = model.VolumeId } else if volumeLabel == "" { volumeLabel = model.VolumeId @@ -70,7 +71,7 @@ func NewCmd(p *print.Printer) *cobra.Command { serverLabel, err := iaasUtils.GetServerName(ctx, apiClient, model.ProjectId, *model.ServerId) if err != nil { - p.Debug(print.ErrorLevel, "get server name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get server name: %v", err) serverLabel = *model.ServerId } else if serverLabel == "" { serverLabel = *model.ServerId @@ -78,7 +79,7 @@ func NewCmd(p *print.Printer) *cobra.Command { if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to update attached volume %q of server %q?", volumeLabel, serverLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -91,7 +92,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("update server volume: %w", err) } - return outputResult(p, model.OutputFormat, volumeLabel, serverLabel, *resp) + return outputResult(params.Printer, model.OutputFormat, volumeLabel, serverLabel, *resp) }, } configureFlags(cmd) diff --git a/internal/cmd/server/volume/update/update_test.go b/internal/cmd/server/volume/update/update_test.go index 46d5a0d20..1f820cff2 100644 --- a/internal/cmd/server/volume/update/update_test.go +++ b/internal/cmd/server/volume/update/update_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -170,7 +171,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -268,7 +269,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.volumeLabel, tt.args.serverLabel, tt.args.volume); (err != nil) != tt.wantErr { diff --git a/internal/cmd/server/volume/volume.go b/internal/cmd/server/volume/volume.go index c851bbcd7..15dd14ef7 100644 --- a/internal/cmd/server/volume/volume.go +++ b/internal/cmd/server/volume/volume.go @@ -1,19 +1,19 @@ package volume import ( + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/cmd/server/volume/attach" "github.com/stackitcloud/stackit-cli/internal/cmd/server/volume/describe" "github.com/stackitcloud/stackit-cli/internal/cmd/server/volume/detach" "github.com/stackitcloud/stackit-cli/internal/cmd/server/volume/list" "github.com/stackitcloud/stackit-cli/internal/cmd/server/volume/update" "github.com/stackitcloud/stackit-cli/internal/pkg/args" - "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" "github.com/spf13/cobra" ) -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "volume", Short: "Provides functionality for server volumes", @@ -21,14 +21,14 @@ func NewCmd(p *print.Printer) *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd, p) + addSubcommands(cmd, params) return cmd } -func addSubcommands(cmd *cobra.Command, p *print.Printer) { - cmd.AddCommand(attach.NewCmd(p)) - cmd.AddCommand(detach.NewCmd(p)) - cmd.AddCommand(update.NewCmd(p)) - cmd.AddCommand(list.NewCmd(p)) - cmd.AddCommand(describe.NewCmd(p)) +func addSubcommands(cmd *cobra.Command, params *params.CmdParams) { + cmd.AddCommand(attach.NewCmd(params)) + cmd.AddCommand(detach.NewCmd(params)) + cmd.AddCommand(update.NewCmd(params)) + cmd.AddCommand(list.NewCmd(params)) + cmd.AddCommand(describe.NewCmd(params)) } diff --git a/internal/cmd/service-account/create/create.go b/internal/cmd/service-account/create/create.go index eedf109aa..fba36dfeb 100644 --- a/internal/cmd/service-account/create/create.go +++ b/internal/cmd/service-account/create/create.go @@ -6,6 +6,7 @@ import ( "fmt" "github.com/goccy/go-yaml" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -29,7 +30,7 @@ type inputModel struct { Name *string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "create", Short: "Creates a service account", @@ -42,26 +43,26 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } - projectLabel, err := projectname.GetProjectName(ctx, p, cmd) + projectLabel, err := projectname.GetProjectName(ctx, params.Printer, cmd) if err != nil { - p.Debug(print.ErrorLevel, "get project name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get project name: %v", err) projectLabel = model.ProjectId } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to create a service account for project %q?", projectLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -74,7 +75,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("create service account: %w", err) } - return outputResult(p, model.OutputFormat, projectLabel, resp) + return outputResult(params.Printer, model.OutputFormat, projectLabel, resp) }, } configureFlags(cmd) diff --git a/internal/cmd/service-account/create/create_test.go b/internal/cmd/service-account/create/create_test.go index d74e9ad3f..1c3fac895 100644 --- a/internal/cmd/service-account/create/create_test.go +++ b/internal/cmd/service-account/create/create_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -117,7 +118,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -213,7 +214,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.projectLabel, tt.args.serviceAccount); (err != nil) != tt.wantErr { diff --git a/internal/cmd/service-account/delete/delete.go b/internal/cmd/service-account/delete/delete.go index 3bb571f87..829d6ac4c 100644 --- a/internal/cmd/service-account/delete/delete.go +++ b/internal/cmd/service-account/delete/delete.go @@ -4,6 +4,7 @@ import ( "context" "fmt" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -24,7 +25,7 @@ type inputModel struct { Email string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("delete %s", emailArg), Short: "Deletes a service account", @@ -37,20 +38,20 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to delete service account %s? (This cannot be undone)", model.Email) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -66,7 +67,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("delete service account: %w", err) } - p.Info("Service account %s deleted\n", model.Email) + params.Printer.Info("Service account %s deleted\n", model.Email) return nil }, } diff --git a/internal/cmd/service-account/delete/delete_test.go b/internal/cmd/service-account/delete/delete_test.go index a48cf2998..9a0dc74ef 100644 --- a/internal/cmd/service-account/delete/delete_test.go +++ b/internal/cmd/service-account/delete/delete_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" @@ -126,7 +127,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/service-account/get-jwks/get_jwks.go b/internal/cmd/service-account/get-jwks/get_jwks.go index e86858c3e..641cba592 100644 --- a/internal/cmd/service-account/get-jwks/get_jwks.go +++ b/internal/cmd/service-account/get-jwks/get_jwks.go @@ -5,6 +5,7 @@ import ( "encoding/json" "fmt" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" "github.com/stackitcloud/stackit-cli/internal/pkg/print" @@ -22,7 +23,7 @@ type inputModel struct { Email string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("get-jwks %s", emailArg), Short: "Shows the JWKS for a service account", @@ -35,13 +36,13 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -54,11 +55,11 @@ func NewCmd(p *print.Printer) *cobra.Command { } jwks := *resp.Keys if len(jwks) == 0 { - p.Info("Empty JWKS for service account %s\n", model.Email) + params.Printer.Info("Empty JWKS for service account %s\n", model.Email) return nil } - return outputResult(p, jwks) + return outputResult(params.Printer, jwks) }, } diff --git a/internal/cmd/service-account/get-jwks/get_jwks_test.go b/internal/cmd/service-account/get-jwks/get_jwks_test.go index 93b049d14..c418ffb22 100644 --- a/internal/cmd/service-account/get-jwks/get_jwks_test.go +++ b/internal/cmd/service-account/get-jwks/get_jwks_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" @@ -69,7 +70,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -168,7 +169,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.serviceAccounts); (err != nil) != tt.wantErr { diff --git a/internal/cmd/service-account/key/create/create.go b/internal/cmd/service-account/key/create/create.go index 8cf058592..6d99a38fb 100644 --- a/internal/cmd/service-account/key/create/create.go +++ b/internal/cmd/service-account/key/create/create.go @@ -6,6 +6,7 @@ import ( "fmt" "time" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -33,7 +34,7 @@ type inputModel struct { PublicKey *string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "create", Short: "Creates a service account key", @@ -56,13 +57,13 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -73,7 +74,7 @@ func NewCmd(p *print.Printer) *cobra.Command { validUntilInfo = fmt.Sprintf("The key will be valid for %d days", *model.ExpiresInDays) } prompt := fmt.Sprintf("Are you sure you want to create a key for service account %s? %s", model.ServiceAccountEmail, validUntilInfo) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -86,13 +87,13 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("create service account key: %w", err) } - p.Info("Created key for service account %s with ID %q\n", model.ServiceAccountEmail, *resp.Id) + params.Printer.Info("Created key for service account %s with ID %q\n", model.ServiceAccountEmail, *resp.Id) key, err := json.MarshalIndent(resp, "", " ") if err != nil { return fmt.Errorf("marshal key: %w", err) } - p.Outputln(string(key)) + params.Printer.Outputln(string(key)) return nil }, } diff --git a/internal/cmd/service-account/key/create/create_test.go b/internal/cmd/service-account/key/create/create_test.go index f2ded7df9..8f07ed01f 100644 --- a/internal/cmd/service-account/key/create/create_test.go +++ b/internal/cmd/service-account/key/create/create_test.go @@ -5,6 +5,7 @@ import ( "testing" "time" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -142,7 +143,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/service-account/key/delete/delete.go b/internal/cmd/service-account/key/delete/delete.go index 20e79fa74..ffd77b76a 100644 --- a/internal/cmd/service-account/key/delete/delete.go +++ b/internal/cmd/service-account/key/delete/delete.go @@ -4,6 +4,7 @@ import ( "context" "fmt" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -30,7 +31,7 @@ type inputModel struct { KeyId string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("delete %s", keyIdArg), Short: "Deletes a service account key", @@ -43,20 +44,20 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to delete the key %s from service account %s?", model.KeyId, model.ServiceAccountEmail) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -69,7 +70,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("delete key: %w", err) } - p.Info("Deleted key %s from service account %s\n", model.KeyId, model.ServiceAccountEmail) + params.Printer.Info("Deleted key %s from service account %s\n", model.KeyId, model.ServiceAccountEmail) return nil }, } diff --git a/internal/cmd/service-account/key/delete/delete_test.go b/internal/cmd/service-account/key/delete/delete_test.go index ca808acd5..9e811b0a6 100644 --- a/internal/cmd/service-account/key/delete/delete_test.go +++ b/internal/cmd/service-account/key/delete/delete_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" @@ -149,7 +150,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/service-account/key/describe/describe.go b/internal/cmd/service-account/key/describe/describe.go index 61bc9af84..3b7c16ca4 100644 --- a/internal/cmd/service-account/key/describe/describe.go +++ b/internal/cmd/service-account/key/describe/describe.go @@ -5,6 +5,7 @@ import ( "encoding/json" "fmt" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -31,7 +32,7 @@ type inputModel struct { KeyId string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("describe %s", keyIdArg), Short: "Shows details of a service account key", @@ -44,12 +45,12 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -61,7 +62,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("read service account key: %w", err) } - return outputResult(p, resp) + return outputResult(params.Printer, resp) }, } configureFlags(cmd) diff --git a/internal/cmd/service-account/key/describe/describe_test.go b/internal/cmd/service-account/key/describe/describe_test.go index 18cd60082..7111fa0a6 100644 --- a/internal/cmd/service-account/key/describe/describe_test.go +++ b/internal/cmd/service-account/key/describe/describe_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" @@ -149,7 +150,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -253,7 +254,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.key); (err != nil) != tt.wantErr { diff --git a/internal/cmd/service-account/key/key.go b/internal/cmd/service-account/key/key.go index 969e3df91..b91d31b6a 100644 --- a/internal/cmd/service-account/key/key.go +++ b/internal/cmd/service-account/key/key.go @@ -1,19 +1,19 @@ package key import ( + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/cmd/service-account/key/create" "github.com/stackitcloud/stackit-cli/internal/cmd/service-account/key/delete" "github.com/stackitcloud/stackit-cli/internal/cmd/service-account/key/describe" "github.com/stackitcloud/stackit-cli/internal/cmd/service-account/key/list" "github.com/stackitcloud/stackit-cli/internal/cmd/service-account/key/update" "github.com/stackitcloud/stackit-cli/internal/pkg/args" - "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" "github.com/spf13/cobra" ) -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "key", Short: "Provides functionality for service account keys", @@ -21,14 +21,14 @@ func NewCmd(p *print.Printer) *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd, p) + addSubcommands(cmd, params) return cmd } -func addSubcommands(cmd *cobra.Command, p *print.Printer) { - cmd.AddCommand(create.NewCmd(p)) - cmd.AddCommand(delete.NewCmd(p)) - cmd.AddCommand(describe.NewCmd(p)) - cmd.AddCommand(list.NewCmd(p)) - cmd.AddCommand(update.NewCmd(p)) +func addSubcommands(cmd *cobra.Command, params *params.CmdParams) { + cmd.AddCommand(create.NewCmd(params)) + cmd.AddCommand(delete.NewCmd(params)) + cmd.AddCommand(describe.NewCmd(params)) + cmd.AddCommand(list.NewCmd(params)) + cmd.AddCommand(update.NewCmd(params)) } diff --git a/internal/cmd/service-account/key/list/list.go b/internal/cmd/service-account/key/list/list.go index 66c7fafcb..194c66486 100644 --- a/internal/cmd/service-account/key/list/list.go +++ b/internal/cmd/service-account/key/list/list.go @@ -7,6 +7,7 @@ import ( "github.com/goccy/go-yaml" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -31,7 +32,7 @@ type inputModel struct { Limit *int64 } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "list", Short: "Lists all service account keys", @@ -50,13 +51,13 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -69,7 +70,7 @@ func NewCmd(p *print.Printer) *cobra.Command { } keys := *resp.Items if len(keys) == 0 { - p.Info("No keys found for service account %s\n", model.ServiceAccountEmail) + params.Printer.Info("No keys found for service account %s\n", model.ServiceAccountEmail) return nil } @@ -78,7 +79,7 @@ func NewCmd(p *print.Printer) *cobra.Command { keys = keys[:*model.Limit] } - return outputResult(p, model.OutputFormat, keys) + return outputResult(params.Printer, model.OutputFormat, keys) }, } diff --git a/internal/cmd/service-account/key/list/list_test.go b/internal/cmd/service-account/key/list/list_test.go index 46933a70c..ebde67439 100644 --- a/internal/cmd/service-account/key/list/list_test.go +++ b/internal/cmd/service-account/key/list/list_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -228,7 +229,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.keys); (err != nil) != tt.wantErr { diff --git a/internal/cmd/service-account/key/update/update.go b/internal/cmd/service-account/key/update/update.go index 5aba23b81..9df30b2a9 100644 --- a/internal/cmd/service-account/key/update/update.go +++ b/internal/cmd/service-account/key/update/update.go @@ -6,6 +6,7 @@ import ( "fmt" "time" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -38,7 +39,7 @@ type inputModel struct { Deactivate bool } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("update %s", keyIdArg), Short: "Updates a service account key", @@ -60,20 +61,20 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to update the key with ID %q?", model.KeyId) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -90,7 +91,7 @@ func NewCmd(p *print.Printer) *cobra.Command { if err != nil { return fmt.Errorf("marshal key: %w", err) } - p.Info("%s", string(key)) + params.Printer.Info("%s", string(key)) return nil }, } diff --git a/internal/cmd/service-account/key/update/update_test.go b/internal/cmd/service-account/key/update/update_test.go index 706739579..84dde9c6d 100644 --- a/internal/cmd/service-account/key/update/update_test.go +++ b/internal/cmd/service-account/key/update/update_test.go @@ -5,6 +5,7 @@ import ( "testing" "time" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -196,7 +197,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/service-account/list/list.go b/internal/cmd/service-account/list/list.go index c4ea786d2..063e4176a 100644 --- a/internal/cmd/service-account/list/list.go +++ b/internal/cmd/service-account/list/list.go @@ -7,6 +7,7 @@ import ( "github.com/goccy/go-yaml" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -29,7 +30,7 @@ type inputModel struct { Limit *int64 } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "list", Short: "Lists all service accounts", @@ -42,13 +43,13 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -61,12 +62,12 @@ func NewCmd(p *print.Printer) *cobra.Command { } serviceAccounts := *resp.Items if len(serviceAccounts) == 0 { - projectLabel, err := projectname.GetProjectName(ctx, p, cmd) + projectLabel, err := projectname.GetProjectName(ctx, params.Printer, cmd) if err != nil { - p.Debug(print.ErrorLevel, "get project name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get project name: %v", err) projectLabel = model.ProjectId } - p.Info("No service accounts found for project %q\n", projectLabel) + params.Printer.Info("No service accounts found for project %q\n", projectLabel) return nil } @@ -75,7 +76,7 @@ func NewCmd(p *print.Printer) *cobra.Command { serviceAccounts = serviceAccounts[:*model.Limit] } - return outputResult(p, model.OutputFormat, serviceAccounts) + return outputResult(params.Printer, model.OutputFormat, serviceAccounts) }, } diff --git a/internal/cmd/service-account/list/list_test.go b/internal/cmd/service-account/list/list_test.go index d212eabc8..161f050e2 100644 --- a/internal/cmd/service-account/list/list_test.go +++ b/internal/cmd/service-account/list/list_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -218,7 +219,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.serviceAccounts); (err != nil) != tt.wantErr { diff --git a/internal/cmd/service-account/service_account.go b/internal/cmd/service-account/service_account.go index 8a4f4634d..9c2462bfa 100644 --- a/internal/cmd/service-account/service_account.go +++ b/internal/cmd/service-account/service_account.go @@ -1,6 +1,7 @@ package serviceaccount import ( + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/cmd/service-account/create" "github.com/stackitcloud/stackit-cli/internal/cmd/service-account/delete" getjwks "github.com/stackitcloud/stackit-cli/internal/cmd/service-account/get-jwks" @@ -8,13 +9,12 @@ import ( "github.com/stackitcloud/stackit-cli/internal/cmd/service-account/list" "github.com/stackitcloud/stackit-cli/internal/cmd/service-account/token" "github.com/stackitcloud/stackit-cli/internal/pkg/args" - "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" "github.com/spf13/cobra" ) -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "service-account", Short: "Provides functionality for service accounts", @@ -22,16 +22,16 @@ func NewCmd(p *print.Printer) *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd, p) + addSubcommands(cmd, params) return cmd } -func addSubcommands(cmd *cobra.Command, p *print.Printer) { - cmd.AddCommand(list.NewCmd(p)) - cmd.AddCommand(create.NewCmd(p)) - cmd.AddCommand(delete.NewCmd(p)) - cmd.AddCommand(getjwks.NewCmd(p)) +func addSubcommands(cmd *cobra.Command, params *params.CmdParams) { + cmd.AddCommand(list.NewCmd(params)) + cmd.AddCommand(create.NewCmd(params)) + cmd.AddCommand(delete.NewCmd(params)) + cmd.AddCommand(getjwks.NewCmd(params)) - cmd.AddCommand(key.NewCmd(p)) - cmd.AddCommand(token.NewCmd(p)) + cmd.AddCommand(key.NewCmd(params)) + cmd.AddCommand(token.NewCmd(params)) } diff --git a/internal/cmd/service-account/token/create/create.go b/internal/cmd/service-account/token/create/create.go index 8f96b4002..83c249644 100644 --- a/internal/cmd/service-account/token/create/create.go +++ b/internal/cmd/service-account/token/create/create.go @@ -7,6 +7,7 @@ import ( "github.com/goccy/go-yaml" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -32,7 +33,7 @@ type inputModel struct { TTLDays *int64 } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "create", Short: "Creates an access token for a service account", @@ -52,20 +53,20 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to create an access token for service account %s?", model.ServiceAccountEmail) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -78,7 +79,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("create access token: %w", err) } - return outputResult(p, model.OutputFormat, model.ServiceAccountEmail, token) + return outputResult(params.Printer, model.OutputFormat, model.ServiceAccountEmail, token) }, } diff --git a/internal/cmd/service-account/token/create/create_test.go b/internal/cmd/service-account/token/create/create_test.go index d5d18e3e9..ffa760ce5 100644 --- a/internal/cmd/service-account/token/create/create_test.go +++ b/internal/cmd/service-account/token/create/create_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -121,7 +122,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -219,7 +220,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.serviceAccountEmail, tt.args.token); (err != nil) != tt.wantErr { diff --git a/internal/cmd/service-account/token/list/list.go b/internal/cmd/service-account/token/list/list.go index 48b793f42..5a51b0ab7 100644 --- a/internal/cmd/service-account/token/list/list.go +++ b/internal/cmd/service-account/token/list/list.go @@ -6,6 +6,7 @@ import ( "fmt" "github.com/goccy/go-yaml" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -32,7 +33,7 @@ type inputModel struct { Limit *int64 } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "list", Short: "Lists access tokens of a service account", @@ -55,13 +56,13 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -74,7 +75,7 @@ func NewCmd(p *print.Printer) *cobra.Command { } tokensMetadata := *resp.Items if len(tokensMetadata) == 0 { - p.Info("No tokens found for service account with email %q\n", model.ServiceAccountEmail) + params.Printer.Info("No tokens found for service account with email %q\n", model.ServiceAccountEmail) return nil } @@ -83,7 +84,7 @@ func NewCmd(p *print.Printer) *cobra.Command { tokensMetadata = tokensMetadata[:*model.Limit] } - return outputResult(p, model.OutputFormat, tokensMetadata) + return outputResult(params.Printer, model.OutputFormat, tokensMetadata) }, } diff --git a/internal/cmd/service-account/token/list/list_test.go b/internal/cmd/service-account/token/list/list_test.go index fc3f5b54b..e03ee657f 100644 --- a/internal/cmd/service-account/token/list/list_test.go +++ b/internal/cmd/service-account/token/list/list_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -228,7 +229,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.tokensMetadata); (err != nil) != tt.wantErr { diff --git a/internal/cmd/service-account/token/revoke/revoke.go b/internal/cmd/service-account/token/revoke/revoke.go index 212f5c2a6..18dfe9fb1 100644 --- a/internal/cmd/service-account/token/revoke/revoke.go +++ b/internal/cmd/service-account/token/revoke/revoke.go @@ -4,6 +4,7 @@ import ( "context" "fmt" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -29,7 +30,7 @@ type inputModel struct { TokenId string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("revoke %s", tokenIdArg), Short: "Revokes an access token of a service account", @@ -46,20 +47,20 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to revoke the access token with ID %q?", model.TokenId) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -72,7 +73,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("revoke access token: %w", err) } - p.Info("Revoked access token with ID %q\n", model.TokenId) + params.Printer.Info("Revoked access token with ID %q\n", model.TokenId) return nil }, } diff --git a/internal/cmd/service-account/token/revoke/revoke_test.go b/internal/cmd/service-account/token/revoke/revoke_test.go index 3cf5b1246..17387aceb 100644 --- a/internal/cmd/service-account/token/revoke/revoke_test.go +++ b/internal/cmd/service-account/token/revoke/revoke_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" @@ -149,7 +150,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/service-account/token/token.go b/internal/cmd/service-account/token/token.go index 45570ea97..ce4b4eac6 100644 --- a/internal/cmd/service-account/token/token.go +++ b/internal/cmd/service-account/token/token.go @@ -1,17 +1,17 @@ package token import ( + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/cmd/service-account/token/create" "github.com/stackitcloud/stackit-cli/internal/cmd/service-account/token/list" "github.com/stackitcloud/stackit-cli/internal/cmd/service-account/token/revoke" "github.com/stackitcloud/stackit-cli/internal/pkg/args" - "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" "github.com/spf13/cobra" ) -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "token", Short: "Provides functionality for service account tokens", @@ -19,12 +19,12 @@ func NewCmd(p *print.Printer) *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd, p) + addSubcommands(cmd, params) return cmd } -func addSubcommands(cmd *cobra.Command, p *print.Printer) { - cmd.AddCommand(create.NewCmd(p)) - cmd.AddCommand(list.NewCmd(p)) - cmd.AddCommand(revoke.NewCmd(p)) +func addSubcommands(cmd *cobra.Command, params *params.CmdParams) { + cmd.AddCommand(create.NewCmd(params)) + cmd.AddCommand(list.NewCmd(params)) + cmd.AddCommand(revoke.NewCmd(params)) } diff --git a/internal/cmd/ske/cluster/cluster.go b/internal/cmd/ske/cluster/cluster.go index c5bb7b36b..0158b7173 100644 --- a/internal/cmd/ske/cluster/cluster.go +++ b/internal/cmd/ske/cluster/cluster.go @@ -1,6 +1,7 @@ package cluster import ( + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/cmd/ske/cluster/create" "github.com/stackitcloud/stackit-cli/internal/cmd/ske/cluster/delete" "github.com/stackitcloud/stackit-cli/internal/cmd/ske/cluster/describe" @@ -8,13 +9,12 @@ import ( "github.com/stackitcloud/stackit-cli/internal/cmd/ske/cluster/list" "github.com/stackitcloud/stackit-cli/internal/cmd/ske/cluster/update" "github.com/stackitcloud/stackit-cli/internal/pkg/args" - "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" "github.com/spf13/cobra" ) -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "cluster", Short: "Provides functionality for SKE cluster", @@ -22,15 +22,15 @@ func NewCmd(p *print.Printer) *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd, p) + addSubcommands(cmd, params) return cmd } -func addSubcommands(cmd *cobra.Command, p *print.Printer) { - cmd.AddCommand(generatepayload.NewCmd(p)) - cmd.AddCommand(create.NewCmd(p)) - cmd.AddCommand(delete.NewCmd(p)) - cmd.AddCommand(describe.NewCmd(p)) - cmd.AddCommand(list.NewCmd(p)) - cmd.AddCommand(update.NewCmd(p)) +func addSubcommands(cmd *cobra.Command, params *params.CmdParams) { + cmd.AddCommand(generatepayload.NewCmd(params)) + cmd.AddCommand(create.NewCmd(params)) + cmd.AddCommand(delete.NewCmd(params)) + cmd.AddCommand(describe.NewCmd(params)) + cmd.AddCommand(list.NewCmd(params)) + cmd.AddCommand(update.NewCmd(params)) } diff --git a/internal/cmd/ske/cluster/create/create.go b/internal/cmd/ske/cluster/create/create.go index 195b343ca..5c017d90d 100644 --- a/internal/cmd/ske/cluster/create/create.go +++ b/internal/cmd/ske/cluster/create/create.go @@ -7,6 +7,7 @@ import ( "github.com/goccy/go-yaml" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -36,7 +37,7 @@ type inputModel struct { Payload *ske.CreateOrUpdateClusterPayload } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("create %s", clusterNameArg), Short: "Creates an SKE cluster", @@ -64,33 +65,33 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } - projectLabel, err := projectname.GetProjectName(ctx, p, cmd) + projectLabel, err := projectname.GetProjectName(ctx, params.Printer, cmd) if err != nil { - p.Debug(print.ErrorLevel, "get project name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get project name: %v", err) projectLabel = model.ProjectId } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to create a cluster for project %q?", projectLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } } // Configure ServiceEnable API client - serviceEnablementApiClient, err := serviceEnablementClient.ConfigureClient(p) + serviceEnablementApiClient, err := serviceEnablementClient.ConfigureClient(params.Printer) if err != nil { return err } @@ -134,7 +135,7 @@ func NewCmd(p *print.Printer) *cobra.Command { // Wait for async operation, if async mode not enabled if !model.Async { - s := spinner.New(p) + s := spinner.New(params.Printer) s.Start("Creating cluster") _, err = wait.CreateOrUpdateClusterWaitHandler(ctx, apiClient, model.ProjectId, name).WaitWithContext(ctx) if err != nil { @@ -143,7 +144,7 @@ func NewCmd(p *print.Printer) *cobra.Command { s.Stop() } - return outputResult(p, model.OutputFormat, model.Async, projectLabel, resp) + return outputResult(params.Printer, model.OutputFormat, model.Async, projectLabel, resp) }, } configureFlags(cmd) diff --git a/internal/cmd/ske/cluster/create/create_test.go b/internal/cmd/ske/cluster/create/create_test.go index 6e2e92b3f..3cc543398 100644 --- a/internal/cmd/ske/cluster/create/create_test.go +++ b/internal/cmd/ske/cluster/create/create_test.go @@ -5,6 +5,7 @@ import ( "testing" "time" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -228,7 +229,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -342,7 +343,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.async, tt.args.projectLabel, tt.args.cluster); (err != nil) != tt.wantErr { diff --git a/internal/cmd/ske/cluster/delete/delete.go b/internal/cmd/ske/cluster/delete/delete.go index 657001abe..b78925dd5 100644 --- a/internal/cmd/ske/cluster/delete/delete.go +++ b/internal/cmd/ske/cluster/delete/delete.go @@ -4,6 +4,7 @@ import ( "context" "fmt" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -26,7 +27,7 @@ type inputModel struct { ClusterName string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("delete %s", clusterNameArg), Short: "Deletes a SKE cluster", @@ -39,20 +40,20 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to delete cluster %q? (This cannot be undone)", model.ClusterName) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -67,7 +68,7 @@ func NewCmd(p *print.Printer) *cobra.Command { // Wait for async operation, if async mode not enabled if !model.Async { - s := spinner.New(p) + s := spinner.New(params.Printer) s.Start("Deleting cluster") _, err = wait.DeleteClusterWaitHandler(ctx, apiClient, model.ProjectId, model.ClusterName).WaitWithContext(ctx) if err != nil { @@ -80,7 +81,7 @@ func NewCmd(p *print.Printer) *cobra.Command { if model.Async { operationState = "Triggered deletion of" } - p.Info("%s cluster %q\n", operationState, model.ClusterName) + params.Printer.Info("%s cluster %q\n", operationState, model.ClusterName) return nil }, } diff --git a/internal/cmd/ske/cluster/delete/delete_test.go b/internal/cmd/ske/cluster/delete/delete_test.go index b15c7254c..b51e559ad 100644 --- a/internal/cmd/ske/cluster/delete/delete_test.go +++ b/internal/cmd/ske/cluster/delete/delete_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" @@ -126,7 +127,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/ske/cluster/describe/describe.go b/internal/cmd/ske/cluster/describe/describe.go index 52fb7af4d..1b6d08dc8 100644 --- a/internal/cmd/ske/cluster/describe/describe.go +++ b/internal/cmd/ske/cluster/describe/describe.go @@ -7,6 +7,7 @@ import ( "github.com/goccy/go-yaml" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -27,7 +28,7 @@ type inputModel struct { ClusterName string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("describe %s", clusterNameArg), Short: "Shows details of a SKE cluster", @@ -43,12 +44,12 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -60,7 +61,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("read SKE cluster: %w", err) } - return outputResult(p, model.OutputFormat, resp) + return outputResult(params.Printer, model.OutputFormat, resp) }, } return cmd diff --git a/internal/cmd/ske/cluster/describe/describe_test.go b/internal/cmd/ske/cluster/describe/describe_test.go index 99e644af4..13a1b9468 100644 --- a/internal/cmd/ske/cluster/describe/describe_test.go +++ b/internal/cmd/ske/cluster/describe/describe_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" @@ -126,7 +127,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -231,7 +232,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.cluster); (err != nil) != tt.wantErr { diff --git a/internal/cmd/ske/cluster/generate-payload/generate_payload.go b/internal/cmd/ske/cluster/generate-payload/generate_payload.go index 299b5e9ab..b3a94f2ef 100644 --- a/internal/cmd/ske/cluster/generate-payload/generate_payload.go +++ b/internal/cmd/ske/cluster/generate-payload/generate_payload.go @@ -5,6 +5,7 @@ import ( "encoding/json" "fmt" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -30,7 +31,7 @@ type inputModel struct { FilePath *string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "generate-payload", Short: "Generates a payload to create/update SKE clusters", @@ -56,13 +57,13 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -89,7 +90,7 @@ func NewCmd(p *print.Printer) *cobra.Command { } } - return outputResult(p, model.FilePath, payload) + return outputResult(params.Printer, model.FilePath, payload) }, } configureFlags(cmd) diff --git a/internal/cmd/ske/cluster/generate-payload/generate_payload_test.go b/internal/cmd/ske/cluster/generate-payload/generate_payload_test.go index 5516ccf26..ac758cd1d 100644 --- a/internal/cmd/ske/cluster/generate-payload/generate_payload_test.go +++ b/internal/cmd/ske/cluster/generate-payload/generate_payload_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -129,7 +130,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -241,7 +242,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.filePath, tt.args.payload); (err != nil) != tt.wantErr { diff --git a/internal/cmd/ske/cluster/list/list.go b/internal/cmd/ske/cluster/list/list.go index 7b5389454..a3cde587f 100644 --- a/internal/cmd/ske/cluster/list/list.go +++ b/internal/cmd/ske/cluster/list/list.go @@ -6,6 +6,7 @@ import ( "fmt" "github.com/goccy/go-yaml" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -32,7 +33,7 @@ type inputModel struct { Limit *int64 } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "list", Short: "Lists all SKE clusters", @@ -51,19 +52,19 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } // Configure ServiceEnable API client - serviceEnablementApiClient, err := serviceEnablementClient.ConfigureClient(p) + serviceEnablementApiClient, err := serviceEnablementClient.ConfigureClient(params.Printer) if err != nil { return err } @@ -85,12 +86,12 @@ func NewCmd(p *print.Printer) *cobra.Command { } clusters := *resp.Items if len(clusters) == 0 { - projectLabel, err := projectname.GetProjectName(ctx, p, cmd) + projectLabel, err := projectname.GetProjectName(ctx, params.Printer, cmd) if err != nil { - p.Debug(print.ErrorLevel, "get project name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get project name: %v", err) projectLabel = model.ProjectId } - p.Info("No clusters found for project %q\n", projectLabel) + params.Printer.Info("No clusters found for project %q\n", projectLabel) return nil } @@ -99,7 +100,7 @@ func NewCmd(p *print.Printer) *cobra.Command { clusters = clusters[:*model.Limit] } - return outputResult(p, model.OutputFormat, clusters) + return outputResult(params.Printer, model.OutputFormat, clusters) }, } diff --git a/internal/cmd/ske/cluster/list/list_test.go b/internal/cmd/ske/cluster/list/list_test.go index 2959d6be6..c7eb8a01a 100644 --- a/internal/cmd/ske/cluster/list/list_test.go +++ b/internal/cmd/ske/cluster/list/list_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -218,7 +219,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.clusters); (err != nil) != tt.wantErr { diff --git a/internal/cmd/ske/cluster/update/update.go b/internal/cmd/ske/cluster/update/update.go index c407b7fb8..aea878064 100644 --- a/internal/cmd/ske/cluster/update/update.go +++ b/internal/cmd/ske/cluster/update/update.go @@ -6,6 +6,7 @@ import ( "fmt" "github.com/goccy/go-yaml" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -33,7 +34,7 @@ type inputModel struct { Payload ske.CreateOrUpdateClusterPayload } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("update %s", clusterNameArg), Short: "Updates an SKE cluster", @@ -58,20 +59,20 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to update cluster %q?", model.ClusterName) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -96,7 +97,7 @@ func NewCmd(p *print.Printer) *cobra.Command { // Wait for async operation, if async mode not enabled if !model.Async { - s := spinner.New(p) + s := spinner.New(params.Printer) s.Start("Updating cluster") _, err = wait.CreateOrUpdateClusterWaitHandler(ctx, apiClient, model.ProjectId, name).WaitWithContext(ctx) if err != nil { @@ -105,7 +106,7 @@ func NewCmd(p *print.Printer) *cobra.Command { s.Stop() } - return outputResult(p, model.OutputFormat, model.Async, model.ClusterName, resp) + return outputResult(params.Printer, model.OutputFormat, model.Async, model.ClusterName, resp) }, } configureFlags(cmd) diff --git a/internal/cmd/ske/cluster/update/update_test.go b/internal/cmd/ske/cluster/update/update_test.go index 53bce8b78..e4ec07b3f 100644 --- a/internal/cmd/ske/cluster/update/update_test.go +++ b/internal/cmd/ske/cluster/update/update_test.go @@ -5,6 +5,7 @@ import ( "testing" "time" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -216,7 +217,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -322,7 +323,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.async, tt.args.clusterName, tt.args.cluster); (err != nil) != tt.wantErr { diff --git a/internal/cmd/ske/credentials/complete-rotation/complete_rotation.go b/internal/cmd/ske/credentials/complete-rotation/complete_rotation.go index f9936692e..69dd87211 100644 --- a/internal/cmd/ske/credentials/complete-rotation/complete_rotation.go +++ b/internal/cmd/ske/credentials/complete-rotation/complete_rotation.go @@ -4,6 +4,7 @@ import ( "context" "fmt" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -26,7 +27,7 @@ type inputModel struct { ClusterName string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("complete-rotation %s", clusterNameArg), Short: "Completes the rotation of the credentials associated to a SKE cluster", @@ -56,20 +57,20 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to complete the rotation of the credentials for SKE cluster %q?", model.ClusterName) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -84,7 +85,7 @@ func NewCmd(p *print.Printer) *cobra.Command { // Wait for async operation, if async mode not enabled if !model.Async { - s := spinner.New(p) + s := spinner.New(params.Printer) s.Start("Completing credentials rotation") _, err = wait.CompleteCredentialsRotationWaitHandler(ctx, apiClient, model.ProjectId, model.ClusterName).WaitWithContext(ctx) if err != nil { @@ -97,8 +98,8 @@ func NewCmd(p *print.Printer) *cobra.Command { if model.Async { operationState = "Triggered completion of credentials rotation" } - p.Info("%s for cluster %q\n", operationState, model.ClusterName) - p.Warn("Consider updating your kubeconfig with the new credentials, create a new kubeconfig by running:\n $ stackit ske kubeconfig create %s\n", model.ClusterName) + params.Printer.Info("%s for cluster %q\n", operationState, model.ClusterName) + params.Printer.Warn("Consider updating your kubeconfig with the new credentials, create a new kubeconfig by running:\n $ stackit ske kubeconfig create %s\n", model.ClusterName) return nil }, } diff --git a/internal/cmd/ske/credentials/complete-rotation/complete_rotation_test.go b/internal/cmd/ske/credentials/complete-rotation/complete_rotation_test.go index 6b840c3e7..35e773133 100644 --- a/internal/cmd/ske/credentials/complete-rotation/complete_rotation_test.go +++ b/internal/cmd/ske/credentials/complete-rotation/complete_rotation_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" @@ -126,7 +127,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/ske/credentials/credentials.go b/internal/cmd/ske/credentials/credentials.go index 6fdfb1fc3..2770f349a 100644 --- a/internal/cmd/ske/credentials/credentials.go +++ b/internal/cmd/ske/credentials/credentials.go @@ -1,16 +1,16 @@ package credentials import ( + "github.com/stackitcloud/stackit-cli/internal/cmd/params" completerotation "github.com/stackitcloud/stackit-cli/internal/cmd/ske/credentials/complete-rotation" startrotation "github.com/stackitcloud/stackit-cli/internal/cmd/ske/credentials/start-rotation" "github.com/stackitcloud/stackit-cli/internal/pkg/args" - "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" "github.com/spf13/cobra" ) -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "credentials", Short: "Provides functionality for SKE credentials", @@ -18,11 +18,11 @@ func NewCmd(p *print.Printer) *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd, p) + addSubcommands(cmd, params) return cmd } -func addSubcommands(cmd *cobra.Command, p *print.Printer) { - cmd.AddCommand(startrotation.NewCmd(p)) - cmd.AddCommand(completerotation.NewCmd(p)) +func addSubcommands(cmd *cobra.Command, params *params.CmdParams) { + cmd.AddCommand(startrotation.NewCmd(params)) + cmd.AddCommand(completerotation.NewCmd(params)) } diff --git a/internal/cmd/ske/credentials/start-rotation/start_rotation.go b/internal/cmd/ske/credentials/start-rotation/start_rotation.go index b11c42623..485e26a10 100644 --- a/internal/cmd/ske/credentials/start-rotation/start_rotation.go +++ b/internal/cmd/ske/credentials/start-rotation/start_rotation.go @@ -4,6 +4,7 @@ import ( "context" "fmt" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -26,7 +27,7 @@ type inputModel struct { ClusterName string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("start-rotation %s", clusterNameArg), Short: "Starts the rotation of the credentials associated to a SKE cluster", @@ -59,20 +60,20 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to start the rotation of the credentials for SKE cluster %q?", model.ClusterName) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -87,7 +88,7 @@ func NewCmd(p *print.Printer) *cobra.Command { // Wait for async operation, if async mode not enabled if !model.Async { - s := spinner.New(p) + s := spinner.New(params.Printer) s.Start("Starting credentials rotation") _, err = wait.StartCredentialsRotationWaitHandler(ctx, apiClient, model.ProjectId, model.ClusterName).WaitWithContext(ctx) if err != nil { @@ -100,8 +101,8 @@ func NewCmd(p *print.Printer) *cobra.Command { if model.Async { operationState = "Triggered start of credentials rotation" } - p.Info("%s for cluster %q\n", operationState, model.ClusterName) - p.Info("Complete the rotation by running:\n $ stackit ske credentials complete-rotation %s\n", model.ClusterName) + params.Printer.Info("%s for cluster %q\n", operationState, model.ClusterName) + params.Printer.Info("Complete the rotation by running:\n $ stackit ske credentials complete-rotation %s\n", model.ClusterName) return nil }, } diff --git a/internal/cmd/ske/credentials/start-rotation/start_rotation_test.go b/internal/cmd/ske/credentials/start-rotation/start_rotation_test.go index dc5643eaa..29f2825ac 100644 --- a/internal/cmd/ske/credentials/start-rotation/start_rotation_test.go +++ b/internal/cmd/ske/credentials/start-rotation/start_rotation_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" @@ -126,7 +127,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/ske/describe/describe.go b/internal/cmd/ske/describe/describe.go index 6694b04a7..06522d5fb 100644 --- a/internal/cmd/ske/describe/describe.go +++ b/internal/cmd/ske/describe/describe.go @@ -7,6 +7,7 @@ import ( "github.com/goccy/go-yaml" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -23,7 +24,7 @@ type inputModel struct { *globalflags.GlobalFlagModel } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "describe", Short: "Shows overall details regarding SKE", @@ -36,12 +37,12 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -53,7 +54,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("read SKE project details: %w", err) } - return outputResult(p, model.OutputFormat, resp, model.ProjectId) + return outputResult(params.Printer, model.OutputFormat, resp, model.ProjectId) }, } return cmd diff --git a/internal/cmd/ske/describe/describe_test.go b/internal/cmd/ske/describe/describe_test.go index 839ae512e..4fb7cbb64 100644 --- a/internal/cmd/ske/describe/describe_test.go +++ b/internal/cmd/ske/describe/describe_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" serviceEnablementUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/service-enablement/utils" @@ -196,7 +197,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.project, tt.args.projectId); (err != nil) != tt.wantErr { diff --git a/internal/cmd/ske/disable/disable.go b/internal/cmd/ske/disable/disable.go index d00a56715..12685a580 100644 --- a/internal/cmd/ske/disable/disable.go +++ b/internal/cmd/ske/disable/disable.go @@ -4,6 +4,7 @@ import ( "context" "fmt" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -23,7 +24,7 @@ type inputModel struct { *globalflags.GlobalFlagModel } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "disable", Short: "Disables SKE for a project", @@ -36,26 +37,26 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } - projectLabel, err := projectname.GetProjectName(ctx, p, cmd) + projectLabel, err := projectname.GetProjectName(ctx, params.Printer, cmd) if err != nil { - p.Debug(print.ErrorLevel, "get project name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get project name: %v", err) projectLabel = model.ProjectId } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to disable SKE for project %q? (This will delete all associated clusters)", projectLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -70,7 +71,7 @@ func NewCmd(p *print.Printer) *cobra.Command { // Wait for async operation, if async mode not enabled if !model.Async { - s := spinner.New(p) + s := spinner.New(params.Printer) s.Start("Disabling SKE") _, err = wait.DisableServiceWaitHandler(ctx, apiClient, model.Region, model.ProjectId, utils.SKEServiceId).WaitWithContext(ctx) if err != nil { @@ -83,7 +84,7 @@ func NewCmd(p *print.Printer) *cobra.Command { if model.Async { operationState = "Triggered disablement of" } - p.Info("%s SKE for project %q\n", operationState, projectLabel) + params.Printer.Info("%s SKE for project %q\n", operationState, projectLabel) return nil }, } diff --git a/internal/cmd/ske/enable/enable.go b/internal/cmd/ske/enable/enable.go index 9ab8ed7bf..7701c3bf5 100644 --- a/internal/cmd/ske/enable/enable.go +++ b/internal/cmd/ske/enable/enable.go @@ -4,6 +4,7 @@ import ( "context" "fmt" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -23,7 +24,7 @@ type inputModel struct { *globalflags.GlobalFlagModel } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "enable", Short: "Enables SKE for a project", @@ -36,26 +37,26 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } - projectLabel, err := projectname.GetProjectName(ctx, p, cmd) + projectLabel, err := projectname.GetProjectName(ctx, params.Printer, cmd) if err != nil { - p.Debug(print.ErrorLevel, "get project name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get project name: %v", err) projectLabel = model.ProjectId } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to enable SKE for project %q?", projectLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -70,7 +71,7 @@ func NewCmd(p *print.Printer) *cobra.Command { // Wait for async operation, if async mode not enabled if !model.Async { - s := spinner.New(p) + s := spinner.New(params.Printer) s.Start("Enabling SKE") _, err = wait.EnableServiceWaitHandler(ctx, apiClient, model.Region, model.ProjectId, utils.SKEServiceId).WaitWithContext(ctx) if err != nil { @@ -83,7 +84,7 @@ func NewCmd(p *print.Printer) *cobra.Command { if model.Async { operationState = "Triggered enablement of" } - p.Info("%s SKE for project %q\n", operationState, projectLabel) + params.Printer.Info("%s SKE for project %q\n", operationState, projectLabel) return nil }, } diff --git a/internal/cmd/ske/kubeconfig/create/create.go b/internal/cmd/ske/kubeconfig/create/create.go index 8677b033c..e7a252456 100644 --- a/internal/cmd/ske/kubeconfig/create/create.go +++ b/internal/cmd/ske/kubeconfig/create/create.go @@ -6,6 +6,7 @@ import ( "fmt" "github.com/goccy/go-yaml" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -40,7 +41,7 @@ type inputModel struct { Overwrite bool } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("create %s", clusterNameArg), Short: "Creates or update a kubeconfig for an SKE cluster", @@ -77,13 +78,13 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -95,7 +96,7 @@ func NewCmd(p *print.Printer) *cobra.Command { } else { prompt = fmt.Sprintf("Are you sure you want to update your kubeconfig for SKE cluster %q? This will update your kubeconfig file. \nIf it the kubeconfig file doesn't exists, it will create a new one.", model.ClusterName) } - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -156,10 +157,10 @@ func NewCmd(p *print.Printer) *cobra.Command { if err != nil { return fmt.Errorf("write kubeconfig file: %w", err) } - p.Outputf("\nSet kubectl context to %s with: kubectl config use-context %s\n", model.ClusterName, model.ClusterName) + params.Printer.Outputf("\nSet kubectl context to %s with: kubectl config use-context %s\n", model.ClusterName, model.ClusterName) } - return outputResult(p, model.OutputFormat, model.ClusterName, kubeconfigPath, respKubeconfig, respLogin) + return outputResult(params.Printer, model.OutputFormat, model.ClusterName, kubeconfigPath, respKubeconfig, respLogin) }, } configureFlags(cmd) diff --git a/internal/cmd/ske/kubeconfig/create/create_test.go b/internal/cmd/ske/kubeconfig/create/create_test.go index 9743067f8..558d273e5 100644 --- a/internal/cmd/ske/kubeconfig/create/create_test.go +++ b/internal/cmd/ske/kubeconfig/create/create_test.go @@ -7,6 +7,7 @@ import ( "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" "github.com/google/uuid" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -203,7 +204,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -324,7 +325,7 @@ func Test_outputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.clusterName, tt.args.kubeconfigPath, tt.args.respKubeconfig, tt.args.respLogin); (err != nil) != tt.wantErr { diff --git a/internal/cmd/ske/kubeconfig/kubeconfig.go b/internal/cmd/ske/kubeconfig/kubeconfig.go index 44803f14b..ad5482dbe 100644 --- a/internal/cmd/ske/kubeconfig/kubeconfig.go +++ b/internal/cmd/ske/kubeconfig/kubeconfig.go @@ -1,16 +1,16 @@ package kubeconfig import ( + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/cmd/ske/kubeconfig/create" "github.com/stackitcloud/stackit-cli/internal/cmd/ske/kubeconfig/login" "github.com/stackitcloud/stackit-cli/internal/pkg/args" - "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" "github.com/spf13/cobra" ) -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "kubeconfig", Short: "Provides functionality for SKE kubeconfig", @@ -18,11 +18,11 @@ func NewCmd(p *print.Printer) *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd, p) + addSubcommands(cmd, params) return cmd } -func addSubcommands(cmd *cobra.Command, p *print.Printer) { - cmd.AddCommand(create.NewCmd(p)) - cmd.AddCommand(login.NewCmd(p)) +func addSubcommands(cmd *cobra.Command, params *params.CmdParams) { + cmd.AddCommand(create.NewCmd(params)) + cmd.AddCommand(login.NewCmd(params)) } diff --git a/internal/cmd/ske/kubeconfig/login/login.go b/internal/cmd/ske/kubeconfig/login/login.go index 817b33ca4..ce748d242 100644 --- a/internal/cmd/ske/kubeconfig/login/login.go +++ b/internal/cmd/ske/kubeconfig/login/login.go @@ -12,6 +12,7 @@ import ( "strconv" "time" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/cache" "k8s.io/client-go/rest" @@ -34,7 +35,7 @@ const ( refreshBeforeDuration = 15 * time.Minute // 15 min ) -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "login", Short: "Login plugin for kubernetes clients", @@ -74,7 +75,7 @@ func NewCmd(p *print.Printer) *cobra.Command { } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -82,33 +83,33 @@ func NewCmd(p *print.Printer) *cobra.Command { cachedKubeconfig := getCachedKubeConfig(clusterConfig.cacheKey) if cachedKubeconfig == nil { - return GetAndOutputKubeconfig(ctx, p, apiClient, clusterConfig, false, nil) + return GetAndOutputKubeconfig(ctx, params.Printer, apiClient, clusterConfig, false, nil) } certPem, _ := pem.Decode(cachedKubeconfig.CertData) if certPem == nil { _ = cache.DeleteObject(clusterConfig.cacheKey) - return GetAndOutputKubeconfig(ctx, p, apiClient, clusterConfig, false, nil) + return GetAndOutputKubeconfig(ctx, params.Printer, apiClient, clusterConfig, false, nil) } certificate, err := x509.ParseCertificate(certPem.Bytes) if err != nil { _ = cache.DeleteObject(clusterConfig.cacheKey) - return GetAndOutputKubeconfig(ctx, p, apiClient, clusterConfig, false, nil) + return GetAndOutputKubeconfig(ctx, params.Printer, apiClient, clusterConfig, false, nil) } // cert is expired, request new if time.Now().After(certificate.NotAfter.UTC()) { _ = cache.DeleteObject(clusterConfig.cacheKey) - return GetAndOutputKubeconfig(ctx, p, apiClient, clusterConfig, false, nil) + return GetAndOutputKubeconfig(ctx, params.Printer, apiClient, clusterConfig, false, nil) } // cert expires within the next 15min, refresh (try to get a new, use cache on failure) if time.Now().Add(refreshBeforeDuration).After(certificate.NotAfter.UTC()) { - return GetAndOutputKubeconfig(ctx, p, apiClient, clusterConfig, true, cachedKubeconfig) + return GetAndOutputKubeconfig(ctx, params.Printer, apiClient, clusterConfig, true, cachedKubeconfig) } // cert not expired, nor will it expire in the next 15min; therefore, use the cached kubeconfig - if err := output(p, clusterConfig.cacheKey, cachedKubeconfig); err != nil { + if err := output(params.Printer, clusterConfig.cacheKey, cachedKubeconfig); err != nil { return err } return nil diff --git a/internal/cmd/ske/options/options.go b/internal/cmd/ske/options/options.go index 419721d29..6aed48591 100644 --- a/internal/cmd/ske/options/options.go +++ b/internal/cmd/ske/options/options.go @@ -9,6 +9,7 @@ import ( "github.com/goccy/go-yaml" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" "github.com/stackitcloud/stackit-cli/internal/pkg/flags" @@ -37,7 +38,7 @@ type inputModel struct { VolumeTypes bool } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "options", Short: "Lists SKE provider options", @@ -59,13 +60,13 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -77,7 +78,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("get SKE provider options: %w", err) } - return outputResult(p, model, resp) + return outputResult(params.Printer, model, resp) }, } configureFlags(cmd) diff --git a/internal/cmd/ske/options/options_test.go b/internal/cmd/ske/options/options_test.go index 6b1c1ae93..b2181ef5b 100644 --- a/internal/cmd/ske/options/options_test.go +++ b/internal/cmd/ske/options/options_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" @@ -118,7 +119,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -238,7 +239,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.model, tt.args.options); (err != nil) != tt.wantErr { @@ -271,7 +272,7 @@ func TestOutputResultAsTable(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResultAsTable(p, tt.args.options); (err != nil) != tt.wantErr { diff --git a/internal/cmd/ske/ske.go b/internal/cmd/ske/ske.go index 04438af65..137165e06 100644 --- a/internal/cmd/ske/ske.go +++ b/internal/cmd/ske/ske.go @@ -1,6 +1,7 @@ package ske import ( + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/cmd/ske/cluster" "github.com/stackitcloud/stackit-cli/internal/cmd/ske/credentials" "github.com/stackitcloud/stackit-cli/internal/cmd/ske/describe" @@ -9,13 +10,12 @@ import ( "github.com/stackitcloud/stackit-cli/internal/cmd/ske/kubeconfig" "github.com/stackitcloud/stackit-cli/internal/cmd/ske/options" "github.com/stackitcloud/stackit-cli/internal/pkg/args" - "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" "github.com/spf13/cobra" ) -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "ske", Short: "Provides functionality for SKE", @@ -23,16 +23,16 @@ func NewCmd(p *print.Printer) *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd, p) + addSubcommands(cmd, params) return cmd } -func addSubcommands(cmd *cobra.Command, p *print.Printer) { - cmd.AddCommand(describe.NewCmd(p)) - cmd.AddCommand(enable.NewCmd(p)) - cmd.AddCommand(kubeconfig.NewCmd(p)) - cmd.AddCommand(disable.NewCmd(p)) - cmd.AddCommand(cluster.NewCmd(p)) - cmd.AddCommand(credentials.NewCmd(p)) - cmd.AddCommand(options.NewCmd(p)) +func addSubcommands(cmd *cobra.Command, params *params.CmdParams) { + cmd.AddCommand(describe.NewCmd(params)) + cmd.AddCommand(enable.NewCmd(params)) + cmd.AddCommand(kubeconfig.NewCmd(params)) + cmd.AddCommand(disable.NewCmd(params)) + cmd.AddCommand(cluster.NewCmd(params)) + cmd.AddCommand(credentials.NewCmd(params)) + cmd.AddCommand(options.NewCmd(params)) } diff --git a/internal/cmd/volume/create/create.go b/internal/cmd/volume/create/create.go index 90b20d813..06c7da8e8 100644 --- a/internal/cmd/volume/create/create.go +++ b/internal/cmd/volume/create/create.go @@ -6,6 +6,7 @@ import ( "fmt" "github.com/goccy/go-yaml" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" cliErr "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -45,7 +46,7 @@ type inputModel struct { SourceType *string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "create", Short: "Creates a volume", @@ -71,26 +72,26 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } - projectLabel, err := projectname.GetProjectName(ctx, p, cmd) + projectLabel, err := projectname.GetProjectName(ctx, params.Printer, cmd) if err != nil { - p.Debug(print.ErrorLevel, "get project name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get project name: %v", err) projectLabel = model.ProjectId } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to create a volume for project %q?", projectLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -106,7 +107,7 @@ func NewCmd(p *print.Printer) *cobra.Command { // Wait for async operation, if async mode not enabled if !model.Async { - s := spinner.New(p) + s := spinner.New(params.Printer) s.Start("Creating volume") _, err = wait.CreateVolumeWaitHandler(ctx, apiClient, model.ProjectId, volumeId).WaitWithContext(ctx) if err != nil { @@ -115,7 +116,7 @@ func NewCmd(p *print.Printer) *cobra.Command { s.Stop() } - return outputResult(p, model, projectLabel, resp) + return outputResult(params.Printer, model, projectLabel, resp) }, } configureFlags(cmd) diff --git a/internal/cmd/volume/create/create_test.go b/internal/cmd/volume/create/create_test.go index 71f3e1697..716daaad2 100644 --- a/internal/cmd/volume/create/create_test.go +++ b/internal/cmd/volume/create/create_test.go @@ -7,6 +7,7 @@ import ( "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" "github.com/google/uuid" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -202,7 +203,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -310,7 +311,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.model, tt.args.projectLabel, tt.args.volume); (err != nil) != tt.wantErr { diff --git a/internal/cmd/volume/delete/delete.go b/internal/cmd/volume/delete/delete.go index d07a9e8f5..e9730faf9 100644 --- a/internal/cmd/volume/delete/delete.go +++ b/internal/cmd/volume/delete/delete.go @@ -4,6 +4,7 @@ import ( "context" "fmt" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -28,7 +29,7 @@ type inputModel struct { VolumeId string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("delete %s", volumeIdArg), Short: "Deletes a volume", @@ -45,13 +46,13 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -59,14 +60,14 @@ func NewCmd(p *print.Printer) *cobra.Command { volumeLabel := model.VolumeId volumeName, err := iaasUtils.GetVolumeName(ctx, apiClient, model.ProjectId, model.VolumeId) if err != nil { - p.Debug(print.ErrorLevel, "get volume name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get volume name: %v", err) } else if volumeName != "" { volumeLabel = volumeName } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to delete volume %q?", volumeLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -81,7 +82,7 @@ func NewCmd(p *print.Printer) *cobra.Command { // Wait for async operation, if async mode not enabled if !model.Async { - s := spinner.New(p) + s := spinner.New(params.Printer) s.Start("Deleting volume") _, err = wait.DeleteVolumeWaitHandler(ctx, apiClient, model.ProjectId, model.VolumeId).WaitWithContext(ctx) if err != nil { @@ -94,7 +95,7 @@ func NewCmd(p *print.Printer) *cobra.Command { if model.Async { operationState = "Triggered deletion of" } - p.Info("%s volume %q\n", operationState, volumeLabel) + params.Printer.Info("%s volume %q\n", operationState, volumeLabel) return nil }, } diff --git a/internal/cmd/volume/delete/delete_test.go b/internal/cmd/volume/delete/delete_test.go index 5648c374c..58d10c79c 100644 --- a/internal/cmd/volume/delete/delete_test.go +++ b/internal/cmd/volume/delete/delete_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" @@ -138,7 +139,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/volume/describe/describe.go b/internal/cmd/volume/describe/describe.go index e77073ea2..c8b787456 100644 --- a/internal/cmd/volume/describe/describe.go +++ b/internal/cmd/volume/describe/describe.go @@ -8,6 +8,7 @@ import ( "github.com/goccy/go-yaml" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -30,7 +31,7 @@ type inputModel struct { VolumeId string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("describe %s", volumeIdArg), Short: "Shows details of a volume", @@ -48,13 +49,13 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -66,7 +67,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("read volume: %w", err) } - return outputResult(p, model.OutputFormat, resp) + return outputResult(params.Printer, model.OutputFormat, resp) }, } return cmd diff --git a/internal/cmd/volume/describe/describe_test.go b/internal/cmd/volume/describe/describe_test.go index 3de0f688b..aa0a0754f 100644 --- a/internal/cmd/volume/describe/describe_test.go +++ b/internal/cmd/volume/describe/describe_test.go @@ -7,6 +7,7 @@ import ( "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" "github.com/google/uuid" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-sdk-go/services/iaas" @@ -137,7 +138,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -240,7 +241,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.volume); (err != nil) != tt.wantErr { diff --git a/internal/cmd/volume/list/list.go b/internal/cmd/volume/list/list.go index 02c643014..feffde80e 100644 --- a/internal/cmd/volume/list/list.go +++ b/internal/cmd/volume/list/list.go @@ -7,6 +7,7 @@ import ( "github.com/goccy/go-yaml" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -31,7 +32,7 @@ type inputModel struct { LabelSelector *string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "list", Short: "Lists all volumes of a project", @@ -57,13 +58,13 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -76,12 +77,12 @@ func NewCmd(p *print.Printer) *cobra.Command { } if resp.Items == nil || len(*resp.Items) == 0 { - projectLabel, err := projectname.GetProjectName(ctx, p, cmd) + projectLabel, err := projectname.GetProjectName(ctx, params.Printer, cmd) if err != nil { - p.Debug(print.ErrorLevel, "get project name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get project name: %v", err) projectLabel = model.ProjectId } - p.Info("No volumes found for project %q\n", projectLabel) + params.Printer.Info("No volumes found for project %q\n", projectLabel) return nil } @@ -91,7 +92,7 @@ func NewCmd(p *print.Printer) *cobra.Command { items = items[:*model.Limit] } - return outputResult(p, model.OutputFormat, items) + return outputResult(params.Printer, model.OutputFormat, items) }, } configureFlags(cmd) diff --git a/internal/cmd/volume/list/list_test.go b/internal/cmd/volume/list/list_test.go index 77cc3808b..daa736ff1 100644 --- a/internal/cmd/volume/list/list_test.go +++ b/internal/cmd/volume/list/list_test.go @@ -7,6 +7,7 @@ import ( "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" "github.com/google/uuid" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -131,7 +132,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -226,7 +227,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.volumes); (err != nil) != tt.wantErr { diff --git a/internal/cmd/volume/performance-class/describe/describe.go b/internal/cmd/volume/performance-class/describe/describe.go index ade26f34a..3105e4fcc 100644 --- a/internal/cmd/volume/performance-class/describe/describe.go +++ b/internal/cmd/volume/performance-class/describe/describe.go @@ -8,6 +8,7 @@ import ( "github.com/goccy/go-yaml" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -30,7 +31,7 @@ type inputModel struct { VolumePerformanceClass string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("describe %s", volumePerformanceClassArg), Short: "Shows details of a volume performance class", @@ -48,13 +49,13 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -66,7 +67,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("read volume performance class: %w", err) } - return outputResult(p, model.OutputFormat, resp) + return outputResult(params.Printer, model.OutputFormat, resp) }, } return cmd diff --git a/internal/cmd/volume/performance-class/describe/describe_test.go b/internal/cmd/volume/performance-class/describe/describe_test.go index 90ba0da3c..b5bf0cf3c 100644 --- a/internal/cmd/volume/performance-class/describe/describe_test.go +++ b/internal/cmd/volume/performance-class/describe/describe_test.go @@ -7,6 +7,7 @@ import ( "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" "github.com/google/uuid" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-sdk-go/services/iaas" @@ -131,7 +132,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -234,7 +235,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.performanceClass); (err != nil) != tt.wantErr { diff --git a/internal/cmd/volume/performance-class/list/list.go b/internal/cmd/volume/performance-class/list/list.go index 4449a5f3e..b48f399f6 100644 --- a/internal/cmd/volume/performance-class/list/list.go +++ b/internal/cmd/volume/performance-class/list/list.go @@ -6,6 +6,7 @@ import ( "fmt" "github.com/goccy/go-yaml" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -32,7 +33,7 @@ type inputModel struct { LabelSelector *string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "list", Short: "Lists all volume performance classes for a project", @@ -58,13 +59,13 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - model, err := parseInput(p, cmd) + model, err := parseInput(params.Printer, cmd) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } @@ -77,12 +78,12 @@ func NewCmd(p *print.Printer) *cobra.Command { } if resp.Items == nil || len(*resp.Items) == 0 { - projectLabel, err := projectname.GetProjectName(ctx, p, cmd) + projectLabel, err := projectname.GetProjectName(ctx, params.Printer, cmd) if err != nil { - p.Debug(print.ErrorLevel, "get project name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get project name: %v", err) projectLabel = model.ProjectId } - p.Info("No volume performance class found for project %q\n", projectLabel) + params.Printer.Info("No volume performance class found for project %q\n", projectLabel) return nil } @@ -92,7 +93,7 @@ func NewCmd(p *print.Printer) *cobra.Command { items = items[:*model.Limit] } - return outputResult(p, model.OutputFormat, items) + return outputResult(params.Printer, model.OutputFormat, items) }, } configureFlags(cmd) diff --git a/internal/cmd/volume/performance-class/list/list_test.go b/internal/cmd/volume/performance-class/list/list_test.go index 9e23bbdfd..6e877390d 100644 --- a/internal/cmd/volume/performance-class/list/list_test.go +++ b/internal/cmd/volume/performance-class/list/list_test.go @@ -7,6 +7,7 @@ import ( "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" "github.com/google/uuid" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -131,7 +132,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -226,7 +227,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.performanceClasses); (err != nil) != tt.wantErr { diff --git a/internal/cmd/volume/performance-class/performance_class.go b/internal/cmd/volume/performance-class/performance_class.go index 4f159606e..6ed86618a 100644 --- a/internal/cmd/volume/performance-class/performance_class.go +++ b/internal/cmd/volume/performance-class/performance_class.go @@ -1,16 +1,16 @@ package performanceclass import ( + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/cmd/volume/performance-class/describe" "github.com/stackitcloud/stackit-cli/internal/cmd/volume/performance-class/list" "github.com/stackitcloud/stackit-cli/internal/pkg/args" - "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" "github.com/spf13/cobra" ) -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "performance-class", Short: "Provides functionality for volume performance classes available inside a project", @@ -18,11 +18,11 @@ func NewCmd(p *print.Printer) *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd, p) + addSubcommands(cmd, params) return cmd } -func addSubcommands(cmd *cobra.Command, p *print.Printer) { - cmd.AddCommand(describe.NewCmd(p)) - cmd.AddCommand(list.NewCmd(p)) +func addSubcommands(cmd *cobra.Command, params *params.CmdParams) { + cmd.AddCommand(describe.NewCmd(params)) + cmd.AddCommand(list.NewCmd(params)) } diff --git a/internal/cmd/volume/resize/resize.go b/internal/cmd/volume/resize/resize.go index 7f966a2e3..afef2a7a3 100644 --- a/internal/cmd/volume/resize/resize.go +++ b/internal/cmd/volume/resize/resize.go @@ -4,6 +4,7 @@ import ( "context" "fmt" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" cliErr "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -30,7 +31,7 @@ type inputModel struct { Size *int64 } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("resize %s", volumeIdArg), Short: "Resizes a volume", @@ -44,26 +45,26 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } volumeLabel, err := iaasUtils.GetVolumeName(ctx, apiClient, model.ProjectId, model.VolumeId) if err != nil { - p.Debug(print.ErrorLevel, "get volume name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get volume name: %v", err) volumeLabel = model.VolumeId } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to resize volume %q?", volumeLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -76,7 +77,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("resize volume: %w", err) } - p.Outputf("Resized volume %q.\n", volumeLabel) + params.Printer.Outputf("Resized volume %q.\n", volumeLabel) return nil }, } diff --git a/internal/cmd/volume/resize/resize_test.go b/internal/cmd/volume/resize/resize_test.go index 15dd9c757..54d02a47f 100644 --- a/internal/cmd/volume/resize/resize_test.go +++ b/internal/cmd/volume/resize/resize_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -152,7 +153,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/volume/update/update.go b/internal/cmd/volume/update/update.go index f7afad38a..0c57f6185 100644 --- a/internal/cmd/volume/update/update.go +++ b/internal/cmd/volume/update/update.go @@ -8,6 +8,7 @@ import ( "github.com/goccy/go-yaml" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" cliErr "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -36,7 +37,7 @@ type inputModel struct { Labels *map[string]string } -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("update %s", volumeIdArg), Short: "Updates a volume", @@ -58,26 +59,26 @@ func NewCmd(p *print.Printer) *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(p, cmd, args) + model, err := parseInput(params.Printer, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(p) + apiClient, err := client.ConfigureClient(params.Printer) if err != nil { return err } volumeLabel, err := iaasUtils.GetVolumeName(ctx, apiClient, model.ProjectId, model.VolumeId) if err != nil { - p.Debug(print.ErrorLevel, "get volume name: %v", err) + params.Printer.Debug(print.ErrorLevel, "get volume name: %v", err) volumeLabel = model.VolumeId } if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to update volume %q?", volumeLabel) - err = p.PromptForConfirmation(prompt) + err = params.Printer.PromptForConfirmation(prompt) if err != nil { return err } @@ -90,7 +91,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("update volume: %w", err) } - return outputResult(p, model.OutputFormat, volumeLabel, resp) + return outputResult(params.Printer, model.OutputFormat, volumeLabel, resp) }, } configureFlags(cmd) diff --git a/internal/cmd/volume/update/update_test.go b/internal/cmd/volume/update/update_test.go index cdfed4017..1628bf26a 100644 --- a/internal/cmd/volume/update/update_test.go +++ b/internal/cmd/volume/update/update_test.go @@ -7,6 +7,7 @@ import ( "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" "github.com/google/uuid" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -176,7 +177,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { p := print.NewPrinter() - cmd := NewCmd(p) + cmd := NewCmd(¶ms.CmdParams{Printer: p}) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -280,7 +281,7 @@ func TestOutputResult(t *testing.T) { }, } p := print.NewPrinter() - p.Cmd = NewCmd(p) + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := outputResult(p, tt.args.outputFormat, tt.args.volumeLabel, tt.args.volume); (err != nil) != tt.wantErr { diff --git a/internal/cmd/volume/volume.go b/internal/cmd/volume/volume.go index fe334cb6f..1e876e85b 100644 --- a/internal/cmd/volume/volume.go +++ b/internal/cmd/volume/volume.go @@ -1,6 +1,7 @@ package volume import ( + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/cmd/volume/create" "github.com/stackitcloud/stackit-cli/internal/cmd/volume/delete" "github.com/stackitcloud/stackit-cli/internal/cmd/volume/describe" @@ -9,13 +10,12 @@ import ( "github.com/stackitcloud/stackit-cli/internal/cmd/volume/resize" "github.com/stackitcloud/stackit-cli/internal/cmd/volume/update" "github.com/stackitcloud/stackit-cli/internal/pkg/args" - "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" "github.com/spf13/cobra" ) -func NewCmd(p *print.Printer) *cobra.Command { +func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: "volume", Short: "Provides functionality for volumes", @@ -23,16 +23,16 @@ func NewCmd(p *print.Printer) *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd, p) + addSubcommands(cmd, params) return cmd } -func addSubcommands(cmd *cobra.Command, p *print.Printer) { - cmd.AddCommand(create.NewCmd(p)) - cmd.AddCommand(delete.NewCmd(p)) - cmd.AddCommand(describe.NewCmd(p)) - cmd.AddCommand(list.NewCmd(p)) - cmd.AddCommand(update.NewCmd(p)) - cmd.AddCommand(resize.NewCmd(p)) - cmd.AddCommand(performanceclass.NewCmd(p)) +func addSubcommands(cmd *cobra.Command, params *params.CmdParams) { + cmd.AddCommand(create.NewCmd(params)) + cmd.AddCommand(delete.NewCmd(params)) + cmd.AddCommand(describe.NewCmd(params)) + cmd.AddCommand(list.NewCmd(params)) + cmd.AddCommand(update.NewCmd(params)) + cmd.AddCommand(resize.NewCmd(params)) + cmd.AddCommand(performanceclass.NewCmd(params)) }