(tty *os.File, output []byte)
| 50 | } |
| 51 | |
| 52 | func readPass(tty *os.File, output []byte) ([]byte, error) { |
| 53 | cursor := output[0:1] |
| 54 | readen := 0 |
| 55 | for { |
| 56 | n, err := tty.Read(cursor) |
| 57 | if n != 1 { |
| 58 | return nil, errors.New("ReadPassword: invalid read size when not in canonical mode") |
| 59 | } |
| 60 | if err != nil { |
| 61 | return nil, errors.New("ReadPassword: " + err.Error()) |
| 62 | } |
| 63 | if cursor[0] == '\n' { |
| 64 | break |
| 65 | } |
| 66 | // Esc or Ctrl+D or Ctrl+C. |
| 67 | if cursor[0] == '\x1b' || cursor[0] == '\x04' || cursor[0] == '\x03' { |
| 68 | return nil, errors.New("ReadPassword: prompt rejected") |
| 69 | } |
| 70 | if cursor[0] == '\x7F' /* DEL */ { |
| 71 | if readen != 0 { |
| 72 | readen-- |
| 73 | cursor = output[readen : readen+1] |
| 74 | } |
| 75 | continue |
| 76 | } |
| 77 | |
| 78 | if readen == cap(output) { |
| 79 | return nil, errors.New("ReadPassword: too long password") |
| 80 | } |
| 81 | |
| 82 | readen++ |
| 83 | cursor = output[readen : readen+1] |
| 84 | } |
| 85 | |
| 86 | return output[0:readen], nil |
| 87 | } |
| 88 | |
| 89 | func ReadPassword(prompt string) (string, error) { |
| 90 | termios, err := TurnOnRawIO(os.Stdin) |
no test coverage detected