-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.go
85 lines (73 loc) · 2.04 KB
/
app.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
package gowatch
import (
"fmt"
"os"
"os/exec"
"strings"
"github.com/sirupsen/logrus"
)
type app interface {
compile() error
start() (*exec.Cmd, error)
restart(*exec.Cmd) error
}
type watcherApp struct {
// directory to watcher for changes
dir string
//flags to use in binary compiled execution
runFlags []string
//flags to use in go build command
buildFlags []string
//compiled binary name to execute
binaryName string
}
func (wa watcherApp) compile() error {
if _, err := os.Stat(wa.binaryName); !os.IsNotExist(err) {
logrus.Debugf("Removing existing binary buildfile %s\n", wa.binaryName)
if err := os.Remove(wa.binaryName); err != nil {
return fmt.Errorf("error to remove existing binary: %v", err)
}
}
buildFlags := []string{"build"}
for _, buildFlag := range wa.buildFlags {
buildFlags = append(buildFlags, buildFlag)
}
return cmdRunBase(wa.dir, "go", buildFlags...).Run()
}
func (wa watcherApp) start() (*exec.Cmd, error) {
cmd := cmdRunBinary(wa.dir, wa.binaryName, wa.runFlags...)
if err := cmd.Start(); err != nil {
return nil, err
}
return cmd, nil
}
func (wa watcherApp) restart(cmd *exec.Cmd) error {
logrus.Debugf("Killing current execution %d\n", cmd.Process.Pid)
if err := cmd.Process.Kill(); err != nil {
return fmt.Errorf("error to kill exiting process running: %v", err)
}
logrus.Debugf("Recompiling...")
if err := wa.compile(); err != nil {
return ErrCmdCompile
}
*cmd = *cmdRunBinary(wa.dir, wa.binaryName, wa.runFlags...)
if err := cmd.Start(); err != nil {
return fmt.Errorf("error to start program: %v", err)
}
return nil
}
func cmdRunBinary(dir, binaryName string, args ...string) *exec.Cmd {
if strings.HasPrefix(binaryName, "/") {
return cmdRunBase(dir, binaryName, args...)
}
return cmdRunBase(dir, fmt.Sprintf("./%s", binaryName), args...)
}
func cmdRunBase(dir, command string, args ...string) *exec.Cmd {
cmd := exec.Command(command, args...)
cmd.Dir = dir
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
logrus.Debugf("Command: %v\n", cmd.Args)
return cmd
}