This repository has been archived by the owner on Dec 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 181
/
Copy pathgosuv.go
228 lines (215 loc) · 4.68 KB
/
gosuv.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package main
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"runtime"
"strings"
"github.com/imroc/req"
"github.com/qiniu/log"
"github.com/urfave/cli"
)
const appID = "app_8Gji4eEAdDx"
var (
version string = "master"
cfg Configuration
)
type TagInfo struct {
Version string `json:"tag_name"`
Body string `json:"body"`
CreatedAt string `json:"created_at"`
}
func githubLatestVersion(repo, name string) (tag TagInfo, err error) {
githubURL := fmt.Sprintf("https://api.github.com/repos/%s/%s/releases/latest", repo, name)
r := req.New()
h := req.Header{}
ghToken := os.Getenv("GITHUB_TOKEN")
if ghToken != "" {
h["Authorization"] = "token " + ghToken
}
res, err := r.Get(githubURL, h)
if err != nil {
return
}
err = res.ToJSON(&tag)
return
}
func githubUpdate(skipConfirm bool) error {
repo, name := "soopsio", "gosuv"
tag, err := githubLatestVersion(repo, name)
if err != nil {
fmt.Println("Update failed:", err)
return err
}
if tag.Version == version {
fmt.Println("No update available, already at the latest version!")
return nil
}
fmt.Println("New version available -- ", tag.Version)
fmt.Print(tag.Body)
if !skipConfirm {
if !askForConfirmation("Would you like to update [Y/n]? ", true) {
return nil
}
}
fmt.Printf("New version available: %s downloading ... \n", tag.Version)
// // fetch the update and apply it
// err = resp.Apply()
// if err != nil {
// return err
// }
cleanVersion := tag.Version
if strings.HasPrefix(cleanVersion, "v") {
cleanVersion = cleanVersion[1:]
}
osArch := runtime.GOOS + "_" + runtime.GOARCH
downloadURL := StringFormat("https://github.com/{repo}/{name}/releases/download/{tag}/{name}_{version}_{os_arch}.tar.gz", map[string]interface{}{
"repo": "codeskyblue",
"name": "gosuv",
"tag": tag.Version,
"version": cleanVersion,
"os_arch": osArch,
})
fmt.Println("Not finished yet. download from:", downloadURL)
// fmt.Printf("Updated to new version: %s!\n", tag.Version)
return nil
}
func checkServerStatus() error {
resp, err := http.Get(cfg.Client.ServerURL + "/api/status")
if err != nil {
return err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
var ret JSONResponse
err = json.Unmarshal(body, &ret)
if err != nil {
return errors.New("json loads error: " + string(body))
}
if ret.Status != 0 {
return fmt.Errorf("%v", ret.Value)
}
return nil
}
func main() {
var defaultConfigPath = filepath.Join(defaultGosuvDir, "conf/config.yml")
app := cli.NewApp()
app.Name = "gosuv"
app.Version = version
app.Usage = "golang port of python-supervisor"
app.Before = func(c *cli.Context) error {
var err error
cfgPath := c.GlobalString("conf")
cfg, err = readConf(cfgPath)
if err != nil {
log.Fatal(err)
}
return nil
}
app.Authors = []cli.Author{
cli.Author{
Name: "codeskyblue",
Email: "[email protected]",
},
}
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "conf, c",
Usage: "config file",
Value: defaultConfigPath,
},
}
app.Commands = []cli.Command{
{
Name: "start-server",
Usage: "Start supervisor and run in background",
Flags: []cli.Flag{
cli.BoolFlag{
Name: "foreground, f",
Usage: "start in foreground",
},
cli.StringFlag{
Name: "conf, c",
Usage: "config file",
Value: defaultConfigPath,
},
},
Action: actionStartServer,
},
{
Name: "status",
Aliases: []string{"st"},
Usage: "Show program status",
Action: actionStatus,
},
{
Name: "start",
Usage: "Start program",
Action: actionStart,
},
{
Name: "stop",
Usage: "Stop program",
Action: actionStop,
},
{
Name: "reload",
Usage: "Reload config file",
Action: actionReload,
},
{
Name: "shutdown",
Usage: "Shutdown server",
Flags: []cli.Flag{
cli.BoolFlag{
Name: "restart, r",
Usage: "restart server(todo)",
},
},
Action: actionShutdown,
},
{
Name: "conftest",
Aliases: []string{"t"},
Usage: "Test if config file is valid",
Action: actionConfigTest,
},
{
Name: "update-self",
Usage: "Update gosuv itself",
Flags: []cli.Flag{
cli.StringFlag{
Name: "channel, c",
Usage: "update channel name, stable or dev",
Value: "stable",
},
cli.BoolFlag{
Name: "yes, y",
Usage: "Do not promote to confirm",
},
},
Action: actionUpdateSelf,
},
{
Name: "edit",
Usage: "Edit config file",
Action: actionEdit,
},
{
Name: "version",
Usage: "Show version",
Aliases: []string{"v"},
Action: actionVersion,
},
}
if err := app.Run(os.Args); err != nil {
os.Exit(1)
}
}