forked from xorpaul/g10k
/
helper.go
151 lines (138 loc) · 4.12 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
package main
import (
"fmt"
"log"
"os"
"os/exec"
"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 {
log.Print("DEBUG " + 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.Set(color.FgRed)
log.Fatal(s)
color.Unset()
}
// fileExists checks if the given file exists and return a bool
func fileExists(file string) bool {
if _, err := os.Stat(file); os.IsNotExist(err) {
return false
}
return true
}
// 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 {
// 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("createOrPurgeDir(): Trying to create dir: " + dir + " called from " + callingFunction)
os.Mkdir(dir, 0777)
} else {
Debugf("createOrPurgeDir(): Trying to remove: " + dir + " called from " + callingFunction)
if err := os.RemoveAll(dir); err != nil {
log.Print("createOrPurgeDir(): error: removing dir failed", err)
}
Debugf("createOrPurgeDir(): Trying to create dir: " + dir + " called from " + callingFunction)
os.Mkdir(dir, 0777)
}
}
}
func purgeDir(dir string, callingFunction string) {
if !fileExists(dir) {
Debugf("purgeDir(): Unnecessary to remove dir: " + dir + " it does not exist. Called from " + callingFunction)
} else {
Debugf("purgeDir(): 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("executeCommand(): 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()
}
mutex.Lock()
syncGitTime += duration
mutex.Unlock()
if allowFail && 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 {
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
}