-
Notifications
You must be signed in to change notification settings - Fork 51
/
helper.go
375 lines (338 loc) · 10.6 KB
/
helper.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
package main
import (
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"os/exec"
"regexp"
"runtime"
"strconv"
"strings"
"syscall"
"time"
"github.com/fatih/color"
"github.com/kballard/go-shellquote"
"golang.org/x/sys/unix"
)
var validationMessages []string
// Debugf is a helper function for debug logging if global variable debug is set to true
func Debugf(s string) {
if debug {
pc, _, _, _ := runtime.Caller(1)
callingFunctionName := strings.Split(runtime.FuncForPC(pc).Name(), ".")[len(strings.Split(runtime.FuncForPC(pc).Name(), "."))-1]
if strings.HasPrefix(callingFunctionName, "func") {
// check for anonymous function names
log.Print("DEBUG " + fmt.Sprint(s))
} else {
log.Print("DEBUG " + callingFunctionName + "(): " + fmt.Sprint(s))
}
}
}
// Verbosef is a helper function for verbose logging if global variable verbose is set to true
func Verbosef(s string) {
if debug || verbose {
log.Print(fmt.Sprint(s))
}
}
// Infof is a helper function for info logging if global variable info is set to true
func Infof(s string) {
if debug || verbose || info {
color.Green(s)
}
}
// Validatef is a helper function for validation logging if global variable validate is set to true
func Validatef() {
if len(validationMessages) > 0 {
for _, message := range validationMessages {
color.New(color.FgRed).Fprintln(os.Stdout, message)
}
os.Exit(1)
} else {
color.New(color.FgGreen).Fprintln(os.Stdout, "Configuration successfully parsed.")
os.Exit(0)
}
}
// Warnf is a helper function for warning logging
func Warnf(s string) {
color.Set(color.FgYellow)
fmt.Println(s)
color.Unset()
}
// Fatalf is a helper function for fatal logging
func Fatalf(s string) {
if validate {
validationMessages = append(validationMessages, s)
} else {
color.New(color.FgRed).Fprintln(os.Stderr, s)
os.Exit(1)
}
}
// fileExists checks if the given file exists and returns a bool
func fileExists(file string) bool {
//Debugf("checking for file existence " + file)
if _, err := os.Lstat(file); os.IsNotExist(err) {
return false
}
return true
}
// isDir checks if the given dir exists and returns a bool
func isDir(dir string) bool {
fi, err := os.Stat(dir)
if os.IsNotExist(err) {
return false
}
if fi.Mode().IsDir() {
return true
}
return false
}
// normalizeDir removes from the given directory path multiple redundant slashes and removes a trailing slash
func normalizeDir(dir string) string {
if strings.Count(dir, "//") > 0 {
dir = normalizeDir(strings.Replace(dir, "//", "/", -1))
}
dir = strings.TrimSuffix(dir, "/")
return dir
}
// checkDirAndCreate tests if the given directory exists and tries to create it
func checkDirAndCreate(dir string, name string) string {
if !dryRun {
if len(dir) != 0 {
if !fileExists(dir) {
//log.Printf("checkDirAndCreate(): trying to create dir '%s' as %s", dir, name){
if err := os.MkdirAll(dir, 0777); err != nil {
Fatalf("checkDirAndCreate(): Error: failed to create directory: " + dir)
}
} else {
if !isDir(dir) {
Fatalf("checkDirAndCreate(): Error: " + dir + " exists, but is not a directory! Exiting!")
} else {
if unix.Access(dir, unix.W_OK) != nil {
Fatalf("checkDirAndCreate(): Error: " + dir + " exists, but is not writable! Exiting!")
}
}
}
} else {
// TODO make dir optional
Fatalf("checkDirAndCreate(): Error: dir setting '" + name + "' missing! Exiting!")
}
}
dir = normalizeDir(dir)
Debugf("Using as " + name + ": " + dir)
return dir
}
func createOrPurgeDir(dir string, callingFunction string) {
if !dryRun {
if !fileExists(dir) {
Debugf("Trying to create dir: " + dir + " called from " + callingFunction)
os.MkdirAll(dir, 0777)
} else {
Debugf("Trying to remove: " + dir + " called from " + callingFunction)
if err := os.RemoveAll(dir); err != nil {
log.Print("createOrPurgeDir(): error: removing dir failed", err)
}
Debugf("Trying to create dir: " + dir + " called from " + callingFunction)
os.MkdirAll(dir, 0777)
}
}
}
func purgeDir(dir string, callingFunction string) {
if !fileExists(dir) {
Debugf("Unnecessary to remove dir: " + dir + " it does not exist. Called from " + callingFunction)
} else {
Debugf("Trying to remove: " + dir + " called from " + callingFunction)
if err := os.RemoveAll(dir); err != nil {
log.Print("purgeDir(): os.RemoveAll() error: removing dir failed: ", err.Error())
if err = syscall.Unlink(dir); err != nil {
log.Print("purgeDir(): syscall.Unlink() error: removing link failed: ", err.Error())
}
}
}
}
func executeCommand(command string, commandDir string, timeout int, allowFail bool, disableHttpProxy bool) ExecResult {
if len(commandDir) > 0 {
Debugf("Executing " + command + " in cwd " + commandDir)
} else {
Debugf("Executing " + command)
}
parts := strings.SplitN(command, " ", 2)
cmd := parts[0]
cmdArgs := []string{}
if len(parts) > 1 {
args, err := shellquote.Split(parts[1])
if err != nil {
Debugf("err: " + fmt.Sprint(err))
} else {
cmdArgs = args
}
}
before := time.Now()
execCommand := exec.Command(cmd, cmdArgs...)
if len(commandDir) > 0 {
execCommand.Dir = commandDir
}
if disableHttpProxy {
Debugf("found matching NO_PROXY URL, trying to disable http_proxy and https_proxy env variables for " + command)
// execCommand.Env = append(os.Environ(), "http_proxy=")
// execCommand.Env = append(os.Environ(), "https_proxy=")
os.Unsetenv("http_proxy")
os.Unsetenv("https_proxy")
os.Unsetenv("HTTP_PROXY")
os.Unsetenv("HTTPS_PROXY")
}
execCommand.Env = os.Environ()
out, err := execCommand.CombinedOutput()
duration := time.Since(before).Seconds()
er := ExecResult{0, string(out)}
if msg, ok := err.(*exec.ExitError); ok { // there is error code
er.returnCode = msg.Sys().(syscall.WaitStatus).ExitStatus()
}
if (allowFail || config.UseCacheFallback) && err != nil {
Debugf("Executing " + command + " took " + strconv.FormatFloat(duration, 'f', 5, 64) + "s")
} else {
Verbosef("Executing " + command + " took " + strconv.FormatFloat(duration, 'f', 5, 64) + "s")
}
if err != nil {
er.returnCode = 1
er.output = fmt.Sprint(err) + " " + fmt.Sprint(string(out))
}
return er
}
// funcName return the function name as a string
func funcName() string {
pc, _, _, _ := runtime.Caller(1)
completeFuncname := runtime.FuncForPC(pc).Name()
return strings.Split(completeFuncname, ".")[len(strings.Split(completeFuncname, "."))-1]
}
func timeTrack(start time.Time, name string) {
duration := time.Since(start).Seconds()
if name == "resolveForgeModules" {
syncForgeTime = duration
} else if name == "resolveGitRepositories" {
syncGitTime = duration
}
Debugf(name + "() took " + strconv.FormatFloat(duration, 'f', 5, 64) + "s")
}
// checkForAndExecutePostrunCommand check if a `postrun` command was specified in the g10k config and executes it
func checkForAndExecutePostrunCommand() {
if len(config.PostRunCommand) > 0 {
postrunCommandString := strings.Join(config.PostRunCommand, " ")
postrunCommandString = strings.Replace(postrunCommandString, "$modifieddirs", strings.Join(needSyncDirs, " "), -1)
needSyncEnvText := ""
for needSyncEnv := range needSyncEnvs {
needSyncEnvText += needSyncEnv + " "
}
postrunCommandString = strings.Replace(postrunCommandString, "$modifiedenvs", needSyncEnvText, -1)
postrunCommandString = strings.Replace(postrunCommandString, "$branchparam", branchParam, -1)
er := executeCommand(postrunCommandString, "", config.Timeout, false, false)
Debugf("postrun command '" + postrunCommandString + "' terminated with exit code " + strconv.Itoa(er.returnCode))
}
}
// getSha256sumFile return the SHA256 hash sum of the given file
func getSha256sumFile(file string) string {
// https://golang.org/pkg/crypto/sha256/#New
f, err := os.Open(file)
if err != nil {
Fatalf("failed to open file " + file + " to calculate SHA256 sum. Error: " + err.Error())
}
defer f.Close()
h := sha256.New()
if _, err := io.Copy(h, f); err != nil {
Fatalf("failed to calculate SHA256 sum of file " + file + " Error: " + err.Error())
}
return hex.EncodeToString(h.Sum(nil))
}
// moveFile uses io.Copy to create a copy of the given file https://stackoverflow.com/a/50741908/682847
func moveFile(sourcePath, destPath string, deleteSourceFileToggle bool) error {
inputFile, err := os.Open(sourcePath)
if err != nil {
return fmt.Errorf("couldn't open source file: %s", err)
}
outputFile, err := os.Create(destPath)
if err != nil {
inputFile.Close()
return fmt.Errorf("couldn't open dest file: %s", err)
}
defer outputFile.Close()
_, err = io.Copy(outputFile, inputFile)
inputFile.Close()
if err != nil {
return fmt.Errorf("writing to output file failed: %s", err)
}
if deleteSourceFileToggle {
// The copy was successful, so now delete the original file
err = os.Remove(sourcePath)
if err != nil {
return fmt.Errorf("failed removing original file: %s", err)
}
}
return nil
}
func stringSliceContains(slice []string, element string) bool {
for _, e := range slice {
if e == element {
return true
}
}
return false
}
func writeStructJSONFile(file string, v interface{}) {
content, err := json.MarshalIndent(v, "", " ")
if err != nil {
Warnf("Could not encode JSON file " + file + " " + err.Error())
}
err = ioutil.WriteFile(file, content, 0644)
if err != nil {
Warnf("Could not write JSON file " + file + " " + err.Error())
}
}
func readDeployResultFile(file string) DeployResult {
// Open our jsonFile
jsonFile, err := os.Open(file)
// if we os.Open returns an error then handle it
if err != nil {
Warnf("Could not open JSON file " + file + " " + err.Error())
}
defer jsonFile.Close()
byteValue, err := ioutil.ReadAll(jsonFile)
if err != nil {
Warnf("Could not read JSON file " + file + " " + err.Error())
}
var dr DeployResult
json.Unmarshal([]byte(byteValue), &dr)
return dr
}
func stripComponent(component string, env string) string {
if regexp.MustCompile(`^/.*/$`).MatchString(component) {
return regexp.MustCompile(component[1:len(component)-1]).ReplaceAllString(env, "")
} else {
return strings.TrimPrefix(env, component)
}
}
func matchGitRemoteURLNoProxy(url string) bool {
noProxy := os.Getenv("NO_PROXY")
for _, np := range strings.Split(noProxy, ",") {
if len(np) > 0 {
if strings.Contains(url, np) {
Debugf("found NO_PROXY setting: " + np + " matching " + url)
return true
}
}
}
// do the same for lower case environment variable name
noProxyL := os.Getenv("no_proxy")
for _, np := range strings.Split(noProxyL, ",") {
if len(np) > 0 {
if strings.Contains(url, np) {
Debugf("found no_proxy setting: " + np + " matching " + url)
return true
}
}
}
return false
}