forked from ThatNerdyPikachu/nspbuild
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
169 lines (141 loc) · 2.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
package main
import (
"archive/zip"
"io"
"net/http"
"os"
"regexp"
"strings"
)
func isEverythingNil(list []string) bool {
nils := 0
for _, v := range list {
if v == "" {
nils++
}
}
if len(list) == nils {
return true
}
return false
}
func del(list []string, index int) []string {
n := []string{}
for i, v := range list {
if i != index {
n = append(n, v)
}
}
return n
}
func parse(list []string) []string {
pq := false
args := []string{}
q := []string{}
for _, v := range list {
if pq == false && !strings.HasPrefix(v, "\"") {
args = append(args, v)
} else if pq == false && strings.HasPrefix(v, "\"") {
pq = true
q = append(q, strings.TrimPrefix(v, "\""))
} else if pq == true && !strings.HasSuffix(v, "\"") {
q = append(q, v)
} else if pq == true && strings.HasSuffix(v, "\"") {
q = append(q, strings.TrimSuffix(v, "\""))
args = append(args, strings.Join(q, " "))
q = []string{}
pq = false
}
}
return args
}
func org(args []string, items []string) map[string]string {
m := map[string]string{}
for i, v := range args {
m[items[i]] = v
}
return m
}
func mapToSlice(m map[string]string) []string {
a := []string{}
for _, v := range m {
a = append(a, v)
}
return a
}
func fileExists(f string) bool {
_, err := os.Stat(f)
if err == nil {
return true
}
return false
}
func isHex(s string) bool {
r := regexp.MustCompile("\\A\\b[0-9a-fA-F]+\\b")
return r.MatchString(s)
}
func chkErr(e error) {
if e != nil {
panic(e)
}
}
func copyFile(src, dst string) error {
source, err := os.Open(src)
if err != nil {
return err
}
defer source.Close()
dest, err := os.Create(dst)
if err != nil {
return err
}
defer dest.Close()
_, err = io.Copy(dest, source)
if err != nil {
return err
}
return nil
}
func download(url, output string) error {
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
out, err := os.Create(output)
if err != nil {
return err
}
defer out.Close()
_, err = io.Copy(out, resp.Body)
if err != nil {
return err
}
return nil
}
func unzipFile(in, file, out string) error {
files, err := zip.OpenReader(in)
if err != nil {
return err
}
defer files.Close()
for _, v := range files.File {
if v.Name == file {
source, err := v.Open()
if err != nil {
return err
}
defer source.Close()
dest, err := os.Create(out)
if err != nil {
return err
}
defer dest.Close()
_, err = io.Copy(dest, source)
if err != nil {
return err
}
}
}
return nil
}