Skip to content

Commit ce06f09

Browse files
committed
Upgrade to urfave/cli/v3
1 parent 221bdc9 commit ce06f09

6 files changed

Lines changed: 44 additions & 50 deletions

File tree

.github/workflows/release_test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ jobs:
1111
runs-on: ubuntu-latest
1212
strategy:
1313
matrix:
14-
go: [ '1.18' , '1.19' , '1.20' , '1.21' , '1.22' , '1.23' , '1.24' ]
14+
go: [ '1.22' , '1.23' , '1.24' ]
1515
steps:
1616
- name: Set up Go ${{ matrix.go }}
1717
uses: actions/setup-go@v5

examples/main.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package main
22

33
import (
4+
"context"
45
"fmt"
56
"github.com/taobig/xcli"
67
"github.com/taobig/xcli/examples/internal"
7-
"github.com/urfave/cli/v2"
8+
"github.com/urfave/cli/v3"
89
"os"
910
)
1011

@@ -23,9 +24,9 @@ func main() {
2324
},
2425
}
2526

26-
startAction := func(cCtx *cli.Context) {
27-
fmt.Println("arg test:", cCtx.Bool("test")) //获取参数示例一
28-
fmt.Println("arg configFile:", configFile) //获取参数示例二
27+
startAction := func(ctx context.Context, cmd *cli.Command) {
28+
fmt.Println("arg test:", cmd.Bool("test")) //获取参数示例一
29+
fmt.Println("arg configFile:", configFile) //获取参数示例二
2930

3031
fmt.Println("ENV_VAR:", os.Getenv("ENV_VAR")) //获取环境变量示例
3132

@@ -76,5 +77,4 @@ func main() {
7677
}
7778
var commands []*cli.Command
7879
xCli.Start(flags, commands)
79-
return
8080
}

go.mod

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
11
module github.com/taobig/xcli
22

3-
go 1.18
3+
go 1.22
4+
5+
toolchain go1.22.12
46

57
retract v0.0.8
68

79
require (
810
github.com/kardianos/service v1.2.2
911
github.com/sirupsen/logrus v1.9.3
10-
github.com/urfave/cli/v2 v2.27.6
12+
github.com/urfave/cli/v3 v3.2.0
1113
)
1214

13-
require (
14-
github.com/cpuguy83/go-md2man/v2 v2.0.5 // indirect
15-
github.com/russross/blackfriday/v2 v2.1.0 // indirect
16-
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 // indirect
17-
golang.org/x/sys v0.22.0 // indirect
18-
)
15+
require golang.org/x/sys v0.22.0 // indirect

go.sum

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,23 @@
1-
github.com/cpuguy83/go-md2man/v2 v2.0.5 h1:ZtcqGrnekaHpVLArFSe4HK5DoKx1T0rq2DwVB0alcyc=
2-
github.com/cpuguy83/go-md2man/v2 v2.0.5/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
31
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
42
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
53
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
64
github.com/kardianos/service v1.2.2 h1:ZvePhAHfvo0A7Mftk/tEzqEZ7Q4lgnR8sGz4xu1YX60=
75
github.com/kardianos/service v1.2.2/go.mod h1:CIMRFEJVL+0DS1a3Nx06NaMn4Dz63Ng6O7dl0qH0zVM=
86
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
97
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
10-
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
11-
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
128
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
139
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
1410
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
15-
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
1611
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
17-
github.com/urfave/cli/v2 v2.27.6 h1:VdRdS98FNhKZ8/Az8B7MTyGQmpIr36O1EHybx/LaZ4g=
18-
github.com/urfave/cli/v2 v2.27.6/go.mod h1:3Sevf16NykTbInEnD0yKkjDAeZDS0A6bzhBH5hrMvTQ=
19-
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 h1:gEOO8jv9F4OT7lGCjxCBTO/36wtF6j2nSip77qHd4x4=
20-
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1/go.mod h1:Ohn+xnUBiLI6FVj/9LpzZWtj1/D6lUovWYBkxHVV3aM=
12+
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
13+
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
14+
github.com/urfave/cli/v3 v3.2.0 h1:m8WIXY0U9LCuUl5r+0fqLWDhNYWt6qvlW+GcF4EoXf8=
15+
github.com/urfave/cli/v3 v3.2.0/go.mod h1:FJSKtM/9AiiTOJL4fJ6TbMUkxBXn7GO9guZqoZtpYpo=
2116
golang.org/x/sys v0.0.0-20201015000850-e3ed0017c211/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
2217
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
2318
golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI=
2419
golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
2520
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
2621
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
2722
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
23+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

systemservice.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
package xcli
22

33
import (
4+
"context"
45
"github.com/kardianos/service"
5-
"github.com/urfave/cli/v2"
6+
"github.com/urfave/cli/v3"
67
)
78

89
type SystemService struct {
9-
cCtx *cli.Context
10+
ctx context.Context
11+
cmd *cli.Command
1012

11-
startCallback func(cCtx *cli.Context)
13+
startCallback func(ctx context.Context, cmd *cli.Command)
1214
stopCallback func() error
1315
}
1416

@@ -18,7 +20,7 @@ func (ss *SystemService) Start(s service.Service) error {
1820
}
1921

2022
func (ss *SystemService) run() {
21-
ss.startCallback(ss.cCtx)
23+
ss.startCallback(ss.ctx, ss.cmd)
2224
}
2325

2426
func (ss *SystemService) Stop(s service.Service) error {

xcli.go

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ package xcli
22

33
import (
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

2526
type 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

Comments
 (0)