-
Notifications
You must be signed in to change notification settings - Fork 22
/
stdio.go
53 lines (45 loc) · 1.67 KB
/
stdio.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
package stdio
import (
"bufio"
"io"
"os"
"github.com/AlecAivazis/survey/v2/terminal"
"github.com/mattn/go-isatty"
)
// Disable password length checking to let influxdb handle it
const MinPasswordLen = 0
type StdIO interface {
// Write prints some bytes to stdout.
Write(p []byte) (n int, err error)
// WriteErr prints some bytes to stderr.
WriteErr(p []byte) (n int, err error)
// Banner displays informational text to the user.
Banner(message string) error
// Error displays an error message to the user.
Error(message string) error
// IsInteractive signals whether interactive I/O is supported.
IsInteractive() bool
// GetStringInput prompts the user for arbitrary input.
GetStringInput(prompt, defaultValue string) (string, error)
// GetSecret prompts the user for a secret.
GetSecret(prompt string, minLen int) (string, error)
// GetPassword prompts the user for a secret twice, and inputs must match.
// Uses stdio.MinPasswordLen as the minimum input length
GetPassword(prompt string) (string, error)
// GetConfirm asks the user for a y/n answer to a prompt.
GetConfirm(prompt string) bool
}
func newTerminalStdio(in terminal.FileReader, out terminal.FileWriter, err io.Writer) StdIO {
interactiveIn := isatty.IsTerminal(in.Fd()) || isatty.IsCygwinTerminal(in.Fd())
interactiveOut := isatty.IsTerminal(out.Fd()) || isatty.IsCygwinTerminal(out.Fd())
if interactiveIn && interactiveOut {
return &interactiveStdio{in: in, out: out, err: err}
}
return &noninteractiveStdio{
in: bufio.NewScanner(in),
out: out,
err: err,
}
}
// TerminalStdio interacts with users over stdin/stdout/stderr.
var TerminalStdio = newTerminalStdio(os.Stdin, os.Stdout, os.Stderr)