Skip to content

Commit 75e2b9d

Browse files
committed
adapted observability plans list command to return valid json and yaml when no plans are found
1 parent f4bfba9 commit 75e2b9d

2 files changed

Lines changed: 15 additions & 12 deletions

File tree

internal/cmd/observability/plans/plans.go

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -66,23 +66,21 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
6666
if err != nil {
6767
return fmt.Errorf("get Observability service plans: %w", err)
6868
}
69-
plans := *resp.Plans
70-
if len(plans) == 0 {
71-
projectLabel, err := projectname.GetProjectName(ctx, params.Printer, params.CliVersion, cmd)
72-
if err != nil {
73-
params.Printer.Debug(print.ErrorLevel, "get project name: %v", err)
74-
projectLabel = model.ProjectId
75-
}
76-
params.Printer.Info("No plans found for project %q\n", projectLabel)
77-
return nil
69+
70+
plans := resp.GetPlans()
71+
72+
projectLabel, err := projectname.GetProjectName(ctx, params.Printer, params.CliVersion, cmd)
73+
if err != nil {
74+
params.Printer.Debug(print.ErrorLevel, "get project name: %v", err)
75+
projectLabel = model.ProjectId
7876
}
7977

8078
// Truncate output
8179
if model.Limit != nil && len(plans) > int(*model.Limit) {
8280
plans = plans[:*model.Limit]
8381
}
8482

85-
return outputResult(params.Printer, model.OutputFormat, plans)
83+
return outputResult(params.Printer, model.OutputFormat, projectLabel, plans)
8684
},
8785
}
8886

@@ -122,8 +120,12 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *observabili
122120
return req
123121
}
124122

125-
func outputResult(p *print.Printer, outputFormat string, plans []observability.Plan) error {
123+
func outputResult(p *print.Printer, outputFormat, projectLabel string, plans []observability.Plan) error {
126124
return p.OutputResult(outputFormat, plans, func() error {
125+
if len(plans) == 0 {
126+
p.Outputf("No plans found for project %q\n", projectLabel)
127+
return nil
128+
}
127129
table := tables.NewTable()
128130
table.SetHeader("ID", "PLAN NAME", "DESCRIPTION")
129131
for i := range plans {

internal/cmd/observability/plans/plans_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ func TestBuildRequest(t *testing.T) {
150150
func TestOutputResult(t *testing.T) {
151151
type args struct {
152152
outputFormat string
153+
projectLabel string
153154
plans []observability.Plan
154155
}
155156
tests := []struct {
@@ -180,7 +181,7 @@ func TestOutputResult(t *testing.T) {
180181
params := testparams.NewTestParams()
181182
for _, tt := range tests {
182183
t.Run(tt.name, func(t *testing.T) {
183-
if err := outputResult(params.Printer, tt.args.outputFormat, tt.args.plans); (err != nil) != tt.wantErr {
184+
if err := outputResult(params.Printer, tt.args.outputFormat, tt.args.projectLabel, tt.args.plans); (err != nil) != tt.wantErr {
184185
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
185186
}
186187
})

0 commit comments

Comments
 (0)