-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathpm.go
69 lines (55 loc) · 1.44 KB
/
pm.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
package main
import (
"fmt"
"os"
"os/exec"
"time"
)
type processManager struct {
conf *config
oscmd *exec.Cmd
}
func (pm *processManager) formatBuildTime(duration time.Duration) string {
return fmt.Sprintf("%.2f(s)", duration.Seconds())
}
func (pm *processManager) run() {
logger.Debug("building application...")
start := time.Now()
os.Remove(pm.conf.build)
out, err := exec.Command("go", "build", "-o", pm.conf.build).CombinedOutput()
if err != nil {
logger.Errorf("build failed! %s", err.Error())
fmt.Printf("%s", out)
return
}
// build success, display build time
logger.Infof("build took %s", pm.formatBuildTime(time.Since(start)))
if pm.conf.Test {
testOut, testErr := exec.Command("go", "test").CombinedOutput()
if testErr != nil {
logger.Error("Tests failed!")
fmt.Printf("==========\n%s==========\n", testOut)
} else {
logger.Info("Tests OK!")
}
}
pm.oscmd = exec.Command(pm.conf.build, pm.conf.Args...)
pm.oscmd.Stdout = os.Stdout
pm.oscmd.Stdin = os.Stdin
pm.oscmd.Stderr = os.Stderr
logger.Debugf("starting application with arguments: %v", pm.conf.Args)
err = pm.oscmd.Start()
if err != nil {
logger.Errorf("error while starting application! %s", err.Error())
}
}
func (pm *processManager) stop() {
logger.Debug("stopping application")
if pm.oscmd == nil {
return
}
err := pm.oscmd.Process.Kill()
if err != nil {
logger.Errorf("error while stopping application! %s", err.Error())
}
}