package util import ( "bufio" "fmt" "os" "strings" "syscall" "golang.org/x/term" ) type Prompter struct { scanner *bufio.Scanner } func NewPrompter(scanner *bufio.Scanner) *Prompter { return &Prompter{scanner} } func (p Prompter) Prompt(prompt string) (string, error) { printPrompt(prompt) if p.scanner.Scan() { return strings.TrimSpace(p.scanner.Text()), nil } if err := p.scanner.Err(); err != nil { return "", err } return "", fmt.Errorf("%s must not be empty", prompt) } func PromptForPassword(prompt string) (string, error) { printPrompt(prompt) password, err := term.ReadPassword(int(syscall.Stdin)) fmt.Fprintln(os.Stderr) if err != nil { return "", err } return strings.TrimSpace(string(password)), nil } func printPrompt(prompt string) { fmt.Fprint(os.Stderr, prompt, ": ") }