PromptSecretInput shows an interactive password input prompt with masking The input is masked for security and includes validation Returns the entered secret value or an error
(title, description string)
| 14 | // The input is masked for security and includes validation |
| 15 | // Returns the entered secret value or an error |
| 16 | func PromptSecretInput(title, description string) (string, error) { |
| 17 | // Check if stdin is a TTY - if not, we can't show interactive forms |
| 18 | if !tty.IsStderrTerminal() { |
| 19 | return "", errors.New("interactive input not available (not a TTY)") |
| 20 | } |
| 21 | |
| 22 | var value string |
| 23 | |
| 24 | form := huh.NewForm( |
| 25 | huh.NewGroup( |
| 26 | huh.NewInput(). |
| 27 | Title(title). |
| 28 | Description(description). |
| 29 | EchoMode(huh.EchoModePassword). // Masks input for security |
| 30 | Validate(func(s string) error { |
| 31 | if s == "" { |
| 32 | return errors.New("value cannot be empty") |
| 33 | } |
| 34 | return nil |
| 35 | }). |
| 36 | Value(&value), |
| 37 | ), |
| 38 | ).WithTheme(styles.HuhTheme).WithAccessible(IsAccessibleMode()) |
| 39 | |
| 40 | if err := form.Run(); err != nil { |
| 41 | return "", err |
| 42 | } |
| 43 | |
| 44 | return value, nil |
| 45 | } |