-
Notifications
You must be signed in to change notification settings - Fork 0
/
xcli.go
329 lines (311 loc) · 9.34 KB
/
xcli.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
package xcli
import (
"bufio"
"fmt"
"github.com/kardianos/service"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
"io"
"os"
"os/exec"
"path/filepath"
"strings"
)
var DefaultStopCallback = func() error { return nil }
var (
buildTime = "" //编译时自动填充,请不要人工赋值
gitHash = "" //编译时自动填充,请不要人工赋值
//workingDirectory string //参数变量
installArguments cli.StringSlice //参数变量
)
type XCli struct {
serviceConfig *ServiceConfig
startCallback func(cCtx *cli.Context)
stopCallback func() error
}
func New(conf *ServiceConfig, startCallback func(cCtx *cli.Context), stopCallback func() error) (*XCli, error) {
xCli := &XCli{
serviceConfig: conf,
startCallback: startCallback,
stopCallback: stopCallback,
}
return xCli, nil
}
func (x *XCli) createSystemService(cCtx *cli.Context) (service.Service, error) {
conf := x.serviceConfig
startCallback := x.startCallback
stopCallback := x.stopCallback
dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
return nil, err
}
// 默认install service时,WorkingDirectory就是其二进制程序所在的目录。这样便于加载配置文件。
workingDir := strings.Replace(dir, "\\", "/", -1)
logrus.Debugf("workingDir: %s", workingDir)
logrus.Debugf("installArguments: %+v", installArguments.String())
var arguments []string = installArguments.Value()
svcConfig := &service.Config{
Name: conf.ServiceName,
DisplayName: conf.ServiceDisplayName,
Description: conf.ServiceDescription,
Arguments: arguments,
//Executable: "/usr/local/bin/myapp",
//Dependencies: []string{"After=network.target syslog.target"},
//WorkingDirectory: "",
WorkingDirectory: workingDir,
//Option: service.KeyValue{
// "Restart": "always", // Restart=always
//},
EnvVars: conf.EnvVars,
}
ss := &SystemService{
cCtx: cCtx,
startCallback: startCallback,
stopCallback: stopCallback,
}
s, err := service.New(ss, svcConfig)
if err != nil {
return nil, fmt.Errorf("service New failed, err: %v", err)
}
return s, nil
}
func (x *XCli) Start(flags []cli.Flag, commands []*cli.Command) {
logCommandUsageText := ""
platform := service.Platform()
if platform == "linux-systemd" {
logCommandUsageText = "" +
"ServiceType: " + platform + "\n" +
"journalctl commands:" + "\n" +
"journalctl -u " + x.serviceConfig.ServiceName + "\n" +
"journalctl -n 10 -u " + x.serviceConfig.ServiceName + "\n" +
"journalctl -f -u " + x.serviceConfig.ServiceName + "\n" +
"journalctl --disk-usage 检查当前journal使用磁盘量\n" +
"journalctl --rotate \n" +
"journalctl --vacuum-time=2d 只保留两天的日志\n" +
"journalctl --vacuum-size=1G 只保留1G的日志\n" +
"journalctl --vacuum-files=2 只保留最近的两个日志文件\n" +
"配置systemd自动处理旧日志文件: /etc/systemd/journald.conf. 请记住,编辑配置文件后,需要重新启动journald服务: systemctl restart systemd-journald\n"
} else if platform == "darwin-launchd" {
//logCommandUsageText = "..."
}
defaultCommands := []*cli.Command{
{
Name: "version",
Usage: "Show version",
Action: func(cCtx *cli.Context) error {
//flag.Parse()
fmt.Println("build-time:", buildTime)
fmt.Println("build-hash:", gitHash)
//go1.21有了toolchain指令后,环境变量里go不一定是最终编译使用的go版本,所以请自行使用 go version -m <program> 查看实际编译使用的go版本
fmt.Println("build-go :", "go version -m <program>")
return nil
},
},
{
Name: "install",
Action: x.controlAction,
Flags: []cli.Flag{
// &cli.StringFlag{
// Name: "workingDirectory",
// Aliases: []string{"w"},
// Usage: "The working directory path, must be an absolute path",
// Required: true,
// Destination: &workingDirectory,
// },
//},
&cli.StringSliceFlag{
// ./xxx install -r '-f,/path/of/config.yaml' # 逗号分隔key和value
Name: "argument",
Aliases: []string{"r"},
Usage: "The arguments for install",
//Value: "",
Destination: &installArguments,
},
},
},
{
Name: "uninstall",
Action: x.controlAction,
},
{
Name: "start",
Action: x.controlAction,
},
{
Name: "restart",
Action: x.controlAction,
},
{
Name: "stop",
Action: x.controlAction,
},
{
Name: "log",
Usage: "Show logs",
UsageText: logCommandUsageText,
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "follow",
Aliases: []string{"f"},
Usage: "Show only the most recent journal entries, and continuously print new entries as they are appended to the journal.",
Value: false,
//Destination: ,
},
},
Action: func(cCtx *cli.Context) error {
if platform == "linux-systemd" {
serviceName := x.serviceConfig.ServiceName
//journalctl -u xxxxx.service
//journalctl -f -u xxxxx.service
follow := cCtx.Bool("follow")
if follow {
cmd := exec.Command("journalctl", "-f", "-u", serviceName)
return runCommand(cmd, "StdoutPipe")
} else {
cmd := exec.Command("journalctl", "-u", serviceName)
return runCommand(cmd, "StdoutWithLess")
}
} else {
fmt.Println(service.Platform() + " not support")
}
return nil
},
},
{
Name: "status",
Usage: "Show terse runtime status information about one or more units, followed by most recent log data from the journal.",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "no-pager",
//Aliases: []string{"?"},
Usage: "Do not pipe output into a pager.",
Value: false,
//Destination: ,
},
},
Action: func(cCtx *cli.Context) error {
if platform == "linux-systemd" {
serviceName := x.serviceConfig.ServiceName
//systemctl -l --no-pager status xxxxx.service
//systemctl status xxxxx.service
noPager := cCtx.Bool("no-pager")
if noPager {
cmd := exec.Command("systemctl", "-l", "--no-pager", "status", serviceName)
return runCommand(cmd, "Stdout")
} else {
cmd := exec.Command("systemctl", "status", serviceName)
return runCommand(cmd, "StdoutPipe")
}
} else {
fmt.Println(service.Platform() + " not support")
}
return nil
},
},
}
allCommands := append(defaultCommands, commands...)
app := &cli.App{
Name: x.serviceConfig.ServiceName,
//Usage: "",
Flags: flags,
//Version: "version",
//HideVersion: false, //内置的查看版本命令: xxx --version/xxx -v,因为使用自定义的xxx version command,所以这里不启用
Action: func(cCtx *cli.Context) error {
//在参数解析之后createSystemService,因为其内部可能用到了command参数
systemService, err := x.createSystemService(cCtx)
if err != nil {
return err
}
err = systemService.Run()
if err != nil {
logrus.Errorf("service Run failed, err: %v", err)
return err
}
return nil
},
Commands: allCommands,
}
if err := app.Run(os.Args); err != nil {
logrus.Errorf("Error: %+v", err)
os.Exit(1)
}
}
func (x *XCli) controlAction(cCtx *cli.Context) error {
//在参数解析之后createSystemService,因为其内部可能用到了command参数
systemService, err := x.createSystemService(cCtx)
if err != nil {
return err
}
err = service.Control(systemService, cCtx.Command.Name)
if err != nil {
logrus.Errorf("service %s failed, err: %v", cCtx.Command.Name, err)
return err
}
return nil
}
func runCommand(cmd *exec.Cmd, outputType string) error {
//fmt.Printf("cmd: %s\n", cmd)
if outputType == "StdoutPipe" {
pipe, err := cmd.StdoutPipe()
if err != nil {
logrus.Errorf("cmd.StdoutPipe() failed with %s", err)
return err
}
if err = cmd.Start(); err != nil {
logrus.Errorf("cmd.Start() failed with %s", err)
return err
}
go func(p io.ReadCloser) {
reader := bufio.NewReader(pipe)
line, err := reader.ReadString('\n')
for err == nil {
fmt.Println(line)
line, err = reader.ReadString('\n')
}
}(pipe)
if err = cmd.Wait(); err != nil {
logrus.Errorf("cmd.Wait() failed with %s", err)
return err
}
} else if outputType == "Stdout" {
//cmd.Stdout = os.Stdout
//cmd.Stderr = os.Stderr
//err := cmd.Run()
//if err != nil {
// logrus.Errorf("cmd.Run() failed with %s", err)
// return err
//}
out, err := cmd.Output()
if err != nil {
logrus.Errorf("cmd.Output() failed with %s", err)
return err
}
fmt.Println(string(out))
//out, err := cmd.CombinedOutput()
//if err != nil {
// logrus.Fatalf("cmd.Run() failed with %s", err)
// return err
//}
//fmt.Println(string(out))
} else if outputType == "StdoutWithLess" {
// more和less都是Linux系统中用于在终端显示文件内容的命令,它们之间有一些区别。
// more是一个简单的分页器,只能向下滚动,无法向上滚动,而less是more的改进版本,可以向上和向下滚动,支持更多功能。
stdout, err := cmd.StdoutPipe()
if err != nil {
logrus.Errorf("cmd.StdoutPipe() failed with %s", err)
return err
}
if err = cmd.Start(); err != nil {
logrus.Errorf("cmd.Start() failed with %s", err)
return err
}
moreCmd := exec.Command("less")
moreCmd.Stdin = stdout
moreCmd.Stdout = os.Stdout
if err = moreCmd.Run(); err != nil {
logrus.Errorf("moreCmd.Run() failed with %s", err)
return err
}
}
return nil
}