-
Notifications
You must be signed in to change notification settings - Fork 3
/
cli_entry.go
94 lines (81 loc) · 2.1 KB
/
cli_entry.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
//go:build !heroku
// +build !heroku
// Trakx controller entrypoint
package main
import (
"fmt"
"os"
"github.com/crimist/trakx/controller"
)
func printHelp() {
help := "Commands:\n"
help += fmt.Sprintf(" %-12s returns the status of trakx\n", "status")
help += fmt.Sprintf(" %-12s starts trakx daemon\n", "start")
help += fmt.Sprintf(" %-12s stops trakx daemon\n", "stop")
help += fmt.Sprintf(" %-12s restarts trakx daemon\n", "restart")
help += fmt.Sprintf(" %-12s executes trakx, doesn't return\n", "execute")
help += fmt.Sprintf(" %-12s wipes trakx pid file, use if you encounter errors with start/stop/restart commands\n", "reset")
help += "Usage:\n"
help += fmt.Sprintf(" %s <command>\n", os.Args[0])
help += "Example:\n"
help += fmt.Sprintf(" %s status\n", os.Args[0])
fmt.Print(help)
}
func logFatal(err error) {
fmt.Fprintf(os.Stderr, err.Error()+"\n")
os.Exit(-1)
}
func main() {
if len(os.Args) < 2 {
printHelp()
return
}
controller := controller.NewController()
switch os.Args[1] {
case "status":
pidFileExists, processAlive, heartbeat := controller.Status()
if pidFileExists {
fmt.Println("[✅] valid process id file")
} else {
fmt.Println("[❌] invalid/empty process id file")
}
if processAlive {
fmt.Println("[✅] process is alive")
} else {
fmt.Println("[❌] process is dead")
}
if heartbeat {
fmt.Println("[✅] heartbeat successful")
} else {
fmt.Println("[❌] heartbeat failed")
}
case "execute":
controller.Execute()
case "start":
if err := controller.Start(); err != nil {
logFatal(err)
}
case "stop":
if err := controller.Stop(); err != nil {
logFatal(err)
}
case "restart", "reboot":
fmt.Println("rebooting...")
if err := controller.Stop(); err != nil {
logFatal(err)
}
if err := controller.Start(); err != nil {
logFatal(err)
}
fmt.Println("rebooted!")
case "reset":
fmt.Println("wiping pid file...")
if err := controller.Clear(); err != nil {
logFatal(err)
}
fmt.Println("wiped!")
default:
fmt.Fprintf(os.Stderr, "invalid command: '%s'\n\n", os.Args[1])
printHelp()
}
}