Skip to content

Commit a43870c

Browse files
committed
adapted observability credentials list command to return valid json and yaml when no credentials are found
1 parent 4864adb commit a43870c

2 files changed

Lines changed: 17 additions & 14 deletions

File tree

internal/cmd/observability/credentials/list/list.go

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -68,22 +68,20 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
6868
if err != nil {
6969
return fmt.Errorf("list Observability credentials: %w", err)
7070
}
71-
credentials := *resp.Credentials
72-
if len(credentials) == 0 {
73-
instanceLabel, err := observabilityUtils.GetInstanceName(ctx, apiClient, model.InstanceId, model.ProjectId)
74-
if err != nil {
75-
params.Printer.Debug(print.ErrorLevel, "get instance name: %v", err)
76-
instanceLabel = model.InstanceId
77-
}
78-
params.Printer.Info("No credentials found for instance %q\n", instanceLabel)
79-
return nil
71+
72+
credentials := resp.GetCredentials()
73+
74+
instanceLabel, err := observabilityUtils.GetInstanceName(ctx, apiClient, model.InstanceId, model.ProjectId)
75+
if err != nil {
76+
params.Printer.Debug(print.ErrorLevel, "get instance name: %v", err)
77+
instanceLabel = model.InstanceId
8078
}
8179

8280
// Truncate output
8381
if model.Limit != nil && len(credentials) > int(*model.Limit) {
8482
credentials = credentials[:*model.Limit]
8583
}
86-
return outputResult(params.Printer, model.OutputFormat, credentials)
84+
return outputResult(params.Printer, model.OutputFormat, instanceLabel, credentials)
8785
},
8886
}
8987
configureFlags(cmd)
@@ -124,8 +122,12 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *observabili
124122
return req
125123
}
126124

127-
func outputResult(p *print.Printer, outputFormat string, credentials []observability.ServiceKeysList) error {
125+
func outputResult(p *print.Printer, outputFormat, instanceLabel string, credentials []observability.ServiceKeysList) error {
128126
return p.OutputResult(outputFormat, credentials, func() error {
127+
if len(credentials) == 0 {
128+
p.Outputf("No credentials found for instance %q\n", instanceLabel)
129+
return nil
130+
}
129131
table := tables.NewTable()
130132
table.SetHeader("USERNAME")
131133
for i := range credentials {

internal/cmd/observability/credentials/list/list_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,9 @@ func TestBuildRequest(t *testing.T) {
173173

174174
func TestOutputResult(t *testing.T) {
175175
type args struct {
176-
outputFormat string
177-
credentials []observability.ServiceKeysList
176+
outputFormat string
177+
instanceLabel string
178+
credentials []observability.ServiceKeysList
178179
}
179180
tests := []struct {
180181
name string
@@ -204,7 +205,7 @@ func TestOutputResult(t *testing.T) {
204205
params := testparams.NewTestParams()
205206
for _, tt := range tests {
206207
t.Run(tt.name, func(t *testing.T) {
207-
if err := outputResult(params.Printer, tt.args.outputFormat, tt.args.credentials); (err != nil) != tt.wantErr {
208+
if err := outputResult(params.Printer, tt.args.outputFormat, tt.args.instanceLabel, tt.args.credentials); (err != nil) != tt.wantErr {
208209
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
209210
}
210211
})

0 commit comments

Comments
 (0)