-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun.go
71 lines (59 loc) · 1.13 KB
/
run.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
package main
import (
"io"
"log"
"os"
"os/exec"
"path"
"strings"
)
func RunCommand(configuration Configuration) bool {
logFile, err := openCommandLogFile()
if err != nil {
panic(err)
}
defer func() {
if e := logFile.Close(); e != nil {
panic(e)
}
}()
log.Printf("Running process")
splitCommand := strings.Split(configuration.Command, " ")
command := exec.Command(splitCommand[0], splitCommand[1:]...)
stdoutPipe, err := command.StdoutPipe()
if err != nil {
panic(err)
}
stderrPipe, err := command.StderrPipe()
if err != nil {
panic(err)
}
if err = command.Start(); err != nil {
panic(err)
}
_, err = io.Copy(logFile, stdoutPipe)
if err != nil {
panic(err)
}
_, err = io.Copy(logFile, stderrPipe)
if err != nil {
panic(err)
}
err = command.Wait()
log.Print(err)
return err == nil
}
func openCommandLogFile() (*os.File, error) {
logFilePath := CommandLogFilePath()
if err := os.MkdirAll(path.Dir(logFilePath), 0755); err != nil {
panic(err)
}
logFile, err := os.OpenFile(
logFilePath,
os.O_APPEND|os.O_CREATE|os.O_WRONLY,
0644)
if err != nil {
panic(err)
}
return logFile, err
}