-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmd.go
126 lines (107 loc) · 2.75 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"github.com/urfave/cli"
)
// JSONResponse is for
type JSONResponse struct {
Status int `json:"status"`
Value interface{} `json:"value"`
}
// Program is for
type Program struct {
Name string `yaml:"name" json:"name"`
Command string `yaml:"command" json:"command"`
Environ []string `yaml:"environ" json:"environ"`
Dir string `yaml:"directory" json:"directory"`
StartAuto bool `yaml:"start_auto" json:"startAuto"`
StartRetries int `yaml:"start_retries" json:"startRetries"`
StartSeconds int `yaml:"start_seconds,omitempty" json:"startSeconds"`
StopTimeout int `yaml:"stop_timeout,omitempty" json:"stopTimeout"`
retryCount int
User string `yaml:"user,omitempty" json:"user"`
// Notifications Notifications `yaml:"notifications,omitempty" json:"-"`
// WebHook WebHook `yaml:"webhook,omitempty" json:"-"`
}
func shutdownGosuv(c *cli.Context) error {
ret, err := postFormWithAuth(remoteURL+"/api/shutdown", nil, key)
if err != nil {
log.Fatal(err)
}
fmt.Println(ret.Value)
return nil
}
func statusGosuv(c *cli.Context) error {
resp, err := getWithAuth(remoteURL+"/api/programs", key)
if err != nil {
log.Fatal(err)
}
var programs = make([]struct {
Program Program `json:"program"`
Status string `json:"status"`
}, 0)
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
err = json.Unmarshal(body, &programs)
if err != nil {
log.Fatal(err)
}
format := "%-23s\t%-8s\n"
fmt.Printf(format, "PROGRAM NAME", "STATUS")
for _, p := range programs {
fmt.Printf(format, p.Program.Name, p.Status)
}
return nil
}
func startProgram(c *cli.Context) error {
programName := c.Args().First()
success, err := programOperator("start", programName)
if err != nil {
log.Fatal(err)
}
if success {
fmt.Println(programName + " started")
} else {
fmt.Println(programName + " started failed")
}
return nil
}
func stopProgram(c *cli.Context) error {
programName := c.Args().First()
success, err := programOperator("stop", programName)
if err != nil {
log.Fatal(err)
}
if success {
fmt.Println(programName + " stopped")
} else {
fmt.Println(programName + " stopped failed")
}
return nil
}
func programOperator(cmd, programName string) (success bool, err error) {
resp, err := postWithAuth(remoteURL+"/api/programs/"+programName+"/"+cmd, "", nil, key)
if err != nil {
log.Fatal(err)
}
var v = struct {
Status int `json:"status"`
}{}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
err = json.Unmarshal(body, &v)
if err != nil {
log.Fatal(err)
}
success = (v.Status == 0)
return
}