-
Notifications
You must be signed in to change notification settings - Fork 31
/
utils.go
122 lines (106 loc) · 2.82 KB
/
utils.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
package main
import (
"bufio"
"fmt"
"os"
"strings"
)
// ConfigCommaDelimitedString holds an array of strings to enable
// a single flag to take in multiple values via comma delimitation
type ConfigCommaDelimitedString []string
// Set adds a comma-delimited item to the main object
func (ccds *ConfigCommaDelimitedString) Set(item string) error {
*ccds = append(*ccds, strings.Split(item, ",")...)
return nil
}
// String representation
func (ccds *ConfigCommaDelimitedString) String() string {
return strings.Join(*ccds, ",")
}
// ConfigMultiflagString holds an array of strings from a single
// flag that's been specified multiple times
type ConfigMultiflagString []string
// Set adds an item to the main object
func (cmfs *ConfigMultiflagString) Set(item string) error {
*cmfs = append(*cmfs, item)
return nil
}
// String representation
func (cmfs *ConfigMultiflagString) String() string {
return strings.Join(*cmfs, ",")
}
func getCurrentWorkingDirectory() string {
cwd, err := os.Getwd()
if err != nil {
panic(err)
}
return cwd
}
const confirmationTrueCanonical = "y"
var confirmationTrue = []string{confirmationTrueCanonical, "yes", "yupp", "yeah", "yea", "ok", "okay"}
const confirmationFalseCanonical = "n"
var confirmationFalse = []string{confirmationFalseCanonical, "no", "nope", "nah", "neh", "stop", "dont"}
func confirm(reader *bufio.Reader, question string, byDefault bool, retryText ...string) bool {
var options string
if byDefault {
options = fmt.Sprintf("%s/%s", strings.ToUpper(confirmationTrueCanonical), confirmationFalseCanonical)
} else {
options = fmt.Sprintf("%s/%s", confirmationTrueCanonical, strings.ToUpper(confirmationFalseCanonical))
}
fmt.Printf("%s [%s]: ", question, options)
userInput, err := reader.ReadString('\n')
if err != nil {
panic(err)
}
if len(userInput) < 2 {
return byDefault
}
content := strings.Trim(
strings.ToLower(userInput),
" \r\n.,;",
)
confirmation := false
if sliceContainsString(confirmationTrue, content) {
confirmation = true
} else if sliceContainsString(confirmationFalse, content) {
confirmation = false
} else if len(retryText) > 0 {
fmt.Println(retryText[0])
confirmation = confirm(reader, question, byDefault, retryText...)
}
return confirmation
}
func directoryExists(pathToDirectory string) bool {
fileInfo, err := os.Lstat(pathToDirectory)
if err != nil {
if os.IsNotExist(err) {
return false
}
panic(err)
}
if fileInfo.IsDir() {
return true
}
return false
}
func fileExists(pathToFile string) bool {
fileInfo, err := os.Lstat(pathToFile)
if err != nil {
if os.IsNotExist(err) {
return false
}
panic(err)
}
if fileInfo.IsDir() {
return false
}
return true
}
func sliceContainsString(slice []string, search string) bool {
for _, sliceItem := range slice {
if search == sliceItem {
return true
}
}
return false
}