Skip to content

Commit 3f6a8be

Browse files
committed
adapted key pair list command to align to expectations
1 parent 6752d88 commit 3f6a8be

2 files changed

Lines changed: 17 additions & 8 deletions

File tree

internal/cmd/key-pair/list/list.go

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"strings"
77

8+
"github.com/stackitcloud/stackit-cli/internal/pkg/projectname"
89
"github.com/stackitcloud/stackit-cli/internal/pkg/types"
910

1011
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
@@ -70,24 +71,26 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
7071
return err
7172
}
7273

74+
projectLabel, err := projectname.GetProjectName(ctx, params.Printer, params.CliVersion, cmd)
75+
if err != nil {
76+
return fmt.Errorf("list key pairs: %w", err)
77+
}
78+
7379
// Call API
7480
req := buildRequest(ctx, model, apiClient)
7581
resp, err := req.Execute()
7682
if err != nil {
7783
return fmt.Errorf("list key pairs: %w", err)
7884
}
7985

80-
if resp.Items == nil || len(*resp.Items) == 0 {
81-
params.Printer.Info("No key pairs found\n")
82-
return nil
83-
}
86+
items := resp.GetItems()
8487

85-
items := *resp.Items
88+
// Truncate output
8689
if model.Limit != nil && len(items) > int(*model.Limit) {
8790
items = items[:*model.Limit]
8891
}
8992

90-
return outputResult(params.Printer, model.OutputFormat, items)
93+
return outputResult(params.Printer, model.OutputFormat, projectLabel, items)
9194
},
9295
}
9396
configureFlags(cmd)
@@ -128,8 +131,13 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APICli
128131
return req
129132
}
130133

131-
func outputResult(p *print.Printer, outputFormat string, keyPairs []iaas.Keypair) error {
134+
func outputResult(p *print.Printer, outputFormat, projectLabel string, keyPairs []iaas.Keypair) error {
132135
return p.OutputResult(outputFormat, keyPairs, func() error {
136+
if len(keyPairs) == 0 {
137+
p.Outputf("No key pairs found for project %q\n", projectLabel)
138+
return nil
139+
}
140+
133141
table := tables.NewTable()
134142
table.SetHeader("KEY PAIR NAME", "LABELS", "FINGERPRINT", "CREATED AT", "UPDATED AT")
135143

internal/cmd/key-pair/list/list_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ func TestBuildRequest(t *testing.T) {
153153
func Test_outputResult(t *testing.T) {
154154
type args struct {
155155
outputFormat string
156+
projectLabel string
156157
keyPairs []iaas.Keypair
157158
}
158159
tests := []struct {
@@ -176,7 +177,7 @@ func Test_outputResult(t *testing.T) {
176177
t.Run(tt.name, func(t *testing.T) {
177178
params := testparams.NewTestParams()
178179

179-
if err := outputResult(params.Printer, tt.args.outputFormat, tt.args.keyPairs); (err != nil) != tt.wantErr {
180+
if err := outputResult(params.Printer, tt.args.outputFormat, tt.args.projectLabel, tt.args.keyPairs); (err != nil) != tt.wantErr {
180181
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
181182
}
182183
})

0 commit comments

Comments
 (0)