/
term.go
161 lines (132 loc) · 2.6 KB
/
term.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package core
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
"github.com/howeyc/gopass"
)
/*
Exit exits with priting error message.
*/
func Exit(why interface{}, code ...int) {
var message string
exitcode := 1
if len(code) > 0 {
exitcode = code[0]
}
switch why := why.(type) {
case string:
message = why
case fmt.Stringer:
message = why.String()
case error:
message = why.Error()
default:
message = fmt.Sprintf("%v", why)
}
println(message)
// now die
os.Exit(exitcode)
}
/*
TerminalGetPasswordValue return password
*/
func TerminalGetPasswordValue(question string) (result string) {
fmt.Printf("%v> ", question)
var b []byte
b, _ = gopass.GetPasswdMasked()
result = string(b)
return
}
/*
GetTermValue calls user input.
*/
func TerminalGetStringValue(question string, defs ...string) (result string) {
def := ""
if len(defs) > 0 {
def = strings.TrimSpace(defs[0])
}
if def != "" {
fmt.Printf("%v [default:%v]> ", question, def)
} else {
fmt.Printf("%v> ", question)
}
reader := bufio.NewReader(os.Stdin)
if response, err := reader.ReadString('\n'); err != nil {
panic(err)
} else {
result = strings.TrimSpace(response)
}
if result == "" {
return def
}
return
}
func TerminalGetStringValueChoices(question string, choices []string, defs ...string) (result string) {
def := ""
if len(defs) > 0 {
def = strings.TrimSpace(defs[0])
}
outer:
for {
if def != "" {
fmt.Printf("%v [choices: %v, default:%v]> ", question, choices, def)
} else {
fmt.Printf("%v [choices: %v]> ", question, choices)
}
reader := bufio.NewReader(os.Stdin)
if response, err := reader.ReadString('\n'); err != nil {
panic(err)
} else {
result = strings.TrimSpace(response)
}
if result == "" {
result = def
}
for _, choice := range choices {
if choice == result {
break outer
}
}
}
return result
}
/*
GetTermIntValue waits for user input for number
*/
func TerminalGetIntValue(question string, def int) (result int) {
result = def
var (
err error
)
for {
value := TerminalGetStringValue(question, fmt.Sprintf("%v", def))
if value == "" {
return def
}
if result, err = strconv.Atoi(value); err == nil {
return
}
println("error:", err.Error())
}
return def
}
func TerminalGetBoolValue(question string, def bool) (result bool) {
result = def
for {
value := TerminalGetStringValue(question, fmt.Sprintf("%v", def))
if value == "" {
return def
}
if boolTrue.MatchString(value) {
return true
} else if boolFalse.MatchString(value) {
return false
} else {
println("error:", "please answer")
}
}
return
}