diff --git a/internal/cmd/beta/alb/list/list.go b/internal/cmd/beta/alb/list/list.go index 2c4d8924d..292e2e3ae 100644 --- a/internal/cmd/beta/alb/list/list.go +++ b/internal/cmd/beta/alb/list/list.go @@ -25,8 +25,7 @@ type inputModel struct { } const ( - labelSelectorFlag = "label-selector" - limitFlag = "limit" + limitFlag = "limit" ) func NewCmd(params *params.CmdParams) *cobra.Command { @@ -73,19 +72,14 @@ func NewCmd(params *params.CmdParams) *cobra.Command { if err != nil { return fmt.Errorf("list load balancerse: %w", err) } + items := response.GetLoadBalancers() - if items := response.LoadBalancers; items == nil || len(*items) == 0 { - params.Printer.Info("No load balancers found for project %q", projectLabel) - } else { - if model.Limit != nil && len(*items) > int(*model.Limit) { - *items = (*items)[:*model.Limit] - } - if err := outputResult(params.Printer, model.OutputFormat, *items); err != nil { - return fmt.Errorf("output loadbalancers: %w", err) - } + // Truncate output + if model.Limit != nil && len(items) > int(*model.Limit) { + items = items[:*model.Limit] } - return nil + return outputResult(params.Printer, model.OutputFormat, projectLabel, items) }, } @@ -125,8 +119,13 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *alb.APIClie return request } -func outputResult(p *print.Printer, outputFormat string, items []alb.LoadBalancer) error { +func outputResult(p *print.Printer, outputFormat, projectLabel string, items []alb.LoadBalancer) error { return p.OutputResult(outputFormat, items, func() error { + if len(items) == 0 { + p.Outputf("No load balancers found for project %q", projectLabel) + return nil + } + table := tables.NewTable() table.SetHeader("NAME", "EXTERNAL ADDRESS", "REGION", "STATUS", "VERSION", "ERRORS") for i := range items { diff --git a/internal/cmd/beta/alb/list/list_test.go b/internal/cmd/beta/alb/list/list_test.go index c623ea0b8..ba44e216e 100644 --- a/internal/cmd/beta/alb/list/list_test.go +++ b/internal/cmd/beta/alb/list/list_test.go @@ -2,6 +2,7 @@ package list import ( "context" + "github.com/stackitcloud/stackit-cli/internal/pkg/utils" "strconv" "testing" @@ -19,11 +20,14 @@ import ( type testCtxKey struct{} var ( - testCtx = context.WithValue(context.Background(), testCtxKey{}, "foo") - testClient = &alb.APIClient{} - testProjectId = uuid.NewString() - testRegion = "eu01" - testLimit int64 = 10 + testCtx = context.WithValue(context.Background(), testCtxKey{}, "foo") + testClient = &alb.APIClient{} + testProjectId = uuid.NewString() +) + +const ( + testRegion = "eu01" + testLimit int64 = 10 ) func fixtureFlagValues(mods ...func(flagValues map[string]string)) map[string]string { @@ -41,7 +45,7 @@ func fixtureFlagValues(mods ...func(flagValues map[string]string)) map[string]st func fixtureInputModel(mods ...func(model *inputModel)) *inputModel { model := &inputModel{ GlobalFlagModel: &globalflags.GlobalFlagModel{ProjectId: testProjectId, Region: testRegion, Verbosity: globalflags.VerbosityDefault}, - Limit: &testLimit, + Limit: utils.Ptr(testLimit), } for _, mod := range mods { mod(model) @@ -136,6 +140,7 @@ func TestBuildRequest(t *testing.T) { func Test_outputResult(t *testing.T) { type args struct { outputFormat string + projectLabel string items []alb.LoadBalancer } tests := []struct { @@ -164,7 +169,7 @@ func Test_outputResult(t *testing.T) { p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if err := outputResult(p, tt.args.outputFormat, tt.args.items); (err != nil) != tt.wantErr { + if err := outputResult(p, tt.args.outputFormat, tt.args.projectLabel, tt.args.items); (err != nil) != tt.wantErr { t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr) } }) diff --git a/internal/cmd/beta/alb/observability-credentials/list/list.go b/internal/cmd/beta/alb/observability-credentials/list/list.go index 961455b53..272511702 100644 --- a/internal/cmd/beta/alb/observability-credentials/list/list.go +++ b/internal/cmd/beta/alb/observability-credentials/list/list.go @@ -68,13 +68,9 @@ func NewCmd(params *params.CmdParams) *cobra.Command { if err != nil { return fmt.Errorf("list credentials: %w", err) } + items := resp.GetCredentials() - if resp.Credentials == nil || len(*resp.Credentials) == 0 { - params.Printer.Info("No credentials found\n") - return nil - } - - items := *resp.Credentials + // Truncate output if model.Limit != nil && len(items) > int(*model.Limit) { items = items[:*model.Limit] } @@ -122,6 +118,11 @@ func outputResult(p *print.Printer, outputFormat string, items []alb.Credentials } return p.OutputResult(outputFormat, items, func() error { + if len(items) == 0 { + p.Outputf("No credentials found\n") + return nil + } + table := tables.NewTable() table.SetHeader("CREDENTIAL REF", "DISPLAYNAME", "USERNAME", "REGION") diff --git a/internal/cmd/beta/alb/plans/plans.go b/internal/cmd/beta/alb/plans/plans.go index 6bb5e01fc..0724860a1 100644 --- a/internal/cmd/beta/alb/plans/plans.go +++ b/internal/cmd/beta/alb/plans/plans.go @@ -62,16 +62,9 @@ func NewCmd(params *params.CmdParams) *cobra.Command { if err != nil { return fmt.Errorf("list plans: %w", err) } + items := response.GetValidPlans() - if items := response.ValidPlans; items == nil || len(*items) == 0 { - params.Printer.Info("No plans found for project %q", projectLabel) - } else { - if err := outputResult(params.Printer, model.OutputFormat, *items); err != nil { - return fmt.Errorf("output plans: %w", err) - } - } - - return nil + return outputResult(params.Printer, model.OutputFormat, projectLabel, items) }, } @@ -98,8 +91,13 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *alb.APIClie return request } -func outputResult(p *print.Printer, outputFormat string, items []alb.PlanDetails) error { +func outputResult(p *print.Printer, outputFormat, projectLabel string, items []alb.PlanDetails) error { return p.OutputResult(outputFormat, items, func() error { + if len(items) == 0 { + p.Outputf("No plans found for project %q", projectLabel) + return nil + } + table := tables.NewTable() table.SetHeader("PLAN ID", "NAME", "FLAVOR", "MAX CONNS", "DESCRIPTION") for _, item := range items { diff --git a/internal/cmd/beta/alb/plans/plans_test.go b/internal/cmd/beta/alb/plans/plans_test.go index 1a31d711b..ebb7ed8f0 100644 --- a/internal/cmd/beta/alb/plans/plans_test.go +++ b/internal/cmd/beta/alb/plans/plans_test.go @@ -21,9 +21,10 @@ var ( testCtx = context.WithValue(context.Background(), testCtxKey{}, "foo") testClient = &alb.APIClient{} testProjectId = uuid.NewString() - testRegion = "eu01" ) +const testRegion = "eu01" + func fixtureFlagValues(mods ...func(flagValues map[string]string)) map[string]string { flagValues := map[string]string{ globalflags.ProjectIdFlag: testProjectId, @@ -132,6 +133,7 @@ func TestBuildRequest(t *testing.T) { func Test_outputResult(t *testing.T) { type args struct { outputFormat string + projectLabel string items []alb.PlanDetails } tests := []struct { @@ -160,7 +162,7 @@ func Test_outputResult(t *testing.T) { p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if err := outputResult(p, tt.args.outputFormat, tt.args.items); (err != nil) != tt.wantErr { + if err := outputResult(p, tt.args.outputFormat, tt.args.projectLabel, tt.args.items); (err != nil) != tt.wantErr { t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr) } })