/
command.go
48 lines (39 loc) · 979 Bytes
/
command.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
package operation
import (
"os"
"os/exec"
"path"
log "github.com/sirupsen/logrus"
)
func buildPath(dir string) string {
cwd, err := os.Getwd()
if err != nil {
log.WithError(err).Fatal("Unable to get current working directory")
}
return path.Join(cwd, dir)
}
type commandHandler = func() error
type errorHandler = func(error)
func handleCommand(path, command string) commandHandler {
return func() error {
exec := exec.Command("sh", "-c", command)
exec.Dir = buildPath(path)
if log.IsLevelEnabled(log.DebugLevel) {
exec.Stderr = os.Stderr
exec.Stdout = os.Stdout
}
return exec.Run()
}
}
func handleGenericError(logWithFields *log.Entry, prefix string) errorHandler {
return func(err error) {
if err != nil {
logWithFields.WithError(err).Fatalf("%s failed", prefix)
} else {
logWithFields.Debugf("%s started", prefix)
}
}
}
func runCommand(handleCommand commandHandler, handleError errorHandler) {
handleError(handleCommand())
}