|
9 | 9 | "github.com/ambient-code/platform/components/ambient-cli/pkg/config" |
10 | 10 | "github.com/ambient-code/platform/components/ambient-cli/pkg/connection" |
11 | 11 | "github.com/ambient-code/platform/components/ambient-cli/pkg/output" |
| 12 | + sdkclient "github.com/ambient-code/platform/components/ambient-sdk/go-sdk/client" |
12 | 13 | sdktypes "github.com/ambient-code/platform/components/ambient-sdk/go-sdk/types" |
13 | 14 | "github.com/spf13/cobra" |
14 | 15 | ) |
@@ -103,7 +104,11 @@ var getCmd = &cobra.Command{ |
103 | 104 | ctx, cancel := context.WithTimeout(context.Background(), cfg.GetRequestTimeout()) |
104 | 105 | defer cancel() |
105 | 106 |
|
106 | | - credential, err := client.Credentials().Get(ctx, args[0]) |
| 107 | + credID, err := resolveCredentialID(ctx, client, args[0]) |
| 108 | + if err != nil { |
| 109 | + return err |
| 110 | + } |
| 111 | + credential, err := client.Credentials().Get(ctx, credID) |
107 | 112 | if err != nil { |
108 | 113 | return fmt.Errorf("get credential %q: %w", args[0], err) |
109 | 114 | } |
@@ -187,7 +192,7 @@ var createCmd = &cobra.Command{ |
187 | 192 | return fmt.Errorf("build credential: %w", err) |
188 | 193 | } |
189 | 194 |
|
190 | | - created, err := client.Credentials().Create(ctx, cred) |
| 195 | + created, err := client.Credentials().CreateCompat(ctx, cred) |
191 | 196 | if err != nil { |
192 | 197 | return fmt.Errorf("create credential: %w", err) |
193 | 198 | } |
@@ -259,7 +264,11 @@ var updateCmd = &cobra.Command{ |
259 | 264 | patch = patch.Annotations(updateArgs.annotations) |
260 | 265 | } |
261 | 266 |
|
262 | | - updated, err := client.Credentials().Update(ctx, args[0], patch.Build()) |
| 267 | + credID, err := resolveCredentialID(ctx, client, args[0]) |
| 268 | + if err != nil { |
| 269 | + return err |
| 270 | + } |
| 271 | + updated, err := client.Credentials().Update(ctx, credID, patch.Build()) |
263 | 272 | if err != nil { |
264 | 273 | return fmt.Errorf("update credential: %w", err) |
265 | 274 | } |
@@ -296,7 +305,11 @@ var deleteCmd = &cobra.Command{ |
296 | 305 | ctx, cancel := context.WithTimeout(context.Background(), cfg.GetRequestTimeout()) |
297 | 306 | defer cancel() |
298 | 307 |
|
299 | | - if err := client.Credentials().Delete(ctx, args[0]); err != nil { |
| 308 | + credID, err := resolveCredentialID(ctx, client, args[0]) |
| 309 | + if err != nil { |
| 310 | + return err |
| 311 | + } |
| 312 | + if err := client.Credentials().Delete(ctx, credID); err != nil { |
300 | 313 | return fmt.Errorf("delete credential: %w", err) |
301 | 314 | } |
302 | 315 |
|
@@ -329,7 +342,11 @@ var tokenCmd = &cobra.Command{ |
329 | 342 | ctx, cancel := context.WithTimeout(context.Background(), cfg.GetRequestTimeout()) |
330 | 343 | defer cancel() |
331 | 344 |
|
332 | | - resp, err := client.Credentials().GetToken(ctx, args[0]) |
| 345 | + credID, err := resolveCredentialID(ctx, client, args[0]) |
| 346 | + if err != nil { |
| 347 | + return err |
| 348 | + } |
| 349 | + resp, err := client.Credentials().GetToken(ctx, credID) |
333 | 350 | if err != nil { |
334 | 351 | return fmt.Errorf("get token for credential %q: %w", args[0], err) |
335 | 352 | } |
@@ -451,6 +468,18 @@ func init() { |
451 | 468 | bindCmd.Flags().StringVar(&bindArgs.project, "project", "", "Project to bind the credential to (required)") |
452 | 469 | } |
453 | 470 |
|
| 471 | +func resolveCredentialID(ctx context.Context, client *sdkclient.Client, nameOrID string) (string, error) { |
| 472 | + cred, err := client.Credentials().Get(ctx, nameOrID) |
| 473 | + if err == nil { |
| 474 | + return cred.ID, nil |
| 475 | + } |
| 476 | + cred, err = client.Credentials().FindByName(ctx, nameOrID) |
| 477 | + if err != nil { |
| 478 | + return "", fmt.Errorf("credential %q not found by ID or name", nameOrID) |
| 479 | + } |
| 480 | + return cred.ID, nil |
| 481 | +} |
| 482 | + |
454 | 483 | func printCredentialTable(printer *output.Printer, credentials []sdktypes.Credential) error { |
455 | 484 | columns := []output.Column{ |
456 | 485 | {Name: "ID", Width: 27}, |
|
0 commit comments