-
Notifications
You must be signed in to change notification settings - Fork 41
/
commands.go
97 lines (78 loc) · 1.93 KB
/
commands.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
package main
import (
"github.com/gookit/cliapp"
"io/ioutil"
"os/exec"
"github.com/gookit/color"
"fmt"
)
type Config struct {
// will exec command. eg. "go run main.go"
Cmd string `json:"cmd"`
// serve name for will exec command
Name string `json:"name"`
// run in the background
Daemon bool
// the pid file. eg "/var/run/serve.pid"
PidFile string `json:"pidFile"`
// the command run dir.
WorkDir string `json:"workDir"`
}
var config *Config
// config file
var confFile string
// eg: cliapp serve:start
func ServerStart() *cliapp.Command {
c := &cliapp.Command{
Name: "start",
}
// c.StrOpt(&config.Pid, "pid", "", "", "the running server PID file")
c.StrOpt(&confFile, "config", "c", "serve-config.json", "the running json config file path")
c.BoolOpt(&config.Daemon, "daemon", "d", false, "the running server PID file")
return c
}
func startServer() int {
if config.Daemon {
command := exec.Command("gonne", "start")
command.Start()
fmt.Printf("server start, [PID] %d running...\n", command.Process.Pid)
ioutil.WriteFile(config.PidFile, []byte(fmt.Sprintf("%d", command.Process.Pid)), 0666)
config.Daemon = false
return 0
} else {
fmt.Println("gonne start")
}
// front run
// startHttp()
return 0
}
func ServerStop() *cliapp.Command {
cmd := &cliapp.Command{
Name: "stop",
UseFor: "stop the running server by PID file",
}
cmd.Func = func(_ *cliapp.Command, _ []string) int {
return stopServer()
}
return cmd
}
func stopServer() int {
bs, _ := ioutil.ReadFile(config.PidFile)
command := exec.Command("kill", string(bs))
command.Start()
color.Success.Println("server stopped")
return 0
}
func ServerRestart() *cliapp.Command {
cmd := &cliapp.Command{
Name: "restart",
UseFor: "restart the running server by PID file",
}
cmd.Func = func(c *cliapp.Command, _ []string) int {
// c.App().SubRun("stop", []string{"-c", confFile})
stopServer()
startServer()
return 0
}
return cmd
}