-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathutils.go
74 lines (60 loc) · 1.3 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
package main
import (
"fmt"
"os"
"os/exec"
"os/signal"
"strings"
"sync"
"syscall"
)
func runCommand(cmd string) bool {
c := exec.Command(cmd)
output, err := c.CombinedOutput()
logCommandOutput(cmd, string(output))
if err != nil {
Log.WithField("cmd", cmd).Warn("Command failed")
return false
}
Log.Debug("Command returned successfully")
return true
}
func logCommandOutput(cmd string, output string) {
pathParts := strings.Split(cmd, "/")
filename := pathParts[len(pathParts)-1]
if output == "" {
return
}
logLines := strings.Split(strings.TrimSpace(output), "\n")
for _, line := range logLines {
Log.Infof("[%s] %s", filename, line)
}
}
func withPanicLogging(f func()) {
defer func() {
if r := recover(); r != nil {
err := fmt.Errorf("Recovered from panic(%+v)", r)
Log.WithField("error", err).Panicf("Stopped with panic: %s", err.Error())
}
}()
f()
}
func waitForSignals() {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
for sig := range c {
Log.WithField("signal", sig).Infof("Signalled.")
switch sig {
case syscall.SIGTERM, os.Interrupt:
Log.Infof("Shutting down.")
os.Exit(0)
default:
Log.Warnf("Unknown signal %s", sig.String())
}
}
}
func mutexed(mu *sync.Mutex, f func()) {
defer mu.Unlock()
mu.Lock()
f()
}