-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathutils.go
253 lines (210 loc) · 5.52 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
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
package common
import (
"archive/tar"
"bufio"
"crypto/rand"
"encoding/base64"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strings"
"github.com/hashicorp/go-version"
"github.com/mgutz/ansi"
)
var (
procfileRegex = regexp.MustCompile("^([A-Za-z0-9_]+):\\s*(.+)$")
envVarPattern = "\\$([A-Z_]+[A-Z0-9_]*)"
envVarRegex = regexp.MustCompile(envVarPattern)
MsgReset string = ansi.ColorCode("reset")
PrintTitle, PrintlnTitle = printers(" ", ansi.ColorCode("green+h"))
PrintL0, PrintlnL0 = printers(" ", ansi.ColorCode("magenta"))
PrintL1, PrintlnL1 = printers(" ", ansi.ColorCode("white"))
PrintL2, PrintlnL2 = printers(" ----> ", ansi.ColorCode("black+h"))
PrintError, PrintlnError = printers(" ", ansi.ColorCode("red"))
PrintWarning, PrintlnWarning = printers(" ", ansi.ColorCode("yellow"))
)
func printers(prefix string, color string) (print func(format string, a ...interface{}), println func(format string, a ...interface{})) {
print = func(format string, a ...interface{}) {
fmt.Printf(color+prefix+format+MsgReset, a...)
}
println = func(format string, a ...interface{}) {
print(format+"\n", a...)
}
return print, println
}
type Process struct {
Name string
Command string
}
func FileExists(filename string) bool {
if _, err := os.Stat(filename); os.IsNotExist(err) {
return false
} else {
return true
}
}
func ContainsString(slice []string, desired string) bool {
for _, item := range slice {
if item == desired {
return true
}
}
return false
}
func CompareVersions(desired string, actual string) (bool, error) {
act, err := version.NewVersion(actual)
if err != nil {
return false, err
}
des, err := version.NewConstraint(desired)
if err != nil {
return false, err
}
return des.Check(act), nil
}
func ParseProcfile(procfile string) ([]*Process, error) {
procs := []*Process{}
buf, err := ioutil.ReadFile(procfile)
if err != nil {
return nil, err
}
for _, line := range strings.Split(string(buf), "\n") {
if matches := procfileRegex.FindStringSubmatch(line); matches != nil {
name, command := matches[1], matches[2]
procs = append(procs, &Process{Name: name, Command: command})
}
}
return procs, nil
}
func ParseEnvironmentVariables(line string) (string, error) {
line = envVarRegex.ReplaceAllString(line, "_env:$1")
return line, nil
}
func ParseUniqueInt(line string) (string, error) {
return strings.Replace(line, "{{UNIQUE_INT}}", "_unique:int", -1), nil
}
func ParsePort(command string) (hasFound bool, port string) {
portRegexp := regexp.MustCompile(`(?:-p|--port=)[[:blank:]]*(\d+)`)
ports := portRegexp.FindAllStringSubmatch(command, -1)
if len(ports) != 1 {
return false, ""
} else {
return true, ports[0][1]
}
}
func RemovePortIfEnvVar(command string) string {
portEnvVarRegexp := regexp.MustCompile(`[[:blank:]]*(-p|--port=)[[:blank:]]*` + envVarPattern)
return portEnvVarRegexp.ReplaceAllString(command, "")
}
func AskUser(message string) string {
answer := ""
for strings.TrimSpace(answer) == "" {
PrintL1("%s: ", message)
fmt.Scanln(&answer)
}
return answer
}
func AskUserWithDefault(message string, defaultValue string, shouldPrompt bool) string {
if !shouldPrompt {
return defaultValue
}
printedDefaultValue := defaultValue
if printedDefaultValue == "" {
printedDefaultValue = "default: none"
}
PrintL1("%s [%s] ", message, printedDefaultValue)
scanner := bufio.NewScanner(os.Stdin)
scanner.Scan()
if scanner.Err() != nil || strings.TrimSpace(scanner.Text()) == "" {
return defaultValue
}
return scanner.Text()
}
func AskYesOrNo(message string, defaultValue bool, shouldPrompt bool) bool {
if !shouldPrompt {
return defaultValue
}
var prompt string
if defaultValue {
prompt = "[Y/n]"
} else {
prompt = "[y/N]"
}
answer := "none"
for answer != "y" && answer != "n" && answer != "" {
PrintL1("%s %s ", message, prompt)
if _, err := fmt.Scanln(&answer); err != nil {
return defaultValue
}
answer = strings.TrimSpace(strings.ToLower(answer))
}
return (answer == "" && defaultValue) || answer == "y"
}
func AskMultipleChoices(message string, choices []string) string {
answer := -1
PrintL1(message)
for answer < 1 || answer > len(choices) {
for i, choice := range choices {
fmt.Printf(" %d: %s\n", i+1, choice)
}
fmt.Printf(" > ")
if _, err := fmt.Scanln(&answer); err != nil {
fmt.Fprint(os.Stderr, "Not a valid integer\n")
}
}
return choices[answer-1]
}
func Tar(source, target string) error {
tarfile, err := os.Create(target)
if err != nil {
return err
}
defer tarfile.Close()
tarball := tar.NewWriter(tarfile)
defer tarball.Close()
info, err := os.Stat(source)
if err != nil {
return nil
}
var baseDir string
if info.IsDir() {
baseDir = filepath.Base(source)
}
return filepath.Walk(source,
func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
header, err := tar.FileInfoHeader(info, info.Name())
if err != nil {
return err
}
if baseDir != "" {
header.Name = filepath.Join(baseDir, strings.TrimPrefix(path, source))
}
if err := tarball.WriteHeader(header); err != nil {
return err
}
if info.IsDir() {
return nil
}
file, err := os.Open(path)
if err != nil {
return err
}
defer file.Close()
_, err = io.Copy(tarball, file)
return err
})
}
func GenerateRandomBase64String(size int) (string, error) {
rb := make([]byte, size)
_, err := rand.Read(rb)
if err != nil {
return "", err
}
return base64.URLEncoding.EncodeToString(rb), nil
}