-
Notifications
You must be signed in to change notification settings - Fork 2
/
util.go
53 lines (46 loc) · 889 Bytes
/
util.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 main
import (
"bufio"
"crypto/rand"
"fmt"
"math/big"
"os"
"strings"
)
func askYesNo(showQuestion func(), defaultValue *bool) bool {
for {
showQuestion()
if defaultValue != nil {
if *defaultValue {
fmt.Print("(Y/n): ")
} else {
fmt.Print("(y/N): ")
}
} else {
fmt.Print("(y/n): ")
}
scanner := bufio.NewScanner(os.Stdin)
scanner.Scan()
c := scanner.Text()
switch strings.ToLower(c) {
case "yes", "y":
return true
case "no", "n":
return false
default:
if defaultValue != nil {
return *defaultValue
}
}
}
}
const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
func randomSecurePassword() string {
b := make([]byte, 6)
size := big.NewInt(int64(len(letterBytes)))
for i := range b {
c, _ := rand.Int(rand.Reader, size)
b[i] = letterBytes[c.Int64()]
}
return string(b)
}