-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathterminal.go
More file actions
43 lines (34 loc) · 812 Bytes
/
terminal.go
File metadata and controls
43 lines (34 loc) · 812 Bytes
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
package utils
import (
"fmt"
"io"
"strings"
"github.com/Laisky/errors/v2"
)
// InputYes require user input `y` or `Y` to continue
func InputYes(hint string) (ok bool, err error) {
fmt.Printf("%s, input y/Y to continue: \n", hint)
var confirm string
_, err = fmt.Scanln(&confirm)
if err != nil {
if err.Error() == "unexpected newline" || errors.Is(err, io.EOF) {
// user input nothing, use default value
confirm = "y"
} else {
return ok, errors.Wrap(err, "read input")
}
}
if strings.ToLower(confirm) != "y" {
return false, nil
}
return true, nil
}
// Input reads input from stdin
func Input(hint string) (input string, err error) {
fmt.Printf("%s: \n", hint)
_, err = fmt.Scanln(&input)
if err != nil {
return "", errors.Wrap(err, "read input")
}
return input, nil
}