-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.go
executable file
·119 lines (99 loc) · 2.09 KB
/
helpers.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
package gostruct
import (
"bytes"
"os"
"os/exec"
"strings"
"unicode"
)
//exists checks if path or file exists
func exists(path string) bool {
if _, err := os.Stat(path); err == nil {
return true
}
return false
}
// uppercaseFirst does exactly what it says it does
func uppercaseFirst(s string) string {
if len(s) < 2 {
return strings.ToLower(s)
}
bts := []byte(s)
lc := bytes.ToUpper([]byte{bts[0]})
rest := bts[1:]
return string(bytes.Join([][]byte{lc, rest}, nil))
}
// createDirectory creates directory and sets permissions to 0777
func createDirectory(path string) error {
err := os.Mkdir(path, 0777)
if err != nil {
return err
}
//give new directory full permissions
err = os.Chmod(path, 0777)
if err != nil {
return err
}
return nil
}
// writeFile contents to file and overwrites if specified
func writeFile(path, contents string, overwrite bool) error {
var err error
if exists(path) && overwrite {
err = os.Remove(path)
}
file, err := os.Create(path)
defer file.Close()
if err != nil {
return err
}
_, err = file.WriteString(contents)
if err != nil {
return err
}
return nil
}
// runCommand runs cli commands
func runCommand(command string) (string, error) {
parts := getCmdParts(command)
cmd := exec.Command(parts[0], parts[1:]...)
err := cmd.Run()
if err != nil {
return "", err
}
return "", nil
}
// getCmdParts normalizes command into a string array
func getCmdParts(command string) []string {
lastQuote := rune(0)
f := func(c rune) bool {
switch {
case c == lastQuote:
lastQuote = rune(0)
return false
case lastQuote != rune(0):
return false
case unicode.In(c, unicode.Quotation_Mark):
lastQuote = c
return false
default:
return unicode.IsSpace(c)
}
}
var parts []string
preParts := strings.FieldsFunc(command, f)
for i := range preParts {
part := preParts[i]
parts = append(parts, strings.Replace(part, "'", "", -1))
}
return parts
}
// inArray determines if string is in array
func inArray(char string, strings []string) bool {
for _, a := range strings {
if a == char {
return true
}
}
return false
}