|
5 | 5 | "fmt" |
6 | 6 | "strings" |
7 | 7 |
|
| 8 | + "github.com/stackitcloud/stackit-cli/internal/pkg/projectname" |
8 | 9 | "github.com/stackitcloud/stackit-cli/internal/pkg/types" |
9 | 10 |
|
10 | 11 | "github.com/stackitcloud/stackit-cli/internal/pkg/utils" |
@@ -70,24 +71,26 @@ func NewCmd(params *types.CmdParams) *cobra.Command { |
70 | 71 | return err |
71 | 72 | } |
72 | 73 |
|
| 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 | + |
73 | 79 | // Call API |
74 | 80 | req := buildRequest(ctx, model, apiClient) |
75 | 81 | resp, err := req.Execute() |
76 | 82 | if err != nil { |
77 | 83 | return fmt.Errorf("list key pairs: %w", err) |
78 | 84 | } |
79 | 85 |
|
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() |
84 | 87 |
|
85 | | - items := *resp.Items |
| 88 | + // Truncate output |
86 | 89 | if model.Limit != nil && len(items) > int(*model.Limit) { |
87 | 90 | items = items[:*model.Limit] |
88 | 91 | } |
89 | 92 |
|
90 | | - return outputResult(params.Printer, model.OutputFormat, items) |
| 93 | + return outputResult(params.Printer, model.OutputFormat, projectLabel, items) |
91 | 94 | }, |
92 | 95 | } |
93 | 96 | configureFlags(cmd) |
@@ -128,8 +131,13 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APICli |
128 | 131 | return req |
129 | 132 | } |
130 | 133 |
|
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 { |
132 | 135 | 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 | + |
133 | 141 | table := tables.NewTable() |
134 | 142 | table.SetHeader("KEY PAIR NAME", "LABELS", "FINGERPRINT", "CREATED AT", "UPDATED AT") |
135 | 143 |
|
|
0 commit comments