Skip to content

Instantly share code, notes, and snippets.

@karrick
Created August 8, 2018 20:08
Show Gist options
  • Save karrick/496217fb5afd036172483a6a2def03b5 to your computer and use it in GitHub Desktop.
Save karrick/496217fb5afd036172483a6a2def03b5 to your computer and use it in GitHub Desktop.
package main
import (
"os"
"golang.org/x/crypto/ssh/terminal"
)
func readHiddenFromTTY(prompt string) (string, error) {
tty, err := os.OpenFile("/dev/tty", os.O_RDWR, 0)
if err != nil {
return "", err
}
if len(prompt) > 0 {
_, err = tty.Write([]byte(prompt))
if err != nil {
_ = tty.Close()
return "", err
}
}
buf, err := terminal.ReadPassword(int(tty.Fd()))
if len(prompt) > 0 {
if _, err2 := tty.Write([]byte{'\n'}); err == nil {
err = err2
}
}
if err2 := tty.Close(); err == nil {
err = err2
}
return string(buf), err
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment