forked from smallstep/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
read.go
71 lines (62 loc) · 1.71 KB
/
read.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package utils
import (
"bufio"
"fmt"
"io"
"io/ioutil"
"os"
"strings"
"syscall"
"github.com/pkg/errors"
"golang.org/x/crypto/ssh/terminal"
)
// ReadAll returns a slice of bytes with the content of the given reader.
func ReadAll(r io.Reader) ([]byte, error) {
b, err := ioutil.ReadAll(r)
return b, errors.Wrap(err, "error reading data")
}
// ReadString reads one line from the given io.Reader.
func ReadString(r io.Reader) (string, error) {
br := bufio.NewReader(r)
str, err := br.ReadString('\n')
if err != nil && err != io.EOF {
return "", errors.Wrap(err, "error reading string")
}
return strings.TrimSpace(str), nil
}
// ReadPassword asks the user for a password using the given prompt. If the
// program is receiving data from STDIN using a pipe, we cannot use
// terminal.ReadPassword on STDIN and we need to open the tty and read from
// it.
//
// This solution works on darwin and linux, but it might not work on other
// OSs.
func ReadPassword(prompt string) ([]byte, error) {
fmt.Fprint(os.Stderr, prompt)
var fd int
if terminal.IsTerminal(syscall.Stdin) {
fd = syscall.Stdin
} else {
tty, err := os.Open("/dev/tty")
if err != nil {
return nil, errors.Wrap(err, "error allocating terminal")
}
defer tty.Close()
fd = int(tty.Fd())
}
pass, err := terminal.ReadPassword(fd)
fmt.Fprintln(os.Stderr)
return pass, errors.Wrap(err, "error reading password")
}
// ReadInput from stdin if something is detected or ask the user for an input
// using the given prompt.
func ReadInput(prompt string) ([]byte, error) {
st, err := os.Stdin.Stat()
if err != nil {
return nil, errors.Wrap(err, "error reading data")
}
if st.Size() > 0 {
return ReadAll(os.Stdin)
}
return ReadPassword(prompt)
}