@@ -2,10 +2,11 @@ package xcli
22
33import (
44 "bufio"
5+ "context"
56 "fmt"
67 "github.com/kardianos/service"
78 "github.com/sirupsen/logrus"
8- "github.com/urfave/cli/v2 "
9+ "github.com/urfave/cli/v3 "
910 "io"
1011 "os"
1112 "os/exec"
@@ -19,16 +20,16 @@ var (
1920 gitHash = "" //编译时自动填充,请不要人工赋值
2021
2122 //workingDirectory string //参数变量
22- installArguments cli. StringSlice //参数变量
23+ installArguments [] string //参数变量
2324)
2425
2526type XCli struct {
2627 serviceConfig * ServiceConfig
27- startCallback func (cCtx * cli.Context )
28+ startCallback func (ctx context. Context , cmd * cli.Command )
2829 stopCallback func () error
2930}
3031
31- func New (conf * ServiceConfig , startCallback func (cCtx * cli.Context ), stopCallback func () error ) (* XCli , error ) {
32+ func New (conf * ServiceConfig , startCallback func (ctx context. Context , cmd * cli.Command ), stopCallback func () error ) (* XCli , error ) {
3233 xCli := & XCli {
3334 serviceConfig : conf ,
3435 startCallback : startCallback ,
@@ -37,7 +38,7 @@ func New(conf *ServiceConfig, startCallback func(cCtx *cli.Context), stopCallbac
3738 return xCli , nil
3839}
3940
40- func (x * XCli ) createSystemService (cCtx * cli.Context ) (service.Service , error ) {
41+ func (x * XCli ) createSystemService (ctx context. Context , cmd * cli.Command ) (service.Service , error ) {
4142 conf := x .serviceConfig
4243 startCallback := x .startCallback
4344 stopCallback := x .stopCallback
@@ -50,14 +51,11 @@ func (x *XCli) createSystemService(cCtx *cli.Context) (service.Service, error) {
5051 workingDir := strings .Replace (dir , "\\ " , "/" , - 1 )
5152 logrus .Debugf ("workingDir: %s" , workingDir )
5253
53- logrus .Debugf ("installArguments: %+v" , installArguments .String ())
54- var arguments []string = installArguments .Value ()
55-
5654 svcConfig := & service.Config {
5755 Name : conf .ServiceName ,
5856 DisplayName : conf .ServiceDisplayName ,
5957 Description : conf .ServiceDescription ,
60- Arguments : arguments ,
58+ Arguments : installArguments ,
6159 //Executable: "/usr/local/bin/myapp",
6260 //Dependencies: []string{"After=network.target syslog.target"},
6361 //WorkingDirectory: "",
@@ -69,7 +67,8 @@ func (x *XCli) createSystemService(cCtx *cli.Context) (service.Service, error) {
6967 }
7068
7169 ss := & SystemService {
72- cCtx : cCtx ,
70+ ctx : ctx ,
71+ cmd : cmd ,
7372 startCallback : startCallback ,
7473 stopCallback : stopCallback ,
7574 }
@@ -104,7 +103,7 @@ func (x *XCli) Start(flags []cli.Flag, commands []*cli.Command) {
104103 {
105104 Name : "version" ,
106105 Usage : "Show version" ,
107- Action : func (cCtx * cli.Context ) error {
106+ Action : func (ctx context. Context , cmd * cli.Command ) error {
108107 //flag.Parse()
109108 fmt .Println ("build-time:" , buildTime )
110109 fmt .Println ("build-hash:" , gitHash )
@@ -164,12 +163,12 @@ func (x *XCli) Start(flags []cli.Flag, commands []*cli.Command) {
164163 //Destination: ,
165164 },
166165 },
167- Action : func (cCtx * cli.Context ) error {
166+ Action : func (ctx context. Context , cmd * cli.Command ) error {
168167 if platform == "linux-systemd" {
169168 serviceName := x .serviceConfig .ServiceName
170169 //journalctl -u xxxxx.service
171170 //journalctl -f -u xxxxx.service
172- follow := cCtx .Bool ("follow" )
171+ follow := cmd .Bool ("follow" )
173172 if follow {
174173 cmd := exec .Command ("journalctl" , "-f" , "-u" , serviceName )
175174 return runCommand (cmd , "StdoutPipe" )
@@ -195,12 +194,12 @@ func (x *XCli) Start(flags []cli.Flag, commands []*cli.Command) {
195194 //Destination: ,
196195 },
197196 },
198- Action : func (cCtx * cli.Context ) error {
197+ Action : func (ctx context. Context , cmd * cli.Command ) error {
199198 if platform == "linux-systemd" {
200199 serviceName := x .serviceConfig .ServiceName
201200 //systemctl -l --no-pager status xxxxx.service
202201 //systemctl status xxxxx.service
203- noPager := cCtx .Bool ("no-pager" )
202+ noPager := cmd .Bool ("no-pager" )
204203 if noPager {
205204 cmd := exec .Command ("systemctl" , "-l" , "--no-pager" , "status" , serviceName )
206205 return runCommand (cmd , "Stdout" )
@@ -216,15 +215,15 @@ func (x *XCli) Start(flags []cli.Flag, commands []*cli.Command) {
216215 },
217216 }
218217 allCommands := append (defaultCommands , commands ... )
219- app := & cli.App {
218+ app := & cli.Command {
220219 Name : x .serviceConfig .ServiceName ,
221220 //Usage: "",
222221 Flags : flags ,
223222 //Version: "version",
224223 //HideVersion: false, //内置的查看版本命令: xxx --version/xxx -v,因为使用自定义的xxx version command,所以这里不启用
225- Action : func (cCtx * cli.Context ) error {
224+ Action : func (ctx context. Context , cmd * cli.Command ) error {
226225 //在参数解析之后createSystemService,因为其内部可能用到了command参数
227- systemService , err := x .createSystemService (cCtx )
226+ systemService , err := x .createSystemService (ctx , cmd )
228227 if err != nil {
229228 return err
230229 }
@@ -239,21 +238,21 @@ func (x *XCli) Start(flags []cli.Flag, commands []*cli.Command) {
239238 Commands : allCommands ,
240239 }
241240
242- if err := app .Run (os .Args ); err != nil {
241+ if err := app .Run (context . Background (), os .Args ); err != nil {
243242 logrus .Errorf ("Error: %+v" , err )
244243 os .Exit (1 )
245244 }
246245}
247246
248- func (x * XCli ) controlAction (cCtx * cli.Context ) error {
247+ func (x * XCli ) controlAction (ctx context. Context , cmd * cli.Command ) error {
249248 //在参数解析之后createSystemService,因为其内部可能用到了command参数
250- systemService , err := x .createSystemService (cCtx )
249+ systemService , err := x .createSystemService (ctx , cmd )
251250 if err != nil {
252251 return err
253252 }
254- err = service .Control (systemService , cCtx . Command .Name )
253+ err = service .Control (systemService , cmd .Name )
255254 if err != nil {
256- logrus .Errorf ("service %s failed, err: %v" , cCtx . Command .Name , err )
255+ logrus .Errorf ("service %s failed, err: %v" , cmd .Name , err )
257256 return err
258257 }
259258 return nil
0 commit comments