Skip to content

Commit 111062a

Browse files
committed
adapted key pair list command to align to expectations
1 parent fb181fb commit 111062a

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
@@ -155,6 +155,7 @@ func TestBuildRequest(t *testing.T) {
155155
func Test_outputResult(t *testing.T) {
156156
type args struct {
157157
outputFormat string
158+
projectLabel string
158159
keyPairs []iaas.Keypair
159160
}
160161
tests := []struct {
@@ -179,7 +180,7 @@ func Test_outputResult(t *testing.T) {
179180
p := print.NewPrinter()
180181
p.Cmd = NewCmd(&types.CmdParams{Printer: p})
181182

182-
if err := outputResult(p, tt.args.outputFormat, tt.args.keyPairs); (err != nil) != tt.wantErr {
183+
if err := outputResult(p, tt.args.outputFormat, tt.args.projectLabel, tt.args.keyPairs); (err != nil) != tt.wantErr {
183184
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
184185
}
185186
})

0 commit comments

Comments
 (0)