-
Notifications
You must be signed in to change notification settings - Fork 19
/
cmd.go
105 lines (89 loc) · 2.16 KB
/
cmd.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package main
import (
"fmt"
"io/ioutil"
"os"
"runtime"
"github.com/ShinyTrinkets/overseer"
log "github.com/azer/logger"
cli "github.com/jawher/mow.cli"
quote "github.com/kballard/go-shellquote"
yml "gopkg.in/yaml.v3"
)
// Process represents an OS process
type Process struct {
Cmd string `yaml:"cmd"`
Cwd string `yaml:"cwd"`
Env []string `yaml:"env"`
Delay uint `yaml:"delay"`
Retry uint `yaml:"retry"`
}
const (
name = "Overseer"
descrip = "(<>..<>)"
)
var (
// injected by go build
commitHash string
buildTime string
)
func main() {
overseer.SetupLogBuilder(func(name string) overseer.Logger {
return log.New(name)
})
app := cli.App(name, descrip)
ver := (name + " " + descrip + "\n" + runtime.GOOS + " " + runtime.GOARCH +
"\n\n◇ Revision: " + commitHash + "\n◇ Compiled: " + buildTime)
app.Version("v version", ver)
app.Command("start", "Run Overseer", cmdRunAll)
app.Run(os.Args)
}
func cmdRunAll(cmd *cli.Cmd) {
cmd.Spec = "[-c]"
cfgFile := cmd.StringOpt("c config", "config.yml", "the config used to define procs")
cmd.Action = func() {
text, err := ioutil.ReadFile(*cfgFile)
if err != nil {
fmt.Printf("Cannot read config. Error: %v\n", err)
return
}
cfg := make(map[string]Process)
if err := yml.Unmarshal(text, &cfg); err != nil {
fmt.Printf("Cannot parse config. Error: %v\n", err)
return
}
fmt.Println("Starting procs. Press Ctrl+C to stop...")
ovr := overseer.NewOverseer()
for id, proc := range cfg {
fmt.Printf("PROC: %#v\n", proc)
if proc.Cmd == "" {
fmt.Printf("Proc '%s': Cmd field cannot be empty!\n", id)
continue
}
args, err := quote.Split(proc.Cmd)
if err != nil {
fmt.Printf("Proc '%s': Cannot split args. Error: %v\n", id, err)
continue
}
opts := overseer.Options{
Buffered: false, Streaming: true,
Env: os.Environ(),
}
if proc.Cwd != "" {
opts.Dir = proc.Cwd
}
if proc.Delay > 0 {
opts.DelayStart = proc.Delay
}
if proc.Retry > 0 {
opts.RetryTimes = proc.Retry
}
p := ovr.Add(id, args[0], args[1:], opts)
if p == nil {
continue
}
}
ovr.SuperviseAll()
fmt.Println("\nShutdown.")
}
}