promptForGenericAPIKeyUnified prompts the user for a generic API key
(req SecretRequirement, config EngineSecretConfig)
| 386 | |
| 387 | // promptForGenericAPIKeyUnified prompts the user for a generic API key |
| 388 | func promptForGenericAPIKeyUnified(req SecretRequirement, config EngineSecretConfig) error { |
| 389 | engineSecretsLog.Printf("Prompting for API key: %s", req.Name) |
| 390 | |
| 391 | // Get engine option for label |
| 392 | opt := constants.GetEngineOption(req.EngineName) |
| 393 | label := req.Name |
| 394 | if opt != nil { |
| 395 | label = opt.Label |
| 396 | } |
| 397 | |
| 398 | fmt.Fprintln(os.Stderr, "") |
| 399 | fmt.Fprintf(os.Stderr, "%s requires an API key.\n", label) |
| 400 | fmt.Fprintln(os.Stderr, "") |
| 401 | if req.KeyURL != "" { |
| 402 | fmt.Fprintln(os.Stderr, "Get your API key from:") |
| 403 | fmt.Fprintln(os.Stderr, console.FormatCommandMessage(" "+req.KeyURL)) |
| 404 | fmt.Fprintln(os.Stderr, "") |
| 405 | } |
| 406 | |
| 407 | var apiKey string |
| 408 | form := huh.NewForm( |
| 409 | huh.NewGroup( |
| 410 | huh.NewInput(). |
| 411 | Title(fmt.Sprintf("Paste your %s API key:", label)). |
| 412 | Description("The key will be stored securely as a repository secret"). |
| 413 | EchoMode(huh.EchoModePassword). |
| 414 | Value(&apiKey). |
| 415 | Validate(func(s string) error { |
| 416 | if len(s) < 10 { |
| 417 | return errors.New("API key appears to be too short") |
| 418 | } |
| 419 | return nil |
| 420 | }), |
| 421 | ), |
| 422 | ).WithTheme(styles.HuhTheme).WithAccessible(console.IsAccessibleMode()) |
| 423 | |
| 424 | if err := form.RunWithContext(config.ctx()); err != nil { |
| 425 | return fmt.Errorf("failed to get %s API key: %w", label, err) |
| 426 | } |
| 427 | |
| 428 | fmt.Fprintln(os.Stderr, console.FormatSuccessMessage(label+" API key received")) |
| 429 | |
| 430 | // Upload to repository if we have a repo slug |
| 431 | if config.RepoSlug != "" { |
| 432 | return uploadSecretToRepo(req.Name, apiKey, config.RepoSlug, config.Verbose) |
| 433 | } |
| 434 | |
| 435 | return nil |
| 436 | } |
| 437 | |
| 438 | // checkOptionalSecret checks if an optional secret is available (without prompting) |
| 439 | func checkOptionalSecret(req SecretRequirement, config EngineSecretConfig) error { |
no test coverage detected