-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathhelper.go
191 lines (175 loc) · 5.32 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
package main
import (
"fmt"
"log"
"os"
"os/exec"
"runtime"
"strconv"
"strings"
"syscall"
"time"
"github.com/fatih/color"
"github.com/kballard/go-shellquote"
)
// Debugf is a helper function for debug logging if global variable debug is set to true
func Debugf(s string) {
if debug != false {
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 != false || verbose != false {
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 != false || verbose != false || info != false {
color.Green(s)
}
}
// 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) {
color.Red(s)
os.Exit(1)
}
// fileExists checks if the given file exists and return a bool
func fileExists(file string) bool {
//Debugf("checking for file existance " + file)
if _, err := os.Stat(file); os.IsNotExist(err) {
return false
}
return true
}
// dirExists checks if the given dir exists and return a bool
func isDir(dir string) bool {
fi, err := os.Stat(dir)
if os.IsNotExist(err) {
return false
} else {
if fi.Mode().IsDir() {
return true
} else {
return false
}
}
}
// 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 {
// TODO make dir optional
Fatalf("checkDirAndCreate(): Error: dir setting '" + name + "' missing! Exiting!")
}
}
if !strings.HasSuffix(dir, "/") {
dir = 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)
if err = syscall.Unlink(dir); err != nil {
log.Print("purgeDir(): syscall.Unlink() error: removing link failed: ", err)
}
}
}
}
func executeCommand(command string, timeout int, allowFail bool) ExecResult {
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()
out, err := exec.Command(cmd, cmdArgs...).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 {
if !allowFail && !config.UseCacheFallback {
Fatalf("executeCommand(): git command failed: " + command + " " + err.Error() + "\nOutput: " + string(out) +
"\nIf you are using GitLab be sure that you added your deploy key to your repository")
} else {
er.returnCode = 1
er.output = fmt.Sprint(err)
}
}
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")
}