diff --git a/internal/cmd/argus/argus.go b/internal/cmd/argus/argus.go index 7609c9cfb..38dc77321 100644 --- a/internal/cmd/argus/argus.go +++ b/internal/cmd/argus/argus.go @@ -4,12 +4,13 @@ import ( "github.com/stackitcloud/stackit-cli/internal/cmd/argus/instance" "github.com/stackitcloud/stackit-cli/internal/cmd/argus/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() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: "argus", Short: "Provides functionality for Argus", @@ -17,11 +18,11 @@ func NewCmd() *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd) + addSubcommands(cmd, p) return cmd } -func addSubcommands(cmd *cobra.Command) { - cmd.AddCommand(plans.NewCmd()) - cmd.AddCommand(instance.NewCmd()) +func addSubcommands(cmd *cobra.Command, p *print.Printer) { + cmd.AddCommand(plans.NewCmd(p)) + cmd.AddCommand(instance.NewCmd(p)) } diff --git a/internal/cmd/argus/instance/create/create.go b/internal/cmd/argus/instance/create/create.go index 352e67022..962a206e9 100644 --- a/internal/cmd/argus/instance/create/create.go +++ b/internal/cmd/argus/instance/create/create.go @@ -11,6 +11,7 @@ import ( "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/print" "github.com/stackitcloud/stackit-cli/internal/pkg/projectname" "github.com/stackitcloud/stackit-cli/internal/pkg/services/argus/client" argusUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/argus/utils" @@ -35,7 +36,7 @@ type inputModel struct { PlanId *string } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: "create", Short: "Creates an Argus instance", @@ -105,7 +106,7 @@ func NewCmd() *cobra.Command { if model.Async { operationState = "Triggered creation of" } - cmd.Printf("%s instance for project %q. Instance ID: %s\n", operationState, projectLabel, instanceId) + p.Outputf("%s instance for project %q. Instance ID: %s\n", operationState, projectLabel, instanceId) return nil }, } diff --git a/internal/cmd/argus/instance/create/create_test.go b/internal/cmd/argus/instance/create/create_test.go index 9edf81191..9dd6cd436 100644 --- a/internal/cmd/argus/instance/create/create_test.go +++ b/internal/cmd/argus/instance/create/create_test.go @@ -173,7 +173,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { - cmd := NewCmd() + cmd := NewCmd(nil) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/argus/instance/delete/delete.go b/internal/cmd/argus/instance/delete/delete.go index 345fc7758..a1022540e 100644 --- a/internal/cmd/argus/instance/delete/delete.go +++ b/internal/cmd/argus/instance/delete/delete.go @@ -9,6 +9,7 @@ import ( "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" + "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/services/argus/client" argusUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/argus/utils" "github.com/stackitcloud/stackit-cli/internal/pkg/spinner" @@ -28,7 +29,7 @@ type inputModel struct { InstanceId string } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("delete %s", instanceIdArg), Short: "Deletes an Argus instance", @@ -87,7 +88,7 @@ func NewCmd() *cobra.Command { if model.Async { operationState = "Triggered deletion of" } - cmd.Printf("%s instance %q\n", operationState, instanceLabel) + p.Info("%s instance %q\n", operationState, instanceLabel) return nil }, } diff --git a/internal/cmd/argus/instance/delete/delete_test.go b/internal/cmd/argus/instance/delete/delete_test.go index b9e905e9f..502431d5e 100644 --- a/internal/cmd/argus/instance/delete/delete_test.go +++ b/internal/cmd/argus/instance/delete/delete_test.go @@ -136,7 +136,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { - cmd := NewCmd() + cmd := NewCmd(nil) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/argus/instance/describe/describe.go b/internal/cmd/argus/instance/describe/describe.go index a87826061..09d154e68 100644 --- a/internal/cmd/argus/instance/describe/describe.go +++ b/internal/cmd/argus/instance/describe/describe.go @@ -9,6 +9,7 @@ import ( "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" + "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/services/argus/client" "github.com/stackitcloud/stackit-cli/internal/pkg/tables" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -26,7 +27,7 @@ type inputModel struct { InstanceId string } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("describe %s", instanceIdArg), Short: "Shows details of an Argus instance", @@ -59,7 +60,7 @@ func NewCmd() *cobra.Command { return fmt.Errorf("read Argus instance: %w", err) } - return outputResult(cmd, model.OutputFormat, resp) + return outputResult(cmd, model.OutputFormat, resp, p) }, } return cmd @@ -84,7 +85,7 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *argus.APICl return req } -func outputResult(cmd *cobra.Command, outputFormat string, instance *argus.GetInstanceResponse) error { +func outputResult(cmd *cobra.Command, outputFormat string, instance *argus.GetInstanceResponse, p *print.Printer) error { switch outputFormat { case globalflags.PrettyOutputFormat: @@ -120,7 +121,7 @@ func outputResult(cmd *cobra.Command, outputFormat string, instance *argus.GetIn if err != nil { return fmt.Errorf("marshal Argus instance: %w", err) } - cmd.Println(string(details)) + p.Outputln(string(details)) return nil } diff --git a/internal/cmd/argus/instance/describe/describe_test.go b/internal/cmd/argus/instance/describe/describe_test.go index 4a6f22a4b..ac060428a 100644 --- a/internal/cmd/argus/instance/describe/describe_test.go +++ b/internal/cmd/argus/instance/describe/describe_test.go @@ -136,7 +136,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { - cmd := NewCmd() + cmd := NewCmd(nil) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/argus/instance/instance.go b/internal/cmd/argus/instance/instance.go index 629c43ad9..af10b9a7c 100644 --- a/internal/cmd/argus/instance/instance.go +++ b/internal/cmd/argus/instance/instance.go @@ -7,12 +7,13 @@ import ( "github.com/stackitcloud/stackit-cli/internal/cmd/argus/instance/list" "github.com/stackitcloud/stackit-cli/internal/cmd/argus/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() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: "instance", Short: "Provides functionality for Argus instances", @@ -20,14 +21,14 @@ func NewCmd() *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd) + addSubcommands(cmd, p) return cmd } -func addSubcommands(cmd *cobra.Command) { - cmd.AddCommand(create.NewCmd()) - cmd.AddCommand(update.NewCmd()) - cmd.AddCommand(delete.NewCmd()) - cmd.AddCommand(list.NewCmd()) - cmd.AddCommand(describe.NewCmd()) +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)) } diff --git a/internal/cmd/argus/instance/list/list.go b/internal/cmd/argus/instance/list/list.go index 702172ed2..269d46a6a 100644 --- a/internal/cmd/argus/instance/list/list.go +++ b/internal/cmd/argus/instance/list/list.go @@ -10,6 +10,7 @@ import ( "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/print" "github.com/stackitcloud/stackit-cli/internal/pkg/projectname" "github.com/stackitcloud/stackit-cli/internal/pkg/services/argus/client" "github.com/stackitcloud/stackit-cli/internal/pkg/tables" @@ -27,7 +28,7 @@ type inputModel struct { Limit *int64 } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: "list", Short: "Lists all Argus instances", @@ -69,7 +70,7 @@ func NewCmd() *cobra.Command { if err != nil { projectLabel = model.ProjectId } - cmd.Printf("No instances found for project %q\n", projectLabel) + p.Info("No instances found for project %q\n", projectLabel) return nil } @@ -78,7 +79,7 @@ func NewCmd() *cobra.Command { instances = instances[:*model.Limit] } - return outputResult(cmd, model.OutputFormat, instances) + return outputResult(cmd, model.OutputFormat, instances, p) }, } @@ -115,14 +116,14 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *argus.APICl return req } -func outputResult(cmd *cobra.Command, outputFormat string, instances []argus.ProjectInstanceFull) error { +func outputResult(cmd *cobra.Command, outputFormat string, instances []argus.ProjectInstanceFull, p *print.Printer) error { switch outputFormat { case globalflags.JSONOutputFormat: details, err := json.MarshalIndent(instances, "", " ") if err != nil { return fmt.Errorf("marshal Argus instance list: %w", err) } - cmd.Println(string(details)) + p.Outputln(string(details)) return nil default: diff --git a/internal/cmd/argus/instance/update/update.go b/internal/cmd/argus/instance/update/update.go index a1d807dad..f1a9fa7bc 100644 --- a/internal/cmd/argus/instance/update/update.go +++ b/internal/cmd/argus/instance/update/update.go @@ -11,6 +11,7 @@ import ( "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/print" "github.com/stackitcloud/stackit-cli/internal/pkg/services/argus/client" argusUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/argus/utils" "github.com/stackitcloud/stackit-cli/internal/pkg/spinner" @@ -38,7 +39,7 @@ type inputModel struct { PlanId *string } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("update %s", instanceIdArg), Short: "Updates an Argus instance", @@ -112,7 +113,7 @@ func NewCmd() *cobra.Command { if model.Async { operationState = "Triggered update of" } - cmd.Printf("%s instance %q\n", operationState, instanceLabel) + p.Info("%s instance %q\n", operationState, instanceLabel) return nil }, } diff --git a/internal/cmd/argus/instance/update/update_test.go b/internal/cmd/argus/instance/update/update_test.go index 9c0864840..330e6943f 100644 --- a/internal/cmd/argus/instance/update/update_test.go +++ b/internal/cmd/argus/instance/update/update_test.go @@ -238,7 +238,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { - cmd := NewCmd() + cmd := NewCmd(nil) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/argus/plans/plans.go b/internal/cmd/argus/plans/plans.go index a74e5c534..6c0da62ae 100644 --- a/internal/cmd/argus/plans/plans.go +++ b/internal/cmd/argus/plans/plans.go @@ -10,6 +10,7 @@ import ( "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/print" "github.com/stackitcloud/stackit-cli/internal/pkg/projectname" "github.com/stackitcloud/stackit-cli/internal/pkg/services/argus/client" "github.com/stackitcloud/stackit-cli/internal/pkg/tables" @@ -27,7 +28,7 @@ type inputModel struct { Limit *int64 } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: "plans", Short: "Lists all Argus service plans", @@ -69,7 +70,7 @@ func NewCmd() *cobra.Command { if err != nil { projectLabel = model.ProjectId } - cmd.Printf("No plans found for project %q\n", projectLabel) + p.Info("No plans found for project %q\n", projectLabel) return nil } @@ -78,7 +79,7 @@ func NewCmd() *cobra.Command { plans = plans[:*model.Limit] } - return outputResult(cmd, model.OutputFormat, plans) + return outputResult(cmd, model.OutputFormat, plans, p) }, } @@ -115,14 +116,14 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *argus.APICl return req } -func outputResult(cmd *cobra.Command, outputFormat string, plans []argus.Plan) error { +func outputResult(cmd *cobra.Command, outputFormat string, plans []argus.Plan, p *print.Printer) error { switch outputFormat { case globalflags.JSONOutputFormat: details, err := json.MarshalIndent(plans, "", " ") if err != nil { return fmt.Errorf("marshal Argus plans: %w", err) } - cmd.Println(string(details)) + p.Outputln(string(details)) return nil default: diff --git a/internal/cmd/auth/auth.go b/internal/cmd/auth/auth.go index a539f7432..cad07aaed 100644 --- a/internal/cmd/auth/auth.go +++ b/internal/cmd/auth/auth.go @@ -23,6 +23,6 @@ func NewCmd(p *print.Printer) *cobra.Command { } func addSubcommands(cmd *cobra.Command, p *print.Printer) { - cmd.AddCommand(login.NewCmd()) + cmd.AddCommand(login.NewCmd(p)) cmd.AddCommand(activateserviceaccount.NewCmd(p)) } diff --git a/internal/cmd/auth/login/login.go b/internal/cmd/auth/login/login.go index c1474dc29..55f3663b0 100644 --- a/internal/cmd/auth/login/login.go +++ b/internal/cmd/auth/login/login.go @@ -6,11 +6,12 @@ import ( "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() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: "login", Short: "Logs in to the STACKIT CLI", @@ -27,7 +28,7 @@ func NewCmd() *cobra.Command { return fmt.Errorf("authorization failed: %w", err) } - cmd.Println("Successfully logged into STACKIT CLI.") + p.Info("Successfully logged into STACKIT CLI.\n") return nil }, } diff --git a/internal/cmd/curl/curl.go b/internal/cmd/curl/curl.go index 573bcc67b..c13dcd7c3 100644 --- a/internal/cmd/curl/curl.go +++ b/internal/cmd/curl/curl.go @@ -16,6 +16,7 @@ import ( "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/print" "github.com/spf13/cobra" ) @@ -43,7 +44,7 @@ type inputModel struct { OutputFile *string } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("curl %s", urlArg), Short: "Executes an authenticated HTTP request to an endpoint", @@ -97,7 +98,7 @@ func NewCmd() *cobra.Command { } }() - err = outputResponse(cmd, model, resp) + err = outputResponse(model, resp, p) if err != nil { return err } @@ -199,7 +200,7 @@ func buildRequest(model *inputModel, bearerToken string) (*http.Request, error) return req, nil } -func outputResponse(cmd *cobra.Command, model *inputModel, resp *http.Response) error { +func outputResponse(model *inputModel, resp *http.Response, p *print.Printer) error { output := make([]byte, 0) if model.IncludeResponseHeaders { respHeader, err := httputil.DumpResponse(resp, false) @@ -215,7 +216,7 @@ func outputResponse(cmd *cobra.Command, model *inputModel, resp *http.Response) output = append(output, respBody...) if model.OutputFile == nil { - cmd.Println(string(output)) + p.Outputln(string(output)) } else { err = os.WriteFile(*model.OutputFile, output, 0o600) if err != nil { diff --git a/internal/cmd/curl/curl_test.go b/internal/cmd/curl/curl_test.go index bb60d27aa..da57f3d04 100644 --- a/internal/cmd/curl/curl_test.go +++ b/internal/cmd/curl/curl_test.go @@ -205,7 +205,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { - cmd := NewCmd() + cmd := NewCmd(nil) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/dns/dns.go b/internal/cmd/dns/dns.go index d6154b4be..a89e959c5 100644 --- a/internal/cmd/dns/dns.go +++ b/internal/cmd/dns/dns.go @@ -24,5 +24,5 @@ func NewCmd(p *print.Printer) *cobra.Command { func addSubcommands(cmd *cobra.Command, p *print.Printer) { cmd.AddCommand(zone.NewCmd(p)) - cmd.AddCommand(recordset.NewCmd()) + cmd.AddCommand(recordset.NewCmd(p)) } diff --git a/internal/cmd/dns/record-set/create/create.go b/internal/cmd/dns/record-set/create/create.go index a8dbe3ff0..6a7389791 100644 --- a/internal/cmd/dns/record-set/create/create.go +++ b/internal/cmd/dns/record-set/create/create.go @@ -10,6 +10,7 @@ import ( "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/print" "github.com/stackitcloud/stackit-cli/internal/pkg/services/dns/client" dnsUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/dns/utils" "github.com/stackitcloud/stackit-cli/internal/pkg/spinner" @@ -41,7 +42,7 @@ type inputModel struct { Type string } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: "create", Short: "Creates a DNS record set", @@ -101,7 +102,7 @@ func NewCmd() *cobra.Command { if model.Async { operationState = "Triggered creation of" } - cmd.Printf("%s record set for zone %s. Record set ID: %s\n", operationState, zoneLabel, recordSetId) + p.Outputf("%s record set for zone %s. Record set ID: %s\n", operationState, zoneLabel, recordSetId) return nil }, } diff --git a/internal/cmd/dns/record-set/create/create_test.go b/internal/cmd/dns/record-set/create/create_test.go index 341e05fa3..c8d095748 100644 --- a/internal/cmd/dns/record-set/create/create_test.go +++ b/internal/cmd/dns/record-set/create/create_test.go @@ -239,7 +239,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { - cmd := NewCmd() + cmd := NewCmd(nil) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/dns/record-set/delete/delete.go b/internal/cmd/dns/record-set/delete/delete.go index 003f00a0c..29217a3a6 100644 --- a/internal/cmd/dns/record-set/delete/delete.go +++ b/internal/cmd/dns/record-set/delete/delete.go @@ -10,6 +10,7 @@ import ( "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/print" "github.com/stackitcloud/stackit-cli/internal/pkg/services/dns/client" dnsUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/dns/utils" "github.com/stackitcloud/stackit-cli/internal/pkg/spinner" @@ -32,7 +33,7 @@ type inputModel struct { RecordSetId string } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("delete %s", recordSetIdArg), Short: "Deletes a DNS record set", @@ -99,7 +100,7 @@ func NewCmd() *cobra.Command { if model.Async { operationState = "Triggered deletion of" } - cmd.Printf("%s record set %s of zone %s\n", operationState, recordSetLabel, zoneLabel) + p.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 b5964065d..a60e5b63b 100644 --- a/internal/cmd/dns/record-set/delete/delete_test.go +++ b/internal/cmd/dns/record-set/delete/delete_test.go @@ -163,7 +163,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { - cmd := NewCmd() + cmd := NewCmd(nil) 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 46c8546bc..073d23e96 100644 --- a/internal/cmd/dns/record-set/describe/describe.go +++ b/internal/cmd/dns/record-set/describe/describe.go @@ -11,6 +11,7 @@ import ( "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/print" "github.com/stackitcloud/stackit-cli/internal/pkg/services/dns/client" "github.com/stackitcloud/stackit-cli/internal/pkg/tables" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -31,7 +32,7 @@ type inputModel struct { RecordSetId string } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("describe %s", recordSetIdArg), Short: "Shows details of a DNS record set", @@ -66,7 +67,7 @@ func NewCmd() *cobra.Command { } recordSet := resp.Rrset - return outputResult(cmd, model.OutputFormat, recordSet) + return outputResult(cmd, model.OutputFormat, recordSet, p) }, } configureFlags(cmd) @@ -100,7 +101,7 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *dns.APIClie return req } -func outputResult(cmd *cobra.Command, outputFormat string, recordSet *dns.RecordSet) error { +func outputResult(cmd *cobra.Command, outputFormat string, recordSet *dns.RecordSet, p *print.Printer) error { switch outputFormat { case globalflags.PrettyOutputFormat: records := *recordSet.Records @@ -133,7 +134,7 @@ func outputResult(cmd *cobra.Command, outputFormat string, recordSet *dns.Record if err != nil { return fmt.Errorf("marshal DNS record set: %w", err) } - cmd.Println(string(details)) + p.Outputln(string(details)) return nil } diff --git a/internal/cmd/dns/record-set/describe/describe_test.go b/internal/cmd/dns/record-set/describe/describe_test.go index 5801d41d4..c3dd68d55 100644 --- a/internal/cmd/dns/record-set/describe/describe_test.go +++ b/internal/cmd/dns/record-set/describe/describe_test.go @@ -163,7 +163,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { - cmd := NewCmd() + cmd := NewCmd(nil) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/dns/record-set/list/list.go b/internal/cmd/dns/record-set/list/list.go index 95a195e44..815c935dd 100644 --- a/internal/cmd/dns/record-set/list/list.go +++ b/internal/cmd/dns/record-set/list/list.go @@ -11,6 +11,7 @@ import ( "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/print" "github.com/stackitcloud/stackit-cli/internal/pkg/services/dns/client" dnsUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/dns/utils" "github.com/stackitcloud/stackit-cli/internal/pkg/tables" @@ -46,7 +47,7 @@ type inputModel struct { PageSize int64 } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: "list", Short: "List DNS record sets", @@ -92,10 +93,10 @@ func NewCmd() *cobra.Command { if err != nil { zoneLabel = model.ZoneId } - cmd.Printf("No record sets found for zone %s matching the criteria\n", zoneLabel) + p.Info("No record sets found for zone %s matching the criteria\n", zoneLabel) return nil } - return outputResult(cmd, model.OutputFormat, recordSets) + return outputResult(cmd, model.OutputFormat, recordSets, p) }, } @@ -220,14 +221,14 @@ func fetchRecordSets(ctx context.Context, model *inputModel, apiClient dnsClient return recordSets, nil } -func outputResult(cmd *cobra.Command, outputFormat string, recordSets []dns.RecordSet) error { +func outputResult(cmd *cobra.Command, outputFormat string, recordSets []dns.RecordSet, p *print.Printer) error { switch outputFormat { case globalflags.JSONOutputFormat: details, err := json.MarshalIndent(recordSets, "", " ") if err != nil { return fmt.Errorf("marshal DNS record set list: %w", err) } - cmd.Println(string(details)) + p.Outputln(string(details)) return nil default: diff --git a/internal/cmd/dns/record-set/list/list_test.go b/internal/cmd/dns/record-set/list/list_test.go index aad867561..e647eb819 100644 --- a/internal/cmd/dns/record-set/list/list_test.go +++ b/internal/cmd/dns/record-set/list/list_test.go @@ -213,7 +213,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { - cmd := NewCmd() + cmd := NewCmd(nil) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/dns/record-set/record_set.go b/internal/cmd/dns/record-set/record_set.go index efba550e1..698750f4b 100644 --- a/internal/cmd/dns/record-set/record_set.go +++ b/internal/cmd/dns/record-set/record_set.go @@ -7,12 +7,13 @@ import ( "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/pkg/args" + "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" "github.com/spf13/cobra" ) -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: "record-set", Short: "Provides functionality for DNS record set", @@ -20,14 +21,14 @@ func NewCmd() *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd) + addSubcommands(cmd, p) return cmd } -func addSubcommands(cmd *cobra.Command) { - cmd.AddCommand(list.NewCmd()) - cmd.AddCommand(create.NewCmd()) - cmd.AddCommand(describe.NewCmd()) - cmd.AddCommand(delete.NewCmd()) - cmd.AddCommand(update.NewCmd()) +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)) } diff --git a/internal/cmd/dns/record-set/update/update.go b/internal/cmd/dns/record-set/update/update.go index ebc2c5e5c..8f50c75df 100644 --- a/internal/cmd/dns/record-set/update/update.go +++ b/internal/cmd/dns/record-set/update/update.go @@ -10,6 +10,7 @@ import ( "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/print" "github.com/stackitcloud/stackit-cli/internal/pkg/services/dns/client" dnsUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/dns/utils" "github.com/stackitcloud/stackit-cli/internal/pkg/spinner" @@ -40,7 +41,7 @@ type inputModel struct { TTL *int64 } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("update %s", recordSetIdArg), Short: "Updates a DNS record set", @@ -104,7 +105,7 @@ func NewCmd() *cobra.Command { if model.Async { operationState = "Triggered update of" } - cmd.Printf("%s record set %s of zone %s\n", operationState, recordSetLabel, zoneLabel) + p.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 301a74050..087ce4158 100644 --- a/internal/cmd/dns/record-set/update/update_test.go +++ b/internal/cmd/dns/record-set/update/update_test.go @@ -243,7 +243,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { - cmd := NewCmd() + cmd := NewCmd(nil) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/dns/zone/delete/delete.go b/internal/cmd/dns/zone/delete/delete.go index c2b70ba03..e12a7e547 100644 --- a/internal/cmd/dns/zone/delete/delete.go +++ b/internal/cmd/dns/zone/delete/delete.go @@ -9,6 +9,7 @@ import ( "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" + "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/services/dns/client" dnsUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/dns/utils" "github.com/stackitcloud/stackit-cli/internal/pkg/spinner" @@ -28,7 +29,7 @@ type inputModel struct { ZoneId string } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("delete %s", zoneIdArg), Short: "Deletes a DNS zone", @@ -89,7 +90,7 @@ func NewCmd() *cobra.Command { if model.Async { operationState = "Triggered deletion of" } - cmd.Printf("%s zone %s\n", operationState, zoneLabel) + p.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 06dbd7d4b..a2bd86748 100644 --- a/internal/cmd/dns/zone/delete/delete_test.go +++ b/internal/cmd/dns/zone/delete/delete_test.go @@ -136,7 +136,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { - cmd := NewCmd() + cmd := NewCmd(nil) 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 b9bbf7043..d21888729 100644 --- a/internal/cmd/dns/zone/describe/describe.go +++ b/internal/cmd/dns/zone/describe/describe.go @@ -9,6 +9,7 @@ import ( "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" + "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/services/dns/client" "github.com/stackitcloud/stackit-cli/internal/pkg/tables" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -26,7 +27,7 @@ type inputModel struct { ZoneId string } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("describe %s", zoneIdArg), Short: "Shows details of a DNS zone", @@ -60,7 +61,7 @@ func NewCmd() *cobra.Command { } zone := resp.Zone - return outputResult(cmd, model.OutputFormat, zone) + return outputResult(cmd, model.OutputFormat, zone, p) }, } return cmd @@ -85,7 +86,7 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *dns.APIClie return req } -func outputResult(cmd *cobra.Command, outputFormat string, zone *dns.Zone) error { +func outputResult(cmd *cobra.Command, outputFormat string, zone *dns.Zone, p *print.Printer) error { switch outputFormat { case globalflags.PrettyOutputFormat: table := tables.NewTable() @@ -129,7 +130,7 @@ func outputResult(cmd *cobra.Command, outputFormat string, zone *dns.Zone) error if err != nil { return fmt.Errorf("marshal DNS zone: %w", err) } - cmd.Println(string(details)) + p.Outputln(string(details)) return nil } diff --git a/internal/cmd/dns/zone/describe/describe_test.go b/internal/cmd/dns/zone/describe/describe_test.go index e85152646..2b402efab 100644 --- a/internal/cmd/dns/zone/describe/describe_test.go +++ b/internal/cmd/dns/zone/describe/describe_test.go @@ -136,7 +136,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { - cmd := NewCmd() + cmd := NewCmd(nil) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/dns/zone/list/list.go b/internal/cmd/dns/zone/list/list.go index 81af21198..ef22fec3e 100644 --- a/internal/cmd/dns/zone/list/list.go +++ b/internal/cmd/dns/zone/list/list.go @@ -11,6 +11,7 @@ import ( "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/print" "github.com/stackitcloud/stackit-cli/internal/pkg/projectname" "github.com/stackitcloud/stackit-cli/internal/pkg/services/dns/client" "github.com/stackitcloud/stackit-cli/internal/pkg/tables" @@ -44,7 +45,7 @@ type inputModel struct { PageSize int64 } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: "list", Short: "List DNS zones", @@ -87,11 +88,11 @@ func NewCmd() *cobra.Command { if err != nil { projectLabel = model.ProjectId } - cmd.Printf("No zones found for project %q matching the criteria\n", projectLabel) + p.Info("No zones found for project %q matching the criteria\n", projectLabel) return nil } - return outputResult(cmd, model.OutputFormat, zones) + return outputResult(cmd, model.OutputFormat, zones, p) }, } configureFlags(cmd) @@ -208,7 +209,7 @@ func fetchZones(ctx context.Context, model *inputModel, apiClient dnsClient) ([] return zones, nil } -func outputResult(cmd *cobra.Command, outputFormat string, zones []dns.Zone) error { +func outputResult(cmd *cobra.Command, outputFormat string, zones []dns.Zone, p *print.Printer) error { switch outputFormat { case globalflags.JSONOutputFormat: // Show details @@ -216,7 +217,7 @@ func outputResult(cmd *cobra.Command, outputFormat string, zones []dns.Zone) err if err != nil { return fmt.Errorf("marshal DNS zone list: %w", err) } - cmd.Println(string(details)) + p.Outputln(string(details)) return nil default: diff --git a/internal/cmd/dns/zone/list/list_test.go b/internal/cmd/dns/zone/list/list_test.go index 4fb2c315c..9a3b6c011 100644 --- a/internal/cmd/dns/zone/list/list_test.go +++ b/internal/cmd/dns/zone/list/list_test.go @@ -208,7 +208,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { - cmd := NewCmd() + cmd := NewCmd(nil) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/dns/zone/update/update.go b/internal/cmd/dns/zone/update/update.go index 18509e02e..447254d33 100644 --- a/internal/cmd/dns/zone/update/update.go +++ b/internal/cmd/dns/zone/update/update.go @@ -10,6 +10,7 @@ import ( "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/print" "github.com/stackitcloud/stackit-cli/internal/pkg/services/dns/client" dnsUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/dns/utils" "github.com/stackitcloud/stackit-cli/internal/pkg/spinner" @@ -50,7 +51,7 @@ type inputModel struct { ContactEmail *string } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("update %s", zoneIdArg), Short: "Updates a DNS zone", @@ -112,7 +113,7 @@ func NewCmd() *cobra.Command { if model.Async { operationState = "Triggered update of" } - cmd.Printf("%s zone %s\n", operationState, zoneLabel) + p.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 c1d4e35a0..1be550af6 100644 --- a/internal/cmd/dns/zone/update/update_test.go +++ b/internal/cmd/dns/zone/update/update_test.go @@ -244,7 +244,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { - cmd := NewCmd() + cmd := NewCmd(nil) 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 f5e9a2f65..c3578401f 100644 --- a/internal/cmd/dns/zone/zone.go +++ b/internal/cmd/dns/zone/zone.go @@ -26,9 +26,9 @@ func NewCmd(p *print.Printer) *cobra.Command { } func addSubcommands(cmd *cobra.Command, p *print.Printer) { - cmd.AddCommand(list.NewCmd()) + cmd.AddCommand(list.NewCmd(p)) cmd.AddCommand(create.NewCmd(p)) - cmd.AddCommand(describe.NewCmd()) - cmd.AddCommand(update.NewCmd()) - cmd.AddCommand(delete.NewCmd()) + cmd.AddCommand(describe.NewCmd(p)) + cmd.AddCommand(update.NewCmd(p)) + cmd.AddCommand(delete.NewCmd(p)) } diff --git a/internal/cmd/logme/credentials/create/create.go b/internal/cmd/logme/credentials/create/create.go index 4c102c1ee..b34269aad 100644 --- a/internal/cmd/logme/credentials/create/create.go +++ b/internal/cmd/logme/credentials/create/create.go @@ -10,6 +10,7 @@ import ( "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/print" "github.com/stackitcloud/stackit-cli/internal/pkg/services/logme/client" logmeUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/logme/utils" @@ -28,7 +29,7 @@ type inputModel struct { HidePassword bool } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: "create", Short: "Creates credentials for a LogMe instance", @@ -75,20 +76,20 @@ func NewCmd() *cobra.Command { return fmt.Errorf("create LogMe credentials: %w", err) } - cmd.Printf("Created credentials for instance %q. Credentials ID: %s\n\n", instanceLabel, *resp.Id) + p.Outputf("Created credentials for instance %q. Credentials ID: %s\n\n", instanceLabel, *resp.Id) // The username field cannot be set by the user so we only display it if it's not returned empty username := *resp.Raw.Credentials.Username if username != "" { - cmd.Printf("Username: %s\n", *resp.Raw.Credentials.Username) + p.Outputf("Username: %s\n", *resp.Raw.Credentials.Username) } if model.HidePassword { - cmd.Printf("Password: \n") + p.Outputf("Password: \n") } else { - cmd.Printf("Password: %s\n", *resp.Raw.Credentials.Password) + p.Outputf("Password: %s\n", *resp.Raw.Credentials.Password) } - cmd.Printf("Host: %s\n", *resp.Raw.Credentials.Host) - cmd.Printf("Port: %d\n", *resp.Raw.Credentials.Port) - cmd.Printf("URI: %s\n", *resp.Uri) + p.Outputf("Host: %s\n", *resp.Raw.Credentials.Host) + p.Outputf("Port: %d\n", *resp.Raw.Credentials.Port) + p.Outputf("URI: %s\n", *resp.Uri) return nil }, } diff --git a/internal/cmd/logme/credentials/create/create_test.go b/internal/cmd/logme/credentials/create/create_test.go index c0fd85a18..504c90265 100644 --- a/internal/cmd/logme/credentials/create/create_test.go +++ b/internal/cmd/logme/credentials/create/create_test.go @@ -118,7 +118,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { - cmd := NewCmd() + cmd := NewCmd(nil) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/logme/credentials/credentials.go b/internal/cmd/logme/credentials/credentials.go index 641956995..1b884797a 100644 --- a/internal/cmd/logme/credentials/credentials.go +++ b/internal/cmd/logme/credentials/credentials.go @@ -6,12 +6,13 @@ import ( "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/pkg/args" + "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" "github.com/spf13/cobra" ) -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: "credentials", Short: "Provides functionality for LogMe credentials", @@ -19,13 +20,13 @@ func NewCmd() *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd) + addSubcommands(cmd, p) return cmd } -func addSubcommands(cmd *cobra.Command) { - cmd.AddCommand(create.NewCmd()) - cmd.AddCommand(delete.NewCmd()) - cmd.AddCommand(describe.NewCmd()) - cmd.AddCommand(list.NewCmd()) +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)) } diff --git a/internal/cmd/logme/credentials/delete/delete.go b/internal/cmd/logme/credentials/delete/delete.go index 36b92c071..c108b9c7c 100644 --- a/internal/cmd/logme/credentials/delete/delete.go +++ b/internal/cmd/logme/credentials/delete/delete.go @@ -10,6 +10,7 @@ import ( "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/print" "github.com/stackitcloud/stackit-cli/internal/pkg/services/logme/client" logmeUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/logme/utils" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -30,7 +31,7 @@ type inputModel struct { CredentialsId string } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("delete %s", credentialsIdArg), Short: "Deletes credentials of a LogMe instance", @@ -79,7 +80,7 @@ func NewCmd() *cobra.Command { return fmt.Errorf("delete LogMe credentials: %w", err) } - cmd.Printf("Deleted credentials %s of instance %q\n", credentialsLabel, instanceLabel) + p.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 13b772d80..0ed08ed98 100644 --- a/internal/cmd/logme/credentials/delete/delete_test.go +++ b/internal/cmd/logme/credentials/delete/delete_test.go @@ -163,7 +163,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { - cmd := NewCmd() + cmd := NewCmd(nil) 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 0d8893c05..a830a5a2a 100644 --- a/internal/cmd/logme/credentials/describe/describe.go +++ b/internal/cmd/logme/credentials/describe/describe.go @@ -10,6 +10,7 @@ import ( "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/print" "github.com/stackitcloud/stackit-cli/internal/pkg/services/logme/client" "github.com/stackitcloud/stackit-cli/internal/pkg/tables" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -30,7 +31,7 @@ type inputModel struct { CredentialsId string } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("describe %s", credentialsIdArg), Short: "Shows details of credentials of a LogMe instance", @@ -64,7 +65,7 @@ func NewCmd() *cobra.Command { return fmt.Errorf("describe LogMe credentials: %w", err) } - return outputResult(cmd, model.OutputFormat, resp) + return outputResult(cmd, model.OutputFormat, resp, p) }, } configureFlags(cmd) @@ -98,7 +99,7 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *logme.APICl return req } -func outputResult(cmd *cobra.Command, outputFormat string, credentials *logme.CredentialsResponse) error { +func outputResult(cmd *cobra.Command, outputFormat string, credentials *logme.CredentialsResponse, p *print.Printer) error { switch outputFormat { case globalflags.PrettyOutputFormat: table := tables.NewTable() @@ -124,7 +125,7 @@ func outputResult(cmd *cobra.Command, outputFormat string, credentials *logme.Cr if err != nil { return fmt.Errorf("marshal LogMe credentials: %w", err) } - cmd.Println(string(details)) + p.Outputln(string(details)) return nil } diff --git a/internal/cmd/logme/credentials/describe/describe_test.go b/internal/cmd/logme/credentials/describe/describe_test.go index b71e7bd88..4cecdaad2 100644 --- a/internal/cmd/logme/credentials/describe/describe_test.go +++ b/internal/cmd/logme/credentials/describe/describe_test.go @@ -163,7 +163,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { - cmd := NewCmd() + cmd := NewCmd(nil) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/logme/credentials/list/list.go b/internal/cmd/logme/credentials/list/list.go index 98c7c9b6e..9c3e0836b 100644 --- a/internal/cmd/logme/credentials/list/list.go +++ b/internal/cmd/logme/credentials/list/list.go @@ -10,6 +10,7 @@ import ( "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/print" "github.com/stackitcloud/stackit-cli/internal/pkg/services/logme/client" logmeUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/logme/utils" "github.com/stackitcloud/stackit-cli/internal/pkg/tables" @@ -29,7 +30,7 @@ type inputModel struct { Limit *int64 } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: "list", Short: "Lists all credentials' IDs for a LogMe instance", @@ -71,7 +72,7 @@ func NewCmd() *cobra.Command { if err != nil { instanceLabel = model.InstanceId } - cmd.Printf("No credentials found for instance %q\n", instanceLabel) + p.Info("No credentials found for instance %q\n", instanceLabel) return nil } @@ -79,7 +80,7 @@ func NewCmd() *cobra.Command { if model.Limit != nil && len(credentials) > int(*model.Limit) { credentials = credentials[:*model.Limit] } - return outputResult(cmd, model.OutputFormat, credentials) + return outputResult(cmd, model.OutputFormat, credentials, p) }, } configureFlags(cmd) @@ -120,14 +121,14 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *logme.APICl return req } -func outputResult(cmd *cobra.Command, outputFormat string, credentials []logme.CredentialsListItem) error { +func outputResult(cmd *cobra.Command, outputFormat string, credentials []logme.CredentialsListItem, p *print.Printer) error { switch outputFormat { case globalflags.JSONOutputFormat: details, err := json.MarshalIndent(credentials, "", " ") if err != nil { return fmt.Errorf("marshal LogMe credentials list: %w", err) } - cmd.Println(string(details)) + p.Outputln(string(details)) return nil default: diff --git a/internal/cmd/logme/credentials/list/list_test.go b/internal/cmd/logme/credentials/list/list_test.go index d71fcd678..5b6737330 100644 --- a/internal/cmd/logme/credentials/list/list_test.go +++ b/internal/cmd/logme/credentials/list/list_test.go @@ -135,7 +135,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { - cmd := NewCmd() + cmd := NewCmd(nil) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/logme/instance/create/create.go b/internal/cmd/logme/instance/create/create.go index 117e5ae37..9ef071aa2 100644 --- a/internal/cmd/logme/instance/create/create.go +++ b/internal/cmd/logme/instance/create/create.go @@ -12,6 +12,7 @@ import ( "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/print" "github.com/stackitcloud/stackit-cli/internal/pkg/projectname" "github.com/stackitcloud/stackit-cli/internal/pkg/services/logme/client" logmeUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/logme/utils" @@ -55,7 +56,7 @@ type inputModel struct { PlanId *string } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: "create", Short: "Creates a LogMe instance", @@ -128,7 +129,7 @@ func NewCmd() *cobra.Command { if model.Async { operationState = "Triggered creation of" } - cmd.Printf("%s instance for project %q. Instance ID: %s\n", operationState, projectLabel, instanceId) + p.Outputf("%s instance for project %q. Instance ID: %s\n", operationState, projectLabel, instanceId) return nil }, } diff --git a/internal/cmd/logme/instance/create/create_test.go b/internal/cmd/logme/instance/create/create_test.go index f5e1cb0a5..ff41d205a 100644 --- a/internal/cmd/logme/instance/create/create_test.go +++ b/internal/cmd/logme/instance/create/create_test.go @@ -275,7 +275,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { - cmd := NewCmd() + cmd := NewCmd(nil) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/logme/instance/delete/delete.go b/internal/cmd/logme/instance/delete/delete.go index 5817fe84d..697eb18fb 100644 --- a/internal/cmd/logme/instance/delete/delete.go +++ b/internal/cmd/logme/instance/delete/delete.go @@ -9,6 +9,7 @@ import ( "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" + "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/services/logme/client" logmeUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/logme/utils" "github.com/stackitcloud/stackit-cli/internal/pkg/spinner" @@ -28,7 +29,7 @@ type inputModel struct { InstanceId string } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("delete %s", instanceIdArg), Short: "Deletes a LogMe instance", @@ -87,7 +88,7 @@ func NewCmd() *cobra.Command { if model.Async { operationState = "Triggered deletion of" } - cmd.Printf("%s instance %q\n", operationState, instanceLabel) + p.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 d4c8fc9fb..82ae2cb94 100644 --- a/internal/cmd/logme/instance/delete/delete_test.go +++ b/internal/cmd/logme/instance/delete/delete_test.go @@ -136,7 +136,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { - cmd := NewCmd() + cmd := NewCmd(nil) 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 30d054776..55f73a2d4 100644 --- a/internal/cmd/logme/instance/describe/describe.go +++ b/internal/cmd/logme/instance/describe/describe.go @@ -9,6 +9,7 @@ import ( "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" + "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/services/logme/client" "github.com/stackitcloud/stackit-cli/internal/pkg/tables" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -28,7 +29,7 @@ type inputModel struct { InstanceId string } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("describe %s", instanceIdArg), Short: "Shows details of a LogMe instance", @@ -61,7 +62,7 @@ func NewCmd() *cobra.Command { return fmt.Errorf("read LogMe instance: %w", err) } - return outputResult(cmd, model.OutputFormat, resp) + return outputResult(cmd, model.OutputFormat, resp, p) }, } return cmd @@ -86,7 +87,7 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *logme.APICl return req } -func outputResult(cmd *cobra.Command, outputFormat string, instance *logme.Instance) error { +func outputResult(cmd *cobra.Command, outputFormat string, instance *logme.Instance, p *print.Printer) error { switch outputFormat { case globalflags.PrettyOutputFormat: table := tables.NewTable() @@ -119,7 +120,7 @@ func outputResult(cmd *cobra.Command, outputFormat string, instance *logme.Insta if err != nil { return fmt.Errorf("marshal LogMe instance: %w", err) } - cmd.Println(string(details)) + p.Outputln(string(details)) return nil } diff --git a/internal/cmd/logme/instance/describe/describe_test.go b/internal/cmd/logme/instance/describe/describe_test.go index bb342b9a4..2dd60d3ce 100644 --- a/internal/cmd/logme/instance/describe/describe_test.go +++ b/internal/cmd/logme/instance/describe/describe_test.go @@ -136,7 +136,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { - cmd := NewCmd() + cmd := NewCmd(nil) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/logme/instance/instance.go b/internal/cmd/logme/instance/instance.go index 69877a401..534151d57 100644 --- a/internal/cmd/logme/instance/instance.go +++ b/internal/cmd/logme/instance/instance.go @@ -7,12 +7,13 @@ import ( "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/pkg/args" + "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" "github.com/spf13/cobra" ) -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: "instance", Short: "Provides functionality for LogMe instances", @@ -20,14 +21,14 @@ func NewCmd() *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd) + addSubcommands(cmd, p) return cmd } -func addSubcommands(cmd *cobra.Command) { - cmd.AddCommand(create.NewCmd()) - cmd.AddCommand(delete.NewCmd()) - cmd.AddCommand(describe.NewCmd()) - cmd.AddCommand(list.NewCmd()) - cmd.AddCommand(update.NewCmd()) +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)) } diff --git a/internal/cmd/logme/instance/list/list.go b/internal/cmd/logme/instance/list/list.go index a0eb3c500..d37e1da87 100644 --- a/internal/cmd/logme/instance/list/list.go +++ b/internal/cmd/logme/instance/list/list.go @@ -10,6 +10,7 @@ import ( "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/print" "github.com/stackitcloud/stackit-cli/internal/pkg/projectname" "github.com/stackitcloud/stackit-cli/internal/pkg/services/logme/client" "github.com/stackitcloud/stackit-cli/internal/pkg/tables" @@ -27,7 +28,7 @@ type inputModel struct { Limit *int64 } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: "list", Short: "Lists all LogMe instances", @@ -69,7 +70,7 @@ func NewCmd() *cobra.Command { if err != nil { projectLabel = model.ProjectId } - cmd.Printf("No instances found for project %q\n", projectLabel) + p.Info("No instances found for project %q\n", projectLabel) return nil } @@ -78,7 +79,7 @@ func NewCmd() *cobra.Command { instances = instances[:*model.Limit] } - return outputResult(cmd, model.OutputFormat, instances) + return outputResult(cmd, model.OutputFormat, instances, p) }, } @@ -115,14 +116,14 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *logme.APICl return req } -func outputResult(cmd *cobra.Command, outputFormat string, instances []logme.Instance) error { +func outputResult(cmd *cobra.Command, outputFormat string, instances []logme.Instance, p *print.Printer) error { switch outputFormat { case globalflags.JSONOutputFormat: details, err := json.MarshalIndent(instances, "", " ") if err != nil { return fmt.Errorf("marshal LogMe instance list: %w", err) } - cmd.Println(string(details)) + p.Outputln(string(details)) return nil default: diff --git a/internal/cmd/logme/instance/update/update.go b/internal/cmd/logme/instance/update/update.go index 5cd79ca7d..eac0c1cef 100644 --- a/internal/cmd/logme/instance/update/update.go +++ b/internal/cmd/logme/instance/update/update.go @@ -12,6 +12,7 @@ import ( "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/print" "github.com/stackitcloud/stackit-cli/internal/pkg/services/logme/client" logmeUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/logme/utils" "github.com/stackitcloud/stackit-cli/internal/pkg/spinner" @@ -56,7 +57,7 @@ type inputModel struct { PlanId *string } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("update %s", instanceIdArg), Short: "Updates a LogMe instance", @@ -126,7 +127,7 @@ func NewCmd() *cobra.Command { if model.Async { operationState = "Triggered update of" } - cmd.Printf("%s instance %q\n", operationState, instanceLabel) + p.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 1f0065726..633fe1165 100644 --- a/internal/cmd/logme/instance/update/update_test.go +++ b/internal/cmd/logme/instance/update/update_test.go @@ -281,7 +281,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { - cmd := NewCmd() + cmd := NewCmd(nil) 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 8880bfff7..9dc8b77fd 100644 --- a/internal/cmd/logme/logme.go +++ b/internal/cmd/logme/logme.go @@ -5,12 +5,13 @@ import ( "github.com/stackitcloud/stackit-cli/internal/cmd/logme/instance" "github.com/stackitcloud/stackit-cli/internal/cmd/logme/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() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: "logme", Short: "Provides functionality for LogMe", @@ -18,12 +19,12 @@ func NewCmd() *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd) + addSubcommands(cmd, p) return cmd } -func addSubcommands(cmd *cobra.Command) { - cmd.AddCommand(instance.NewCmd()) - cmd.AddCommand(plans.NewCmd()) - cmd.AddCommand(credentials.NewCmd()) +func addSubcommands(cmd *cobra.Command, p *print.Printer) { + cmd.AddCommand(instance.NewCmd(p)) + cmd.AddCommand(plans.NewCmd(p)) + cmd.AddCommand(credentials.NewCmd(p)) } diff --git a/internal/cmd/logme/plans/plans.go b/internal/cmd/logme/plans/plans.go index f7c46d3b9..f28bd8ba7 100644 --- a/internal/cmd/logme/plans/plans.go +++ b/internal/cmd/logme/plans/plans.go @@ -10,6 +10,7 @@ import ( "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/print" "github.com/stackitcloud/stackit-cli/internal/pkg/projectname" "github.com/stackitcloud/stackit-cli/internal/pkg/services/logme/client" "github.com/stackitcloud/stackit-cli/internal/pkg/tables" @@ -27,7 +28,7 @@ type inputModel struct { Limit *int64 } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: "plans", Short: "Lists all LogMe service plans", @@ -69,7 +70,7 @@ func NewCmd() *cobra.Command { if err != nil { projectLabel = model.ProjectId } - cmd.Printf("No plans found for project %q\n", projectLabel) + p.Info("No plans found for project %q\n", projectLabel) return nil } @@ -78,7 +79,7 @@ func NewCmd() *cobra.Command { plans = plans[:*model.Limit] } - return outputResult(cmd, model.OutputFormat, plans) + return outputResult(cmd, model.OutputFormat, plans, p) }, } @@ -115,14 +116,14 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *logme.APICl return req } -func outputResult(cmd *cobra.Command, outputFormat string, plans []logme.Offering) error { +func outputResult(cmd *cobra.Command, outputFormat string, plans []logme.Offering, p *print.Printer) error { switch outputFormat { case globalflags.JSONOutputFormat: details, err := json.MarshalIndent(plans, "", " ") if err != nil { return fmt.Errorf("marshal LogMe plans: %w", err) } - cmd.Println(string(details)) + p.Outputln(string(details)) return nil default: diff --git a/internal/cmd/mariadb/credentials/create/create.go b/internal/cmd/mariadb/credentials/create/create.go index b4dd2d8ee..f24c2576d 100644 --- a/internal/cmd/mariadb/credentials/create/create.go +++ b/internal/cmd/mariadb/credentials/create/create.go @@ -10,6 +10,7 @@ import ( "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/print" "github.com/stackitcloud/stackit-cli/internal/pkg/services/mariadb/client" mariadbUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/mariadb/utils" @@ -28,7 +29,7 @@ type inputModel struct { HidePassword bool } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: "create", Short: "Creates credentials for a MariaDB instance", @@ -75,20 +76,20 @@ func NewCmd() *cobra.Command { return fmt.Errorf("create MariaDB credentials: %w", err) } - cmd.Printf("Created credentials for instance %q. Credentials ID: %s\n\n", instanceLabel, *resp.Id) + p.Outputf("Created credentials for instance %q. Credentials ID: %s\n\n", instanceLabel, *resp.Id) // The username field cannot be set by the user so we only display it if it's not returned empty username := *resp.Raw.Credentials.Username if username != "" { - cmd.Printf("Username: %s\n", *resp.Raw.Credentials.Username) + p.Outputf("Username: %s\n", *resp.Raw.Credentials.Username) } if model.HidePassword { - cmd.Printf("Password: \n") + p.Outputf("Password: \n") } else { - cmd.Printf("Password: %s\n", *resp.Raw.Credentials.Password) + p.Outputf("Password: %s\n", *resp.Raw.Credentials.Password) } - cmd.Printf("Host: %s\n", *resp.Raw.Credentials.Host) - cmd.Printf("Port: %d\n", *resp.Raw.Credentials.Port) - cmd.Printf("URI: %s\n", *resp.Uri) + p.Outputf("Host: %s\n", *resp.Raw.Credentials.Host) + p.Outputf("Port: %d\n", *resp.Raw.Credentials.Port) + p.Outputf("URI: %s\n", *resp.Uri) return nil }, } diff --git a/internal/cmd/mariadb/credentials/create/create_test.go b/internal/cmd/mariadb/credentials/create/create_test.go index 8a4e09d57..3676ee551 100644 --- a/internal/cmd/mariadb/credentials/create/create_test.go +++ b/internal/cmd/mariadb/credentials/create/create_test.go @@ -118,7 +118,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { - cmd := NewCmd() + cmd := NewCmd(nil) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/mariadb/credentials/credentials.go b/internal/cmd/mariadb/credentials/credentials.go index a5bbfd8c4..f8fb1c5d2 100644 --- a/internal/cmd/mariadb/credentials/credentials.go +++ b/internal/cmd/mariadb/credentials/credentials.go @@ -6,12 +6,13 @@ import ( "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/pkg/args" + "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" "github.com/spf13/cobra" ) -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: "credentials", Short: "Provides functionality for MariaDB credentials", @@ -19,13 +20,13 @@ func NewCmd() *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd) + addSubcommands(cmd, p) return cmd } -func addSubcommands(cmd *cobra.Command) { - cmd.AddCommand(create.NewCmd()) - cmd.AddCommand(delete.NewCmd()) - cmd.AddCommand(describe.NewCmd()) - cmd.AddCommand(list.NewCmd()) +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)) } diff --git a/internal/cmd/mariadb/credentials/delete/delete.go b/internal/cmd/mariadb/credentials/delete/delete.go index af05398e2..028626e99 100644 --- a/internal/cmd/mariadb/credentials/delete/delete.go +++ b/internal/cmd/mariadb/credentials/delete/delete.go @@ -10,6 +10,7 @@ import ( "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/print" "github.com/stackitcloud/stackit-cli/internal/pkg/services/mariadb/client" mariadbUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/mariadb/utils" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -30,7 +31,7 @@ type inputModel struct { CredentialsId string } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("delete %s", credentialsIdArg), Short: "Deletes credentials of a MariaDB instance", @@ -79,7 +80,7 @@ func NewCmd() *cobra.Command { return fmt.Errorf("delete MariaDB credentials: %w", err) } - cmd.Printf("Deleted credentials %s of instance %q\n", credentialsLabel, instanceLabel) + p.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 8e73551d1..8834de487 100644 --- a/internal/cmd/mariadb/credentials/delete/delete_test.go +++ b/internal/cmd/mariadb/credentials/delete/delete_test.go @@ -163,7 +163,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { - cmd := NewCmd() + cmd := NewCmd(nil) 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 2469c69af..263d2078c 100644 --- a/internal/cmd/mariadb/credentials/describe/describe.go +++ b/internal/cmd/mariadb/credentials/describe/describe.go @@ -10,6 +10,7 @@ import ( "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/print" "github.com/stackitcloud/stackit-cli/internal/pkg/services/mariadb/client" "github.com/stackitcloud/stackit-cli/internal/pkg/tables" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -30,7 +31,7 @@ type inputModel struct { CredentialsId string } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("describe %s", credentialsIdArg), Short: "Shows details of credentials of a MariaDB instance", @@ -64,7 +65,7 @@ func NewCmd() *cobra.Command { return fmt.Errorf("describe MariaDB credentials: %w", err) } - return outputResult(cmd, model.OutputFormat, resp) + return outputResult(cmd, model.OutputFormat, resp, p) }, } configureFlags(cmd) @@ -98,7 +99,7 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *mariadb.API return req } -func outputResult(cmd *cobra.Command, outputFormat string, credentials *mariadb.CredentialsResponse) error { +func outputResult(cmd *cobra.Command, outputFormat string, credentials *mariadb.CredentialsResponse, p *print.Printer) error { switch outputFormat { case globalflags.PrettyOutputFormat: table := tables.NewTable() @@ -124,7 +125,7 @@ func outputResult(cmd *cobra.Command, outputFormat string, credentials *mariadb. if err != nil { return fmt.Errorf("marshal MariaDB credentials: %w", err) } - cmd.Println(string(details)) + p.Outputln(string(details)) return nil } diff --git a/internal/cmd/mariadb/credentials/describe/describe_test.go b/internal/cmd/mariadb/credentials/describe/describe_test.go index 8941f1e3d..717238033 100644 --- a/internal/cmd/mariadb/credentials/describe/describe_test.go +++ b/internal/cmd/mariadb/credentials/describe/describe_test.go @@ -163,7 +163,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { - cmd := NewCmd() + cmd := NewCmd(nil) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/mariadb/credentials/list/list.go b/internal/cmd/mariadb/credentials/list/list.go index 7ec7b5e1d..3d0a81f50 100644 --- a/internal/cmd/mariadb/credentials/list/list.go +++ b/internal/cmd/mariadb/credentials/list/list.go @@ -10,6 +10,7 @@ import ( "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/print" "github.com/stackitcloud/stackit-cli/internal/pkg/services/mariadb/client" mariadbUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/mariadb/utils" "github.com/stackitcloud/stackit-cli/internal/pkg/tables" @@ -29,7 +30,7 @@ type inputModel struct { Limit *int64 } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: "list", Short: "Lists all credentials' IDs for a MariaDB instance", @@ -71,7 +72,7 @@ func NewCmd() *cobra.Command { if err != nil { instanceLabel = model.InstanceId } - cmd.Printf("No credentials found for instance %q\n", instanceLabel) + p.Info("No credentials found for instance %q\n", instanceLabel) return nil } @@ -79,7 +80,7 @@ func NewCmd() *cobra.Command { if model.Limit != nil && len(credentials) > int(*model.Limit) { credentials = credentials[:*model.Limit] } - return outputResult(cmd, model.OutputFormat, credentials) + return outputResult(cmd, model.OutputFormat, credentials, p) }, } configureFlags(cmd) @@ -120,14 +121,14 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *mariadb.API return req } -func outputResult(cmd *cobra.Command, outputFormat string, credentials []mariadb.CredentialsListItem) error { +func outputResult(cmd *cobra.Command, outputFormat string, credentials []mariadb.CredentialsListItem, p *print.Printer) error { switch outputFormat { case globalflags.JSONOutputFormat: details, err := json.MarshalIndent(credentials, "", " ") if err != nil { return fmt.Errorf("marshal MariaDB credentials list: %w", err) } - cmd.Println(string(details)) + p.Outputln(string(details)) return nil default: diff --git a/internal/cmd/mariadb/credentials/list/list_test.go b/internal/cmd/mariadb/credentials/list/list_test.go index 1abdc3e76..5531af98b 100644 --- a/internal/cmd/mariadb/credentials/list/list_test.go +++ b/internal/cmd/mariadb/credentials/list/list_test.go @@ -135,7 +135,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { - cmd := NewCmd() + cmd := NewCmd(nil) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/mariadb/instance/create/create.go b/internal/cmd/mariadb/instance/create/create.go index c2a43d209..a3491d093 100644 --- a/internal/cmd/mariadb/instance/create/create.go +++ b/internal/cmd/mariadb/instance/create/create.go @@ -12,6 +12,7 @@ import ( "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/print" "github.com/stackitcloud/stackit-cli/internal/pkg/projectname" "github.com/stackitcloud/stackit-cli/internal/pkg/services/mariadb/client" mariadbUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/mariadb/utils" @@ -55,7 +56,7 @@ type inputModel struct { PlanId *string } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: "create", Short: "Creates a MariaDB instance", @@ -128,7 +129,7 @@ func NewCmd() *cobra.Command { if model.Async { operationState = "Triggered creation of" } - cmd.Printf("%s instance for project %q. Instance ID: %s\n", operationState, projectLabel, instanceId) + p.Outputf("%s instance for project %q. Instance ID: %s\n", operationState, projectLabel, instanceId) return nil }, } diff --git a/internal/cmd/mariadb/instance/create/create_test.go b/internal/cmd/mariadb/instance/create/create_test.go index 5f54f92c6..46dcf6ff3 100644 --- a/internal/cmd/mariadb/instance/create/create_test.go +++ b/internal/cmd/mariadb/instance/create/create_test.go @@ -275,7 +275,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { - cmd := NewCmd() + cmd := NewCmd(nil) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/mariadb/instance/delete/delete.go b/internal/cmd/mariadb/instance/delete/delete.go index 7f9004b51..8b99f690c 100644 --- a/internal/cmd/mariadb/instance/delete/delete.go +++ b/internal/cmd/mariadb/instance/delete/delete.go @@ -9,6 +9,7 @@ import ( "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" + "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/services/mariadb/client" mariadbUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/mariadb/utils" "github.com/stackitcloud/stackit-cli/internal/pkg/spinner" @@ -28,7 +29,7 @@ type inputModel struct { InstanceId string } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("delete %s", instanceIdArg), Short: "Deletes a MariaDB instance", @@ -87,7 +88,7 @@ func NewCmd() *cobra.Command { if model.Async { operationState = "Triggered deletion of" } - cmd.Printf("%s instance %q\n", operationState, instanceLabel) + p.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 6cbf076d3..f1b99b143 100644 --- a/internal/cmd/mariadb/instance/delete/delete_test.go +++ b/internal/cmd/mariadb/instance/delete/delete_test.go @@ -136,7 +136,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { - cmd := NewCmd() + cmd := NewCmd(nil) 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 aa02f63a0..d0468dfde 100644 --- a/internal/cmd/mariadb/instance/describe/describe.go +++ b/internal/cmd/mariadb/instance/describe/describe.go @@ -9,6 +9,7 @@ import ( "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" + "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/services/mariadb/client" "github.com/stackitcloud/stackit-cli/internal/pkg/tables" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -28,7 +29,7 @@ type inputModel struct { InstanceId string } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("describe %s", instanceIdArg), Short: "Shows details of a MariaDB instance", @@ -61,7 +62,7 @@ func NewCmd() *cobra.Command { return fmt.Errorf("read MariaDB instance: %w", err) } - return outputResult(cmd, model.OutputFormat, resp) + return outputResult(cmd, model.OutputFormat, resp, p) }, } return cmd @@ -86,7 +87,7 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *mariadb.API return req } -func outputResult(cmd *cobra.Command, outputFormat string, instance *mariadb.Instance) error { +func outputResult(cmd *cobra.Command, outputFormat string, instance *mariadb.Instance, p *print.Printer) error { switch outputFormat { case globalflags.PrettyOutputFormat: table := tables.NewTable() @@ -119,7 +120,7 @@ func outputResult(cmd *cobra.Command, outputFormat string, instance *mariadb.Ins if err != nil { return fmt.Errorf("marshal MariaDB instance: %w", err) } - cmd.Println(string(details)) + p.Outputln(string(details)) return nil } diff --git a/internal/cmd/mariadb/instance/describe/describe_test.go b/internal/cmd/mariadb/instance/describe/describe_test.go index ecd4af094..db3aa4ac7 100644 --- a/internal/cmd/mariadb/instance/describe/describe_test.go +++ b/internal/cmd/mariadb/instance/describe/describe_test.go @@ -136,7 +136,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { - cmd := NewCmd() + cmd := NewCmd(nil) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/mariadb/instance/instance.go b/internal/cmd/mariadb/instance/instance.go index dc68e87ee..71e25309a 100644 --- a/internal/cmd/mariadb/instance/instance.go +++ b/internal/cmd/mariadb/instance/instance.go @@ -7,12 +7,13 @@ import ( "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/pkg/args" + "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" "github.com/spf13/cobra" ) -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: "instance", Short: "Provides functionality for MariaDB instances", @@ -20,14 +21,14 @@ func NewCmd() *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd) + addSubcommands(cmd, p) return cmd } -func addSubcommands(cmd *cobra.Command) { - cmd.AddCommand(create.NewCmd()) - cmd.AddCommand(delete.NewCmd()) - cmd.AddCommand(describe.NewCmd()) - cmd.AddCommand(list.NewCmd()) - cmd.AddCommand(update.NewCmd()) +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)) } diff --git a/internal/cmd/mariadb/instance/list/list.go b/internal/cmd/mariadb/instance/list/list.go index bc1321c42..9718bbd97 100644 --- a/internal/cmd/mariadb/instance/list/list.go +++ b/internal/cmd/mariadb/instance/list/list.go @@ -10,6 +10,7 @@ import ( "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/print" "github.com/stackitcloud/stackit-cli/internal/pkg/projectname" "github.com/stackitcloud/stackit-cli/internal/pkg/services/mariadb/client" "github.com/stackitcloud/stackit-cli/internal/pkg/tables" @@ -27,7 +28,7 @@ type inputModel struct { Limit *int64 } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: "list", Short: "Lists all MariaDB instances", @@ -69,7 +70,7 @@ func NewCmd() *cobra.Command { if err != nil { projectLabel = model.ProjectId } - cmd.Printf("No instances found for project %q\n", projectLabel) + p.Info("No instances found for project %q\n", projectLabel) return nil } @@ -78,7 +79,7 @@ func NewCmd() *cobra.Command { instances = instances[:*model.Limit] } - return outputResult(cmd, model.OutputFormat, instances) + return outputResult(cmd, model.OutputFormat, instances, p) }, } @@ -115,14 +116,14 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *mariadb.API return req } -func outputResult(cmd *cobra.Command, outputFormat string, instances []mariadb.Instance) error { +func outputResult(cmd *cobra.Command, outputFormat string, instances []mariadb.Instance, p *print.Printer) error { switch outputFormat { case globalflags.JSONOutputFormat: details, err := json.MarshalIndent(instances, "", " ") if err != nil { return fmt.Errorf("marshal MariaDB instance list: %w", err) } - cmd.Println(string(details)) + p.Outputln(string(details)) return nil default: diff --git a/internal/cmd/mariadb/instance/update/update.go b/internal/cmd/mariadb/instance/update/update.go index 80dcae49d..12d80c7c3 100644 --- a/internal/cmd/mariadb/instance/update/update.go +++ b/internal/cmd/mariadb/instance/update/update.go @@ -12,6 +12,7 @@ import ( "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/print" "github.com/stackitcloud/stackit-cli/internal/pkg/services/mariadb/client" mariadbUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/mariadb/utils" "github.com/stackitcloud/stackit-cli/internal/pkg/spinner" @@ -56,7 +57,7 @@ type inputModel struct { PlanId *string } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("update %s", instanceIdArg), Short: "Updates a MariaDB instance", @@ -126,7 +127,7 @@ func NewCmd() *cobra.Command { if model.Async { operationState = "Triggered update of" } - cmd.Printf("%s instance %q\n", operationState, instanceLabel) + p.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 54e6d11b3..eb7545a53 100644 --- a/internal/cmd/mariadb/instance/update/update_test.go +++ b/internal/cmd/mariadb/instance/update/update_test.go @@ -292,7 +292,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { - cmd := NewCmd() + cmd := NewCmd(nil) 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 e8f9f647d..7058813f9 100644 --- a/internal/cmd/mariadb/mariadb.go +++ b/internal/cmd/mariadb/mariadb.go @@ -5,12 +5,13 @@ import ( "github.com/stackitcloud/stackit-cli/internal/cmd/mariadb/instance" "github.com/stackitcloud/stackit-cli/internal/cmd/mariadb/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() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: "mariadb", Short: "Provides functionality for MariaDB", @@ -18,12 +19,12 @@ func NewCmd() *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd) + addSubcommands(cmd, p) return cmd } -func addSubcommands(cmd *cobra.Command) { - cmd.AddCommand(instance.NewCmd()) - cmd.AddCommand(plans.NewCmd()) - cmd.AddCommand(credentials.NewCmd()) +func addSubcommands(cmd *cobra.Command, p *print.Printer) { + cmd.AddCommand(instance.NewCmd(p)) + cmd.AddCommand(plans.NewCmd(p)) + cmd.AddCommand(credentials.NewCmd(p)) } diff --git a/internal/cmd/mariadb/plans/plans.go b/internal/cmd/mariadb/plans/plans.go index 7e400f7f8..73aab9ffd 100644 --- a/internal/cmd/mariadb/plans/plans.go +++ b/internal/cmd/mariadb/plans/plans.go @@ -10,6 +10,7 @@ import ( "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/print" "github.com/stackitcloud/stackit-cli/internal/pkg/projectname" "github.com/stackitcloud/stackit-cli/internal/pkg/services/mariadb/client" "github.com/stackitcloud/stackit-cli/internal/pkg/tables" @@ -27,7 +28,7 @@ type inputModel struct { Limit *int64 } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: "plans", Short: "Lists all MariaDB service plans", @@ -69,7 +70,7 @@ func NewCmd() *cobra.Command { if err != nil { projectLabel = model.ProjectId } - cmd.Printf("No plans found for project %q\n", projectLabel) + p.Info("No plans found for project %q\n", projectLabel) return nil } @@ -78,7 +79,7 @@ func NewCmd() *cobra.Command { plans = plans[:*model.Limit] } - return outputResult(cmd, model.OutputFormat, plans) + return outputResult(cmd, model.OutputFormat, plans, p) }, } @@ -115,14 +116,14 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *mariadb.API return req } -func outputResult(cmd *cobra.Command, outputFormat string, plans []mariadb.Offering) error { +func outputResult(cmd *cobra.Command, outputFormat string, plans []mariadb.Offering, p *print.Printer) error { switch outputFormat { case globalflags.JSONOutputFormat: details, err := json.MarshalIndent(plans, "", " ") if err != nil { return fmt.Errorf("marshal MariaDB plans: %w", err) } - cmd.Println(string(details)) + p.Outputln(string(details)) return nil default: @@ -131,8 +132,8 @@ func outputResult(cmd *cobra.Command, outputFormat string, plans []mariadb.Offer for i := range plans { o := plans[i] for j := range *o.Plans { - p := (*o.Plans)[j] - table.AddRow(*o.Name, *p.Id, *p.Name, *p.Description) + plan := (*o.Plans)[j] + table.AddRow(*o.Name, *plan.Id, *plan.Name, *plan.Description) } table.AddSeparator() } diff --git a/internal/cmd/mongodbflex/instance/create/create.go b/internal/cmd/mongodbflex/instance/create/create.go index 837082408..a51b6a105 100644 --- a/internal/cmd/mongodbflex/instance/create/create.go +++ b/internal/cmd/mongodbflex/instance/create/create.go @@ -11,6 +11,7 @@ import ( "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/print" "github.com/stackitcloud/stackit-cli/internal/pkg/projectname" "github.com/stackitcloud/stackit-cli/internal/pkg/services/mongodbflex/client" mongodbflexUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/mongodbflex/utils" @@ -55,7 +56,7 @@ type inputModel struct { Type *string } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: "create", Short: "Creates a MongoDB Flex instance", @@ -134,7 +135,7 @@ func NewCmd() *cobra.Command { if model.Async { operationState = "Triggered creation of" } - cmd.Printf("%s instance for project %q. Instance ID: %s\n", operationState, projectLabel, instanceId) + p.Outputf("%s instance for project %q. Instance ID: %s\n", operationState, projectLabel, instanceId) return nil }, } diff --git a/internal/cmd/mongodbflex/instance/create/create_test.go b/internal/cmd/mongodbflex/instance/create/create_test.go index 270751e88..d3dfacad3 100644 --- a/internal/cmd/mongodbflex/instance/create/create_test.go +++ b/internal/cmd/mongodbflex/instance/create/create_test.go @@ -248,7 +248,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { - cmd := NewCmd() + cmd := NewCmd(nil) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/mongodbflex/instance/delete/delete.go b/internal/cmd/mongodbflex/instance/delete/delete.go index 37c2c7654..9246f15b3 100644 --- a/internal/cmd/mongodbflex/instance/delete/delete.go +++ b/internal/cmd/mongodbflex/instance/delete/delete.go @@ -9,6 +9,7 @@ import ( "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" + "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/services/mongodbflex/client" mongodbflexUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/mongodbflex/utils" "github.com/stackitcloud/stackit-cli/internal/pkg/spinner" @@ -28,7 +29,7 @@ type inputModel struct { InstanceId string } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("delete %s", instanceIdArg), Short: "Deletes a MongoDB Flex instance", @@ -87,7 +88,7 @@ func NewCmd() *cobra.Command { if model.Async { operationState = "Triggered deletion of" } - cmd.Printf("%s instance %q\n", operationState, instanceLabel) + p.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 59879251b..2a2d3f2e2 100644 --- a/internal/cmd/mongodbflex/instance/delete/delete_test.go +++ b/internal/cmd/mongodbflex/instance/delete/delete_test.go @@ -136,7 +136,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { - cmd := NewCmd() + cmd := NewCmd(nil) 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 1b91eebf1..139f90c18 100644 --- a/internal/cmd/mongodbflex/instance/describe/describe.go +++ b/internal/cmd/mongodbflex/instance/describe/describe.go @@ -10,6 +10,7 @@ import ( "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" + "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/services/mongodbflex/client" mongodbflexUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/mongodbflex/utils" "github.com/stackitcloud/stackit-cli/internal/pkg/tables" @@ -28,7 +29,7 @@ type inputModel struct { InstanceId string } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("describe %s", instanceIdArg), Short: "Shows details of a MongoDB Flex instance", @@ -61,7 +62,7 @@ func NewCmd() *cobra.Command { return fmt.Errorf("read MongoDB Flex instance: %w", err) } - return outputResult(cmd, model.OutputFormat, resp.Item) + return outputResult(cmd, model.OutputFormat, resp.Item, p) }, } return cmd @@ -86,7 +87,7 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *mongodbflex return req } -func outputResult(cmd *cobra.Command, outputFormat string, instance *mongodbflex.Instance) error { +func outputResult(cmd *cobra.Command, outputFormat string, instance *mongodbflex.Instance, p *print.Printer) error { switch outputFormat { case globalflags.PrettyOutputFormat: aclsArray := *instance.Acl.Items @@ -132,7 +133,7 @@ func outputResult(cmd *cobra.Command, outputFormat string, instance *mongodbflex if err != nil { return fmt.Errorf("marshal MongoDB Flex instance: %w", err) } - cmd.Println(string(details)) + p.Outputln(string(details)) return nil } diff --git a/internal/cmd/mongodbflex/instance/describe/describe_test.go b/internal/cmd/mongodbflex/instance/describe/describe_test.go index 249184c2d..bbc76890e 100644 --- a/internal/cmd/mongodbflex/instance/describe/describe_test.go +++ b/internal/cmd/mongodbflex/instance/describe/describe_test.go @@ -136,7 +136,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { - cmd := NewCmd() + cmd := NewCmd(nil) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/mongodbflex/instance/instance.go b/internal/cmd/mongodbflex/instance/instance.go index 5915fc909..1a770bcc1 100644 --- a/internal/cmd/mongodbflex/instance/instance.go +++ b/internal/cmd/mongodbflex/instance/instance.go @@ -7,12 +7,13 @@ import ( "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/pkg/args" + "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" "github.com/spf13/cobra" ) -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: "instance", Short: "Provides functionality for MongoDB Flex instances", @@ -20,14 +21,14 @@ func NewCmd() *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd) + addSubcommands(cmd, p) return cmd } -func addSubcommands(cmd *cobra.Command) { - cmd.AddCommand(create.NewCmd()) - cmd.AddCommand(delete.NewCmd()) - cmd.AddCommand(describe.NewCmd()) - cmd.AddCommand(list.NewCmd()) - cmd.AddCommand(update.NewCmd()) +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)) } diff --git a/internal/cmd/mongodbflex/instance/list/list.go b/internal/cmd/mongodbflex/instance/list/list.go index 98a5d1290..1378890a7 100644 --- a/internal/cmd/mongodbflex/instance/list/list.go +++ b/internal/cmd/mongodbflex/instance/list/list.go @@ -10,6 +10,7 @@ import ( "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/print" "github.com/stackitcloud/stackit-cli/internal/pkg/projectname" "github.com/stackitcloud/stackit-cli/internal/pkg/services/mongodbflex/client" "github.com/stackitcloud/stackit-cli/internal/pkg/tables" @@ -27,7 +28,7 @@ type inputModel struct { Limit *int64 } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: "list", Short: "Lists all MongoDB Flex instances", @@ -68,7 +69,7 @@ func NewCmd() *cobra.Command { if err != nil { projectLabel = model.ProjectId } - cmd.Printf("No instances found for project %q\n", projectLabel) + p.Info("No instances found for project %q\n", projectLabel) return nil } instances := *resp.Items @@ -78,7 +79,7 @@ func NewCmd() *cobra.Command { instances = instances[:*model.Limit] } - return outputResult(cmd, model.OutputFormat, instances) + return outputResult(cmd, model.OutputFormat, instances, p) }, } @@ -115,14 +116,14 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *mongodbflex return req } -func outputResult(cmd *cobra.Command, outputFormat string, instances []mongodbflex.InstanceListInstance) error { +func outputResult(cmd *cobra.Command, outputFormat string, instances []mongodbflex.InstanceListInstance, p *print.Printer) error { switch outputFormat { case globalflags.JSONOutputFormat: details, err := json.MarshalIndent(instances, "", " ") if err != nil { return fmt.Errorf("marshal MongoDB Flex instance list: %w", err) } - cmd.Println(string(details)) + p.Outputln(string(details)) return nil default: diff --git a/internal/cmd/mongodbflex/instance/update/update.go b/internal/cmd/mongodbflex/instance/update/update.go index 12c3900ee..3a5059ce7 100644 --- a/internal/cmd/mongodbflex/instance/update/update.go +++ b/internal/cmd/mongodbflex/instance/update/update.go @@ -11,6 +11,7 @@ import ( "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/print" "github.com/stackitcloud/stackit-cli/internal/pkg/services/mongodbflex/client" mongodbflexUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/mongodbflex/utils" "github.com/stackitcloud/stackit-cli/internal/pkg/spinner" @@ -52,7 +53,7 @@ type inputModel struct { Type *string } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("update %s", instanceIdArg), Short: "Updates a MongoDB Flex instance", @@ -119,7 +120,7 @@ func NewCmd() *cobra.Command { if model.Async { operationState = "Triggered update of" } - cmd.Printf("%s instance %q\n", operationState, instanceLabel) + p.Info("%s instance %q\n", operationState, instanceLabel) return nil }, } diff --git a/internal/cmd/mongodbflex/instance/update/update_test.go b/internal/cmd/mongodbflex/instance/update/update_test.go index 4da4cb025..9fc3670a4 100644 --- a/internal/cmd/mongodbflex/instance/update/update_test.go +++ b/internal/cmd/mongodbflex/instance/update/update_test.go @@ -278,7 +278,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { - cmd := NewCmd() + cmd := NewCmd(nil) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/mongodbflex/mongodbflex.go b/internal/cmd/mongodbflex/mongodbflex.go index 7b285c3b9..3aa6bf890 100644 --- a/internal/cmd/mongodbflex/mongodbflex.go +++ b/internal/cmd/mongodbflex/mongodbflex.go @@ -5,12 +5,13 @@ import ( "github.com/stackitcloud/stackit-cli/internal/cmd/mongodbflex/options" "github.com/stackitcloud/stackit-cli/internal/cmd/mongodbflex/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() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: "mongodbflex", Short: "Provides functionality for MongoDB Flex", @@ -18,12 +19,12 @@ func NewCmd() *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd) + addSubcommands(cmd, p) return cmd } -func addSubcommands(cmd *cobra.Command) { - cmd.AddCommand(instance.NewCmd()) - cmd.AddCommand(user.NewCmd()) - cmd.AddCommand(options.NewCmd()) +func addSubcommands(cmd *cobra.Command, p *print.Printer) { + cmd.AddCommand(instance.NewCmd(p)) + cmd.AddCommand(user.NewCmd(p)) + cmd.AddCommand(options.NewCmd(p)) } diff --git a/internal/cmd/mongodbflex/options/options.go b/internal/cmd/mongodbflex/options/options.go index 7196b8c4c..1c17343d2 100644 --- a/internal/cmd/mongodbflex/options/options.go +++ b/internal/cmd/mongodbflex/options/options.go @@ -10,6 +10,7 @@ import ( "github.com/stackitcloud/stackit-cli/internal/pkg/flags" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/pager" + "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/services/mongodbflex/client" "github.com/stackitcloud/stackit-cli/internal/pkg/tables" @@ -44,7 +45,7 @@ type flavorStorages struct { Storages *mongodbflex.ListStoragesResponse `json:"storages"` } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: "options", Short: "Lists MongoDB Flex options", @@ -75,7 +76,7 @@ func NewCmd() *cobra.Command { } // Call API - err = buildAndExecuteRequest(ctx, cmd, model, apiClient) + err = buildAndExecuteRequest(ctx, cmd, model, apiClient, p) if err != nil { return fmt.Errorf("get MongoDB Flex options: %w", err) } @@ -129,7 +130,7 @@ type mongoDBFlexOptionsClient interface { ListStoragesExecute(ctx context.Context, projectId, flavorId string) (*mongodbflex.ListStoragesResponse, error) } -func buildAndExecuteRequest(ctx context.Context, cmd *cobra.Command, model *inputModel, apiClient mongoDBFlexOptionsClient) error { +func buildAndExecuteRequest(ctx context.Context, cmd *cobra.Command, model *inputModel, apiClient mongoDBFlexOptionsClient, p *print.Printer) error { var flavors *mongodbflex.ListFlavorsResponse var versions *mongodbflex.ListVersionsResponse var storages *mongodbflex.ListStoragesResponse @@ -154,10 +155,10 @@ func buildAndExecuteRequest(ctx context.Context, cmd *cobra.Command, model *inpu } } - return outputResult(cmd, model, flavors, versions, storages) + return outputResult(cmd, model, flavors, versions, storages, p) } -func outputResult(cmd *cobra.Command, model *inputModel, flavors *mongodbflex.ListFlavorsResponse, versions *mongodbflex.ListVersionsResponse, storages *mongodbflex.ListStoragesResponse) error { +func outputResult(cmd *cobra.Command, model *inputModel, flavors *mongodbflex.ListFlavorsResponse, versions *mongodbflex.ListVersionsResponse, storages *mongodbflex.ListStoragesResponse, p *print.Printer) error { options := &options{} if flavors != nil { options.Flavors = flavors.Flavors @@ -178,7 +179,7 @@ func outputResult(cmd *cobra.Command, model *inputModel, flavors *mongodbflex.Li if err != nil { return fmt.Errorf("marshal MongoDB Flex options: %w", err) } - cmd.Println(string(details)) + p.Outputln(string(details)) return nil default: return outputResultAsTable(cmd, model, options) diff --git a/internal/cmd/mongodbflex/options/options_test.go b/internal/cmd/mongodbflex/options/options_test.go index 41dd53431..4b0ff50a1 100644 --- a/internal/cmd/mongodbflex/options/options_test.go +++ b/internal/cmd/mongodbflex/options/options_test.go @@ -165,7 +165,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { - cmd := NewCmd() + cmd := NewCmd(nil) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -288,14 +288,14 @@ func TestBuildAndExecuteRequest(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { - cmd := NewCmd() + cmd := NewCmd(nil) client := &mongoDBFlexClientMocked{ listFlavorsFails: tt.listFlavorsFails, listVersionsFails: tt.listVersionsFails, listStoragesFails: tt.listStoragesFails, } - err := buildAndExecuteRequest(testCtx, cmd, tt.model, client) + err := buildAndExecuteRequest(testCtx, cmd, tt.model, client, nil) if err != nil && tt.isValid { t.Fatalf("error building and executing request: %v", err) } diff --git a/internal/cmd/mongodbflex/user/create/create.go b/internal/cmd/mongodbflex/user/create/create.go index de670b3ef..b29dfddaf 100644 --- a/internal/cmd/mongodbflex/user/create/create.go +++ b/internal/cmd/mongodbflex/user/create/create.go @@ -10,6 +10,7 @@ import ( "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/print" "github.com/stackitcloud/stackit-cli/internal/pkg/services/mongodbflex/client" mongodbflexUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/mongodbflex/utils" @@ -37,7 +38,7 @@ type inputModel struct { Roles *[]string } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: "create", Short: "Creates a MongoDB Flex user", @@ -90,14 +91,14 @@ func NewCmd() *cobra.Command { } user := resp.Item - cmd.Printf("Created user for instance %q. User ID: %s\n\n", instanceLabel, *user.Id) - cmd.Printf("Username: %s\n", *user.Username) - cmd.Printf("Password: %s\n", *user.Password) - cmd.Printf("Roles: %v\n", *user.Roles) - cmd.Printf("Database: %s\n", *user.Database) - cmd.Printf("Host: %s\n", *user.Host) - cmd.Printf("Port: %d\n", *user.Port) - cmd.Printf("URI: %s\n", *user.Uri) + p.Outputf("Created user for instance %q. User ID: %s\n\n", instanceLabel, *user.Id) + p.Outputf("Username: %s\n", *user.Username) + p.Outputf("Password: %s\n", *user.Password) + p.Outputf("Roles: %v\n", *user.Roles) + p.Outputf("Database: %s\n", *user.Database) + p.Outputf("Host: %s\n", *user.Host) + p.Outputf("Port: %d\n", *user.Port) + p.Outputf("URI: %s\n", *user.Uri) return nil }, diff --git a/internal/cmd/mongodbflex/user/delete/delete.go b/internal/cmd/mongodbflex/user/delete/delete.go index c13123ed5..a610ef710 100644 --- a/internal/cmd/mongodbflex/user/delete/delete.go +++ b/internal/cmd/mongodbflex/user/delete/delete.go @@ -10,6 +10,7 @@ import ( "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/print" "github.com/stackitcloud/stackit-cli/internal/pkg/services/mongodbflex/client" mongodbflexUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/mongodbflex/utils" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -31,7 +32,7 @@ type inputModel struct { UserId string } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("delete %s", userIdArg), Short: "Deletes a MongoDB Flex user", @@ -83,7 +84,7 @@ func NewCmd() *cobra.Command { return fmt.Errorf("delete MongoDB Flex user: %w", err) } - cmd.Printf("Deleted user %q of instance %q\n", userLabel, instanceLabel) + p.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 309fdfcad..7fd731c07 100644 --- a/internal/cmd/mongodbflex/user/delete/delete_test.go +++ b/internal/cmd/mongodbflex/user/delete/delete_test.go @@ -163,7 +163,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { - cmd := NewCmd() + cmd := NewCmd(nil) 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 cd03a0244..e0384aea7 100644 --- a/internal/cmd/mongodbflex/user/describe/describe.go +++ b/internal/cmd/mongodbflex/user/describe/describe.go @@ -10,6 +10,7 @@ import ( "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/print" "github.com/stackitcloud/stackit-cli/internal/pkg/services/mongodbflex/client" "github.com/stackitcloud/stackit-cli/internal/pkg/tables" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -31,7 +32,7 @@ type inputModel struct { UserId string } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("describe %s", userIdArg), Short: "Shows details of a MongoDB Flex user", @@ -69,7 +70,7 @@ func NewCmd() *cobra.Command { return fmt.Errorf("get MongoDB Flex user: %w", err) } - return outputResult(cmd, model.OutputFormat, *resp.Item) + return outputResult(cmd, model.OutputFormat, *resp.Item, p) }, } @@ -104,7 +105,7 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *mongodbflex return req } -func outputResult(cmd *cobra.Command, outputFormat string, user mongodbflex.InstanceResponseUser) error { +func outputResult(cmd *cobra.Command, outputFormat string, user mongodbflex.InstanceResponseUser, p *print.Printer) error { switch outputFormat { case globalflags.PrettyOutputFormat: table := tables.NewTable() @@ -131,7 +132,7 @@ func outputResult(cmd *cobra.Command, outputFormat string, user mongodbflex.Inst if err != nil { return fmt.Errorf("marshal MongoDB Flex user: %w", err) } - cmd.Println(string(details)) + p.Outputln(string(details)) return nil } diff --git a/internal/cmd/mongodbflex/user/describe/describe_test.go b/internal/cmd/mongodbflex/user/describe/describe_test.go index 8ce6b70e4..47a913f80 100644 --- a/internal/cmd/mongodbflex/user/describe/describe_test.go +++ b/internal/cmd/mongodbflex/user/describe/describe_test.go @@ -163,7 +163,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { - cmd := NewCmd() + cmd := NewCmd(nil) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/mongodbflex/user/list/list.go b/internal/cmd/mongodbflex/user/list/list.go index 5ec42bb34..ceb35bcfa 100644 --- a/internal/cmd/mongodbflex/user/list/list.go +++ b/internal/cmd/mongodbflex/user/list/list.go @@ -10,6 +10,7 @@ import ( "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/print" "github.com/stackitcloud/stackit-cli/internal/pkg/services/mongodbflex/client" mongodbflexUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/mongodbflex/utils" "github.com/stackitcloud/stackit-cli/internal/pkg/tables" @@ -30,7 +31,7 @@ type inputModel struct { Limit *int64 } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: "list", Short: "Lists all MongoDB Flex users of an instance", @@ -71,7 +72,7 @@ func NewCmd() *cobra.Command { if err != nil { instanceLabel = *model.InstanceId } - cmd.Printf("No users found for instance %q\n", instanceLabel) + p.Info("No users found for instance %q\n", instanceLabel) return nil } users := *resp.Items @@ -81,7 +82,7 @@ func NewCmd() *cobra.Command { users = users[:*model.Limit] } - return outputResult(cmd, model.OutputFormat, users) + return outputResult(cmd, model.OutputFormat, users, p) }, } @@ -123,14 +124,14 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *mongodbflex return req } -func outputResult(cmd *cobra.Command, outputFormat string, users []mongodbflex.ListUser) error { +func outputResult(cmd *cobra.Command, outputFormat string, users []mongodbflex.ListUser, p *print.Printer) error { switch outputFormat { case globalflags.JSONOutputFormat: details, err := json.MarshalIndent(users, "", " ") if err != nil { return fmt.Errorf("marshal MongoDB Flex user list: %w", err) } - cmd.Println(string(details)) + p.Outputln(string(details)) return nil default: diff --git a/internal/cmd/mongodbflex/user/reset-password/reset_password.go b/internal/cmd/mongodbflex/user/reset-password/reset_password.go index df544d5ec..58bb83ea9 100644 --- a/internal/cmd/mongodbflex/user/reset-password/reset_password.go +++ b/internal/cmd/mongodbflex/user/reset-password/reset_password.go @@ -10,6 +10,7 @@ import ( "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/print" "github.com/stackitcloud/stackit-cli/internal/pkg/services/mongodbflex/client" mongodbflexUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/mongodbflex/utils" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -31,7 +32,7 @@ type inputModel struct { UserId string } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("reset-password %s", userIdArg), Short: "Resets the password of a MongoDB Flex user", @@ -83,10 +84,10 @@ func NewCmd() *cobra.Command { return fmt.Errorf("reset MongoDB Flex user password: %w", err) } - cmd.Printf("Reset password for user %q of instance %q\n\n", userLabel, instanceLabel) - cmd.Printf("Username: %s\n", *user.Username) - cmd.Printf("New password: %s\n", *user.Password) - cmd.Printf("New URI: %s\n", *user.Uri) + p.Outputf("Reset password for user %q of instance %q\n\n", userLabel, instanceLabel) + p.Outputf("Username: %s\n", *user.Username) + p.Outputf("New password: %s\n", *user.Password) + p.Outputf("New URI: %s\n", *user.Uri) return nil }, } 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 aecd8628b..20c1fbff9 100644 --- a/internal/cmd/mongodbflex/user/reset-password/reset_password_test.go +++ b/internal/cmd/mongodbflex/user/reset-password/reset_password_test.go @@ -163,7 +163,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { - cmd := NewCmd() + cmd := NewCmd(nil) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/mongodbflex/user/update/update.go b/internal/cmd/mongodbflex/user/update/update.go index 8e202aea7..a0653373d 100644 --- a/internal/cmd/mongodbflex/user/update/update.go +++ b/internal/cmd/mongodbflex/user/update/update.go @@ -10,6 +10,7 @@ import ( "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/print" "github.com/stackitcloud/stackit-cli/internal/pkg/services/mongodbflex/client" mongodbflexUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/mongodbflex/utils" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -35,7 +36,7 @@ type inputModel struct { Roles *[]string } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("update %s", userIdArg), Short: "Updates a MongoDB Flex user", @@ -84,7 +85,7 @@ func NewCmd() *cobra.Command { return fmt.Errorf("update MongoDB Flex user: %w", err) } - cmd.Printf("Updated user %q of instance %q\n", userLabel, instanceLabel) + p.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 c719ca351..6d4029141 100644 --- a/internal/cmd/mongodbflex/user/update/update_test.go +++ b/internal/cmd/mongodbflex/user/update/update_test.go @@ -195,7 +195,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { - cmd := NewCmd() + cmd := NewCmd(nil) 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 9bdb12903..614b7c2f9 100644 --- a/internal/cmd/mongodbflex/user/user.go +++ b/internal/cmd/mongodbflex/user/user.go @@ -8,12 +8,13 @@ import ( 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/pkg/args" + "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" "github.com/spf13/cobra" ) -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: "user", Short: "Provides functionality for MongoDB Flex users", @@ -21,15 +22,15 @@ func NewCmd() *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd) + addSubcommands(cmd, p) return cmd } -func addSubcommands(cmd *cobra.Command) { - cmd.AddCommand(create.NewCmd()) - cmd.AddCommand(delete.NewCmd()) - cmd.AddCommand(describe.NewCmd()) - cmd.AddCommand(list.NewCmd()) - cmd.AddCommand(resetpassword.NewCmd()) - cmd.AddCommand(update.NewCmd()) +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)) } diff --git a/internal/cmd/object-storage/bucket/bucket.go b/internal/cmd/object-storage/bucket/bucket.go index 5b0e1638b..701fc0934 100644 --- a/internal/cmd/object-storage/bucket/bucket.go +++ b/internal/cmd/object-storage/bucket/bucket.go @@ -6,12 +6,13 @@ import ( "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/pkg/args" + "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" "github.com/spf13/cobra" ) -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: "bucket", Short: "Provides functionality for Object Storage buckets", @@ -19,13 +20,13 @@ func NewCmd() *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd) + addSubcommands(cmd, p) return cmd } -func addSubcommands(cmd *cobra.Command) { - cmd.AddCommand(delete.NewCmd()) - cmd.AddCommand(describe.NewCmd()) - cmd.AddCommand(create.NewCmd()) - cmd.AddCommand(list.NewCmd()) +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)) } diff --git a/internal/cmd/object-storage/bucket/create/create.go b/internal/cmd/object-storage/bucket/create/create.go index e05a060b9..fe44d9a45 100644 --- a/internal/cmd/object-storage/bucket/create/create.go +++ b/internal/cmd/object-storage/bucket/create/create.go @@ -9,6 +9,7 @@ import ( "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" + "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/services/object-storage/client" "github.com/stackitcloud/stackit-cli/internal/pkg/spinner" @@ -26,7 +27,7 @@ type inputModel struct { BucketName string } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("create %s", bucketNameArg), Short: "Creates an Object Storage bucket", @@ -80,7 +81,7 @@ func NewCmd() *cobra.Command { if model.Async { operationState = "Triggered creation of" } - cmd.Printf("%s bucket %q\n", operationState, model.BucketName) + p.Outputf("%s bucket %q\n", operationState, model.BucketName) return nil }, } diff --git a/internal/cmd/object-storage/bucket/create/create_test.go b/internal/cmd/object-storage/bucket/create/create_test.go index 5f20432cf..524091713 100644 --- a/internal/cmd/object-storage/bucket/create/create_test.go +++ b/internal/cmd/object-storage/bucket/create/create_test.go @@ -130,7 +130,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { - cmd := NewCmd() + cmd := NewCmd(nil) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/object-storage/bucket/delete/delete.go b/internal/cmd/object-storage/bucket/delete/delete.go index d4d92e3e1..8068776cf 100644 --- a/internal/cmd/object-storage/bucket/delete/delete.go +++ b/internal/cmd/object-storage/bucket/delete/delete.go @@ -9,6 +9,7 @@ import ( "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" + "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/services/object-storage/client" "github.com/stackitcloud/stackit-cli/internal/pkg/spinner" @@ -26,7 +27,7 @@ type inputModel struct { BucketName string } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("delete %s", bucketNameArg), Short: "Deletes an Object Storage bucket", @@ -80,7 +81,7 @@ func NewCmd() *cobra.Command { if model.Async { operationState = "Triggered deletion of" } - cmd.Printf("%s bucket %q\n", operationState, model.BucketName) + p.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 6a423ea5c..3b2037735 100644 --- a/internal/cmd/object-storage/bucket/delete/delete_test.go +++ b/internal/cmd/object-storage/bucket/delete/delete_test.go @@ -130,7 +130,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { - cmd := NewCmd() + cmd := NewCmd(nil) 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 c3853e240..58aff3066 100644 --- a/internal/cmd/object-storage/bucket/describe/describe.go +++ b/internal/cmd/object-storage/bucket/describe/describe.go @@ -9,6 +9,7 @@ import ( "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" + "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/services/object-storage/client" "github.com/stackitcloud/stackit-cli/internal/pkg/tables" @@ -25,7 +26,7 @@ type inputModel struct { BucketName string } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("describe %s", bucketNameArg), Short: "Shows details of an Object Storage bucket", @@ -58,7 +59,7 @@ func NewCmd() *cobra.Command { return fmt.Errorf("read Object Storage bucket: %w", err) } - return outputResult(cmd, model.OutputFormat, resp.Bucket) + return outputResult(cmd, model.OutputFormat, resp.Bucket, p) }, } return cmd @@ -83,7 +84,7 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *objectstora return req } -func outputResult(cmd *cobra.Command, outputFormat string, bucket *objectstorage.Bucket) error { +func outputResult(cmd *cobra.Command, outputFormat string, bucket *objectstorage.Bucket, p *print.Printer) error { switch outputFormat { case globalflags.PrettyOutputFormat: table := tables.NewTable() @@ -106,7 +107,7 @@ func outputResult(cmd *cobra.Command, outputFormat string, bucket *objectstorage if err != nil { return fmt.Errorf("marshal Object Storage bucket: %w", err) } - cmd.Println(string(details)) + p.Outputln(string(details)) return nil } diff --git a/internal/cmd/object-storage/bucket/describe/describe_test.go b/internal/cmd/object-storage/bucket/describe/describe_test.go index e5ec4f789..e23245368 100644 --- a/internal/cmd/object-storage/bucket/describe/describe_test.go +++ b/internal/cmd/object-storage/bucket/describe/describe_test.go @@ -130,7 +130,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { - cmd := NewCmd() + cmd := NewCmd(nil) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/object-storage/bucket/list/list.go b/internal/cmd/object-storage/bucket/list/list.go index e716b1078..8cf16ba96 100644 --- a/internal/cmd/object-storage/bucket/list/list.go +++ b/internal/cmd/object-storage/bucket/list/list.go @@ -10,6 +10,7 @@ import ( "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/print" "github.com/stackitcloud/stackit-cli/internal/pkg/projectname" "github.com/stackitcloud/stackit-cli/internal/pkg/services/object-storage/client" "github.com/stackitcloud/stackit-cli/internal/pkg/tables" @@ -27,7 +28,7 @@ type inputModel struct { Limit *int64 } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: "list", Short: "Lists all Object Storage buckets", @@ -68,7 +69,7 @@ func NewCmd() *cobra.Command { if err != nil { projectLabel = model.ProjectId } - cmd.Printf("No buckets found for project %s\n", projectLabel) + p.Info("No buckets found for project %s\n", projectLabel) return nil } buckets := *resp.Buckets @@ -78,7 +79,7 @@ func NewCmd() *cobra.Command { buckets = buckets[:*model.Limit] } - return outputResult(cmd, model.OutputFormat, buckets) + return outputResult(cmd, model.OutputFormat, buckets, p) }, } @@ -115,14 +116,14 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *objectstora return req } -func outputResult(cmd *cobra.Command, outputFormat string, buckets []objectstorage.Bucket) error { +func outputResult(cmd *cobra.Command, outputFormat string, buckets []objectstorage.Bucket, p *print.Printer) error { switch outputFormat { case globalflags.JSONOutputFormat: details, err := json.MarshalIndent(buckets, "", " ") if err != nil { return fmt.Errorf("marshal Object Storage bucket list: %w", err) } - cmd.Println(string(details)) + p.Outputln(string(details)) return nil default: diff --git a/internal/cmd/object-storage/credentials-group/create/create.go b/internal/cmd/object-storage/credentials-group/create/create.go index f441b99dc..4925a545c 100644 --- a/internal/cmd/object-storage/credentials-group/create/create.go +++ b/internal/cmd/object-storage/credentials-group/create/create.go @@ -10,6 +10,7 @@ import ( "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/print" "github.com/stackitcloud/stackit-cli/internal/pkg/services/object-storage/client" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -26,7 +27,7 @@ type inputModel struct { CredentialsGroupName string } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: "create", Short: "Creates a credentials group to hold Object Storage access credentials", @@ -65,8 +66,8 @@ func NewCmd() *cobra.Command { return fmt.Errorf("create Object Storage credentials group: %w", err) } - cmd.Printf("Created credentials group %q. Credentials group ID: %s\n\n", *resp.CredentialsGroup.DisplayName, *resp.CredentialsGroup.CredentialsGroupId) - cmd.Printf("URN: %s\n", *resp.CredentialsGroup.Urn) + p.Outputf("Created credentials group %q. Credentials group ID: %s\n\n", *resp.CredentialsGroup.DisplayName, *resp.CredentialsGroup.CredentialsGroupId) + p.Outputf("URN: %s\n", *resp.CredentialsGroup.Urn) return nil }, } 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 bb7ce3212..5b5fbc95a 100644 --- a/internal/cmd/object-storage/credentials-group/create/create_test.go +++ b/internal/cmd/object-storage/credentials-group/create/create_test.go @@ -116,7 +116,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { - cmd := NewCmd() + cmd := NewCmd(nil) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/object-storage/credentials-group/credentials_group.go b/internal/cmd/object-storage/credentials-group/credentials_group.go index 6427642ef..0803796f4 100644 --- a/internal/cmd/object-storage/credentials-group/credentials_group.go +++ b/internal/cmd/object-storage/credentials-group/credentials_group.go @@ -5,12 +5,13 @@ import ( "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/pkg/args" + "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" "github.com/spf13/cobra" ) -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: "credentials-group", Short: "Provides functionality for Object Storage credentials group", @@ -18,12 +19,12 @@ func NewCmd() *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd) + addSubcommands(cmd, p) return cmd } -func addSubcommands(cmd *cobra.Command) { - cmd.AddCommand(create.NewCmd()) - cmd.AddCommand(delete.NewCmd()) - cmd.AddCommand(list.NewCmd()) +func addSubcommands(cmd *cobra.Command, p *print.Printer) { + cmd.AddCommand(create.NewCmd(p)) + cmd.AddCommand(delete.NewCmd(p)) + cmd.AddCommand(list.NewCmd(p)) } diff --git a/internal/cmd/object-storage/credentials-group/delete/delete.go b/internal/cmd/object-storage/credentials-group/delete/delete.go index bdc519414..4e4afa81e 100644 --- a/internal/cmd/object-storage/credentials-group/delete/delete.go +++ b/internal/cmd/object-storage/credentials-group/delete/delete.go @@ -9,6 +9,7 @@ import ( "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" + "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/services/object-storage/client" objectStorageUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/object-storage/utils" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -26,7 +27,7 @@ type inputModel struct { CredentialsGroupId string } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("delete %s", credentialsGroupIdArg), Short: "Deletes a credentials group that holds Object Storage access credentials", @@ -70,7 +71,7 @@ func NewCmd() *cobra.Command { return fmt.Errorf("delete Object Storage credentials group: %w", err) } - cmd.Printf("Deleted credentials group %q\n", credentialsGroupLabel) + p.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 54f403e37..d29e79f99 100644 --- a/internal/cmd/object-storage/credentials-group/delete/delete_test.go +++ b/internal/cmd/object-storage/credentials-group/delete/delete_test.go @@ -136,7 +136,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { - cmd := NewCmd() + cmd := NewCmd(nil) 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 135b969e4..2ff50bffe 100644 --- a/internal/cmd/object-storage/credentials-group/list/list.go +++ b/internal/cmd/object-storage/credentials-group/list/list.go @@ -10,6 +10,7 @@ import ( "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/print" "github.com/stackitcloud/stackit-cli/internal/pkg/services/object-storage/client" "github.com/stackitcloud/stackit-cli/internal/pkg/tables" @@ -26,7 +27,7 @@ type inputModel struct { Limit *int64 } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: "list", Short: "Lists all credentials groups that hold Object Storage access credentials", @@ -64,7 +65,7 @@ func NewCmd() *cobra.Command { } credentialsGroups := *resp.CredentialsGroups if len(credentialsGroups) == 0 { - cmd.Println("No credentials groups found for your project") + p.Info("No credentials groups found for your project") return nil } @@ -72,7 +73,7 @@ func NewCmd() *cobra.Command { if model.Limit != nil && len(credentialsGroups) > int(*model.Limit) { credentialsGroups = credentialsGroups[:*model.Limit] } - return outputResult(cmd, model.OutputFormat, credentialsGroups) + return outputResult(cmd, model.OutputFormat, credentialsGroups, p) }, } configureFlags(cmd) @@ -108,14 +109,14 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *objectstora return req } -func outputResult(cmd *cobra.Command, outputFormat string, credentialsGroups []objectstorage.CredentialsGroup) error { +func outputResult(cmd *cobra.Command, outputFormat string, credentialsGroups []objectstorage.CredentialsGroup, p *print.Printer) error { switch outputFormat { case globalflags.JSONOutputFormat: details, err := json.MarshalIndent(credentialsGroups, "", " ") if err != nil { return fmt.Errorf("marshal Object Storage credentials group list: %w", err) } - cmd.Println(string(details)) + p.Outputln(string(details)) return nil default: 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 f37526b2b..a8058e24e 100644 --- a/internal/cmd/object-storage/credentials-group/list/list_test.go +++ b/internal/cmd/object-storage/credentials-group/list/list_test.go @@ -111,7 +111,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { - cmd := NewCmd() + cmd := NewCmd(nil) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/object-storage/credentials/create/create.go b/internal/cmd/object-storage/credentials/create/create.go index 7088e1874..6445880b3 100644 --- a/internal/cmd/object-storage/credentials/create/create.go +++ b/internal/cmd/object-storage/credentials/create/create.go @@ -14,6 +14,7 @@ import ( "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/print" "github.com/stackitcloud/stackit-cli/internal/pkg/services/object-storage/client" "github.com/stackitcloud/stackit-sdk-go/services/objectstorage" ) @@ -31,7 +32,7 @@ type inputModel struct { HidePassword bool } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: "create", Short: "Creates credentials for an Object Storage credentials group", @@ -83,10 +84,10 @@ func NewCmd() *cobra.Command { expireDate = *resp.Expires } - cmd.Printf("Created credentials in group %q. Credentials ID: %s\n\n", credentialsGroupLabel, *resp.KeyId) - cmd.Printf("Access Key ID: %s\n", *resp.AccessKey) - cmd.Printf("Secret Access Key: %s\n", *resp.SecretAccessKey) - cmd.Printf("Expire Date: %s\n", expireDate) + p.Outputf("Created credentials in group %q. Credentials ID: %s\n\n", credentialsGroupLabel, *resp.KeyId) + p.Outputf("Access Key ID: %s\n", *resp.AccessKey) + p.Outputf("Secret Access Key: %s\n", *resp.SecretAccessKey) + p.Outputf("Expire Date: %s\n", expireDate) return nil }, diff --git a/internal/cmd/object-storage/credentials/create/create_test.go b/internal/cmd/object-storage/credentials/create/create_test.go index 64718e9ec..b13fa63e5 100644 --- a/internal/cmd/object-storage/credentials/create/create_test.go +++ b/internal/cmd/object-storage/credentials/create/create_test.go @@ -175,7 +175,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { - cmd := NewCmd() + cmd := NewCmd(nil) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/object-storage/credentials/credentials.go b/internal/cmd/object-storage/credentials/credentials.go index 4914945e9..e96b86072 100644 --- a/internal/cmd/object-storage/credentials/credentials.go +++ b/internal/cmd/object-storage/credentials/credentials.go @@ -5,12 +5,13 @@ import ( "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/pkg/args" + "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" "github.com/spf13/cobra" ) -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: "credentials", Short: "Provides functionality for Object Storage credentials", @@ -18,12 +19,12 @@ func NewCmd() *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd) + addSubcommands(cmd, p) return cmd } -func addSubcommands(cmd *cobra.Command) { - cmd.AddCommand(create.NewCmd()) - cmd.AddCommand(delete.NewCmd()) - cmd.AddCommand(list.NewCmd()) +func addSubcommands(cmd *cobra.Command, p *print.Printer) { + cmd.AddCommand(create.NewCmd(p)) + cmd.AddCommand(delete.NewCmd(p)) + cmd.AddCommand(list.NewCmd(p)) } diff --git a/internal/cmd/object-storage/credentials/delete/delete.go b/internal/cmd/object-storage/credentials/delete/delete.go index 3a029e0f3..1bfdcc122 100644 --- a/internal/cmd/object-storage/credentials/delete/delete.go +++ b/internal/cmd/object-storage/credentials/delete/delete.go @@ -11,6 +11,7 @@ import ( "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/print" "github.com/stackitcloud/stackit-cli/internal/pkg/services/object-storage/client" objectStorageUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/object-storage/utils" "github.com/stackitcloud/stackit-sdk-go/services/objectstorage" @@ -27,7 +28,7 @@ type inputModel struct { CredentialsId string } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("delete %s", credentialsIdArg), Short: "Deletes credentials of an Object Storage credentials group", @@ -76,7 +77,7 @@ func NewCmd() *cobra.Command { return fmt.Errorf("delete Object Storage credentials: %w", err) } - cmd.Printf("Deleted credentials %q of credentials group %q\n", credentialsLabel, credentialsGroupLabel) + p.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 e594ed7e3..bc023ebf2 100644 --- a/internal/cmd/object-storage/credentials/delete/delete_test.go +++ b/internal/cmd/object-storage/credentials/delete/delete_test.go @@ -155,7 +155,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { - cmd := NewCmd() + cmd := NewCmd(nil) 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 f982bd44e..9855826f1 100644 --- a/internal/cmd/object-storage/credentials/list/list.go +++ b/internal/cmd/object-storage/credentials/list/list.go @@ -12,6 +12,7 @@ import ( "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/print" "github.com/stackitcloud/stackit-cli/internal/pkg/services/object-storage/client" "github.com/stackitcloud/stackit-cli/internal/pkg/tables" @@ -30,7 +31,7 @@ type inputModel struct { Limit *int64 } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: "list", Short: "Lists all credentials for an Object Storage credentials group", @@ -73,7 +74,7 @@ func NewCmd() *cobra.Command { credentialsGroupLabel = model.CredentialsGroupId } - cmd.Printf("No credentials found for credentials group %q\n", credentialsGroupLabel) + p.Info("No credentials found for credentials group %q\n", credentialsGroupLabel) return nil } @@ -81,7 +82,7 @@ func NewCmd() *cobra.Command { if model.Limit != nil && len(credentials) > int(*model.Limit) { credentials = credentials[:*model.Limit] } - return outputResult(cmd, model.OutputFormat, credentials) + return outputResult(cmd, model.OutputFormat, credentials, p) }, } configureFlags(cmd) @@ -123,14 +124,14 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *objectstora return req } -func outputResult(cmd *cobra.Command, outputFormat string, credentials []objectstorage.AccessKey) error { +func outputResult(cmd *cobra.Command, outputFormat string, credentials []objectstorage.AccessKey, p *print.Printer) error { switch outputFormat { case globalflags.JSONOutputFormat: details, err := json.MarshalIndent(credentials, "", " ") if err != nil { return fmt.Errorf("marshal Object Storage credentials list: %w", err) } - cmd.Println(string(details)) + p.Outputln(string(details)) return nil default: diff --git a/internal/cmd/object-storage/credentials/list/list_test.go b/internal/cmd/object-storage/credentials/list/list_test.go index 6299e0022..32feb5457 100644 --- a/internal/cmd/object-storage/credentials/list/list_test.go +++ b/internal/cmd/object-storage/credentials/list/list_test.go @@ -136,7 +136,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { - cmd := NewCmd() + cmd := NewCmd(nil) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/object-storage/disable/disable.go b/internal/cmd/object-storage/disable/disable.go index 44a701f9c..e257d57ae 100644 --- a/internal/cmd/object-storage/disable/disable.go +++ b/internal/cmd/object-storage/disable/disable.go @@ -9,6 +9,7 @@ import ( "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" + "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/projectname" "github.com/stackitcloud/stackit-cli/internal/pkg/services/object-storage/client" @@ -20,7 +21,7 @@ type InputModel struct { *globalflags.GlobalFlagModel } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: "disable", Short: "Disables Object Storage for a project", @@ -68,7 +69,7 @@ func NewCmd() *cobra.Command { if model.Async { operationState = "Triggered disablement of" } - cmd.Printf("%s Object Storage for project %q\n", operationState, projectLabel) + p.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 f76ab50b7..609d5e591 100644 --- a/internal/cmd/object-storage/enable/enable.go +++ b/internal/cmd/object-storage/enable/enable.go @@ -9,6 +9,7 @@ import ( "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" + "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/projectname" "github.com/stackitcloud/stackit-cli/internal/pkg/services/object-storage/client" @@ -20,7 +21,7 @@ type InputModel struct { *globalflags.GlobalFlagModel } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: "enable", Short: "Enables Object Storage for a project", @@ -68,7 +69,7 @@ func NewCmd() *cobra.Command { if model.Async { operationState = "Triggered enablement of" } - cmd.Printf("%s Object Storage for project %q\n", operationState, projectLabel) + p.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 c3a1a38f0..a612598d3 100644 --- a/internal/cmd/object-storage/object_storage.go +++ b/internal/cmd/object-storage/object_storage.go @@ -7,12 +7,13 @@ import ( "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/pkg/args" + "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" "github.com/spf13/cobra" ) -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: "object-storage", Short: "Provides functionality regarding Object Storage", @@ -20,14 +21,14 @@ func NewCmd() *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd) + addSubcommands(cmd, p) return cmd } -func addSubcommands(cmd *cobra.Command) { - cmd.AddCommand(bucket.NewCmd()) - cmd.AddCommand(disable.NewCmd()) - cmd.AddCommand(enable.NewCmd()) - cmd.AddCommand(credentialsGroup.NewCmd()) - cmd.AddCommand(credentials.NewCmd()) +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)) } diff --git a/internal/cmd/opensearch/credentials/create/create.go b/internal/cmd/opensearch/credentials/create/create.go index 4cb9ac1c1..62978f298 100644 --- a/internal/cmd/opensearch/credentials/create/create.go +++ b/internal/cmd/opensearch/credentials/create/create.go @@ -10,6 +10,7 @@ import ( "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/print" "github.com/stackitcloud/stackit-cli/internal/pkg/services/opensearch/client" opensearchUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/opensearch/utils" @@ -28,7 +29,7 @@ type inputModel struct { HidePassword bool } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: "create", Short: "Creates credentials for an OpenSearch instance", @@ -75,19 +76,19 @@ func NewCmd() *cobra.Command { return fmt.Errorf("create OpenSearch credentials: %w", err) } - cmd.Printf("Created credentials for instance %q. Credentials ID: %s\n\n", instanceLabel, *resp.Id) // The username field cannot be set by the user so we only display it if it's not returned empty + p.Outputf("Created credentials for instance %q. Credentials ID: %s\n\n", instanceLabel, *resp.Id) // The username field cannot be set by the user so we only display it if it's not returned empty username := *resp.Raw.Credentials.Username if username != "" { - cmd.Printf("Username: %s\n", *resp.Raw.Credentials.Username) + p.Outputf("Username: %s\n", *resp.Raw.Credentials.Username) } if model.HidePassword { - cmd.Printf("Password: \n") + p.Outputf("Password: \n") } else { - cmd.Printf("Password: %s\n", *resp.Raw.Credentials.Password) + p.Outputf("Password: %s\n", *resp.Raw.Credentials.Password) } - cmd.Printf("Host: %s\n", *resp.Raw.Credentials.Host) - cmd.Printf("Port: %d\n", *resp.Raw.Credentials.Port) - cmd.Printf("URI: %s\n", *resp.Uri) + p.Outputf("Host: %s\n", *resp.Raw.Credentials.Host) + p.Outputf("Port: %d\n", *resp.Raw.Credentials.Port) + p.Outputf("URI: %s\n", *resp.Uri) return nil }, } diff --git a/internal/cmd/opensearch/credentials/create/create_test.go b/internal/cmd/opensearch/credentials/create/create_test.go index 98b84e16f..5ec06a4ae 100644 --- a/internal/cmd/opensearch/credentials/create/create_test.go +++ b/internal/cmd/opensearch/credentials/create/create_test.go @@ -118,7 +118,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { - cmd := NewCmd() + cmd := NewCmd(nil) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/opensearch/credentials/credentials.go b/internal/cmd/opensearch/credentials/credentials.go index e890f45a4..4ea7f3f76 100644 --- a/internal/cmd/opensearch/credentials/credentials.go +++ b/internal/cmd/opensearch/credentials/credentials.go @@ -6,12 +6,13 @@ import ( "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/pkg/args" + "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" "github.com/spf13/cobra" ) -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: "credentials", Short: "Provides functionality for OpenSearch credentials", @@ -19,13 +20,13 @@ func NewCmd() *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd) + addSubcommands(cmd, p) return cmd } -func addSubcommands(cmd *cobra.Command) { - cmd.AddCommand(create.NewCmd()) - cmd.AddCommand(delete.NewCmd()) - cmd.AddCommand(describe.NewCmd()) - cmd.AddCommand(list.NewCmd()) +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)) } diff --git a/internal/cmd/opensearch/credentials/delete/delete.go b/internal/cmd/opensearch/credentials/delete/delete.go index cc4a97a82..f27e1ea5a 100644 --- a/internal/cmd/opensearch/credentials/delete/delete.go +++ b/internal/cmd/opensearch/credentials/delete/delete.go @@ -10,6 +10,7 @@ import ( "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/print" "github.com/stackitcloud/stackit-cli/internal/pkg/services/opensearch/client" opensearchUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/opensearch/utils" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -30,7 +31,7 @@ type inputModel struct { CredentialsId string } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("delete %s", credentialsIdArg), Short: "Deletes credentials of an OpenSearch instance", @@ -79,7 +80,7 @@ func NewCmd() *cobra.Command { return fmt.Errorf("delete OpenSearch credentials: %w", err) } - cmd.Printf("Deleted credentials %s of instance %q\n", credentialsLabel, instanceLabel) + p.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 f96107c88..276e68eaa 100644 --- a/internal/cmd/opensearch/credentials/delete/delete_test.go +++ b/internal/cmd/opensearch/credentials/delete/delete_test.go @@ -163,7 +163,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { - cmd := NewCmd() + cmd := NewCmd(nil) 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 eb826e495..b3d665f4e 100644 --- a/internal/cmd/opensearch/credentials/describe/describe.go +++ b/internal/cmd/opensearch/credentials/describe/describe.go @@ -10,6 +10,7 @@ import ( "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/print" "github.com/stackitcloud/stackit-cli/internal/pkg/services/opensearch/client" "github.com/stackitcloud/stackit-cli/internal/pkg/tables" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -30,7 +31,7 @@ type inputModel struct { CredentialsId string } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("describe %s", credentialsIdArg), Short: "Shows details of credentials of an OpenSearch instance", @@ -64,7 +65,7 @@ func NewCmd() *cobra.Command { return fmt.Errorf("describe OpenSearch credentials: %w", err) } - return outputResult(cmd, model.OutputFormat, resp) + return outputResult(cmd, model.OutputFormat, resp, p) }, } configureFlags(cmd) @@ -98,7 +99,7 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *opensearch. return req } -func outputResult(cmd *cobra.Command, outputFormat string, credentials *opensearch.CredentialsResponse) error { +func outputResult(cmd *cobra.Command, outputFormat string, credentials *opensearch.CredentialsResponse, p *print.Printer) error { switch outputFormat { case globalflags.PrettyOutputFormat: table := tables.NewTable() @@ -124,7 +125,7 @@ func outputResult(cmd *cobra.Command, outputFormat string, credentials *opensear if err != nil { return fmt.Errorf("marshal OpenSearch credentials: %w", err) } - cmd.Println(string(details)) + p.Outputln(string(details)) return nil } diff --git a/internal/cmd/opensearch/credentials/describe/describe_test.go b/internal/cmd/opensearch/credentials/describe/describe_test.go index 69f06b230..5b1354aeb 100644 --- a/internal/cmd/opensearch/credentials/describe/describe_test.go +++ b/internal/cmd/opensearch/credentials/describe/describe_test.go @@ -163,7 +163,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { - cmd := NewCmd() + cmd := NewCmd(nil) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/opensearch/credentials/list/list.go b/internal/cmd/opensearch/credentials/list/list.go index d5c5c7c59..fce5a2050 100644 --- a/internal/cmd/opensearch/credentials/list/list.go +++ b/internal/cmd/opensearch/credentials/list/list.go @@ -10,6 +10,7 @@ import ( "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/print" "github.com/stackitcloud/stackit-cli/internal/pkg/services/opensearch/client" opensearchUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/opensearch/utils" "github.com/stackitcloud/stackit-cli/internal/pkg/tables" @@ -29,7 +30,7 @@ type inputModel struct { Limit *int64 } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: "list", Short: "Lists all credentials' IDs for an OpenSearch instance", @@ -71,7 +72,7 @@ func NewCmd() *cobra.Command { if err != nil { instanceLabel = model.InstanceId } - cmd.Printf("No credentials found for instance %q\n", instanceLabel) + p.Info("No credentials found for instance %q\n", instanceLabel) return nil } @@ -79,7 +80,7 @@ func NewCmd() *cobra.Command { if model.Limit != nil && len(credentials) > int(*model.Limit) { credentials = credentials[:*model.Limit] } - return outputResult(cmd, model.OutputFormat, credentials) + return outputResult(cmd, model.OutputFormat, credentials, p) }, } configureFlags(cmd) @@ -120,14 +121,14 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *opensearch. return req } -func outputResult(cmd *cobra.Command, outputFormat string, credentials []opensearch.CredentialsListItem) error { +func outputResult(cmd *cobra.Command, outputFormat string, credentials []opensearch.CredentialsListItem, p *print.Printer) error { switch outputFormat { case globalflags.JSONOutputFormat: details, err := json.MarshalIndent(credentials, "", " ") if err != nil { return fmt.Errorf("marshal OpenSearch credentials list: %w", err) } - cmd.Println(string(details)) + p.Outputln(string(details)) return nil default: diff --git a/internal/cmd/opensearch/credentials/list/list_test.go b/internal/cmd/opensearch/credentials/list/list_test.go index fb75b52e7..3cd807c35 100644 --- a/internal/cmd/opensearch/credentials/list/list_test.go +++ b/internal/cmd/opensearch/credentials/list/list_test.go @@ -135,7 +135,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { - cmd := NewCmd() + cmd := NewCmd(nil) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/opensearch/instance/create/create.go b/internal/cmd/opensearch/instance/create/create.go index 18c948aa2..4a9507e77 100644 --- a/internal/cmd/opensearch/instance/create/create.go +++ b/internal/cmd/opensearch/instance/create/create.go @@ -12,6 +12,7 @@ import ( "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/print" "github.com/stackitcloud/stackit-cli/internal/pkg/projectname" "github.com/stackitcloud/stackit-cli/internal/pkg/services/opensearch/client" opensearchUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/opensearch/utils" @@ -55,7 +56,7 @@ type inputModel struct { PlanId *string } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: "create", Short: "Creates an OpenSearch instance", @@ -128,7 +129,7 @@ func NewCmd() *cobra.Command { if model.Async { operationState = "Triggered creation of" } - cmd.Printf("%s instance for project %q. Instance ID: %s\n", operationState, projectLabel, instanceId) + p.Outputf("%s instance for project %q. Instance ID: %s\n", operationState, projectLabel, instanceId) return nil }, } diff --git a/internal/cmd/opensearch/instance/create/create_test.go b/internal/cmd/opensearch/instance/create/create_test.go index 9d4e32470..e123fdaf4 100644 --- a/internal/cmd/opensearch/instance/create/create_test.go +++ b/internal/cmd/opensearch/instance/create/create_test.go @@ -275,7 +275,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { - cmd := NewCmd() + cmd := NewCmd(nil) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/opensearch/instance/delete/delete.go b/internal/cmd/opensearch/instance/delete/delete.go index 49b54f582..d786ae041 100644 --- a/internal/cmd/opensearch/instance/delete/delete.go +++ b/internal/cmd/opensearch/instance/delete/delete.go @@ -9,6 +9,7 @@ import ( "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" + "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/services/opensearch/client" opensearchUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/opensearch/utils" "github.com/stackitcloud/stackit-cli/internal/pkg/spinner" @@ -28,7 +29,7 @@ type inputModel struct { InstanceId string } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("delete %s", instanceIdArg), Short: "Deletes an OpenSearch instance", @@ -87,7 +88,7 @@ func NewCmd() *cobra.Command { if model.Async { operationState = "Triggered deletion of" } - cmd.Printf("%s instance %q\n", operationState, instanceLabel) + p.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 096f29217..4ef2fc8a8 100644 --- a/internal/cmd/opensearch/instance/delete/delete_test.go +++ b/internal/cmd/opensearch/instance/delete/delete_test.go @@ -136,7 +136,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { - cmd := NewCmd() + cmd := NewCmd(nil) 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 13e189ec8..20171b880 100644 --- a/internal/cmd/opensearch/instance/describe/describe.go +++ b/internal/cmd/opensearch/instance/describe/describe.go @@ -9,6 +9,7 @@ import ( "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" + "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/services/opensearch/client" "github.com/stackitcloud/stackit-cli/internal/pkg/tables" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -28,7 +29,7 @@ type inputModel struct { InstanceId string } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("describe %s", instanceIdArg), Short: "Shows details of an OpenSearch instance", @@ -61,7 +62,7 @@ func NewCmd() *cobra.Command { return fmt.Errorf("read OpenSearch instance: %w", err) } - return outputResult(cmd, model.OutputFormat, resp) + return outputResult(cmd, model.OutputFormat, resp, p) }, } return cmd @@ -86,7 +87,7 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *opensearch. return req } -func outputResult(cmd *cobra.Command, outputFormat string, instance *opensearch.Instance) error { +func outputResult(cmd *cobra.Command, outputFormat string, instance *opensearch.Instance, p *print.Printer) error { switch outputFormat { case globalflags.PrettyOutputFormat: table := tables.NewTable() @@ -119,7 +120,7 @@ func outputResult(cmd *cobra.Command, outputFormat string, instance *opensearch. if err != nil { return fmt.Errorf("marshal OpenSearch instance: %w", err) } - cmd.Println(string(details)) + p.Outputln(string(details)) return nil } diff --git a/internal/cmd/opensearch/instance/describe/describe_test.go b/internal/cmd/opensearch/instance/describe/describe_test.go index 2dfc8385e..f93c14d58 100644 --- a/internal/cmd/opensearch/instance/describe/describe_test.go +++ b/internal/cmd/opensearch/instance/describe/describe_test.go @@ -136,7 +136,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { - cmd := NewCmd() + cmd := NewCmd(nil) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/opensearch/instance/instance.go b/internal/cmd/opensearch/instance/instance.go index 790526952..649d439ce 100644 --- a/internal/cmd/opensearch/instance/instance.go +++ b/internal/cmd/opensearch/instance/instance.go @@ -7,12 +7,13 @@ import ( "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/pkg/args" + "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" "github.com/spf13/cobra" ) -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: "instance", Short: "Provides functionality for OpenSearch instances", @@ -20,14 +21,14 @@ func NewCmd() *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd) + addSubcommands(cmd, p) return cmd } -func addSubcommands(cmd *cobra.Command) { - cmd.AddCommand(create.NewCmd()) - cmd.AddCommand(delete.NewCmd()) - cmd.AddCommand(describe.NewCmd()) - cmd.AddCommand(list.NewCmd()) - cmd.AddCommand(update.NewCmd()) +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)) } diff --git a/internal/cmd/opensearch/instance/list/list.go b/internal/cmd/opensearch/instance/list/list.go index 67cb15d5e..95a468541 100644 --- a/internal/cmd/opensearch/instance/list/list.go +++ b/internal/cmd/opensearch/instance/list/list.go @@ -10,6 +10,7 @@ import ( "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/print" "github.com/stackitcloud/stackit-cli/internal/pkg/projectname" "github.com/stackitcloud/stackit-cli/internal/pkg/services/opensearch/client" "github.com/stackitcloud/stackit-cli/internal/pkg/tables" @@ -27,7 +28,7 @@ type inputModel struct { Limit *int64 } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: "list", Short: "Lists all OpenSearch instances", @@ -69,7 +70,7 @@ func NewCmd() *cobra.Command { if err != nil { projectLabel = model.ProjectId } - cmd.Printf("No instances found for project %q\n", projectLabel) + p.Info("No instances found for project %q\n", projectLabel) return nil } @@ -78,7 +79,7 @@ func NewCmd() *cobra.Command { instances = instances[:*model.Limit] } - return outputResult(cmd, model.OutputFormat, instances) + return outputResult(cmd, model.OutputFormat, instances, p) }, } @@ -115,14 +116,14 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *opensearch. return req } -func outputResult(cmd *cobra.Command, outputFormat string, instances []opensearch.Instance) error { +func outputResult(cmd *cobra.Command, outputFormat string, instances []opensearch.Instance, p *print.Printer) error { switch outputFormat { case globalflags.JSONOutputFormat: details, err := json.MarshalIndent(instances, "", " ") if err != nil { return fmt.Errorf("marshal OpenSearch instance list: %w", err) } - cmd.Println(string(details)) + p.Outputln(string(details)) return nil default: diff --git a/internal/cmd/opensearch/instance/update/update.go b/internal/cmd/opensearch/instance/update/update.go index f7eeba84a..fba6b527d 100644 --- a/internal/cmd/opensearch/instance/update/update.go +++ b/internal/cmd/opensearch/instance/update/update.go @@ -12,6 +12,7 @@ import ( "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/print" "github.com/stackitcloud/stackit-cli/internal/pkg/services/opensearch/client" opensearchUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/opensearch/utils" "github.com/stackitcloud/stackit-cli/internal/pkg/spinner" @@ -56,7 +57,7 @@ type inputModel struct { PlanId *string } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("update %s", instanceIdArg), Short: "Updates an OpenSearch instance", @@ -126,7 +127,7 @@ func NewCmd() *cobra.Command { if model.Async { operationState = "Triggered update of" } - cmd.Printf("%s instance %q\n", operationState, instanceLabel) + p.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 b012bacbd..628f898ea 100644 --- a/internal/cmd/opensearch/instance/update/update_test.go +++ b/internal/cmd/opensearch/instance/update/update_test.go @@ -292,7 +292,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { - cmd := NewCmd() + cmd := NewCmd(nil) 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 c432ea875..08103e63c 100644 --- a/internal/cmd/opensearch/opensearch.go +++ b/internal/cmd/opensearch/opensearch.go @@ -5,12 +5,13 @@ import ( "github.com/stackitcloud/stackit-cli/internal/cmd/opensearch/instance" "github.com/stackitcloud/stackit-cli/internal/cmd/opensearch/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() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: "opensearch", Short: "Provides functionality for OpenSearch", @@ -18,12 +19,12 @@ func NewCmd() *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd) + addSubcommands(cmd, p) return cmd } -func addSubcommands(cmd *cobra.Command) { - cmd.AddCommand(instance.NewCmd()) - cmd.AddCommand(plans.NewCmd()) - cmd.AddCommand(credentials.NewCmd()) +func addSubcommands(cmd *cobra.Command, p *print.Printer) { + cmd.AddCommand(instance.NewCmd(p)) + cmd.AddCommand(plans.NewCmd(p)) + cmd.AddCommand(credentials.NewCmd(p)) } diff --git a/internal/cmd/opensearch/plans/plans.go b/internal/cmd/opensearch/plans/plans.go index db7932d0b..c72df2c6a 100644 --- a/internal/cmd/opensearch/plans/plans.go +++ b/internal/cmd/opensearch/plans/plans.go @@ -10,6 +10,7 @@ import ( "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/print" "github.com/stackitcloud/stackit-cli/internal/pkg/projectname" "github.com/stackitcloud/stackit-cli/internal/pkg/services/opensearch/client" "github.com/stackitcloud/stackit-cli/internal/pkg/tables" @@ -27,7 +28,7 @@ type inputModel struct { Limit *int64 } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: "plans", Short: "Lists all OpenSearch service plans", @@ -69,7 +70,7 @@ func NewCmd() *cobra.Command { if err != nil { projectLabel = model.ProjectId } - cmd.Printf("No plans found for project %q\n", projectLabel) + p.Info("No plans found for project %q\n", projectLabel) return nil } @@ -78,7 +79,7 @@ func NewCmd() *cobra.Command { plans = plans[:*model.Limit] } - return outputResult(cmd, model.OutputFormat, plans) + return outputResult(cmd, model.OutputFormat, plans, p) }, } @@ -115,14 +116,14 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *opensearch. return req } -func outputResult(cmd *cobra.Command, outputFormat string, plans []opensearch.Offering) error { +func outputResult(cmd *cobra.Command, outputFormat string, plans []opensearch.Offering, p *print.Printer) error { switch outputFormat { case globalflags.JSONOutputFormat: details, err := json.MarshalIndent(plans, "", " ") if err != nil { return fmt.Errorf("marshal OpenSearch plans: %w", err) } - cmd.Println(string(details)) + p.Outputln(string(details)) return nil default: @@ -131,8 +132,8 @@ func outputResult(cmd *cobra.Command, outputFormat string, plans []opensearch.Of for i := range plans { o := plans[i] for j := range *o.Plans { - p := (*o.Plans)[j] - table.AddRow(*o.Name, *p.Id, *p.Name, *p.Description) + plan := (*o.Plans)[j] + table.AddRow(*o.Name, *plan.Id, *plan.Name, *plan.Description) } table.AddSeparator() } diff --git a/internal/cmd/organization/member/add/add.go b/internal/cmd/organization/member/add/add.go index bed1c53fb..daae1d15a 100644 --- a/internal/cmd/organization/member/add/add.go +++ b/internal/cmd/organization/member/add/add.go @@ -9,6 +9,7 @@ import ( "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/print" "github.com/stackitcloud/stackit-cli/internal/pkg/services/authorization/client" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -33,7 +34,7 @@ type inputModel struct { Role *string } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("add %s", subjectArg), Short: "Adds a member to an organization", @@ -78,7 +79,7 @@ func NewCmd() *cobra.Command { return fmt.Errorf("add member: %w", err) } - cmd.Println("Member added") + p.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 cd05dca09..dae68d183 100644 --- a/internal/cmd/organization/member/add/add_test.go +++ b/internal/cmd/organization/member/add/add_test.go @@ -123,7 +123,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { - cmd := NewCmd() + cmd := NewCmd(nil) 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 f44a60180..285a07a99 100644 --- a/internal/cmd/organization/member/list/list.go +++ b/internal/cmd/organization/member/list/list.go @@ -11,6 +11,7 @@ import ( "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/print" "github.com/stackitcloud/stackit-cli/internal/pkg/services/authorization/client" "github.com/stackitcloud/stackit-cli/internal/pkg/tables" @@ -36,7 +37,7 @@ type inputModel struct { SortBy string } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: "list", Short: "Lists members of an organization", @@ -74,7 +75,7 @@ func NewCmd() *cobra.Command { } members := *resp.Members if len(members) == 0 { - cmd.Printf("No members found for organization with ID %q\n", *model.OrganizationId) + p.Info("No members found for organization with ID %q\n", *model.OrganizationId) return nil } @@ -83,7 +84,7 @@ func NewCmd() *cobra.Command { members = members[:*model.Limit] } - return outputResult(cmd, model, members) + return outputResult(cmd, model, members, p) }, } configureFlags(cmd) @@ -130,7 +131,7 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *authorizati return req } -func outputResult(cmd *cobra.Command, model *inputModel, members []authorization.Member) error { +func outputResult(cmd *cobra.Command, model *inputModel, members []authorization.Member, p *print.Printer) error { sortFn := func(i, j int) bool { switch model.SortBy { case "subject": @@ -150,7 +151,7 @@ func outputResult(cmd *cobra.Command, model *inputModel, members []authorization if err != nil { return fmt.Errorf("marshal members: %w", err) } - cmd.Println(string(details)) + p.Outputln(string(details)) return nil default: diff --git a/internal/cmd/organization/member/member.go b/internal/cmd/organization/member/member.go index 0d4737b64..fecdcfc1a 100644 --- a/internal/cmd/organization/member/member.go +++ b/internal/cmd/organization/member/member.go @@ -5,12 +5,13 @@ import ( "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/pkg/args" + "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" "github.com/spf13/cobra" ) -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: "member", Short: "Provides functionality regarding organization members", @@ -18,12 +19,12 @@ func NewCmd() *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd) + addSubcommands(cmd, p) return cmd } -func addSubcommands(cmd *cobra.Command) { - cmd.AddCommand(add.NewCmd()) - cmd.AddCommand(list.NewCmd()) - cmd.AddCommand(remove.NewCmd()) +func addSubcommands(cmd *cobra.Command, p *print.Printer) { + cmd.AddCommand(add.NewCmd(p)) + cmd.AddCommand(list.NewCmd(p)) + cmd.AddCommand(remove.NewCmd(p)) } diff --git a/internal/cmd/organization/member/remove/remove.go b/internal/cmd/organization/member/remove/remove.go index 194f79d8a..884137c91 100644 --- a/internal/cmd/organization/member/remove/remove.go +++ b/internal/cmd/organization/member/remove/remove.go @@ -9,6 +9,7 @@ import ( "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/print" "github.com/stackitcloud/stackit-cli/internal/pkg/services/authorization/client" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -35,7 +36,7 @@ type inputModel struct { Force bool } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("remove %s", subjectArg), Short: "Removes a member from an organization", @@ -84,7 +85,7 @@ func NewCmd() *cobra.Command { return fmt.Errorf("remove member: %w", err) } - cmd.Println("Member removed") + p.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 560b8276f..4befba92a 100644 --- a/internal/cmd/organization/member/remove/remove_test.go +++ b/internal/cmd/organization/member/remove/remove_test.go @@ -136,7 +136,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { - cmd := NewCmd() + cmd := NewCmd(nil) 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 179b4b37b..fb2b6ca57 100644 --- a/internal/cmd/organization/organization.go +++ b/internal/cmd/organization/organization.go @@ -6,12 +6,13 @@ 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/pkg/args" + "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" "github.com/spf13/cobra" ) -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: "organization", Short: "Provides functionality regarding organizations", @@ -22,11 +23,11 @@ func NewCmd() *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd) + addSubcommands(cmd, p) return cmd } -func addSubcommands(cmd *cobra.Command) { - cmd.AddCommand(member.NewCmd()) - cmd.AddCommand(role.NewCmd()) +func addSubcommands(cmd *cobra.Command, p *print.Printer) { + cmd.AddCommand(member.NewCmd(p)) + cmd.AddCommand(role.NewCmd(p)) } diff --git a/internal/cmd/organization/role/list/list.go b/internal/cmd/organization/role/list/list.go index 0cbca1621..9e4c40b62 100644 --- a/internal/cmd/organization/role/list/list.go +++ b/internal/cmd/organization/role/list/list.go @@ -10,6 +10,7 @@ import ( "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/print" "github.com/stackitcloud/stackit-cli/internal/pkg/services/authorization/client" "github.com/stackitcloud/stackit-cli/internal/pkg/tables" @@ -31,7 +32,7 @@ type inputModel struct { Limit *int64 } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: "list", Short: "Lists roles and permissions of an organization", @@ -69,7 +70,7 @@ func NewCmd() *cobra.Command { } roles := *resp.Roles if len(roles) == 0 { - cmd.Printf("No roles found for organization with ID %q\n", *model.OrganizationId) + p.Info("No roles found for organization with ID %q\n", *model.OrganizationId) return nil } @@ -78,7 +79,7 @@ func NewCmd() *cobra.Command { roles = roles[:*model.Limit] } - return outputRolesResult(cmd, model.OutputFormat, roles) + return outputRolesResult(cmd, model.OutputFormat, roles, p) }, } configureFlags(cmd) @@ -115,7 +116,7 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *authorizati return apiClient.ListRoles(ctx, organizationResourceType, *model.OrganizationId) } -func outputRolesResult(cmd *cobra.Command, outputFormat string, roles []authorization.Role) error { +func outputRolesResult(cmd *cobra.Command, outputFormat string, roles []authorization.Role, p *print.Printer) error { switch outputFormat { case globalflags.JSONOutputFormat: // Show details @@ -123,7 +124,7 @@ func outputRolesResult(cmd *cobra.Command, outputFormat string, roles []authoriz if err != nil { return fmt.Errorf("marshal roles: %w", err) } - cmd.Println(string(details)) + p.Outputln(string(details)) return nil default: diff --git a/internal/cmd/organization/role/role.go b/internal/cmd/organization/role/role.go index e35e2e3ef..41f87752a 100644 --- a/internal/cmd/organization/role/role.go +++ b/internal/cmd/organization/role/role.go @@ -3,12 +3,13 @@ package role import ( "github.com/stackitcloud/stackit-cli/internal/cmd/organization/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() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: "role", Short: "Provides functionality regarding organization roles", @@ -16,10 +17,10 @@ func NewCmd() *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd) + addSubcommands(cmd, p) return cmd } -func addSubcommands(cmd *cobra.Command) { - cmd.AddCommand(list.NewCmd()) +func addSubcommands(cmd *cobra.Command, p *print.Printer) { + cmd.AddCommand(list.NewCmd(p)) } diff --git a/internal/cmd/postgresflex/instance/clone/clone.go b/internal/cmd/postgresflex/instance/clone/clone.go index 3bea17ed3..d064cc27a 100644 --- a/internal/cmd/postgresflex/instance/clone/clone.go +++ b/internal/cmd/postgresflex/instance/clone/clone.go @@ -11,6 +11,7 @@ import ( "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/print" "github.com/stackitcloud/stackit-cli/internal/pkg/services/postgresflex/client" postgresflexUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/postgresflex/utils" "github.com/stackitcloud/stackit-cli/internal/pkg/spinner" @@ -39,7 +40,7 @@ type inputModel struct { RecoveryDate *string } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("clone %s", instanceIdArg), Short: "Clones a PostgreSQL Flex instance", @@ -111,7 +112,7 @@ func NewCmd() *cobra.Command { operationState = "Triggered cloning of" } - cmd.Printf("%s instance from instance %q. New Instance ID: %s\n", operationState, instanceLabel, instanceId) + p.Info("%s instance from instance %q. New Instance ID: %s\n", operationState, instanceLabel, instanceId) return nil }, } diff --git a/internal/cmd/postgresflex/instance/clone/clone_test.go b/internal/cmd/postgresflex/instance/clone/clone_test.go index 9d4e0557e..f84f42f40 100644 --- a/internal/cmd/postgresflex/instance/clone/clone_test.go +++ b/internal/cmd/postgresflex/instance/clone/clone_test.go @@ -298,7 +298,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { - cmd := NewCmd() + cmd := NewCmd(nil) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/postgresflex/instance/create/create.go b/internal/cmd/postgresflex/instance/create/create.go index a94a7513c..43a00d002 100644 --- a/internal/cmd/postgresflex/instance/create/create.go +++ b/internal/cmd/postgresflex/instance/create/create.go @@ -11,6 +11,7 @@ import ( "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/print" "github.com/stackitcloud/stackit-cli/internal/pkg/projectname" "github.com/stackitcloud/stackit-cli/internal/pkg/services/postgresflex/client" postgresflexUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/postgresflex/utils" @@ -55,7 +56,7 @@ type inputModel struct { Type *string } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: "create", Short: "Creates a PostgreSQL Flex instance", @@ -134,7 +135,7 @@ func NewCmd() *cobra.Command { if model.Async { operationState = "Triggered creation of" } - cmd.Printf("%s instance for project %q. Instance ID: %s\n", operationState, projectLabel, instanceId) + p.Outputf("%s instance for project %q. Instance ID: %s\n", operationState, projectLabel, instanceId) return nil }, } diff --git a/internal/cmd/postgresflex/instance/create/create_test.go b/internal/cmd/postgresflex/instance/create/create_test.go index 3370e58e7..69e348237 100644 --- a/internal/cmd/postgresflex/instance/create/create_test.go +++ b/internal/cmd/postgresflex/instance/create/create_test.go @@ -248,7 +248,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { - cmd := NewCmd() + cmd := NewCmd(nil) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/postgresflex/instance/delete/delete.go b/internal/cmd/postgresflex/instance/delete/delete.go index 2b5cae439..95f856479 100644 --- a/internal/cmd/postgresflex/instance/delete/delete.go +++ b/internal/cmd/postgresflex/instance/delete/delete.go @@ -9,6 +9,7 @@ import ( "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" + "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/services/postgresflex/client" postgresflexUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/postgresflex/utils" "github.com/stackitcloud/stackit-cli/internal/pkg/spinner" @@ -28,7 +29,7 @@ type inputModel struct { InstanceId string } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("delete %s", instanceIdArg), Short: "Deletes a PostgreSQL Flex instance", @@ -87,7 +88,7 @@ func NewCmd() *cobra.Command { if model.Async { operationState = "Triggered deletion of" } - cmd.Printf("%s instance %q\n", operationState, instanceLabel) + p.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 eab429ccf..c1dec961a 100644 --- a/internal/cmd/postgresflex/instance/delete/delete_test.go +++ b/internal/cmd/postgresflex/instance/delete/delete_test.go @@ -136,7 +136,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { - cmd := NewCmd() + cmd := NewCmd(nil) 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 bf280a125..f299bc4d2 100644 --- a/internal/cmd/postgresflex/instance/describe/describe.go +++ b/internal/cmd/postgresflex/instance/describe/describe.go @@ -10,6 +10,7 @@ import ( "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" + "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/services/postgresflex/client" postgresflexUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/postgresflex/utils" "github.com/stackitcloud/stackit-cli/internal/pkg/tables" @@ -30,7 +31,7 @@ type inputModel struct { InstanceId string } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("describe %s", instanceIdArg), Short: "Shows details of a PostgreSQL Flex instance", @@ -63,7 +64,7 @@ func NewCmd() *cobra.Command { return fmt.Errorf("read PostgreSQL Flex instance: %w", err) } - return outputResult(cmd, model.OutputFormat, resp.Item) + return outputResult(cmd, model.OutputFormat, resp.Item, p) }, } return cmd @@ -88,7 +89,7 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *postgresfle return req } -func outputResult(cmd *cobra.Command, outputFormat string, instance *postgresflex.Instance) error { +func outputResult(cmd *cobra.Command, outputFormat string, instance *postgresflex.Instance, p *print.Printer) error { switch outputFormat { case globalflags.PrettyOutputFormat: aclsArray := *instance.Acl.Items @@ -134,7 +135,7 @@ func outputResult(cmd *cobra.Command, outputFormat string, instance *postgresfle if err != nil { return fmt.Errorf("marshal PostgreSQL Flex instance: %w", err) } - cmd.Println(string(details)) + p.Outputln(string(details)) return nil } diff --git a/internal/cmd/postgresflex/instance/describe/describe_test.go b/internal/cmd/postgresflex/instance/describe/describe_test.go index 965a46f32..bcff29fa6 100644 --- a/internal/cmd/postgresflex/instance/describe/describe_test.go +++ b/internal/cmd/postgresflex/instance/describe/describe_test.go @@ -136,7 +136,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { - cmd := NewCmd() + cmd := NewCmd(nil) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/postgresflex/instance/instance.go b/internal/cmd/postgresflex/instance/instance.go index 0d16db1f8..4a005c13a 100644 --- a/internal/cmd/postgresflex/instance/instance.go +++ b/internal/cmd/postgresflex/instance/instance.go @@ -8,12 +8,13 @@ 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() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: "instance", Short: "Provides functionality for PostgreSQL Flex instances", @@ -21,15 +22,15 @@ func NewCmd() *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd) + addSubcommands(cmd, p) return cmd } -func addSubcommands(cmd *cobra.Command) { - cmd.AddCommand(list.NewCmd()) - cmd.AddCommand(create.NewCmd()) - cmd.AddCommand(describe.NewCmd()) - cmd.AddCommand(update.NewCmd()) - cmd.AddCommand(delete.NewCmd()) - cmd.AddCommand(clone.NewCmd()) +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)) } diff --git a/internal/cmd/postgresflex/instance/list/list.go b/internal/cmd/postgresflex/instance/list/list.go index 98ce881f4..b5c68912e 100644 --- a/internal/cmd/postgresflex/instance/list/list.go +++ b/internal/cmd/postgresflex/instance/list/list.go @@ -10,6 +10,7 @@ import ( "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/print" "github.com/stackitcloud/stackit-cli/internal/pkg/projectname" "github.com/stackitcloud/stackit-cli/internal/pkg/services/postgresflex/client" "github.com/stackitcloud/stackit-cli/internal/pkg/tables" @@ -29,7 +30,7 @@ type inputModel struct { Limit *int64 } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: "list", Short: "Lists all PostgreSQL Flex instances", @@ -70,7 +71,7 @@ func NewCmd() *cobra.Command { if err != nil { projectLabel = model.ProjectId } - cmd.Printf("No instances found for project %q\n", projectLabel) + p.Info("No instances found for project %q\n", projectLabel) return nil } instances := *resp.Items @@ -80,7 +81,7 @@ func NewCmd() *cobra.Command { instances = instances[:*model.Limit] } - return outputResult(cmd, model.OutputFormat, instances) + return outputResult(cmd, model.OutputFormat, instances, p) }, } @@ -117,14 +118,14 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *postgresfle return req } -func outputResult(cmd *cobra.Command, outputFormat string, instances []postgresflex.InstanceListInstance) error { +func outputResult(cmd *cobra.Command, outputFormat string, instances []postgresflex.InstanceListInstance, p *print.Printer) error { switch outputFormat { case globalflags.JSONOutputFormat: details, err := json.MarshalIndent(instances, "", " ") if err != nil { return fmt.Errorf("marshal PostgreSQL Flex instance list: %w", err) } - cmd.Println(string(details)) + p.Outputln(string(details)) return nil default: diff --git a/internal/cmd/postgresflex/instance/update/update.go b/internal/cmd/postgresflex/instance/update/update.go index 0a860ad71..7075c9dfe 100644 --- a/internal/cmd/postgresflex/instance/update/update.go +++ b/internal/cmd/postgresflex/instance/update/update.go @@ -11,6 +11,7 @@ import ( "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/print" "github.com/stackitcloud/stackit-cli/internal/pkg/services/postgresflex/client" postgresflexUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/postgresflex/utils" "github.com/stackitcloud/stackit-cli/internal/pkg/spinner" @@ -52,7 +53,7 @@ type inputModel struct { Type *string } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("update %s", instanceIdArg), Short: "Updates a PostgreSQL Flex instance", @@ -119,7 +120,7 @@ func NewCmd() *cobra.Command { if model.Async { operationState = "Triggered update of" } - cmd.Printf("%s instance %q\n", operationState, instanceLabel) + p.Info("%s instance %q\n", operationState, instanceLabel) return nil }, } diff --git a/internal/cmd/postgresflex/instance/update/update_test.go b/internal/cmd/postgresflex/instance/update/update_test.go index 3c56d055f..16f6d3769 100644 --- a/internal/cmd/postgresflex/instance/update/update_test.go +++ b/internal/cmd/postgresflex/instance/update/update_test.go @@ -278,7 +278,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { - cmd := NewCmd() + cmd := NewCmd(nil) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/postgresflex/options/options.go b/internal/cmd/postgresflex/options/options.go index 327521e5c..59dd9ea1b 100644 --- a/internal/cmd/postgresflex/options/options.go +++ b/internal/cmd/postgresflex/options/options.go @@ -10,6 +10,7 @@ import ( "github.com/stackitcloud/stackit-cli/internal/pkg/flags" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/pager" + "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/services/postgresflex/client" "github.com/stackitcloud/stackit-cli/internal/pkg/tables" @@ -44,7 +45,7 @@ type flavorStorages struct { Storages *postgresflex.ListStoragesResponse `json:"storages"` } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: "options", Short: "Lists PostgreSQL Flex options", @@ -75,7 +76,7 @@ func NewCmd() *cobra.Command { } // Call API - err = buildAndExecuteRequest(ctx, cmd, model, apiClient) + err = buildAndExecuteRequest(ctx, cmd, model, apiClient, p) if err != nil { return fmt.Errorf("get PostgreSQL Flex options: %w", err) } @@ -129,7 +130,7 @@ type postgresFlexOptionsClient interface { ListStoragesExecute(ctx context.Context, projectId, flavorId string) (*postgresflex.ListStoragesResponse, error) } -func buildAndExecuteRequest(ctx context.Context, cmd *cobra.Command, model *inputModel, apiClient postgresFlexOptionsClient) error { +func buildAndExecuteRequest(ctx context.Context, cmd *cobra.Command, model *inputModel, apiClient postgresFlexOptionsClient, p *print.Printer) error { var flavors *postgresflex.ListFlavorsResponse var versions *postgresflex.ListVersionsResponse var storages *postgresflex.ListStoragesResponse @@ -154,10 +155,10 @@ func buildAndExecuteRequest(ctx context.Context, cmd *cobra.Command, model *inpu } } - return outputResult(cmd, model, flavors, versions, storages) + return outputResult(cmd, model, flavors, versions, storages, p) } -func outputResult(cmd *cobra.Command, model *inputModel, flavors *postgresflex.ListFlavorsResponse, versions *postgresflex.ListVersionsResponse, storages *postgresflex.ListStoragesResponse) error { +func outputResult(cmd *cobra.Command, model *inputModel, flavors *postgresflex.ListFlavorsResponse, versions *postgresflex.ListVersionsResponse, storages *postgresflex.ListStoragesResponse, p *print.Printer) error { options := &options{} if flavors != nil { options.Flavors = flavors.Flavors @@ -178,7 +179,7 @@ func outputResult(cmd *cobra.Command, model *inputModel, flavors *postgresflex.L if err != nil { return fmt.Errorf("marshal PostgreSQL Flex options: %w", err) } - cmd.Println(string(details)) + p.Outputln(string(details)) return nil default: return outputResultAsTable(cmd, model, options) diff --git a/internal/cmd/postgresflex/options/options_test.go b/internal/cmd/postgresflex/options/options_test.go index 67c449c95..107a89f44 100644 --- a/internal/cmd/postgresflex/options/options_test.go +++ b/internal/cmd/postgresflex/options/options_test.go @@ -165,7 +165,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { - cmd := NewCmd() + cmd := NewCmd(nil) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) @@ -288,14 +288,14 @@ func TestBuildAndExecuteRequest(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { - cmd := NewCmd() + cmd := NewCmd(nil) client := &postgresFlexClientMocked{ listFlavorsFails: tt.listFlavorsFails, listVersionsFails: tt.listVersionsFails, listStoragesFails: tt.listStoragesFails, } - err := buildAndExecuteRequest(testCtx, cmd, tt.model, client) + err := buildAndExecuteRequest(testCtx, cmd, tt.model, client, nil) if err != nil && tt.isValid { t.Fatalf("error building and executing request: %v", err) } diff --git a/internal/cmd/postgresflex/postgresflex.go b/internal/cmd/postgresflex/postgresflex.go index 3f74e35c8..8dba63f4a 100644 --- a/internal/cmd/postgresflex/postgresflex.go +++ b/internal/cmd/postgresflex/postgresflex.go @@ -5,12 +5,13 @@ import ( "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() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: "postgresflex", Aliases: []string{"postgresqlflex"}, @@ -19,12 +20,12 @@ func NewCmd() *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd) + addSubcommands(cmd, p) return cmd } -func addSubcommands(cmd *cobra.Command) { - cmd.AddCommand(instance.NewCmd()) - cmd.AddCommand(user.NewCmd()) - cmd.AddCommand(options.NewCmd()) +func addSubcommands(cmd *cobra.Command, p *print.Printer) { + cmd.AddCommand(instance.NewCmd(p)) + cmd.AddCommand(user.NewCmd(p)) + cmd.AddCommand(options.NewCmd(p)) } diff --git a/internal/cmd/postgresflex/user/create/create.go b/internal/cmd/postgresflex/user/create/create.go index 07cbf6d8a..05cdabe47 100644 --- a/internal/cmd/postgresflex/user/create/create.go +++ b/internal/cmd/postgresflex/user/create/create.go @@ -10,6 +10,7 @@ import ( "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/print" "github.com/stackitcloud/stackit-cli/internal/pkg/services/postgresflex/client" postgresflexUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/postgresflex/utils" @@ -35,7 +36,7 @@ type inputModel struct { Roles *[]string } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: "create", Short: "Creates a PostgreSQL Flex user", @@ -88,13 +89,13 @@ func NewCmd() *cobra.Command { } user := resp.Item - cmd.Printf("Created user for instance %q. User ID: %s\n\n", instanceLabel, *user.Id) - cmd.Printf("Username: %s\n", *user.Username) - cmd.Printf("Password: %s\n", *user.Password) - cmd.Printf("Roles: %v\n", *user.Roles) - cmd.Printf("Host: %s\n", *user.Host) - cmd.Printf("Port: %d\n", *user.Port) - cmd.Printf("URI: %s\n", *user.Uri) + p.Outputf("Created user for instance %q. User ID: %s\n\n", instanceLabel, *user.Id) + p.Outputf("Username: %s\n", *user.Username) + p.Outputf("Password: %s\n", *user.Password) + p.Outputf("Roles: %v\n", *user.Roles) + p.Outputf("Host: %s\n", *user.Host) + p.Outputf("Port: %d\n", *user.Port) + p.Outputf("URI: %s\n", *user.Uri) return nil }, diff --git a/internal/cmd/postgresflex/user/delete/delete.go b/internal/cmd/postgresflex/user/delete/delete.go index 430747215..83af974cf 100644 --- a/internal/cmd/postgresflex/user/delete/delete.go +++ b/internal/cmd/postgresflex/user/delete/delete.go @@ -10,6 +10,7 @@ import ( "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/print" "github.com/stackitcloud/stackit-cli/internal/pkg/services/postgresflex/client" postgresflexUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/postgresflex/utils" @@ -30,7 +31,7 @@ type inputModel struct { UserId string } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("delete %s", userIdArg), Short: "Deletes a PostgreSQL Flex user", @@ -83,7 +84,7 @@ func NewCmd() *cobra.Command { return fmt.Errorf("delete PostgreSQL Flex user: %w", err) } - cmd.Printf("Deleted user %q of instance %q\n", userLabel, instanceLabel) + p.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 fc1a42e00..1a7bb7d94 100644 --- a/internal/cmd/postgresflex/user/delete/delete_test.go +++ b/internal/cmd/postgresflex/user/delete/delete_test.go @@ -157,7 +157,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { - cmd := NewCmd() + cmd := NewCmd(nil) 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 a24e432db..c4ae1f829 100644 --- a/internal/cmd/postgresflex/user/describe/describe.go +++ b/internal/cmd/postgresflex/user/describe/describe.go @@ -10,6 +10,7 @@ import ( "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/print" "github.com/stackitcloud/stackit-cli/internal/pkg/services/postgresflex/client" "github.com/stackitcloud/stackit-cli/internal/pkg/tables" @@ -30,7 +31,7 @@ type inputModel struct { UserId string } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("describe %s", userIdArg), Short: "Shows details of a PostgreSQL Flex user", @@ -68,7 +69,7 @@ func NewCmd() *cobra.Command { return fmt.Errorf("get MongoDB Flex user: %w", err) } - return outputResult(cmd, model.OutputFormat, *resp.Item) + return outputResult(cmd, model.OutputFormat, *resp.Item, p) }, } @@ -103,7 +104,7 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *postgresfle return req } -func outputResult(cmd *cobra.Command, outputFormat string, user postgresflex.UserResponse) error { +func outputResult(cmd *cobra.Command, outputFormat string, user postgresflex.UserResponse, p *print.Printer) error { switch outputFormat { case globalflags.PrettyOutputFormat: table := tables.NewTable() @@ -128,7 +129,7 @@ func outputResult(cmd *cobra.Command, outputFormat string, user postgresflex.Use if err != nil { return fmt.Errorf("marshal MongoDB Flex user: %w", err) } - cmd.Println(string(details)) + p.Outputln(string(details)) return nil } diff --git a/internal/cmd/postgresflex/user/describe/describe_test.go b/internal/cmd/postgresflex/user/describe/describe_test.go index adc47ba07..4bc121ee7 100644 --- a/internal/cmd/postgresflex/user/describe/describe_test.go +++ b/internal/cmd/postgresflex/user/describe/describe_test.go @@ -157,7 +157,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { - cmd := NewCmd() + cmd := NewCmd(nil) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/postgresflex/user/list/list.go b/internal/cmd/postgresflex/user/list/list.go index cb0ecff20..adbd4fbba 100644 --- a/internal/cmd/postgresflex/user/list/list.go +++ b/internal/cmd/postgresflex/user/list/list.go @@ -10,6 +10,7 @@ import ( "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/print" "github.com/stackitcloud/stackit-cli/internal/pkg/services/postgresflex/client" postgresflexUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/postgresflex/utils" "github.com/stackitcloud/stackit-cli/internal/pkg/tables" @@ -30,7 +31,7 @@ type inputModel struct { Limit *int64 } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: "list", Short: "Lists all PostgreSQL Flex users of an instance", @@ -71,7 +72,7 @@ func NewCmd() *cobra.Command { if err != nil { instanceLabel = *model.InstanceId } - cmd.Printf("No users found for instance %q\n", instanceLabel) + p.Info("No users found for instance %q\n", instanceLabel) return nil } users := *resp.Items @@ -81,7 +82,7 @@ func NewCmd() *cobra.Command { users = users[:*model.Limit] } - return outputResult(cmd, model.OutputFormat, users) + return outputResult(cmd, model.OutputFormat, users, p) }, } @@ -123,14 +124,14 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *postgresfle return req } -func outputResult(cmd *cobra.Command, outputFormat string, users []postgresflex.ListUsersResponseItem) error { +func outputResult(cmd *cobra.Command, outputFormat string, users []postgresflex.ListUsersResponseItem, p *print.Printer) error { switch outputFormat { case globalflags.JSONOutputFormat: details, err := json.MarshalIndent(users, "", " ") if err != nil { return fmt.Errorf("marshal PostgreSQL Flex user list: %w", err) } - cmd.Println(string(details)) + p.Outputln(string(details)) return nil default: diff --git a/internal/cmd/postgresflex/user/reset-password/reset_password.go b/internal/cmd/postgresflex/user/reset-password/reset_password.go index b8754e56c..48cba14be 100644 --- a/internal/cmd/postgresflex/user/reset-password/reset_password.go +++ b/internal/cmd/postgresflex/user/reset-password/reset_password.go @@ -10,6 +10,7 @@ import ( "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/print" "github.com/stackitcloud/stackit-cli/internal/pkg/services/postgresflex/client" postgresflexUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/postgresflex/utils" @@ -30,7 +31,7 @@ type inputModel struct { UserId string } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("reset-password %s", userIdArg), Short: "Resets the password of a PostgreSQL Flex user", @@ -82,10 +83,10 @@ func NewCmd() *cobra.Command { return fmt.Errorf("reset PostgreSQL Flex user password: %w", err) } - cmd.Printf("Reset password for user %q of instance %q\n\n", userLabel, instanceLabel) - cmd.Printf("Username: %s\n", *user.Item.Username) - cmd.Printf("New password: %s\n", *user.Item.Password) - cmd.Printf("New URI: %s\n", *user.Item.Uri) + p.Outputf("Reset password for user %q of instance %q\n\n", userLabel, instanceLabel) + p.Outputf("Username: %s\n", *user.Item.Username) + p.Outputf("New password: %s\n", *user.Item.Password) + p.Outputf("New URI: %s\n", *user.Item.Uri) return nil }, } 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 954982534..b312e5867 100644 --- a/internal/cmd/postgresflex/user/reset-password/reset_password_test.go +++ b/internal/cmd/postgresflex/user/reset-password/reset_password_test.go @@ -157,7 +157,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { - cmd := NewCmd() + cmd := NewCmd(nil) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/postgresflex/user/update/update.go b/internal/cmd/postgresflex/user/update/update.go index 560531627..9d2f7454c 100644 --- a/internal/cmd/postgresflex/user/update/update.go +++ b/internal/cmd/postgresflex/user/update/update.go @@ -10,6 +10,7 @@ import ( "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/print" "github.com/stackitcloud/stackit-cli/internal/pkg/services/postgresflex/client" postgresflexUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/postgresflex/utils" @@ -32,7 +33,7 @@ type inputModel struct { Roles *[]string } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("update %s", userIdArg), Short: "Updates a PostgreSQL Flex user", @@ -81,7 +82,7 @@ func NewCmd() *cobra.Command { return fmt.Errorf("update PostgreSQL Flex user: %w", err) } - cmd.Printf("Updated user %q of instance %q\n", userLabel, instanceLabel) + p.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 d337ac32c..7f5f50092 100644 --- a/internal/cmd/postgresflex/user/update/update_test.go +++ b/internal/cmd/postgresflex/user/update/update_test.go @@ -166,7 +166,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { - cmd := NewCmd() + cmd := NewCmd(nil) 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 df90fac71..018244197 100644 --- a/internal/cmd/postgresflex/user/user.go +++ b/internal/cmd/postgresflex/user/user.go @@ -8,12 +8,13 @@ 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() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: "user", Short: "Provides functionality for PostgreSQL Flex users", @@ -21,15 +22,15 @@ func NewCmd() *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd) + addSubcommands(cmd, p) return cmd } -func addSubcommands(cmd *cobra.Command) { - cmd.AddCommand(create.NewCmd()) - cmd.AddCommand(list.NewCmd()) - cmd.AddCommand(describe.NewCmd()) - cmd.AddCommand(update.NewCmd()) - cmd.AddCommand(delete.NewCmd()) - cmd.AddCommand(resetpassword.NewCmd()) +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)) } diff --git a/internal/cmd/project/create/create.go b/internal/cmd/project/create/create.go index 47e17f009..004ed8cc7 100644 --- a/internal/cmd/project/create/create.go +++ b/internal/cmd/project/create/create.go @@ -12,6 +12,7 @@ import ( "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/print" "github.com/stackitcloud/stackit-cli/internal/pkg/services/resourcemanager/client" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -36,7 +37,7 @@ type inputModel struct { Labels *map[string]string } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: "create", Short: "Creates a STACKIT project", @@ -81,7 +82,7 @@ func NewCmd() *cobra.Command { return fmt.Errorf("create project: %w", err) } - cmd.Printf("Created project under the parent with ID %q. Project ID: %s\n", *model.ParentId, *resp.ProjectId) + p.Outputf("Created project under the parent with ID %q. Project ID: %s\n", *model.ParentId, *resp.ProjectId) return nil }, } diff --git a/internal/cmd/project/create/create_test.go b/internal/cmd/project/create/create_test.go index 15b495339..b7e051328 100644 --- a/internal/cmd/project/create/create_test.go +++ b/internal/cmd/project/create/create_test.go @@ -139,7 +139,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { - cmd := NewCmd() + cmd := NewCmd(nil) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/project/describe/describe.go b/internal/cmd/project/describe/describe.go index f6ca653ef..e57aa9aef 100644 --- a/internal/cmd/project/describe/describe.go +++ b/internal/cmd/project/describe/describe.go @@ -10,6 +10,7 @@ import ( "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/print" "github.com/stackitcloud/stackit-cli/internal/pkg/services/resourcemanager/client" "github.com/stackitcloud/stackit-cli/internal/pkg/tables" @@ -26,7 +27,7 @@ type inputModel struct { IncludeParents bool } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: "describe", Short: "Shows details of a STACKIT project", @@ -63,7 +64,7 @@ func NewCmd() *cobra.Command { return fmt.Errorf("read project details: %w", err) } - return outputResult(cmd, model.OutputFormat, resp) + return outputResult(cmd, model.OutputFormat, resp, p) }, } configureFlags(cmd) @@ -92,7 +93,7 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *resourceman return req } -func outputResult(cmd *cobra.Command, outputFormat string, project *resourcemanager.ProjectResponseWithParents) error { +func outputResult(cmd *cobra.Command, outputFormat string, project *resourcemanager.ProjectResponseWithParents, p *print.Printer) error { switch outputFormat { case globalflags.PrettyOutputFormat: table := tables.NewTable() @@ -116,7 +117,7 @@ func outputResult(cmd *cobra.Command, outputFormat string, project *resourcemana if err != nil { return fmt.Errorf("marshal project details: %w", err) } - cmd.Println(string(details)) + p.Outputln(string(details)) return nil } diff --git a/internal/cmd/project/describe/describe_test.go b/internal/cmd/project/describe/describe_test.go index 51513d8c9..61979b130 100644 --- a/internal/cmd/project/describe/describe_test.go +++ b/internal/cmd/project/describe/describe_test.go @@ -97,7 +97,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { - cmd := NewCmd() + cmd := NewCmd(nil) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/project/list/list.go b/internal/cmd/project/list/list.go index 6743cd337..3996e9e57 100644 --- a/internal/cmd/project/list/list.go +++ b/internal/cmd/project/list/list.go @@ -11,6 +11,7 @@ import ( "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/print" "github.com/stackitcloud/stackit-cli/internal/pkg/services/resourcemanager/client" "github.com/stackitcloud/stackit-cli/internal/pkg/tables" @@ -40,7 +41,7 @@ type inputModel struct { PageSize int64 } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: "list", Short: "Lists STACKIT projects", @@ -76,11 +77,11 @@ func NewCmd() *cobra.Command { return err } if len(projects) == 0 { - cmd.Print("No projects found matching the criteria\n") + p.Info("No projects found matching the criteria\n") return nil } - return outputResult(cmd, model.OutputFormat, projects) + return outputResult(cmd, model.OutputFormat, projects, p) }, } configureFlags(cmd) @@ -194,14 +195,14 @@ func fetchProjects(ctx context.Context, model *inputModel, apiClient resourceMan return projects, nil } -func outputResult(cmd *cobra.Command, outputFormat string, projects []resourcemanager.ProjectResponse) error { +func outputResult(cmd *cobra.Command, outputFormat string, projects []resourcemanager.ProjectResponse, p *print.Printer) error { switch outputFormat { case globalflags.JSONOutputFormat: details, err := json.MarshalIndent(projects, "", " ") if err != nil { return fmt.Errorf("marshal projects list: %w", err) } - cmd.Println(string(details)) + p.Outputln(string(details)) return nil default: diff --git a/internal/cmd/project/list/list_test.go b/internal/cmd/project/list/list_test.go index dddd312c1..3d505ac58 100644 --- a/internal/cmd/project/list/list_test.go +++ b/internal/cmd/project/list/list_test.go @@ -194,7 +194,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { - cmd := NewCmd() + cmd := NewCmd(nil) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/project/member/add/add.go b/internal/cmd/project/member/add/add.go index 5618daaf3..2272cc498 100644 --- a/internal/cmd/project/member/add/add.go +++ b/internal/cmd/project/member/add/add.go @@ -10,6 +10,7 @@ import ( "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/print" "github.com/stackitcloud/stackit-cli/internal/pkg/projectname" "github.com/stackitcloud/stackit-cli/internal/pkg/services/authorization/client" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -33,7 +34,7 @@ type inputModel struct { Role *string } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("add %s", subjectArg), Short: "Adds a member to a project", @@ -83,7 +84,7 @@ func NewCmd() *cobra.Command { return fmt.Errorf("add member: %w", err) } - cmd.Printf("Added the role %q to %s on project %q\n", *model.Role, model.Subject, projectLabel) + p.Info("Added the role %q to %s on project %q\n", *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 5499c9ff2..e59825633 100644 --- a/internal/cmd/project/member/add/add_test.go +++ b/internal/cmd/project/member/add/add_test.go @@ -122,7 +122,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { - cmd := NewCmd() + cmd := NewCmd(nil) 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 1979785f2..439b85af4 100644 --- a/internal/cmd/project/member/list/list.go +++ b/internal/cmd/project/member/list/list.go @@ -11,6 +11,7 @@ import ( "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/print" "github.com/stackitcloud/stackit-cli/internal/pkg/projectname" "github.com/stackitcloud/stackit-cli/internal/pkg/services/authorization/client" "github.com/stackitcloud/stackit-cli/internal/pkg/tables" @@ -35,7 +36,7 @@ type inputModel struct { SortBy string } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: "list", Short: "Lists members of a project", @@ -77,7 +78,7 @@ func NewCmd() *cobra.Command { if err != nil { projectLabel = model.ProjectId } - cmd.Printf("No members found for project %q\n", projectLabel) + p.Info("No members found for project %q\n", projectLabel) return nil } @@ -86,7 +87,7 @@ func NewCmd() *cobra.Command { members = members[:*model.Limit] } - return outputResult(cmd, model, members) + return outputResult(cmd, model, members, p) }, } configureFlags(cmd) @@ -131,7 +132,7 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *authorizati return req } -func outputResult(cmd *cobra.Command, model *inputModel, members []authorization.Member) error { +func outputResult(cmd *cobra.Command, model *inputModel, members []authorization.Member, p *print.Printer) error { sortFn := func(i, j int) bool { switch model.SortBy { case "subject": @@ -151,7 +152,7 @@ func outputResult(cmd *cobra.Command, model *inputModel, members []authorization if err != nil { return fmt.Errorf("marshal members: %w", err) } - cmd.Println(string(details)) + p.Outputln(string(details)) return nil default: diff --git a/internal/cmd/project/member/member.go b/internal/cmd/project/member/member.go index 9791c3153..38340c1f2 100644 --- a/internal/cmd/project/member/member.go +++ b/internal/cmd/project/member/member.go @@ -5,12 +5,13 @@ import ( "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() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: "member", Short: "Provides functionality regarding project members", @@ -18,12 +19,12 @@ func NewCmd() *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd) + addSubcommands(cmd, p) return cmd } -func addSubcommands(cmd *cobra.Command) { - cmd.AddCommand(add.NewCmd()) - cmd.AddCommand(list.NewCmd()) - cmd.AddCommand(remove.NewCmd()) +func addSubcommands(cmd *cobra.Command, p *print.Printer) { + cmd.AddCommand(add.NewCmd(p)) + cmd.AddCommand(list.NewCmd(p)) + cmd.AddCommand(remove.NewCmd(p)) } diff --git a/internal/cmd/project/member/remove/remove.go b/internal/cmd/project/member/remove/remove.go index fc8d3498f..b52eadce2 100644 --- a/internal/cmd/project/member/remove/remove.go +++ b/internal/cmd/project/member/remove/remove.go @@ -10,6 +10,7 @@ import ( "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/print" "github.com/stackitcloud/stackit-cli/internal/pkg/projectname" "github.com/stackitcloud/stackit-cli/internal/pkg/services/authorization/client" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -35,7 +36,7 @@ type inputModel struct { Force bool } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("remove %s", subjectArg), Short: "Removes a member from a project", @@ -89,7 +90,7 @@ func NewCmd() *cobra.Command { return fmt.Errorf("remove member: %w", err) } - cmd.Printf("Removed the role %q from %s on project %q\n", *model.Role, model.Subject, projectLabel) + p.Info("Removed the role %q from %s on project %q\n", *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 f4faf6bc7..c39df7e82 100644 --- a/internal/cmd/project/member/remove/remove_test.go +++ b/internal/cmd/project/member/remove/remove_test.go @@ -135,7 +135,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { - cmd := NewCmd() + cmd := NewCmd(nil) 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 5cfabe4fa..b4cf79228 100644 --- a/internal/cmd/project/project.go +++ b/internal/cmd/project/project.go @@ -33,11 +33,11 @@ func NewCmd(p *print.Printer) *cobra.Command { } func addSubcommands(cmd *cobra.Command, p *print.Printer) { - cmd.AddCommand(create.NewCmd()) - cmd.AddCommand(update.NewCmd()) + cmd.AddCommand(create.NewCmd(p)) + cmd.AddCommand(update.NewCmd(p)) cmd.AddCommand(delete.NewCmd(p)) - cmd.AddCommand(describe.NewCmd()) - cmd.AddCommand(list.NewCmd()) - cmd.AddCommand(member.NewCmd()) - cmd.AddCommand(role.NewCmd()) + cmd.AddCommand(describe.NewCmd(p)) + cmd.AddCommand(list.NewCmd(p)) + cmd.AddCommand(member.NewCmd(p)) + cmd.AddCommand(role.NewCmd(p)) } diff --git a/internal/cmd/project/role/list/list.go b/internal/cmd/project/role/list/list.go index 3dbc651a6..c6ad17f0e 100644 --- a/internal/cmd/project/role/list/list.go +++ b/internal/cmd/project/role/list/list.go @@ -10,6 +10,7 @@ import ( "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/print" "github.com/stackitcloud/stackit-cli/internal/pkg/projectname" "github.com/stackitcloud/stackit-cli/internal/pkg/services/authorization/client" "github.com/stackitcloud/stackit-cli/internal/pkg/tables" @@ -30,7 +31,7 @@ type inputModel struct { Limit *int64 } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: "list", Short: "Lists roles and permissions of a project", @@ -72,7 +73,7 @@ func NewCmd() *cobra.Command { if err != nil { projectLabel = model.ProjectId } - cmd.Printf("No roles found for project %q\n", projectLabel) + p.Info("No roles found for project %q\n", projectLabel) return nil } @@ -81,7 +82,7 @@ func NewCmd() *cobra.Command { roles = roles[:*model.Limit] } - return outputRolesResult(cmd, model.OutputFormat, roles) + return outputRolesResult(cmd, model.OutputFormat, roles, p) }, } configureFlags(cmd) @@ -116,7 +117,7 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *authorizati return apiClient.ListRoles(ctx, projectResourceType, model.GlobalFlagModel.ProjectId) } -func outputRolesResult(cmd *cobra.Command, outputFormat string, roles []authorization.Role) error { +func outputRolesResult(cmd *cobra.Command, outputFormat string, roles []authorization.Role, p *print.Printer) error { switch outputFormat { case globalflags.JSONOutputFormat: // Show details @@ -124,7 +125,7 @@ func outputRolesResult(cmd *cobra.Command, outputFormat string, roles []authoriz if err != nil { return fmt.Errorf("marshal roles: %w", err) } - cmd.Println(string(details)) + p.Outputln(string(details)) return nil default: diff --git a/internal/cmd/project/role/role.go b/internal/cmd/project/role/role.go index 90780c604..c32d3d224 100644 --- a/internal/cmd/project/role/role.go +++ b/internal/cmd/project/role/role.go @@ -3,12 +3,13 @@ package role import ( "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() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: "role", Short: "Provides functionality regarding project roles", @@ -16,10 +17,10 @@ func NewCmd() *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd) + addSubcommands(cmd, p) return cmd } -func addSubcommands(cmd *cobra.Command) { - cmd.AddCommand(list.NewCmd()) +func addSubcommands(cmd *cobra.Command, p *print.Printer) { + cmd.AddCommand(list.NewCmd(p)) } diff --git a/internal/cmd/project/update/update.go b/internal/cmd/project/update/update.go index e28aae98b..b14db39df 100644 --- a/internal/cmd/project/update/update.go +++ b/internal/cmd/project/update/update.go @@ -11,6 +11,7 @@ import ( "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/print" "github.com/stackitcloud/stackit-cli/internal/pkg/projectname" "github.com/stackitcloud/stackit-cli/internal/pkg/services/resourcemanager/client" @@ -35,7 +36,7 @@ type inputModel struct { Labels *map[string]string } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: "update", Short: "Updates a STACKIT project", @@ -86,7 +87,7 @@ func NewCmd() *cobra.Command { return fmt.Errorf("update project: %w", err) } - cmd.Printf("Updated project %q\n", projectLabel) + p.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 57cf25bc6..7a7f0454e 100644 --- a/internal/cmd/project/update/update_test.go +++ b/internal/cmd/project/update/update_test.go @@ -129,7 +129,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { - cmd := NewCmd() + cmd := NewCmd(nil) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/rabbitmq/credentials/create/create.go b/internal/cmd/rabbitmq/credentials/create/create.go index 71e1bf2b0..65659edc6 100644 --- a/internal/cmd/rabbitmq/credentials/create/create.go +++ b/internal/cmd/rabbitmq/credentials/create/create.go @@ -10,6 +10,7 @@ import ( "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/print" "github.com/stackitcloud/stackit-cli/internal/pkg/services/rabbitmq/client" rabbitmqUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/rabbitmq/utils" @@ -28,7 +29,7 @@ type inputModel struct { HidePassword bool } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: "create", Short: "Creates credentials for a RabbitMQ instance", @@ -75,20 +76,20 @@ func NewCmd() *cobra.Command { return fmt.Errorf("create RabbitMQ credentials: %w", err) } - cmd.Printf("Created credentials for instance %q. Credentials ID: %s\n\n", instanceLabel, *resp.Id) + p.Outputf("Created credentials for instance %q. Credentials ID: %s\n\n", instanceLabel, *resp.Id) // The username field cannot be set by the user so we only display it if it's not returned empty username := *resp.Raw.Credentials.Username if username != "" { - cmd.Printf("Username: %s\n", *resp.Raw.Credentials.Username) + p.Outputf("Username: %s\n", *resp.Raw.Credentials.Username) } if model.HidePassword { - cmd.Printf("Password: \n") + p.Outputf("Password: \n") } else { - cmd.Printf("Password: %s\n", *resp.Raw.Credentials.Password) + p.Outputf("Password: %s\n", *resp.Raw.Credentials.Password) } - cmd.Printf("Host: %s\n", *resp.Raw.Credentials.Host) - cmd.Printf("Port: %d\n", *resp.Raw.Credentials.Port) - cmd.Printf("URI: %s\n", *resp.Uri) + p.Outputf("Host: %s\n", *resp.Raw.Credentials.Host) + p.Outputf("Port: %d\n", *resp.Raw.Credentials.Port) + p.Outputf("URI: %s\n", *resp.Uri) return nil }, } diff --git a/internal/cmd/rabbitmq/credentials/create/create_test.go b/internal/cmd/rabbitmq/credentials/create/create_test.go index 124687871..c68535494 100644 --- a/internal/cmd/rabbitmq/credentials/create/create_test.go +++ b/internal/cmd/rabbitmq/credentials/create/create_test.go @@ -118,7 +118,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { - cmd := NewCmd() + cmd := NewCmd(nil) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/rabbitmq/credentials/credentials.go b/internal/cmd/rabbitmq/credentials/credentials.go index e6fd55a7e..38ec2c552 100644 --- a/internal/cmd/rabbitmq/credentials/credentials.go +++ b/internal/cmd/rabbitmq/credentials/credentials.go @@ -6,12 +6,13 @@ import ( "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() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: "credentials", Short: "Provides functionality for RabbitMQ credentials", @@ -19,13 +20,13 @@ func NewCmd() *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd) + addSubcommands(cmd, p) return cmd } -func addSubcommands(cmd *cobra.Command) { - cmd.AddCommand(create.NewCmd()) - cmd.AddCommand(delete.NewCmd()) - cmd.AddCommand(describe.NewCmd()) - cmd.AddCommand(list.NewCmd()) +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)) } diff --git a/internal/cmd/rabbitmq/credentials/delete/delete.go b/internal/cmd/rabbitmq/credentials/delete/delete.go index 6a466de9e..4b004b0f5 100644 --- a/internal/cmd/rabbitmq/credentials/delete/delete.go +++ b/internal/cmd/rabbitmq/credentials/delete/delete.go @@ -10,6 +10,7 @@ import ( "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/print" "github.com/stackitcloud/stackit-cli/internal/pkg/services/rabbitmq/client" rabbitmqUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/rabbitmq/utils" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -30,7 +31,7 @@ type inputModel struct { CredentialsId string } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("delete %s", credentialsIdArg), Short: "Deletes credentials of a RabbitMQ instance", @@ -79,7 +80,7 @@ func NewCmd() *cobra.Command { return fmt.Errorf("delete RabbitMQ credentials: %w", err) } - cmd.Printf("Deleted credentials %s of instance %q\n", credentialsLabel, instanceLabel) + p.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 19b572f82..ecf10d176 100644 --- a/internal/cmd/rabbitmq/credentials/delete/delete_test.go +++ b/internal/cmd/rabbitmq/credentials/delete/delete_test.go @@ -163,7 +163,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { - cmd := NewCmd() + cmd := NewCmd(nil) 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 136c78de8..11c014f97 100644 --- a/internal/cmd/rabbitmq/credentials/describe/describe.go +++ b/internal/cmd/rabbitmq/credentials/describe/describe.go @@ -10,6 +10,7 @@ import ( "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/print" "github.com/stackitcloud/stackit-cli/internal/pkg/services/rabbitmq/client" "github.com/stackitcloud/stackit-cli/internal/pkg/tables" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -30,7 +31,7 @@ type inputModel struct { CredentialsId string } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("describe %s", credentialsIdArg), Short: "Shows details of credentials of a RabbitMQ instance", @@ -64,7 +65,7 @@ func NewCmd() *cobra.Command { return fmt.Errorf("describe RabbitMQ credentials: %w", err) } - return outputResult(cmd, model.OutputFormat, resp) + return outputResult(cmd, model.OutputFormat, resp, p) }, } configureFlags(cmd) @@ -98,7 +99,7 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *rabbitmq.AP return req } -func outputResult(cmd *cobra.Command, outputFormat string, credentials *rabbitmq.CredentialsResponse) error { +func outputResult(cmd *cobra.Command, outputFormat string, credentials *rabbitmq.CredentialsResponse, p *print.Printer) error { switch outputFormat { case globalflags.PrettyOutputFormat: table := tables.NewTable() @@ -124,7 +125,7 @@ func outputResult(cmd *cobra.Command, outputFormat string, credentials *rabbitmq if err != nil { return fmt.Errorf("marshal RabbitMQ credentials: %w", err) } - cmd.Println(string(details)) + p.Outputln(string(details)) return nil } diff --git a/internal/cmd/rabbitmq/credentials/describe/describe_test.go b/internal/cmd/rabbitmq/credentials/describe/describe_test.go index b2be161f7..b3bc93579 100644 --- a/internal/cmd/rabbitmq/credentials/describe/describe_test.go +++ b/internal/cmd/rabbitmq/credentials/describe/describe_test.go @@ -163,7 +163,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { - cmd := NewCmd() + cmd := NewCmd(nil) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/rabbitmq/credentials/list/list.go b/internal/cmd/rabbitmq/credentials/list/list.go index 0c53dad36..01e49e506 100644 --- a/internal/cmd/rabbitmq/credentials/list/list.go +++ b/internal/cmd/rabbitmq/credentials/list/list.go @@ -10,6 +10,7 @@ import ( "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/print" "github.com/stackitcloud/stackit-cli/internal/pkg/services/rabbitmq/client" rabbitmqUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/rabbitmq/utils" "github.com/stackitcloud/stackit-cli/internal/pkg/tables" @@ -29,7 +30,7 @@ type inputModel struct { Limit *int64 } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: "list", Short: "Lists all credentials' IDs for a RabbitMQ instance", @@ -71,7 +72,7 @@ func NewCmd() *cobra.Command { if err != nil { instanceLabel = model.InstanceId } - cmd.Printf("No credentials found for instance %q\n", instanceLabel) + p.Info("No credentials found for instance %q\n", instanceLabel) return nil } @@ -79,7 +80,7 @@ func NewCmd() *cobra.Command { if model.Limit != nil && len(credentials) > int(*model.Limit) { credentials = credentials[:*model.Limit] } - return outputResult(cmd, model.OutputFormat, credentials) + return outputResult(cmd, model.OutputFormat, credentials, p) }, } configureFlags(cmd) @@ -120,14 +121,14 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *rabbitmq.AP return req } -func outputResult(cmd *cobra.Command, outputFormat string, credentials []rabbitmq.CredentialsListItem) error { +func outputResult(cmd *cobra.Command, outputFormat string, credentials []rabbitmq.CredentialsListItem, p *print.Printer) error { switch outputFormat { case globalflags.JSONOutputFormat: details, err := json.MarshalIndent(credentials, "", " ") if err != nil { return fmt.Errorf("marshal RabbitMQ credentials list: %w", err) } - cmd.Println(string(details)) + p.Outputln(string(details)) return nil default: diff --git a/internal/cmd/rabbitmq/credentials/list/list_test.go b/internal/cmd/rabbitmq/credentials/list/list_test.go index 78f822c50..ba398c659 100644 --- a/internal/cmd/rabbitmq/credentials/list/list_test.go +++ b/internal/cmd/rabbitmq/credentials/list/list_test.go @@ -135,7 +135,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { - cmd := NewCmd() + cmd := NewCmd(nil) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/rabbitmq/instance/create/create.go b/internal/cmd/rabbitmq/instance/create/create.go index 15dfc9605..40b0515ed 100644 --- a/internal/cmd/rabbitmq/instance/create/create.go +++ b/internal/cmd/rabbitmq/instance/create/create.go @@ -12,6 +12,7 @@ import ( "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/print" "github.com/stackitcloud/stackit-cli/internal/pkg/projectname" "github.com/stackitcloud/stackit-cli/internal/pkg/services/rabbitmq/client" rabbitmqUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/rabbitmq/utils" @@ -55,7 +56,7 @@ type inputModel struct { PlanId *string } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: "create", Short: "Creates a RabbitMQ instance", @@ -128,7 +129,7 @@ func NewCmd() *cobra.Command { if model.Async { operationState = "Triggered creation of" } - cmd.Printf("%s instance for project %q. Instance ID: %s\n", operationState, projectLabel, instanceId) + p.Outputf("%s instance for project %q. Instance ID: %s\n", operationState, projectLabel, instanceId) return nil }, } diff --git a/internal/cmd/rabbitmq/instance/create/create_test.go b/internal/cmd/rabbitmq/instance/create/create_test.go index eacbf4a09..535db19fd 100644 --- a/internal/cmd/rabbitmq/instance/create/create_test.go +++ b/internal/cmd/rabbitmq/instance/create/create_test.go @@ -275,7 +275,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { - cmd := NewCmd() + cmd := NewCmd(nil) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/rabbitmq/instance/delete/delete.go b/internal/cmd/rabbitmq/instance/delete/delete.go index 2fd61f832..80acccb82 100644 --- a/internal/cmd/rabbitmq/instance/delete/delete.go +++ b/internal/cmd/rabbitmq/instance/delete/delete.go @@ -9,6 +9,7 @@ import ( "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" + "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/services/rabbitmq/client" rabbitmqUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/rabbitmq/utils" "github.com/stackitcloud/stackit-cli/internal/pkg/spinner" @@ -28,7 +29,7 @@ type inputModel struct { InstanceId string } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("delete %s", instanceIdArg), Short: "Deletes a RabbitMQ instance", @@ -87,7 +88,7 @@ func NewCmd() *cobra.Command { if model.Async { operationState = "Triggered deletion of" } - cmd.Printf("%s instance %q\n", operationState, instanceLabel) + p.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 08500bee0..f9c2f565a 100644 --- a/internal/cmd/rabbitmq/instance/delete/delete_test.go +++ b/internal/cmd/rabbitmq/instance/delete/delete_test.go @@ -136,7 +136,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { - cmd := NewCmd() + cmd := NewCmd(nil) 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 4f90b6946..559dafe19 100644 --- a/internal/cmd/rabbitmq/instance/describe/describe.go +++ b/internal/cmd/rabbitmq/instance/describe/describe.go @@ -9,6 +9,7 @@ import ( "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" + "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/services/rabbitmq/client" "github.com/stackitcloud/stackit-cli/internal/pkg/tables" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -28,7 +29,7 @@ type inputModel struct { InstanceId string } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("describe %s", instanceIdArg), Short: "Shows details of a RabbitMQ instance", @@ -61,7 +62,7 @@ func NewCmd() *cobra.Command { return fmt.Errorf("read RabbitMQ instance: %w", err) } - return outputResult(cmd, model.OutputFormat, resp) + return outputResult(cmd, model.OutputFormat, resp, p) }, } return cmd @@ -86,7 +87,7 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *rabbitmq.AP return req } -func outputResult(cmd *cobra.Command, outputFormat string, instance *rabbitmq.Instance) error { +func outputResult(cmd *cobra.Command, outputFormat string, instance *rabbitmq.Instance, p *print.Printer) error { switch outputFormat { case globalflags.PrettyOutputFormat: table := tables.NewTable() @@ -119,7 +120,7 @@ func outputResult(cmd *cobra.Command, outputFormat string, instance *rabbitmq.In if err != nil { return fmt.Errorf("marshal RabbitMQ instance: %w", err) } - cmd.Println(string(details)) + p.Outputln(string(details)) return nil } diff --git a/internal/cmd/rabbitmq/instance/describe/describe_test.go b/internal/cmd/rabbitmq/instance/describe/describe_test.go index 03306dde4..616b7731a 100644 --- a/internal/cmd/rabbitmq/instance/describe/describe_test.go +++ b/internal/cmd/rabbitmq/instance/describe/describe_test.go @@ -136,7 +136,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { - cmd := NewCmd() + cmd := NewCmd(nil) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/rabbitmq/instance/instance.go b/internal/cmd/rabbitmq/instance/instance.go index 9e58a61e5..b9a73a3e0 100644 --- a/internal/cmd/rabbitmq/instance/instance.go +++ b/internal/cmd/rabbitmq/instance/instance.go @@ -7,12 +7,13 @@ import ( "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() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: "instance", Short: "Provides functionality for RabbitMQ instances", @@ -20,14 +21,14 @@ func NewCmd() *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd) + addSubcommands(cmd, p) return cmd } -func addSubcommands(cmd *cobra.Command) { - cmd.AddCommand(create.NewCmd()) - cmd.AddCommand(delete.NewCmd()) - cmd.AddCommand(describe.NewCmd()) - cmd.AddCommand(list.NewCmd()) - cmd.AddCommand(update.NewCmd()) +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)) } diff --git a/internal/cmd/rabbitmq/instance/list/list.go b/internal/cmd/rabbitmq/instance/list/list.go index 576801ba2..f17808f0f 100644 --- a/internal/cmd/rabbitmq/instance/list/list.go +++ b/internal/cmd/rabbitmq/instance/list/list.go @@ -10,6 +10,7 @@ import ( "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/print" "github.com/stackitcloud/stackit-cli/internal/pkg/projectname" "github.com/stackitcloud/stackit-cli/internal/pkg/services/rabbitmq/client" "github.com/stackitcloud/stackit-cli/internal/pkg/tables" @@ -27,7 +28,7 @@ type inputModel struct { Limit *int64 } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: "list", Short: "Lists all RabbitMQ instances", @@ -69,7 +70,7 @@ func NewCmd() *cobra.Command { if err != nil { projectLabel = model.ProjectId } - cmd.Printf("No instances found for project %q\n", projectLabel) + p.Info("No instances found for project %q\n", projectLabel) return nil } @@ -78,7 +79,7 @@ func NewCmd() *cobra.Command { instances = instances[:*model.Limit] } - return outputResult(cmd, model.OutputFormat, instances) + return outputResult(cmd, model.OutputFormat, instances, p) }, } @@ -115,14 +116,14 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *rabbitmq.AP return req } -func outputResult(cmd *cobra.Command, outputFormat string, instances []rabbitmq.Instance) error { +func outputResult(cmd *cobra.Command, outputFormat string, instances []rabbitmq.Instance, p *print.Printer) error { switch outputFormat { case globalflags.JSONOutputFormat: details, err := json.MarshalIndent(instances, "", " ") if err != nil { return fmt.Errorf("marshal RabbitMQ instance list: %w", err) } - cmd.Println(string(details)) + p.Outputln(string(details)) return nil default: diff --git a/internal/cmd/rabbitmq/instance/update/update.go b/internal/cmd/rabbitmq/instance/update/update.go index 345d04a34..a61a518c5 100644 --- a/internal/cmd/rabbitmq/instance/update/update.go +++ b/internal/cmd/rabbitmq/instance/update/update.go @@ -12,6 +12,7 @@ import ( "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/print" "github.com/stackitcloud/stackit-cli/internal/pkg/services/rabbitmq/client" rabbitmqUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/rabbitmq/utils" "github.com/stackitcloud/stackit-cli/internal/pkg/spinner" @@ -56,7 +57,7 @@ type inputModel struct { PlanId *string } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("update %s", instanceIdArg), Short: "Updates a RabbitMQ instance", @@ -126,7 +127,7 @@ func NewCmd() *cobra.Command { if model.Async { operationState = "Triggered update of" } - cmd.Printf("%s instance %q\n", operationState, instanceLabel) + p.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 1e4d205f7..aa169639d 100644 --- a/internal/cmd/rabbitmq/instance/update/update_test.go +++ b/internal/cmd/rabbitmq/instance/update/update_test.go @@ -292,7 +292,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { - cmd := NewCmd() + cmd := NewCmd(nil) 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 773e6679b..f9165f866 100644 --- a/internal/cmd/rabbitmq/plans/plans.go +++ b/internal/cmd/rabbitmq/plans/plans.go @@ -10,6 +10,7 @@ import ( "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/print" "github.com/stackitcloud/stackit-cli/internal/pkg/projectname" "github.com/stackitcloud/stackit-cli/internal/pkg/services/rabbitmq/client" "github.com/stackitcloud/stackit-cli/internal/pkg/tables" @@ -27,7 +28,7 @@ type inputModel struct { Limit *int64 } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: "plans", Short: "Lists all RabbitMQ service plans", @@ -69,7 +70,7 @@ func NewCmd() *cobra.Command { if err != nil { projectLabel = model.ProjectId } - cmd.Printf("No plans found for project %q\n", projectLabel) + p.Info("No plans found for project %q\n", projectLabel) return nil } @@ -78,7 +79,7 @@ func NewCmd() *cobra.Command { plans = plans[:*model.Limit] } - return outputResult(cmd, model.OutputFormat, plans) + return outputResult(cmd, model.OutputFormat, plans, p) }, } @@ -115,14 +116,14 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *rabbitmq.AP return req } -func outputResult(cmd *cobra.Command, outputFormat string, plans []rabbitmq.Offering) error { +func outputResult(cmd *cobra.Command, outputFormat string, plans []rabbitmq.Offering, p *print.Printer) error { switch outputFormat { case globalflags.JSONOutputFormat: details, err := json.MarshalIndent(plans, "", " ") if err != nil { return fmt.Errorf("marshal RabbitMQ plans: %w", err) } - cmd.Println(string(details)) + p.Outputln(string(details)) return nil default: @@ -131,8 +132,8 @@ func outputResult(cmd *cobra.Command, outputFormat string, plans []rabbitmq.Offe for i := range plans { o := plans[i] for j := range *o.Plans { - p := (*o.Plans)[j] - table.AddRow(*o.Name, *p.Id, *p.Name, *p.Description) + plan := (*o.Plans)[j] + table.AddRow(*o.Name, *plan.Id, *plan.Name, *plan.Description) } table.AddSeparator() } diff --git a/internal/cmd/rabbitmq/rabbitmq.go b/internal/cmd/rabbitmq/rabbitmq.go index 916e9cc9f..26b5db9bb 100644 --- a/internal/cmd/rabbitmq/rabbitmq.go +++ b/internal/cmd/rabbitmq/rabbitmq.go @@ -5,12 +5,13 @@ import ( "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() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: "rabbitmq", Short: "Provides functionality for RabbitMQ", @@ -18,12 +19,12 @@ func NewCmd() *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd) + addSubcommands(cmd, p) return cmd } -func addSubcommands(cmd *cobra.Command) { - cmd.AddCommand(instance.NewCmd()) - cmd.AddCommand(plans.NewCmd()) - cmd.AddCommand(credentials.NewCmd()) +func addSubcommands(cmd *cobra.Command, p *print.Printer) { + cmd.AddCommand(instance.NewCmd(p)) + cmd.AddCommand(plans.NewCmd(p)) + cmd.AddCommand(credentials.NewCmd(p)) } diff --git a/internal/cmd/redis/credentials/create/create.go b/internal/cmd/redis/credentials/create/create.go index 8dbf3731a..febdf2f8e 100644 --- a/internal/cmd/redis/credentials/create/create.go +++ b/internal/cmd/redis/credentials/create/create.go @@ -10,6 +10,7 @@ import ( "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/print" "github.com/stackitcloud/stackit-cli/internal/pkg/services/redis/client" redisUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/redis/utils" @@ -28,7 +29,7 @@ type inputModel struct { HidePassword bool } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: "create", Short: "Creates credentials for a Redis instance", @@ -75,20 +76,20 @@ func NewCmd() *cobra.Command { return fmt.Errorf("create Redis credentials: %w", err) } - cmd.Printf("Created credentials for instance %q. Credentials ID: %s\n\n", instanceLabel, *resp.Id) + p.Outputf("Created credentials for instance %q. Credentials ID: %s\n\n", instanceLabel, *resp.Id) // The username field cannot be set by the user so we only display it if it's not returned empty username := *resp.Raw.Credentials.Username if username != "" { - cmd.Printf("Username: %s\n", *resp.Raw.Credentials.Username) + p.Outputf("Username: %s\n", *resp.Raw.Credentials.Username) } if model.HidePassword { - cmd.Printf("Password: \n") + p.Outputf("Password: \n") } else { - cmd.Printf("Password: %s\n", *resp.Raw.Credentials.Password) + p.Outputf("Password: %s\n", *resp.Raw.Credentials.Password) } - cmd.Printf("Host: %s\n", *resp.Raw.Credentials.Host) - cmd.Printf("Port: %d\n", *resp.Raw.Credentials.Port) - cmd.Printf("URI: %s\n", *resp.Uri) + p.Outputf("Host: %s\n", *resp.Raw.Credentials.Host) + p.Outputf("Port: %d\n", *resp.Raw.Credentials.Port) + p.Outputf("URI: %s\n", *resp.Uri) return nil }, } diff --git a/internal/cmd/redis/credentials/create/create_test.go b/internal/cmd/redis/credentials/create/create_test.go index 6f0e24f88..5f1a247f1 100644 --- a/internal/cmd/redis/credentials/create/create_test.go +++ b/internal/cmd/redis/credentials/create/create_test.go @@ -118,7 +118,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { - cmd := NewCmd() + cmd := NewCmd(nil) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/redis/credentials/credentials.go b/internal/cmd/redis/credentials/credentials.go index 36eac3341..42e6226da 100644 --- a/internal/cmd/redis/credentials/credentials.go +++ b/internal/cmd/redis/credentials/credentials.go @@ -6,12 +6,13 @@ import ( "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() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: "credentials", Short: "Provides functionality for Redis credentials", @@ -19,13 +20,13 @@ func NewCmd() *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd) + addSubcommands(cmd, p) return cmd } -func addSubcommands(cmd *cobra.Command) { - cmd.AddCommand(create.NewCmd()) - cmd.AddCommand(delete.NewCmd()) - cmd.AddCommand(describe.NewCmd()) - cmd.AddCommand(list.NewCmd()) +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)) } diff --git a/internal/cmd/redis/credentials/delete/delete.go b/internal/cmd/redis/credentials/delete/delete.go index 78067568c..d8e3de670 100644 --- a/internal/cmd/redis/credentials/delete/delete.go +++ b/internal/cmd/redis/credentials/delete/delete.go @@ -10,6 +10,7 @@ import ( "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/print" "github.com/stackitcloud/stackit-cli/internal/pkg/services/redis/client" redisUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/redis/utils" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -30,7 +31,7 @@ type inputModel struct { CredentialsId string } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("delete %s", credentialsIdArg), Short: "Deletes credentials of a Redis instance", @@ -79,7 +80,7 @@ func NewCmd() *cobra.Command { return fmt.Errorf("delete Redis credentials: %w", err) } - cmd.Printf("Deleted credentials %s of instance %q\n", credentialsLabel, instanceLabel) + p.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 981be15c8..575dac9f0 100644 --- a/internal/cmd/redis/credentials/delete/delete_test.go +++ b/internal/cmd/redis/credentials/delete/delete_test.go @@ -163,7 +163,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { - cmd := NewCmd() + cmd := NewCmd(nil) 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 676c2ce5b..3f534bea2 100644 --- a/internal/cmd/redis/credentials/describe/describe.go +++ b/internal/cmd/redis/credentials/describe/describe.go @@ -10,6 +10,7 @@ import ( "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/print" "github.com/stackitcloud/stackit-cli/internal/pkg/services/redis/client" "github.com/stackitcloud/stackit-cli/internal/pkg/tables" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -30,7 +31,7 @@ type inputModel struct { CredentialsId string } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("describe %s", credentialsIdArg), Short: "Shows details of credentials of a Redis instance", @@ -64,7 +65,7 @@ func NewCmd() *cobra.Command { return fmt.Errorf("describe Redis credentials: %w", err) } - return outputResult(cmd, model.OutputFormat, resp) + return outputResult(cmd, model.OutputFormat, resp, p) }, } configureFlags(cmd) @@ -98,7 +99,7 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *redis.APICl return req } -func outputResult(cmd *cobra.Command, outputFormat string, credentials *redis.CredentialsResponse) error { +func outputResult(cmd *cobra.Command, outputFormat string, credentials *redis.CredentialsResponse, p *print.Printer) error { switch outputFormat { case globalflags.PrettyOutputFormat: table := tables.NewTable() @@ -124,7 +125,7 @@ func outputResult(cmd *cobra.Command, outputFormat string, credentials *redis.Cr if err != nil { return fmt.Errorf("marshal Redis credentials: %w", err) } - cmd.Println(string(details)) + p.Outputln(string(details)) return nil } diff --git a/internal/cmd/redis/credentials/describe/describe_test.go b/internal/cmd/redis/credentials/describe/describe_test.go index 1331b8054..45f2fa6f5 100644 --- a/internal/cmd/redis/credentials/describe/describe_test.go +++ b/internal/cmd/redis/credentials/describe/describe_test.go @@ -163,7 +163,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { - cmd := NewCmd() + cmd := NewCmd(nil) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/redis/credentials/list/list.go b/internal/cmd/redis/credentials/list/list.go index 1919fda99..bd71080d6 100644 --- a/internal/cmd/redis/credentials/list/list.go +++ b/internal/cmd/redis/credentials/list/list.go @@ -10,6 +10,7 @@ import ( "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/print" "github.com/stackitcloud/stackit-cli/internal/pkg/services/redis/client" redisUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/redis/utils" "github.com/stackitcloud/stackit-cli/internal/pkg/tables" @@ -29,7 +30,7 @@ type inputModel struct { Limit *int64 } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: "list", Short: "Lists all credentials' IDs for a Redis instance", @@ -71,7 +72,7 @@ func NewCmd() *cobra.Command { if err != nil { instanceLabel = model.InstanceId } - cmd.Printf("No credentials found for instance %q\n", instanceLabel) + p.Info("No credentials found for instance %q\n", instanceLabel) return nil } @@ -79,7 +80,7 @@ func NewCmd() *cobra.Command { if model.Limit != nil && len(credentials) > int(*model.Limit) { credentials = credentials[:*model.Limit] } - return outputResult(cmd, model.OutputFormat, credentials) + return outputResult(cmd, model.OutputFormat, credentials, p) }, } configureFlags(cmd) @@ -120,14 +121,14 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *redis.APICl return req } -func outputResult(cmd *cobra.Command, outputFormat string, credentials []redis.CredentialsListItem) error { +func outputResult(cmd *cobra.Command, outputFormat string, credentials []redis.CredentialsListItem, p *print.Printer) error { switch outputFormat { case globalflags.JSONOutputFormat: details, err := json.MarshalIndent(credentials, "", " ") if err != nil { return fmt.Errorf("marshal Redis credentials list: %w", err) } - cmd.Println(string(details)) + p.Outputln(string(details)) return nil default: diff --git a/internal/cmd/redis/credentials/list/list_test.go b/internal/cmd/redis/credentials/list/list_test.go index 80634e531..749a288d3 100644 --- a/internal/cmd/redis/credentials/list/list_test.go +++ b/internal/cmd/redis/credentials/list/list_test.go @@ -135,7 +135,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { - cmd := NewCmd() + cmd := NewCmd(nil) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/redis/instance/create/create.go b/internal/cmd/redis/instance/create/create.go index 621704854..40cc8232c 100644 --- a/internal/cmd/redis/instance/create/create.go +++ b/internal/cmd/redis/instance/create/create.go @@ -12,6 +12,7 @@ import ( "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/print" "github.com/stackitcloud/stackit-cli/internal/pkg/projectname" "github.com/stackitcloud/stackit-cli/internal/pkg/services/redis/client" redisUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/redis/utils" @@ -55,7 +56,7 @@ type inputModel struct { PlanId *string } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: "create", Short: "Creates a Redis instance", @@ -128,7 +129,7 @@ func NewCmd() *cobra.Command { if model.Async { operationState = "Triggered creation of" } - cmd.Printf("%s instance for project %q. Instance ID: %s\n", operationState, projectLabel, instanceId) + p.Outputf("%s instance for project %q. Instance ID: %s\n", operationState, projectLabel, instanceId) return nil }, } diff --git a/internal/cmd/redis/instance/create/create_test.go b/internal/cmd/redis/instance/create/create_test.go index e9c6e39f8..5529a3627 100644 --- a/internal/cmd/redis/instance/create/create_test.go +++ b/internal/cmd/redis/instance/create/create_test.go @@ -275,7 +275,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { - cmd := NewCmd() + cmd := NewCmd(nil) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/redis/instance/delete/delete.go b/internal/cmd/redis/instance/delete/delete.go index a677aa4f3..224a04275 100644 --- a/internal/cmd/redis/instance/delete/delete.go +++ b/internal/cmd/redis/instance/delete/delete.go @@ -9,6 +9,7 @@ import ( "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" + "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/services/redis/client" redisUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/redis/utils" "github.com/stackitcloud/stackit-cli/internal/pkg/spinner" @@ -28,7 +29,7 @@ type inputModel struct { InstanceId string } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("delete %s", instanceIdArg), Short: "Deletes a Redis instance", @@ -87,7 +88,7 @@ func NewCmd() *cobra.Command { if model.Async { operationState = "Triggered deletion of" } - cmd.Printf("%s instance %q\n", operationState, instanceLabel) + p.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 0729e4889..e1e637428 100644 --- a/internal/cmd/redis/instance/delete/delete_test.go +++ b/internal/cmd/redis/instance/delete/delete_test.go @@ -136,7 +136,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { - cmd := NewCmd() + cmd := NewCmd(nil) 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 b7207c54b..e1474a7d7 100644 --- a/internal/cmd/redis/instance/describe/describe.go +++ b/internal/cmd/redis/instance/describe/describe.go @@ -9,6 +9,7 @@ import ( "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" + "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/services/redis/client" "github.com/stackitcloud/stackit-cli/internal/pkg/tables" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -28,7 +29,7 @@ type inputModel struct { InstanceId string } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("describe %s", instanceIdArg), Short: "Shows details of a Redis instance", @@ -61,7 +62,7 @@ func NewCmd() *cobra.Command { return fmt.Errorf("read Redis instance: %w", err) } - return outputResult(cmd, model.OutputFormat, resp) + return outputResult(cmd, model.OutputFormat, resp, p) }, } return cmd @@ -86,7 +87,7 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *redis.APICl return req } -func outputResult(cmd *cobra.Command, outputFormat string, instance *redis.Instance) error { +func outputResult(cmd *cobra.Command, outputFormat string, instance *redis.Instance, p *print.Printer) error { switch outputFormat { case globalflags.PrettyOutputFormat: table := tables.NewTable() @@ -119,7 +120,7 @@ func outputResult(cmd *cobra.Command, outputFormat string, instance *redis.Insta if err != nil { return fmt.Errorf("marshal Redis instance: %w", err) } - cmd.Println(string(details)) + p.Outputln(string(details)) return nil } diff --git a/internal/cmd/redis/instance/describe/describe_test.go b/internal/cmd/redis/instance/describe/describe_test.go index 4a8142f77..d19561e30 100644 --- a/internal/cmd/redis/instance/describe/describe_test.go +++ b/internal/cmd/redis/instance/describe/describe_test.go @@ -136,7 +136,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { - cmd := NewCmd() + cmd := NewCmd(nil) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/redis/instance/instance.go b/internal/cmd/redis/instance/instance.go index 920351de2..3518921b5 100644 --- a/internal/cmd/redis/instance/instance.go +++ b/internal/cmd/redis/instance/instance.go @@ -7,12 +7,13 @@ import ( "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() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: "instance", Short: "Provides functionality for Redis instances", @@ -20,14 +21,14 @@ func NewCmd() *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd) + addSubcommands(cmd, p) return cmd } -func addSubcommands(cmd *cobra.Command) { - cmd.AddCommand(create.NewCmd()) - cmd.AddCommand(delete.NewCmd()) - cmd.AddCommand(describe.NewCmd()) - cmd.AddCommand(list.NewCmd()) - cmd.AddCommand(update.NewCmd()) +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)) } diff --git a/internal/cmd/redis/instance/list/list.go b/internal/cmd/redis/instance/list/list.go index 3a393c95e..765859aec 100644 --- a/internal/cmd/redis/instance/list/list.go +++ b/internal/cmd/redis/instance/list/list.go @@ -10,6 +10,7 @@ import ( "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/print" "github.com/stackitcloud/stackit-cli/internal/pkg/projectname" "github.com/stackitcloud/stackit-cli/internal/pkg/services/redis/client" "github.com/stackitcloud/stackit-cli/internal/pkg/tables" @@ -27,7 +28,7 @@ type inputModel struct { Limit *int64 } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: "list", Short: "Lists all Redis instances", @@ -69,7 +70,7 @@ func NewCmd() *cobra.Command { if err != nil { projectLabel = model.ProjectId } - cmd.Printf("No instances found for project %q\n", projectLabel) + p.Info("No instances found for project %q\n", projectLabel) return nil } @@ -78,7 +79,7 @@ func NewCmd() *cobra.Command { instances = instances[:*model.Limit] } - return outputResult(cmd, model.OutputFormat, instances) + return outputResult(cmd, model.OutputFormat, instances, p) }, } @@ -115,14 +116,14 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *redis.APICl return req } -func outputResult(cmd *cobra.Command, outputFormat string, instances []redis.Instance) error { +func outputResult(cmd *cobra.Command, outputFormat string, instances []redis.Instance, p *print.Printer) error { switch outputFormat { case globalflags.JSONOutputFormat: details, err := json.MarshalIndent(instances, "", " ") if err != nil { return fmt.Errorf("marshal Redis instance list: %w", err) } - cmd.Println(string(details)) + p.Outputln(string(details)) return nil default: diff --git a/internal/cmd/redis/instance/update/update.go b/internal/cmd/redis/instance/update/update.go index 7fe63ca8f..c2f79261e 100644 --- a/internal/cmd/redis/instance/update/update.go +++ b/internal/cmd/redis/instance/update/update.go @@ -12,6 +12,7 @@ import ( "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/print" "github.com/stackitcloud/stackit-cli/internal/pkg/services/redis/client" redisUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/redis/utils" "github.com/stackitcloud/stackit-cli/internal/pkg/spinner" @@ -56,7 +57,7 @@ type inputModel struct { PlanId *string } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("update %s", instanceIdArg), Short: "Updates a Redis instance", @@ -126,7 +127,7 @@ func NewCmd() *cobra.Command { if model.Async { operationState = "Triggered update of" } - cmd.Printf("%s instance %q\n", operationState, instanceLabel) + p.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 fc15e2b5f..1f913d7b3 100644 --- a/internal/cmd/redis/instance/update/update_test.go +++ b/internal/cmd/redis/instance/update/update_test.go @@ -292,7 +292,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { - cmd := NewCmd() + cmd := NewCmd(nil) 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 86232c1fb..2bb6c55af 100644 --- a/internal/cmd/redis/plans/plans.go +++ b/internal/cmd/redis/plans/plans.go @@ -10,6 +10,7 @@ import ( "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/print" "github.com/stackitcloud/stackit-cli/internal/pkg/projectname" "github.com/stackitcloud/stackit-cli/internal/pkg/services/redis/client" "github.com/stackitcloud/stackit-cli/internal/pkg/tables" @@ -27,7 +28,7 @@ type inputModel struct { Limit *int64 } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: "plans", Short: "Lists all Redis service plans", @@ -69,7 +70,7 @@ func NewCmd() *cobra.Command { if err != nil { projectLabel = model.ProjectId } - cmd.Printf("No plans found for project %q\n", projectLabel) + p.Info("No plans found for project %q\n", projectLabel) return nil } @@ -78,7 +79,7 @@ func NewCmd() *cobra.Command { plans = plans[:*model.Limit] } - return outputResult(cmd, model.OutputFormat, plans) + return outputResult(cmd, model.OutputFormat, plans, p) }, } @@ -115,14 +116,14 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *redis.APICl return req } -func outputResult(cmd *cobra.Command, outputFormat string, plans []redis.Offering) error { +func outputResult(cmd *cobra.Command, outputFormat string, plans []redis.Offering, p *print.Printer) error { switch outputFormat { case globalflags.JSONOutputFormat: details, err := json.MarshalIndent(plans, "", " ") if err != nil { return fmt.Errorf("marshal Redis plans: %w", err) } - cmd.Println(string(details)) + p.Outputln(string(details)) return nil default: @@ -131,8 +132,8 @@ func outputResult(cmd *cobra.Command, outputFormat string, plans []redis.Offerin for i := range plans { o := plans[i] for j := range *o.Plans { - p := (*o.Plans)[j] - table.AddRow(*o.Name, *p.Id, *p.Name, *p.Description) + plan := (*o.Plans)[j] + table.AddRow(*o.Name, *plan.Id, *plan.Name, *plan.Description) } table.AddSeparator() } diff --git a/internal/cmd/redis/redis.go b/internal/cmd/redis/redis.go index 25e306217..3b07f00c9 100644 --- a/internal/cmd/redis/redis.go +++ b/internal/cmd/redis/redis.go @@ -5,12 +5,13 @@ import ( "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() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: "redis", Short: "Provides functionality for Redis", @@ -18,12 +19,12 @@ func NewCmd() *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd) + addSubcommands(cmd, p) return cmd } -func addSubcommands(cmd *cobra.Command) { - cmd.AddCommand(instance.NewCmd()) - cmd.AddCommand(plans.NewCmd()) - cmd.AddCommand(credentials.NewCmd()) +func addSubcommands(cmd *cobra.Command, p *print.Printer) { + cmd.AddCommand(instance.NewCmd(p)) + cmd.AddCommand(plans.NewCmd(p)) + cmd.AddCommand(credentials.NewCmd(p)) } diff --git a/internal/cmd/root.go b/internal/cmd/root.go index 45fa3f419..007019f6d 100644 --- a/internal/cmd/root.go +++ b/internal/cmd/root.go @@ -48,14 +48,14 @@ func NewRootCmd(version, date string, p *print.Printer) *cobra.Command { }, RunE: func(cmd *cobra.Command, args []string) error { if flags.FlagToBoolValue(cmd, "version") { - cmd.Printf("STACKIT CLI (BETA)\n") + p.Outputf("STACKIT CLI (BETA)\n") parsedDate, err := time.Parse(time.RFC3339, date) if err != nil { - cmd.Printf("Version: %s\n", version) + p.Outputf("Version: %s\n", version) return nil } - cmd.Printf("Version: %s (%s)\n", version, parsedDate.Format(time.DateOnly)) + p.Outputf("Version: %s (%s)\n", version, parsedDate.Format(time.DateOnly)) return nil } @@ -90,24 +90,24 @@ func configureFlags(cmd *cobra.Command) error { } func addSubcommands(cmd *cobra.Command, p *print.Printer) { - cmd.AddCommand(argus.NewCmd()) + cmd.AddCommand(argus.NewCmd(p)) cmd.AddCommand(auth.NewCmd(p)) cmd.AddCommand(config.NewCmd()) - cmd.AddCommand(curl.NewCmd()) + cmd.AddCommand(curl.NewCmd(p)) cmd.AddCommand(dns.NewCmd(p)) - cmd.AddCommand(logme.NewCmd()) - cmd.AddCommand(mariadb.NewCmd()) - cmd.AddCommand(mongodbflex.NewCmd()) - cmd.AddCommand(objectstorage.NewCmd()) - cmd.AddCommand(opensearch.NewCmd()) - cmd.AddCommand(organization.NewCmd()) - cmd.AddCommand(postgresflex.NewCmd()) + cmd.AddCommand(logme.NewCmd(p)) + cmd.AddCommand(mariadb.NewCmd(p)) + cmd.AddCommand(mongodbflex.NewCmd(p)) + cmd.AddCommand(objectstorage.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()) - cmd.AddCommand(redis.NewCmd()) - cmd.AddCommand(secretsmanager.NewCmd()) - cmd.AddCommand(serviceaccount.NewCmd()) - cmd.AddCommand(ske.NewCmd()) + 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)) } // 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 42964c1e9..33f173d93 100644 --- a/internal/cmd/secrets-manager/instance/create/create.go +++ b/internal/cmd/secrets-manager/instance/create/create.go @@ -10,6 +10,7 @@ import ( "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/print" "github.com/stackitcloud/stackit-cli/internal/pkg/projectname" "github.com/stackitcloud/stackit-cli/internal/pkg/services/secrets-manager/client" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -30,7 +31,7 @@ type inputModel struct { Acls *[]string } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: "create", Short: "Creates a Secrets Manager instance", @@ -91,7 +92,7 @@ If you want to retry configuring the ACLs, you can do it via: } } - cmd.Printf("Created instance for project %q. Instance ID: %s\n", projectLabel, instanceId) + p.Outputf("Created instance for project %q. Instance ID: %s\n", projectLabel, instanceId) return nil }, } diff --git a/internal/cmd/secrets-manager/instance/create/create_test.go b/internal/cmd/secrets-manager/instance/create/create_test.go index e8531fa7a..76375bc58 100644 --- a/internal/cmd/secrets-manager/instance/create/create_test.go +++ b/internal/cmd/secrets-manager/instance/create/create_test.go @@ -182,7 +182,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { - cmd := NewCmd() + cmd := NewCmd(nil) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/secrets-manager/instance/delete/delete.go b/internal/cmd/secrets-manager/instance/delete/delete.go index 85df4fd8b..479e9d646 100644 --- a/internal/cmd/secrets-manager/instance/delete/delete.go +++ b/internal/cmd/secrets-manager/instance/delete/delete.go @@ -10,6 +10,7 @@ import ( "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" + "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/services/secrets-manager/client" secretsmanagerUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/secrets-manager/utils" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -25,7 +26,7 @@ type inputModel struct { InstanceId string } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("delete %s", instanceIdArg), Short: "Deletes a Secrets Manager instance", @@ -69,7 +70,7 @@ func NewCmd() *cobra.Command { return fmt.Errorf("delete Secrets Manager instance: %w", err) } - cmd.Printf("Deleted instance %q \n", model.InstanceId) + p.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 28ddcab1b..3fa907dfe 100644 --- a/internal/cmd/secrets-manager/instance/delete/delete_test.go +++ b/internal/cmd/secrets-manager/instance/delete/delete_test.go @@ -136,7 +136,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { - cmd := NewCmd() + cmd := NewCmd(nil) 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 559f42f73..9153442c7 100644 --- a/internal/cmd/secrets-manager/instance/describe/describe.go +++ b/internal/cmd/secrets-manager/instance/describe/describe.go @@ -10,6 +10,7 @@ import ( "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" + "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/services/secrets-manager/client" "github.com/stackitcloud/stackit-cli/internal/pkg/tables" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -27,7 +28,7 @@ type inputModel struct { InstanceId string } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("describe %s", instanceIdArg), Short: "Shows details of a Secrets Manager instance", @@ -67,7 +68,7 @@ func NewCmd() *cobra.Command { return fmt.Errorf("read Secrets Manager instance ACLs: %w", err) } - return outputResult(cmd, model.OutputFormat, instance, aclList) + return outputResult(cmd, model.OutputFormat, instance, aclList, p) }, } return cmd @@ -97,7 +98,7 @@ func buildListACLsRequest(ctx context.Context, model *inputModel, apiClient *sec return req } -func outputResult(cmd *cobra.Command, outputFormat string, instance *secretsmanager.Instance, aclList *secretsmanager.AclList) error { +func outputResult(cmd *cobra.Command, outputFormat string, instance *secretsmanager.Instance, aclList *secretsmanager.AclList, p *print.Printer) error { switch outputFormat { case globalflags.PrettyOutputFormat: @@ -140,7 +141,7 @@ func outputResult(cmd *cobra.Command, outputFormat string, instance *secretsmana if err != nil { return fmt.Errorf("marshal Secrets Manager instance: %w", err) } - cmd.Println(string(details)) + p.Outputln(string(details)) return nil } diff --git a/internal/cmd/secrets-manager/instance/describe/describe_test.go b/internal/cmd/secrets-manager/instance/describe/describe_test.go index 58f16e67d..6ec20eb30 100644 --- a/internal/cmd/secrets-manager/instance/describe/describe_test.go +++ b/internal/cmd/secrets-manager/instance/describe/describe_test.go @@ -144,7 +144,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { - cmd := NewCmd() + cmd := NewCmd(nil) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/secrets-manager/instance/instance.go b/internal/cmd/secrets-manager/instance/instance.go index 9a3283638..cc38f6ca7 100644 --- a/internal/cmd/secrets-manager/instance/instance.go +++ b/internal/cmd/secrets-manager/instance/instance.go @@ -7,12 +7,13 @@ import ( "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() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: "instance", Short: "Provides functionality for Secrets Manager instances", @@ -20,14 +21,14 @@ func NewCmd() *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd) + addSubcommands(cmd, p) return cmd } -func addSubcommands(cmd *cobra.Command) { - cmd.AddCommand(list.NewCmd()) - cmd.AddCommand(create.NewCmd()) - cmd.AddCommand(delete.NewCmd()) - cmd.AddCommand(describe.NewCmd()) - cmd.AddCommand(update.NewCmd()) +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)) } diff --git a/internal/cmd/secrets-manager/instance/list/list.go b/internal/cmd/secrets-manager/instance/list/list.go index 08ce4d38d..9fa3fea8a 100644 --- a/internal/cmd/secrets-manager/instance/list/list.go +++ b/internal/cmd/secrets-manager/instance/list/list.go @@ -10,6 +10,7 @@ import ( "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/print" "github.com/stackitcloud/stackit-cli/internal/pkg/projectname" "github.com/stackitcloud/stackit-cli/internal/pkg/services/secrets-manager/client" "github.com/stackitcloud/stackit-cli/internal/pkg/tables" @@ -27,7 +28,7 @@ type inputModel struct { Limit *int64 } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: "list", Short: "Lists all Secrets Manager instances", @@ -69,7 +70,7 @@ func NewCmd() *cobra.Command { if err != nil { projectLabel = model.ProjectId } - cmd.Printf("No instances found for project %q\n", projectLabel) + p.Info("No instances found for project %q\n", projectLabel) return nil } instances := *resp.Instances @@ -79,7 +80,7 @@ func NewCmd() *cobra.Command { instances = instances[:*model.Limit] } - return outputResult(cmd, model.OutputFormat, instances) + return outputResult(cmd, model.OutputFormat, instances, p) }, } @@ -116,14 +117,14 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *secretsmana return req } -func outputResult(cmd *cobra.Command, outputFormat string, instances []secretsmanager.Instance) error { +func outputResult(cmd *cobra.Command, outputFormat string, instances []secretsmanager.Instance, p *print.Printer) error { switch outputFormat { case globalflags.JSONOutputFormat: details, err := json.MarshalIndent(instances, "", " ") if err != nil { return fmt.Errorf("marshal Secrets Manager instance list: %w", err) } - cmd.Println(string(details)) + p.Outputln(string(details)) return nil default: diff --git a/internal/cmd/secrets-manager/instance/update/update.go b/internal/cmd/secrets-manager/instance/update/update.go index cb38c6d71..cee06b5b1 100644 --- a/internal/cmd/secrets-manager/instance/update/update.go +++ b/internal/cmd/secrets-manager/instance/update/update.go @@ -11,6 +11,7 @@ import ( "github.com/stackitcloud/stackit-cli/internal/pkg/flags" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" + "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/services/secrets-manager/client" secretsManagerUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/secrets-manager/utils" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -32,7 +33,7 @@ type inputModel struct { Acls *[]string } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("update %s", instanceIdArg), Short: "Updates a Secrets Manager instance", @@ -76,7 +77,7 @@ func NewCmd() *cobra.Command { return fmt.Errorf("update Secrets Manager instance: %w", err) } - cmd.Printf("Updated instance %q\n", instanceLabel) + p.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 b36c6169b..fb858ebdc 100644 --- a/internal/cmd/secrets-manager/instance/update/update_test.go +++ b/internal/cmd/secrets-manager/instance/update/update_test.go @@ -196,7 +196,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { - cmd := NewCmd() + cmd := NewCmd(nil) 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 a7753e5ad..d9c78f035 100644 --- a/internal/cmd/secrets-manager/secrets_manager.go +++ b/internal/cmd/secrets-manager/secrets_manager.go @@ -4,12 +4,13 @@ import ( "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() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: "secrets-manager", Short: "Provides functionality for Secrets Manager", @@ -17,11 +18,11 @@ func NewCmd() *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd) + addSubcommands(cmd, p) return cmd } -func addSubcommands(cmd *cobra.Command) { - cmd.AddCommand(instance.NewCmd()) - cmd.AddCommand(user.NewCmd()) +func addSubcommands(cmd *cobra.Command, p *print.Printer) { + cmd.AddCommand(instance.NewCmd(p)) + cmd.AddCommand(user.NewCmd(p)) } diff --git a/internal/cmd/secrets-manager/user/create/create.go b/internal/cmd/secrets-manager/user/create/create.go index d8fae03d6..33dadfea2 100644 --- a/internal/cmd/secrets-manager/user/create/create.go +++ b/internal/cmd/secrets-manager/user/create/create.go @@ -10,6 +10,7 @@ import ( "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/print" "github.com/stackitcloud/stackit-cli/internal/pkg/services/secrets-manager/client" secretsManagerUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/secrets-manager/utils" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -32,7 +33,7 @@ type inputModel struct { Write *bool } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: "create", Short: "Creates a Secrets Manager user", @@ -83,11 +84,11 @@ func NewCmd() *cobra.Command { return fmt.Errorf("create Secrets Manager user: %w", err) } - cmd.Printf("Created user for instance %q. User ID: %s\n\n", instanceLabel, *resp.Id) - cmd.Printf("Username: %s\n", *resp.Username) - cmd.Printf("Password: %s\n", *resp.Password) - cmd.Printf("Description: %s\n", *resp.Description) - cmd.Printf("Write Access: %t\n", *resp.Write) + p.Outputf("Created user for instance %q. User ID: %s\n\n", instanceLabel, *resp.Id) + p.Outputf("Username: %s\n", *resp.Username) + p.Outputf("Password: %s\n", *resp.Password) + p.Outputf("Description: %s\n", *resp.Description) + p.Outputf("Write Access: %t\n", *resp.Write) return nil }, diff --git a/internal/cmd/secrets-manager/user/delete/delete.go b/internal/cmd/secrets-manager/user/delete/delete.go index 618aaed2b..2ebb49ab2 100644 --- a/internal/cmd/secrets-manager/user/delete/delete.go +++ b/internal/cmd/secrets-manager/user/delete/delete.go @@ -10,6 +10,7 @@ import ( "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/print" "github.com/stackitcloud/stackit-cli/internal/pkg/services/secrets-manager/client" secretsManagerUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/secrets-manager/utils" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -31,7 +32,7 @@ type inputModel struct { UserId string } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("delete %s", userIdArg), Short: "Deletes a Secrets Manager user", @@ -83,7 +84,7 @@ func NewCmd() *cobra.Command { return fmt.Errorf("delete Secrets Manager user: %w", err) } - cmd.Printf("Deleted user %s of instance %q\n", userLabel, instanceLabel) + p.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 815a4a6a4..0b4605d6d 100644 --- a/internal/cmd/secrets-manager/user/delete/delete_test.go +++ b/internal/cmd/secrets-manager/user/delete/delete_test.go @@ -151,7 +151,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { - cmd := NewCmd() + cmd := NewCmd(nil) 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 bca914815..89baf958c 100644 --- a/internal/cmd/secrets-manager/user/describe/describe.go +++ b/internal/cmd/secrets-manager/user/describe/describe.go @@ -10,6 +10,7 @@ import ( "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/print" "github.com/stackitcloud/stackit-cli/internal/pkg/services/secrets-manager/client" "github.com/stackitcloud/stackit-cli/internal/pkg/tables" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -31,7 +32,7 @@ type inputModel struct { UserId string } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("describe %s", userIdArg), Short: "Shows details of a Secrets Manager user", @@ -65,7 +66,7 @@ func NewCmd() *cobra.Command { return fmt.Errorf("get Secrets Manager user: %w", err) } - return outputResult(cmd, model.OutputFormat, *resp) + return outputResult(cmd, model.OutputFormat, *resp, p) }, } @@ -100,7 +101,7 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *secretsmana return req } -func outputResult(cmd *cobra.Command, outputFormat string, user secretsmanager.User) error { +func outputResult(cmd *cobra.Command, outputFormat string, user secretsmanager.User, p *print.Printer) error { switch outputFormat { case globalflags.PrettyOutputFormat: table := tables.NewTable() @@ -129,7 +130,7 @@ func outputResult(cmd *cobra.Command, outputFormat string, user secretsmanager.U if err != nil { return fmt.Errorf("marshal Secrets Manager user: %w", err) } - cmd.Println(string(details)) + p.Outputln(string(details)) return nil } diff --git a/internal/cmd/secrets-manager/user/describe/describe_test.go b/internal/cmd/secrets-manager/user/describe/describe_test.go index 06e0d1728..e9c055f93 100644 --- a/internal/cmd/secrets-manager/user/describe/describe_test.go +++ b/internal/cmd/secrets-manager/user/describe/describe_test.go @@ -163,7 +163,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { - cmd := NewCmd() + cmd := NewCmd(nil) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/secrets-manager/user/list/list.go b/internal/cmd/secrets-manager/user/list/list.go index 92a4632b6..161f5a5b9 100644 --- a/internal/cmd/secrets-manager/user/list/list.go +++ b/internal/cmd/secrets-manager/user/list/list.go @@ -10,6 +10,7 @@ import ( "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/print" "github.com/stackitcloud/stackit-cli/internal/pkg/services/secrets-manager/client" secretsManagerUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/secrets-manager/utils" "github.com/stackitcloud/stackit-cli/internal/pkg/tables" @@ -30,7 +31,7 @@ type inputModel struct { Limit *int64 } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: "list", Short: "Lists all Secrets Manager users", @@ -71,7 +72,7 @@ func NewCmd() *cobra.Command { if err != nil { instanceLabel = *model.InstanceId } - cmd.Printf("No users found for instance %q\n", instanceLabel) + p.Info("No users found for instance %q\n", instanceLabel) return nil } users := *resp.Users @@ -81,7 +82,7 @@ func NewCmd() *cobra.Command { users = users[:*model.Limit] } - return outputResult(cmd, model.OutputFormat, users) + return outputResult(cmd, model.OutputFormat, users, p) }, } @@ -123,14 +124,14 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *secretsmana return req } -func outputResult(cmd *cobra.Command, outputFormat string, users []secretsmanager.User) error { +func outputResult(cmd *cobra.Command, outputFormat string, users []secretsmanager.User, p *print.Printer) error { switch outputFormat { case globalflags.JSONOutputFormat: details, err := json.MarshalIndent(users, "", " ") if err != nil { return fmt.Errorf("marshal Secrets Manager user list: %w", err) } - cmd.Println(string(details)) + p.Outputln(string(details)) return nil default: diff --git a/internal/cmd/secrets-manager/user/update/update.go b/internal/cmd/secrets-manager/user/update/update.go index 9fe27207d..2151194f2 100644 --- a/internal/cmd/secrets-manager/user/update/update.go +++ b/internal/cmd/secrets-manager/user/update/update.go @@ -10,6 +10,7 @@ import ( "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/print" "github.com/stackitcloud/stackit-cli/internal/pkg/services/secrets-manager/client" secretsManagerUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/secrets-manager/utils" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -35,7 +36,7 @@ type inputModel struct { DisableWrite *bool } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("update %s", userIdArg), Short: "Updates the write privileges Secrets Manager user", @@ -91,7 +92,7 @@ func NewCmd() *cobra.Command { return fmt.Errorf("update Secrets Manager user: %w", err) } - cmd.Printf("Updated user %s of instance %q\n", userLabel, instanceLabel) + p.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 d33da4484..8d39b9c36 100644 --- a/internal/cmd/secrets-manager/user/update/update_test.go +++ b/internal/cmd/secrets-manager/user/update/update_test.go @@ -187,7 +187,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { - cmd := NewCmd() + cmd := NewCmd(nil) 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 76379621b..8dcd68410 100644 --- a/internal/cmd/secrets-manager/user/user.go +++ b/internal/cmd/secrets-manager/user/user.go @@ -2,6 +2,7 @@ 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" @@ -12,7 +13,7 @@ import ( "github.com/stackitcloud/stackit-cli/internal/cmd/secrets-manager/user/update" ) -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: "user", Short: "Provides functionality for Secrets Manager users", @@ -20,14 +21,14 @@ func NewCmd() *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd) + addSubcommands(cmd, p) return cmd } -func addSubcommands(cmd *cobra.Command) { - cmd.AddCommand(list.NewCmd()) - cmd.AddCommand(create.NewCmd()) - cmd.AddCommand(delete.NewCmd()) - cmd.AddCommand(describe.NewCmd()) - cmd.AddCommand(update.NewCmd()) +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)) } diff --git a/internal/cmd/service-account/create/create.go b/internal/cmd/service-account/create/create.go index 038a2067e..336b6b5c9 100644 --- a/internal/cmd/service-account/create/create.go +++ b/internal/cmd/service-account/create/create.go @@ -10,6 +10,7 @@ import ( "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/print" "github.com/stackitcloud/stackit-cli/internal/pkg/projectname" "github.com/stackitcloud/stackit-cli/internal/pkg/services/service-account/client" @@ -26,7 +27,7 @@ type inputModel struct { Name *string } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: "create", Short: "Creates a service account", @@ -70,7 +71,7 @@ func NewCmd() *cobra.Command { return fmt.Errorf("create service account: %w", err) } - cmd.Printf("Created service account for project %q. Email: %s\n", projectLabel, *resp.Email) + p.Outputf("Created service account for project %q. Email: %s\n", projectLabel, *resp.Email) return nil }, } diff --git a/internal/cmd/service-account/create/create_test.go b/internal/cmd/service-account/create/create_test.go index ea1eb597b..66c3da314 100644 --- a/internal/cmd/service-account/create/create_test.go +++ b/internal/cmd/service-account/create/create_test.go @@ -115,7 +115,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { - cmd := NewCmd() + cmd := NewCmd(nil) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/service-account/delete/delete.go b/internal/cmd/service-account/delete/delete.go index edcdccec6..39d34cb97 100644 --- a/internal/cmd/service-account/delete/delete.go +++ b/internal/cmd/service-account/delete/delete.go @@ -9,6 +9,7 @@ import ( "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" + "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/services/service-account/client" "github.com/spf13/cobra" @@ -24,7 +25,7 @@ type inputModel struct { Email string } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("delete %s", emailArg), Short: "Deletes a service account", @@ -66,7 +67,7 @@ func NewCmd() *cobra.Command { return fmt.Errorf("delete service account: %w", err) } - cmd.Printf("Service account %s deleted\n", model.Email) + p.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 bc055b60e..8653abe86 100644 --- a/internal/cmd/service-account/delete/delete_test.go +++ b/internal/cmd/service-account/delete/delete_test.go @@ -124,7 +124,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { - cmd := NewCmd() + cmd := NewCmd(nil) 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 b06ba7736..4479e363b 100644 --- a/internal/cmd/service-account/get-jwks/get_jwks.go +++ b/internal/cmd/service-account/get-jwks/get_jwks.go @@ -7,6 +7,7 @@ import ( "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/services/service-account/client" "github.com/spf13/cobra" @@ -21,7 +22,7 @@ type inputModel struct { Email string } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("get-jwks %s", emailArg), Short: "Shows the JWKS for a service account", @@ -53,11 +54,11 @@ func NewCmd() *cobra.Command { } jwks := *resp.Keys if len(jwks) == 0 { - cmd.Printf("Empty JWKS for service account %s\n", model.Email) + p.Info("Empty JWKS for service account %s\n", model.Email) return nil } - return outputResult(cmd, jwks) + return outputResult(jwks, p) }, } @@ -77,11 +78,11 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *serviceacco return req } -func outputResult(cmd *cobra.Command, serviceAccounts []serviceaccount.JWK) error { +func outputResult(serviceAccounts []serviceaccount.JWK, p *print.Printer) error { details, err := json.MarshalIndent(serviceAccounts, "", " ") if err != nil { return fmt.Errorf("marshal JWK list: %w", err) } - cmd.Println(string(details)) + p.Outputln(string(details)) return nil } 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 893178fd5..616579f21 100644 --- a/internal/cmd/service-account/get-jwks/get_jwks_test.go +++ b/internal/cmd/service-account/get-jwks/get_jwks_test.go @@ -67,7 +67,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { - cmd := NewCmd() + cmd := NewCmd(nil) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/service-account/key/create/create.go b/internal/cmd/service-account/key/create/create.go index 9e57769db..3ae088943 100644 --- a/internal/cmd/service-account/key/create/create.go +++ b/internal/cmd/service-account/key/create/create.go @@ -12,6 +12,7 @@ import ( "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/print" "github.com/stackitcloud/stackit-cli/internal/pkg/services/service-account/client" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -33,7 +34,7 @@ type inputModel struct { PublicKey *string } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: "create", Short: "Creates a service account key", @@ -86,13 +87,13 @@ func NewCmd() *cobra.Command { return fmt.Errorf("create service account key: %w", err) } - cmd.PrintErrf("Created key for service account %s with ID %q\n", model.ServiceAccountEmail, *resp.Id) + p.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) } - cmd.Println(string(key)) + p.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 7bd1d0d3b..1a621d14b 100644 --- a/internal/cmd/service-account/key/create/create_test.go +++ b/internal/cmd/service-account/key/create/create_test.go @@ -140,7 +140,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { - cmd := NewCmd() + cmd := NewCmd(nil) 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 b54fdde92..8aa4ce525 100644 --- a/internal/cmd/service-account/key/delete/delete.go +++ b/internal/cmd/service-account/key/delete/delete.go @@ -10,6 +10,7 @@ import ( "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/print" "github.com/stackitcloud/stackit-cli/internal/pkg/services/service-account/client" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -30,7 +31,7 @@ type inputModel struct { KeyId string } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("delete %s", keyIdArg), Short: "Deletes a service account key", @@ -69,7 +70,7 @@ func NewCmd() *cobra.Command { return fmt.Errorf("delete key: %w", err) } - cmd.Printf("Deleted key %s from service account %s\n", model.KeyId, model.ServiceAccountEmail) + p.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 03f5ab0ab..056e8c04c 100644 --- a/internal/cmd/service-account/key/delete/delete_test.go +++ b/internal/cmd/service-account/key/delete/delete_test.go @@ -147,7 +147,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { - cmd := NewCmd() + cmd := NewCmd(nil) 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 1112f0694..d9460b88e 100644 --- a/internal/cmd/service-account/key/describe/describe.go +++ b/internal/cmd/service-account/key/describe/describe.go @@ -10,6 +10,7 @@ import ( "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/print" "github.com/stackitcloud/stackit-cli/internal/pkg/services/service-account/client" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -30,7 +31,7 @@ type inputModel struct { KeyId string } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("describe %s", keyIdArg), Short: "Shows details of a service account key", @@ -60,7 +61,7 @@ func NewCmd() *cobra.Command { return fmt.Errorf("read service account key: %w", err) } - return outputResult(cmd, resp) + return outputResult(resp, p) }, } configureFlags(cmd) @@ -102,11 +103,11 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *serviceacco return req } -func outputResult(cmd *cobra.Command, key *serviceaccount.GetServiceAccountKeyResponse) error { +func outputResult(key *serviceaccount.GetServiceAccountKeyResponse, p *print.Printer) error { marshaledKey, err := json.MarshalIndent(key, "", " ") if err != nil { return fmt.Errorf("marshal service account key: %w", err) } - cmd.Println(string(marshaledKey)) + p.Outputln(string(marshaledKey)) return nil } diff --git a/internal/cmd/service-account/key/describe/describe_test.go b/internal/cmd/service-account/key/describe/describe_test.go index 7f0ffba05..06797196d 100644 --- a/internal/cmd/service-account/key/describe/describe_test.go +++ b/internal/cmd/service-account/key/describe/describe_test.go @@ -147,7 +147,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { - cmd := NewCmd() + cmd := NewCmd(nil) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/service-account/key/key.go b/internal/cmd/service-account/key/key.go index b180160fd..96ec51ae3 100644 --- a/internal/cmd/service-account/key/key.go +++ b/internal/cmd/service-account/key/key.go @@ -7,12 +7,13 @@ import ( "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() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: "key", Short: "Provides functionality regarding service account keys", @@ -20,14 +21,14 @@ func NewCmd() *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd) + addSubcommands(cmd, p) return cmd } -func addSubcommands(cmd *cobra.Command) { - cmd.AddCommand(create.NewCmd()) - cmd.AddCommand(delete.NewCmd()) - cmd.AddCommand(describe.NewCmd()) - cmd.AddCommand(list.NewCmd()) - cmd.AddCommand(update.NewCmd()) +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)) } diff --git a/internal/cmd/service-account/key/list/list.go b/internal/cmd/service-account/key/list/list.go index c3fcfbf5d..8e0852ddf 100644 --- a/internal/cmd/service-account/key/list/list.go +++ b/internal/cmd/service-account/key/list/list.go @@ -10,6 +10,7 @@ import ( "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/print" "github.com/stackitcloud/stackit-cli/internal/pkg/services/service-account/client" "github.com/stackitcloud/stackit-cli/internal/pkg/tables" @@ -29,7 +30,7 @@ type inputModel struct { Limit *int64 } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: "list", Short: "Lists all service account keys", @@ -67,7 +68,7 @@ func NewCmd() *cobra.Command { } keys := *resp.Items if len(keys) == 0 { - cmd.Printf("No keys found for service account %s\n", model.ServiceAccountEmail) + p.Info("No keys found for service account %s\n", model.ServiceAccountEmail) return nil } @@ -76,7 +77,7 @@ func NewCmd() *cobra.Command { keys = keys[:*model.Limit] } - return outputResult(cmd, model.OutputFormat, keys) + return outputResult(cmd, model.OutputFormat, keys, p) }, } @@ -126,14 +127,14 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *serviceacco return req } -func outputResult(cmd *cobra.Command, outputFormat string, keys []serviceaccount.ServiceAccountKeyListResponse) error { +func outputResult(cmd *cobra.Command, outputFormat string, keys []serviceaccount.ServiceAccountKeyListResponse, p *print.Printer) error { switch outputFormat { case globalflags.JSONOutputFormat: details, err := json.MarshalIndent(keys, "", " ") if err != nil { return fmt.Errorf("marshal keys metadata: %w", err) } - cmd.Println(string(details)) + p.Outputln(string(details)) return nil default: diff --git a/internal/cmd/service-account/key/update/update.go b/internal/cmd/service-account/key/update/update.go index d0440c1d1..2d44080f9 100644 --- a/internal/cmd/service-account/key/update/update.go +++ b/internal/cmd/service-account/key/update/update.go @@ -12,6 +12,7 @@ import ( "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/print" "github.com/stackitcloud/stackit-cli/internal/pkg/services/service-account/client" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -38,7 +39,7 @@ type inputModel struct { Deactivate bool } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("update %s", keyIdArg), Short: "Updates a service account key", @@ -90,7 +91,7 @@ func NewCmd() *cobra.Command { if err != nil { return fmt.Errorf("marshal key: %w", err) } - cmd.Println(string(key)) + p.Info(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 6d419e0c9..9e15d4d14 100644 --- a/internal/cmd/service-account/key/update/update_test.go +++ b/internal/cmd/service-account/key/update/update_test.go @@ -194,7 +194,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { - cmd := NewCmd() + cmd := NewCmd(nil) 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 8f72f5ccf..30653e2d4 100644 --- a/internal/cmd/service-account/list/list.go +++ b/internal/cmd/service-account/list/list.go @@ -10,6 +10,7 @@ import ( "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/print" "github.com/stackitcloud/stackit-cli/internal/pkg/projectname" "github.com/stackitcloud/stackit-cli/internal/pkg/services/service-account/client" "github.com/stackitcloud/stackit-cli/internal/pkg/tables" @@ -27,7 +28,7 @@ type inputModel struct { Limit *int64 } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: "list", Short: "Lists all service accounts", @@ -63,7 +64,7 @@ func NewCmd() *cobra.Command { if err != nil { projectLabel = model.ProjectId } - cmd.Printf("No service accounts found for project %q\n", projectLabel) + p.Info("No service accounts found for project %q\n", projectLabel) return nil } @@ -72,7 +73,7 @@ func NewCmd() *cobra.Command { serviceAccounts = serviceAccounts[:*model.Limit] } - return outputResult(cmd, model.OutputFormat, serviceAccounts) + return outputResult(cmd, model.OutputFormat, serviceAccounts, p) }, } @@ -109,14 +110,14 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *serviceacco return req } -func outputResult(cmd *cobra.Command, outputFormat string, serviceAccounts []serviceaccount.ServiceAccount) error { +func outputResult(cmd *cobra.Command, outputFormat string, serviceAccounts []serviceaccount.ServiceAccount, p *print.Printer) error { switch outputFormat { case globalflags.JSONOutputFormat: details, err := json.MarshalIndent(serviceAccounts, "", " ") if err != nil { return fmt.Errorf("marshal service accounts list: %w", err) } - cmd.Println(string(details)) + p.Outputln(string(details)) default: table := tables.NewTable() table.SetHeader("ID", "EMAIL") diff --git a/internal/cmd/service-account/service_account.go b/internal/cmd/service-account/service_account.go index 3669100d4..8a4f4634d 100644 --- a/internal/cmd/service-account/service_account.go +++ b/internal/cmd/service-account/service_account.go @@ -8,12 +8,13 @@ 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() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: "service-account", Short: "Provides functionality for service accounts", @@ -21,16 +22,16 @@ func NewCmd() *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd) + addSubcommands(cmd, p) return cmd } -func addSubcommands(cmd *cobra.Command) { - cmd.AddCommand(list.NewCmd()) - cmd.AddCommand(create.NewCmd()) - cmd.AddCommand(delete.NewCmd()) - cmd.AddCommand(getjwks.NewCmd()) +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)) - cmd.AddCommand(key.NewCmd()) - cmd.AddCommand(token.NewCmd()) + cmd.AddCommand(key.NewCmd(p)) + cmd.AddCommand(token.NewCmd(p)) } diff --git a/internal/cmd/service-account/token/create/create.go b/internal/cmd/service-account/token/create/create.go index 71e050c05..26c3ca5ba 100644 --- a/internal/cmd/service-account/token/create/create.go +++ b/internal/cmd/service-account/token/create/create.go @@ -10,6 +10,7 @@ import ( "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/print" "github.com/stackitcloud/stackit-cli/internal/pkg/services/service-account/client" "github.com/spf13/cobra" @@ -30,7 +31,7 @@ type inputModel struct { TTLDays *int64 } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: "create", Short: "Creates an access token for a service account", @@ -76,9 +77,9 @@ func NewCmd() *cobra.Command { return fmt.Errorf("create access token: %w", err) } - cmd.Printf("Created access token for service account %s. Token ID: %s\n\n", model.ServiceAccountEmail, *token.Id) - cmd.Printf("Valid until: %s\n", *token.ValidUntil) - cmd.Printf("Token: %s\n", *token.Token) + p.Outputf("Created access token for service account %s. Token ID: %s\n\n", model.ServiceAccountEmail, *token.Id) + p.Outputf("Valid until: %s\n", *token.ValidUntil) + p.Outputf("Token: %s\n", *token.Token) return nil }, } diff --git a/internal/cmd/service-account/token/create/create_test.go b/internal/cmd/service-account/token/create/create_test.go index 0813a9573..edaeca401 100644 --- a/internal/cmd/service-account/token/create/create_test.go +++ b/internal/cmd/service-account/token/create/create_test.go @@ -119,7 +119,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { - cmd := NewCmd() + cmd := NewCmd(nil) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/service-account/token/list/list.go b/internal/cmd/service-account/token/list/list.go index 66aa10dc3..4df3b55f5 100644 --- a/internal/cmd/service-account/token/list/list.go +++ b/internal/cmd/service-account/token/list/list.go @@ -10,6 +10,7 @@ import ( "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/print" "github.com/stackitcloud/stackit-cli/internal/pkg/services/service-account/client" "github.com/stackitcloud/stackit-cli/internal/pkg/tables" @@ -29,7 +30,7 @@ type inputModel struct { Limit *int64 } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: "list", Short: "Lists access tokens of a service account", @@ -71,7 +72,7 @@ func NewCmd() *cobra.Command { } tokensMetadata := *resp.Items if len(tokensMetadata) == 0 { - cmd.Printf("No tokens found for service account with email %q\n", model.ServiceAccountEmail) + p.Info("No tokens found for service account with email %q\n", model.ServiceAccountEmail) return nil } @@ -80,7 +81,7 @@ func NewCmd() *cobra.Command { tokensMetadata = tokensMetadata[:*model.Limit] } - return outputResult(cmd, model.OutputFormat, tokensMetadata) + return outputResult(cmd, model.OutputFormat, tokensMetadata, p) }, } @@ -130,14 +131,14 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *serviceacco return req } -func outputResult(cmd *cobra.Command, outputFormat string, tokensMetadata []serviceaccount.AccessTokenMetadata) error { +func outputResult(cmd *cobra.Command, outputFormat string, tokensMetadata []serviceaccount.AccessTokenMetadata, p *print.Printer) error { switch outputFormat { case globalflags.JSONOutputFormat: details, err := json.MarshalIndent(tokensMetadata, "", " ") if err != nil { return fmt.Errorf("marshal tokens metadata: %w", err) } - cmd.Println(string(details)) + p.Outputln(string(details)) return nil default: diff --git a/internal/cmd/service-account/token/revoke/revoke.go b/internal/cmd/service-account/token/revoke/revoke.go index 15ab869a6..73cf64f78 100644 --- a/internal/cmd/service-account/token/revoke/revoke.go +++ b/internal/cmd/service-account/token/revoke/revoke.go @@ -10,6 +10,7 @@ import ( "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/print" "github.com/stackitcloud/stackit-cli/internal/pkg/services/service-account/client" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -29,7 +30,7 @@ type inputModel struct { TokenId string } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("revoke %s", tokenIdArg), Short: "Revokes an access token of a service account", @@ -72,7 +73,7 @@ func NewCmd() *cobra.Command { return fmt.Errorf("revoke access token: %w", err) } - cmd.Printf("Revoked access token with ID %q\n", model.TokenId) + p.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 8e8a5fbe4..824f210d5 100644 --- a/internal/cmd/service-account/token/revoke/revoke_test.go +++ b/internal/cmd/service-account/token/revoke/revoke_test.go @@ -147,7 +147,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { - cmd := NewCmd() + cmd := NewCmd(nil) 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 7233f8929..af6d363d4 100644 --- a/internal/cmd/service-account/token/token.go +++ b/internal/cmd/service-account/token/token.go @@ -5,12 +5,13 @@ import ( "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() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: "token", Short: "Provides functionality regarding service account tokens", @@ -18,12 +19,12 @@ func NewCmd() *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd) + addSubcommands(cmd, p) return cmd } -func addSubcommands(cmd *cobra.Command) { - cmd.AddCommand(create.NewCmd()) - cmd.AddCommand(list.NewCmd()) - cmd.AddCommand(revoke.NewCmd()) +func addSubcommands(cmd *cobra.Command, p *print.Printer) { + cmd.AddCommand(create.NewCmd(p)) + cmd.AddCommand(list.NewCmd(p)) + cmd.AddCommand(revoke.NewCmd(p)) } diff --git a/internal/cmd/ske/cluster/cluster.go b/internal/cmd/ske/cluster/cluster.go index 478bd0e32..c5bb7b36b 100644 --- a/internal/cmd/ske/cluster/cluster.go +++ b/internal/cmd/ske/cluster/cluster.go @@ -8,12 +8,13 @@ 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() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: "cluster", Short: "Provides functionality for SKE cluster", @@ -21,15 +22,15 @@ func NewCmd() *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd) + addSubcommands(cmd, p) return cmd } -func addSubcommands(cmd *cobra.Command) { - cmd.AddCommand(generatepayload.NewCmd()) - cmd.AddCommand(create.NewCmd()) - cmd.AddCommand(delete.NewCmd()) - cmd.AddCommand(describe.NewCmd()) - cmd.AddCommand(list.NewCmd()) - cmd.AddCommand(update.NewCmd()) +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)) } diff --git a/internal/cmd/ske/cluster/create/create.go b/internal/cmd/ske/cluster/create/create.go index 81c72d2f8..1d30718c4 100644 --- a/internal/cmd/ske/cluster/create/create.go +++ b/internal/cmd/ske/cluster/create/create.go @@ -11,6 +11,7 @@ import ( "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/print" "github.com/stackitcloud/stackit-cli/internal/pkg/projectname" "github.com/stackitcloud/stackit-cli/internal/pkg/services/ske/client" skeUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/ske/utils" @@ -33,7 +34,7 @@ type inputModel struct { Payload *ske.CreateOrUpdateClusterPayload } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("create %s", clusterNameArg), Short: "Creates an SKE cluster", @@ -135,7 +136,7 @@ func NewCmd() *cobra.Command { if model.Async { operationState = "Triggered creation of" } - cmd.Printf("%s cluster for project %q. Cluster name: %s\n", operationState, projectLabel, name) + p.Outputf("%s cluster for project %q. Cluster name: %s\n", operationState, projectLabel, name) return nil }, } diff --git a/internal/cmd/ske/cluster/create/create_test.go b/internal/cmd/ske/cluster/create/create_test.go index 06aee5249..2583bb864 100644 --- a/internal/cmd/ske/cluster/create/create_test.go +++ b/internal/cmd/ske/cluster/create/create_test.go @@ -225,7 +225,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { - cmd := NewCmd() + cmd := NewCmd(nil) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/ske/cluster/delete/delete.go b/internal/cmd/ske/cluster/delete/delete.go index fb8412554..94e3dd44f 100644 --- a/internal/cmd/ske/cluster/delete/delete.go +++ b/internal/cmd/ske/cluster/delete/delete.go @@ -9,6 +9,7 @@ import ( "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" + "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/services/ske/client" "github.com/stackitcloud/stackit-cli/internal/pkg/spinner" @@ -26,7 +27,7 @@ type inputModel struct { ClusterName string } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("delete %s", clusterNameArg), Short: "Deletes a SKE cluster", @@ -80,7 +81,7 @@ func NewCmd() *cobra.Command { if model.Async { operationState = "Triggered deletion of" } - cmd.Printf("%s cluster %q\n", operationState, model.ClusterName) + p.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 8b0c0f3af..cb2d3aca0 100644 --- a/internal/cmd/ske/cluster/delete/delete_test.go +++ b/internal/cmd/ske/cluster/delete/delete_test.go @@ -124,7 +124,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { - cmd := NewCmd() + cmd := NewCmd(nil) 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 84687b7a8..528d99ed5 100644 --- a/internal/cmd/ske/cluster/describe/describe.go +++ b/internal/cmd/ske/cluster/describe/describe.go @@ -9,6 +9,7 @@ import ( "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" + "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/services/ske/client" "github.com/stackitcloud/stackit-cli/internal/pkg/tables" @@ -25,7 +26,7 @@ type inputModel struct { ClusterName string } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("describe %s", clusterNameArg), Short: "Shows details of a SKE cluster", @@ -58,7 +59,7 @@ func NewCmd() *cobra.Command { return fmt.Errorf("read SKE cluster: %w", err) } - return outputResult(cmd, model.OutputFormat, resp) + return outputResult(cmd, model.OutputFormat, resp, p) }, } return cmd @@ -83,7 +84,7 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *ske.APIClie return req } -func outputResult(cmd *cobra.Command, outputFormat string, cluster *ske.Cluster) error { +func outputResult(cmd *cobra.Command, outputFormat string, cluster *ske.Cluster, p *print.Printer) error { switch outputFormat { case globalflags.PrettyOutputFormat: @@ -111,7 +112,7 @@ func outputResult(cmd *cobra.Command, outputFormat string, cluster *ske.Cluster) if err != nil { return fmt.Errorf("marshal SKE cluster: %w", err) } - cmd.Println(string(details)) + p.Outputln(string(details)) return nil } diff --git a/internal/cmd/ske/cluster/describe/describe_test.go b/internal/cmd/ske/cluster/describe/describe_test.go index 48fa4614a..833db3b0e 100644 --- a/internal/cmd/ske/cluster/describe/describe_test.go +++ b/internal/cmd/ske/cluster/describe/describe_test.go @@ -124,7 +124,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { - cmd := NewCmd() + cmd := NewCmd(nil) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/ske/cluster/generate-payload/generate_payload.go b/internal/cmd/ske/cluster/generate-payload/generate_payload.go index 94eab9704..6bb2f1e20 100644 --- a/internal/cmd/ske/cluster/generate-payload/generate_payload.go +++ b/internal/cmd/ske/cluster/generate-payload/generate_payload.go @@ -10,6 +10,7 @@ import ( "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/print" "github.com/stackitcloud/stackit-cli/internal/pkg/services/ske/client" skeUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/ske/utils" @@ -26,7 +27,7 @@ type inputModel struct { ClusterName *string } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: "generate-payload", Short: "Generates a payload to create/update SKE clusters", @@ -82,7 +83,7 @@ func NewCmd() *cobra.Command { } } - return outputResult(cmd, payload) + return outputResult(payload, p) }, } configureFlags(cmd) @@ -113,12 +114,12 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *ske.APIClie return req } -func outputResult(cmd *cobra.Command, payload *ske.CreateOrUpdateClusterPayload) error { +func outputResult(payload *ske.CreateOrUpdateClusterPayload, p *print.Printer) error { payloadBytes, err := json.MarshalIndent(*payload, "", " ") if err != nil { return fmt.Errorf("marshal payload: %w", err) } - cmd.Println(string(payloadBytes)) + p.Outputln(string(payloadBytes)) return nil } 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 831832835..0b5500dcd 100644 --- a/internal/cmd/ske/cluster/generate-payload/generate_payload_test.go +++ b/internal/cmd/ske/cluster/generate-payload/generate_payload_test.go @@ -110,7 +110,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { - cmd := NewCmd() + cmd := NewCmd(nil) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/ske/cluster/list/list.go b/internal/cmd/ske/cluster/list/list.go index d8468b0f8..fb6777387 100644 --- a/internal/cmd/ske/cluster/list/list.go +++ b/internal/cmd/ske/cluster/list/list.go @@ -10,6 +10,7 @@ import ( "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/print" "github.com/stackitcloud/stackit-cli/internal/pkg/projectname" "github.com/stackitcloud/stackit-cli/internal/pkg/services/ske/client" skeUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/ske/utils" @@ -28,7 +29,7 @@ type inputModel struct { Limit *int64 } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: "list", Short: "Lists all SKE clusters", @@ -79,7 +80,7 @@ func NewCmd() *cobra.Command { if err != nil { projectLabel = model.ProjectId } - cmd.Printf("No clusters found for project %q\n", projectLabel) + p.Info("No clusters found for project %q\n", projectLabel) return nil } @@ -88,7 +89,7 @@ func NewCmd() *cobra.Command { clusters = clusters[:*model.Limit] } - return outputResult(cmd, model.OutputFormat, clusters) + return outputResult(cmd, model.OutputFormat, clusters, p) }, } @@ -125,14 +126,14 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *ske.APIClie return req } -func outputResult(cmd *cobra.Command, outputFormat string, clusters []ske.Cluster) error { +func outputResult(cmd *cobra.Command, outputFormat string, clusters []ske.Cluster, p *print.Printer) error { switch outputFormat { case globalflags.JSONOutputFormat: details, err := json.MarshalIndent(clusters, "", " ") if err != nil { return fmt.Errorf("marshal SKE cluster list: %w", err) } - cmd.Println(string(details)) + p.Outputln(string(details)) return nil default: diff --git a/internal/cmd/ske/cluster/update/update.go b/internal/cmd/ske/cluster/update/update.go index a20ea709b..bc71db85e 100644 --- a/internal/cmd/ske/cluster/update/update.go +++ b/internal/cmd/ske/cluster/update/update.go @@ -11,6 +11,7 @@ import ( "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/print" "github.com/stackitcloud/stackit-cli/internal/pkg/services/ske/client" skeUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/ske/utils" "github.com/stackitcloud/stackit-cli/internal/pkg/spinner" @@ -32,7 +33,7 @@ type inputModel struct { Payload ske.CreateOrUpdateClusterPayload } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("update %s", clusterNameArg), Short: "Updates an SKE cluster", @@ -108,7 +109,7 @@ func NewCmd() *cobra.Command { if model.Async { operationState = "Triggered update of" } - cmd.Printf("%s cluster %q\n", operationState, model.ClusterName) + p.Info("%s cluster %q\n", operationState, model.ClusterName) return nil }, } diff --git a/internal/cmd/ske/cluster/update/update_test.go b/internal/cmd/ske/cluster/update/update_test.go index fd602df4c..74e98972c 100644 --- a/internal/cmd/ske/cluster/update/update_test.go +++ b/internal/cmd/ske/cluster/update/update_test.go @@ -213,7 +213,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { - cmd := NewCmd() + cmd := NewCmd(nil) 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 be20e1a72..582f5247f 100644 --- a/internal/cmd/ske/credentials/credentials.go +++ b/internal/cmd/ske/credentials/credentials.go @@ -4,12 +4,13 @@ import ( "github.com/stackitcloud/stackit-cli/internal/cmd/ske/credentials/describe" "github.com/stackitcloud/stackit-cli/internal/cmd/ske/credentials/rotate" "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() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: "credentials", Short: "Provides functionality for SKE credentials", @@ -17,11 +18,11 @@ func NewCmd() *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd) + addSubcommands(cmd, p) return cmd } -func addSubcommands(cmd *cobra.Command) { - cmd.AddCommand(describe.NewCmd()) - cmd.AddCommand(rotate.NewCmd()) +func addSubcommands(cmd *cobra.Command, p *print.Printer) { + cmd.AddCommand(describe.NewCmd(p)) + cmd.AddCommand(rotate.NewCmd(p)) } diff --git a/internal/cmd/ske/credentials/describe/describe.go b/internal/cmd/ske/credentials/describe/describe.go index 01917510c..402a18cd5 100644 --- a/internal/cmd/ske/credentials/describe/describe.go +++ b/internal/cmd/ske/credentials/describe/describe.go @@ -9,6 +9,7 @@ import ( "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" + "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/services/ske/client" skeUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/ske/utils" "github.com/stackitcloud/stackit-cli/internal/pkg/tables" @@ -26,7 +27,7 @@ type inputModel struct { ClusterName string } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("describe %s", clusterNameArg), Short: "Shows details of the credentials associated to a SKE cluster", @@ -69,7 +70,7 @@ func NewCmd() *cobra.Command { return fmt.Errorf("get SKE credentials: %w", err) } - return outputResult(cmd, model.OutputFormat, resp) + return outputResult(cmd, model.OutputFormat, resp, p) }, } return cmd @@ -94,7 +95,7 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *ske.APIClie return req } -func outputResult(cmd *cobra.Command, outputFormat string, credentials *ske.Credentials) error { +func outputResult(cmd *cobra.Command, outputFormat string, credentials *ske.Credentials, p *print.Printer) error { switch outputFormat { case globalflags.PrettyOutputFormat: table := tables.NewTable() @@ -112,7 +113,7 @@ func outputResult(cmd *cobra.Command, outputFormat string, credentials *ske.Cred if err != nil { return fmt.Errorf("marshal SKE credentials: %w", err) } - cmd.Println(string(details)) + p.Outputln(string(details)) return nil } diff --git a/internal/cmd/ske/credentials/describe/describe_test.go b/internal/cmd/ske/credentials/describe/describe_test.go index faf8b5034..35dc8b801 100644 --- a/internal/cmd/ske/credentials/describe/describe_test.go +++ b/internal/cmd/ske/credentials/describe/describe_test.go @@ -124,7 +124,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { - cmd := NewCmd() + cmd := NewCmd(nil) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/ske/credentials/rotate/rotate.go b/internal/cmd/ske/credentials/rotate/rotate.go index cba8ea31e..74800fb30 100644 --- a/internal/cmd/ske/credentials/rotate/rotate.go +++ b/internal/cmd/ske/credentials/rotate/rotate.go @@ -9,6 +9,7 @@ import ( "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" + "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/services/ske/client" "github.com/stackitcloud/stackit-cli/internal/pkg/spinner" @@ -26,7 +27,7 @@ type inputModel struct { ClusterName string } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("rotate %s", clusterNameArg), Short: "Rotates credentials associated to a SKE cluster", @@ -80,7 +81,7 @@ func NewCmd() *cobra.Command { if model.Async { operationState = "Triggered rotation of" } - cmd.Printf("%s credentials for cluster %q\n", operationState, model.ClusterName) + p.Info("%s credentials for cluster %q\n", operationState, model.ClusterName) return nil }, } diff --git a/internal/cmd/ske/credentials/rotate/rotate_test.go b/internal/cmd/ske/credentials/rotate/rotate_test.go index f137e9698..e7d5713be 100644 --- a/internal/cmd/ske/credentials/rotate/rotate_test.go +++ b/internal/cmd/ske/credentials/rotate/rotate_test.go @@ -124,7 +124,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { - cmd := NewCmd() + cmd := NewCmd(nil) 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 5fd32a095..876b479b5 100644 --- a/internal/cmd/ske/describe/describe.go +++ b/internal/cmd/ske/describe/describe.go @@ -9,6 +9,7 @@ import ( "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" + "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/services/ske/client" "github.com/stackitcloud/stackit-cli/internal/pkg/tables" @@ -20,7 +21,7 @@ type inputModel struct { *globalflags.GlobalFlagModel } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: "describe", Short: "Shows overall details regarding SKE", @@ -50,7 +51,7 @@ func NewCmd() *cobra.Command { return fmt.Errorf("read SKE project details: %w", err) } - return outputResult(cmd, model.OutputFormat, resp) + return outputResult(cmd, model.OutputFormat, resp, p) }, } return cmd @@ -72,7 +73,7 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *ske.APIClie return req } -func outputResult(cmd *cobra.Command, outputFormat string, project *ske.ProjectResponse) error { +func outputResult(cmd *cobra.Command, outputFormat string, project *ske.ProjectResponse, p *print.Printer) error { switch outputFormat { case globalflags.PrettyOutputFormat: table := tables.NewTable() @@ -90,7 +91,7 @@ func outputResult(cmd *cobra.Command, outputFormat string, project *ske.ProjectR if err != nil { return fmt.Errorf("marshal SKE project details: %w", err) } - cmd.Println(string(details)) + p.Outputln(string(details)) return nil } diff --git a/internal/cmd/ske/disable/disable.go b/internal/cmd/ske/disable/disable.go index 3986b4e64..edf0a4593 100644 --- a/internal/cmd/ske/disable/disable.go +++ b/internal/cmd/ske/disable/disable.go @@ -9,6 +9,7 @@ import ( "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" + "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/projectname" "github.com/stackitcloud/stackit-cli/internal/pkg/services/ske/client" "github.com/stackitcloud/stackit-cli/internal/pkg/spinner" @@ -22,7 +23,7 @@ type InputModel struct { *globalflags.GlobalFlagModel } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: "disable", Short: "Disables SKE for a project", @@ -81,7 +82,7 @@ func NewCmd() *cobra.Command { if model.Async { operationState = "Triggered disablement of" } - cmd.Printf("%s SKE for project %q\n", operationState, projectLabel) + p.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 8a576e0ea..c2af18075 100644 --- a/internal/cmd/ske/enable/enable.go +++ b/internal/cmd/ske/enable/enable.go @@ -9,6 +9,7 @@ import ( "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" + "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/projectname" "github.com/stackitcloud/stackit-cli/internal/pkg/services/ske/client" "github.com/stackitcloud/stackit-cli/internal/pkg/spinner" @@ -22,7 +23,7 @@ type InputModel struct { *globalflags.GlobalFlagModel } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: "enable", Short: "Enables SKE for a project", @@ -81,7 +82,7 @@ func NewCmd() *cobra.Command { if model.Async { operationState = "Triggered enablement of" } - cmd.Printf("%s SKE for project %q\n", operationState, projectLabel) + p.Info("%s SKE for project %q\n", operationState, projectLabel) return nil }, } diff --git a/internal/cmd/ske/options/options.go b/internal/cmd/ske/options/options.go index a68917298..9fd9dfb36 100644 --- a/internal/cmd/ske/options/options.go +++ b/internal/cmd/ske/options/options.go @@ -11,6 +11,7 @@ import ( "github.com/stackitcloud/stackit-cli/internal/pkg/flags" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/pager" + "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/services/ske/client" "github.com/stackitcloud/stackit-cli/internal/pkg/tables" @@ -35,7 +36,7 @@ type inputModel struct { VolumeTypes bool } -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: "options", Short: "Lists SKE provider options", @@ -75,7 +76,7 @@ func NewCmd() *cobra.Command { return fmt.Errorf("get SKE provider options: %w", err) } - return outputResult(cmd, model, resp) + return outputResult(cmd, model, resp, p) }, } configureFlags(cmd) @@ -122,14 +123,14 @@ func buildRequest(ctx context.Context, apiClient *ske.APIClient) ske.ApiListProv return req } -func outputResult(cmd *cobra.Command, model *inputModel, options *ske.ProviderOptions) error { +func outputResult(cmd *cobra.Command, model *inputModel, options *ske.ProviderOptions, p *print.Printer) error { switch model.OutputFormat { case globalflags.JSONOutputFormat: details, err := json.MarshalIndent(options, "", " ") if err != nil { return fmt.Errorf("marshal SKE options: %w", err) } - cmd.Println(string(details)) + p.Outputln(string(details)) return nil default: return outputResultAsTable(cmd, model, options) diff --git a/internal/cmd/ske/options/options_test.go b/internal/cmd/ske/options/options_test.go index a9e86f11f..05b1a50e0 100644 --- a/internal/cmd/ske/options/options_test.go +++ b/internal/cmd/ske/options/options_test.go @@ -116,7 +116,7 @@ func TestParseInput(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { - cmd := NewCmd() + cmd := NewCmd(nil) err := globalflags.Configure(cmd.Flags()) if err != nil { t.Fatalf("configure global flags: %v", err) diff --git a/internal/cmd/ske/ske.go b/internal/cmd/ske/ske.go index ebfe89036..4b497ab20 100644 --- a/internal/cmd/ske/ske.go +++ b/internal/cmd/ske/ske.go @@ -8,12 +8,13 @@ import ( "github.com/stackitcloud/stackit-cli/internal/cmd/ske/enable" "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() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: "ske", Short: "Provides functionality for SKE", @@ -21,15 +22,15 @@ func NewCmd() *cobra.Command { Args: args.NoArgs, Run: utils.CmdHelp, } - addSubcommands(cmd) + addSubcommands(cmd, p) return cmd } -func addSubcommands(cmd *cobra.Command) { - cmd.AddCommand(describe.NewCmd()) - cmd.AddCommand(enable.NewCmd()) - cmd.AddCommand(disable.NewCmd()) - cmd.AddCommand(cluster.NewCmd()) - cmd.AddCommand(credentials.NewCmd()) - cmd.AddCommand(options.NewCmd()) +func addSubcommands(cmd *cobra.Command, p *print.Printer) { + cmd.AddCommand(describe.NewCmd(p)) + cmd.AddCommand(enable.NewCmd(p)) + cmd.AddCommand(disable.NewCmd(p)) + cmd.AddCommand(cluster.NewCmd(p)) + cmd.AddCommand(credentials.NewCmd(p)) + cmd.AddCommand(options.NewCmd(p)) }