forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
terminal.go
54 lines (42 loc) · 1.05 KB
/
terminal.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
package util
import (
"fmt"
"io"
"os"
"github.com/docker/docker/pkg/term"
"github.com/golang/glog"
)
func PromptForString(r io.Reader, format string, a ...interface{}) string {
fmt.Printf(format, a...)
return readInput(r)
}
// TODO not tested on other platforms
func PromptForPasswordString(r io.Reader, format string, a ...interface{}) string {
if file, ok := r.(*os.File); ok {
inFd := file.Fd()
if term.IsTerminal(inFd) {
oldState, err := term.SaveState(inFd)
if err != nil {
glog.V(3).Infof("Unable to save terminal state")
return PromptForString(r, format, a...)
}
fmt.Printf(format, a...)
term.DisableEcho(inFd, oldState)
input := readInput(r)
defer term.RestoreTerminal(inFd, oldState)
fmt.Printf("\n")
return input
} else {
glog.V(3).Infof("Stdin is not a terminal")
return PromptForString(r, format, a...)
}
} else {
glog.V(3).Infof("Unable to use a TTY")
return PromptForString(r, format, a...)
}
}
func readInput(r io.Reader) string {
var result string
fmt.Fscan(r, &result)
return result
}