Provides echo-suppressed password prompting via golang.org/x/term for CLI login commands. Added as a platform standard in engineering-standards.md. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
23 lines
586 B
Go
23 lines
586 B
Go
// Package terminal provides secure terminal input helpers for CLI tools.
|
|
package terminal
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
|
|
"golang.org/x/term"
|
|
)
|
|
|
|
// ReadPassword prints the given prompt to stderr and reads a password
|
|
// from the terminal with echo disabled. It prints a newline after the
|
|
// input is complete so the cursor advances normally.
|
|
func ReadPassword(prompt string) (string, error) {
|
|
fmt.Fprint(os.Stderr, prompt)
|
|
b, err := term.ReadPassword(int(os.Stdin.Fd())) //nolint:gosec // fd fits in int
|
|
fmt.Fprintln(os.Stderr)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return string(b), nil
|
|
}
|