Skip to content

Commit 5860cb4

Browse files
Refactor cli (#1240)
* refactor: refactor cmds * refactor: fix a lint error
1 parent 7b0668e commit 5860cb4

File tree

12 files changed

+113
-179
lines changed

12 files changed

+113
-179
lines changed

cmd/aqua-registry/main.go

+8-15
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/aquaproj/registry-tool/pkg/runtime"
1212
"github.com/sirupsen/logrus"
1313
"github.com/suzuki-shunsuke/logrus-error/logerr"
14+
"github.com/suzuki-shunsuke/urfave-cli-v3-util/urfave"
1415
)
1516

1617
var (
@@ -22,25 +23,17 @@ var (
2223
func main() {
2324
rt := runtime.New()
2425
logE := log.New(rt, version)
25-
if err := core(logE, rt); err != nil {
26+
if err := core(logE); err != nil {
2627
logerr.WithError(logE, err).Fatal("aqua failed")
2728
}
2829
}
2930

30-
func core(logE *logrus.Entry, rt *runtime.Runtime) error {
31-
runner := cli.Runner{
32-
Stdin: os.Stdin,
33-
Stdout: os.Stdout,
34-
Stderr: os.Stderr,
35-
LDFlags: &cli.LDFlags{
36-
Version: version,
37-
Commit: commit,
38-
Date: date,
39-
},
40-
LogE: logE,
41-
Runtime: rt,
42-
}
31+
func core(logE *logrus.Entry) error {
4332
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
4433
defer stop()
45-
return runner.Run(ctx, os.Args...) //nolint:wrapcheck
34+
return cli.Run(ctx, logE, &urfave.LDFlags{ //nolint:wrapcheck
35+
Version: version,
36+
Commit: commit,
37+
Date: date,
38+
}, os.Args...)
4639
}

go.mod

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ require (
99
github.com/sirupsen/logrus v1.9.3
1010
github.com/spf13/afero v1.14.0
1111
github.com/suzuki-shunsuke/logrus-error v0.1.4
12-
github.com/suzuki-shunsuke/urfave-cli-v3-util v0.0.3
13-
github.com/urfave/cli/v3 v3.2.0
12+
github.com/suzuki-shunsuke/urfave-cli-v3-util v0.0.4
13+
github.com/urfave/cli/v3 v3.3.2
1414
gopkg.in/yaml.v2 v2.4.0
1515
gopkg.in/yaml.v3 v3.0.1
1616
)

go.sum

+4-4
Original file line numberDiff line numberDiff line change
@@ -109,14 +109,14 @@ github.com/suzuki-shunsuke/go-osenv v0.1.0 h1:hBQ7yaeO1WBZsEWuDj1wrOWF+N7HSWSOpE
109109
github.com/suzuki-shunsuke/go-osenv v0.1.0/go.mod h1:ZlSVi4kYvV51JEtYpdHh9hAxXKLOWExXel3Jo74aacQ=
110110
github.com/suzuki-shunsuke/logrus-error v0.1.4 h1:nWo98uba1fANHdZ9Y5pJ2RKs/PpVjrLzRp5m+mRb9KE=
111111
github.com/suzuki-shunsuke/logrus-error v0.1.4/go.mod h1:WsVvvw6SKSt08/fB2qbnsKIMJA4K1MYCUprqsBJbMiM=
112-
github.com/suzuki-shunsuke/urfave-cli-v3-util v0.0.3 h1:WlB8FC2BH3uVsDoPug7wIFdkb+aAlaKpp07H6x/GjFg=
113-
github.com/suzuki-shunsuke/urfave-cli-v3-util v0.0.3/go.mod h1:B/O/TfFHRZoSeFqZHgwIRf4aWhZiIU46A+UK4ThS2sQ=
112+
github.com/suzuki-shunsuke/urfave-cli-v3-util v0.0.4 h1:cwI1g8vDJZnH/A8Pfc4Vkuc6MIcO4eulf1qiFHYbLB4=
113+
github.com/suzuki-shunsuke/urfave-cli-v3-util v0.0.4/go.mod h1:XzvaaJ7L21jH7CqwZj4FlYCRJYqiEUHMqiFp54je7/M=
114114
github.com/therootcompany/xz v1.0.1 h1:CmOtsn1CbtmyYiusbfmhmkpAAETj0wBIH6kCYaX+xzw=
115115
github.com/therootcompany/xz v1.0.1/go.mod h1:3K3UH1yCKgBneZYhuQUvJ9HPD19UEXEI0BWbMn8qNMY=
116116
github.com/ulikunitz/xz v0.5.12 h1:37Nm15o69RwBkXM0J6A5OlE67RZTfzUxTj8fB3dfcsc=
117117
github.com/ulikunitz/xz v0.5.12/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14=
118-
github.com/urfave/cli/v3 v3.2.0 h1:m8WIXY0U9LCuUl5r+0fqLWDhNYWt6qvlW+GcF4EoXf8=
119-
github.com/urfave/cli/v3 v3.2.0/go.mod h1:FJSKtM/9AiiTOJL4fJ6TbMUkxBXn7GO9guZqoZtpYpo=
118+
github.com/urfave/cli/v3 v3.3.2 h1:BYFVnhhZ8RqT38DxEYVFPPmGFTEf7tJwySTXsVRrS/o=
119+
github.com/urfave/cli/v3 v3.3.2/go.mod h1:FJSKtM/9AiiTOJL4fJ6TbMUkxBXn7GO9guZqoZtpYpo=
120120
github.com/wk8/go-ordered-map/v2 v2.1.8 h1:5h/BUHu93oj4gIdvHHHGsScSTMijfx5PeYkE/fJgbpc=
121121
github.com/wk8/go-ordered-map/v2 v2.1.8/go.mod h1:5nJHM5DyteebpVlHnWMV0rPz6Zp7+xBAnxjb1X5vnTw=
122122
go4.org v0.0.0-20230225012048-214862532bf5 h1:nifaUDeh+rPaBCMPMQHZmvJf+QdpLFnuQPwx+LxVmtc=

pkg/cli/check_repo.go renamed to pkg/cli/checkrepo/command.go

+11-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package cli
1+
package checkrepo
22

33
import (
44
"context"
@@ -9,7 +9,14 @@ import (
99
"github.com/urfave/cli/v3"
1010
)
1111

12-
func (r *Runner) newCheckRepoCommand() *cli.Command {
12+
type runner struct{}
13+
14+
func Command() *cli.Command {
15+
r := &runner{}
16+
return r.Command()
17+
}
18+
19+
func (r *runner) Command() *cli.Command {
1320
return &cli.Command{
1421
Name: "check-repo",
1522
Usage: `Check if GitHub Repository was transferred`,
@@ -22,12 +29,12 @@ e.g.
2229
$ aqua-registry check-repo Azure/aztfy
2330
Azure/aztfexport
2431
`,
25-
Action: r.checkRepoAction,
32+
Action: r.action,
2633
Flags: []cli.Flag{},
2734
}
2835
}
2936

30-
func (r *Runner) checkRepoAction(ctx context.Context, cmd *cli.Command) error {
37+
func (r *runner) action(ctx context.Context, cmd *cli.Command) error {
3138
return checkrepo.CheckRepo( //nolint:wrapcheck
3239
ctx, afero.NewOsFs(), &http.Client{
3340
CheckRedirect: func(_ *http.Request, _ []*http.Request) error {

pkg/cli/completion.go

-90
This file was deleted.

pkg/cli/create_pr_new_pkg.go renamed to pkg/cli/createprnewpkg/command.go

+14-5
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
1-
package cli
1+
package createprnewpkg
22

33
import (
44
"context"
55

66
newpkg "github.com/aquaproj/registry-tool/pkg/create-pr-new-pkg"
7+
"github.com/sirupsen/logrus"
78
"github.com/urfave/cli/v3"
89
)
910

10-
func (r *Runner) newCreatePRNewPkgCommand() *cli.Command {
11+
type runner struct {
12+
logE *logrus.Entry
13+
}
14+
15+
func Command(logE *logrus.Entry) *cli.Command {
16+
return (&runner{logE: logE}).Command()
17+
}
18+
19+
func (r *runner) Command() *cli.Command {
1120
return &cli.Command{
1221
Name: "create-pr-new-pkg",
1322
Usage: `Create a pull request to add new packages`,
@@ -25,10 +34,10 @@ This tool does the following things.
2534
3. Push the commit to origin
2635
4. Open a web browser to create a request with GitHub CLI
2736
`,
28-
Action: r.createPRNewPkgAction,
37+
Action: r.action,
2938
}
3039
}
3140

32-
func (r *Runner) createPRNewPkgAction(ctx context.Context, cmd *cli.Command) error {
33-
return newpkg.CreatePRNewPkgs(ctx, r.LogE, cmd.Args().Slice()...) //nolint:wrapcheck
41+
func (r *runner) action(ctx context.Context, cmd *cli.Command) error {
42+
return newpkg.CreatePRNewPkgs(ctx, r.logE, cmd.Args().Slice()...) //nolint:wrapcheck
3443
}

pkg/cli/generate_registry.go renamed to pkg/cli/gengr/command.go

+10-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package cli
1+
package gengr
22

33
import (
44
"context"
@@ -7,7 +7,13 @@ import (
77
"github.com/urfave/cli/v3"
88
)
99

10-
func (r *Runner) newGenerateRegistryCommand() *cli.Command {
10+
type runner struct{}
11+
12+
func Command() *cli.Command {
13+
return (&runner{}).Command()
14+
}
15+
16+
func (r *runner) Command() *cli.Command {
1117
return &cli.Command{
1218
Name: "generate-registry",
1319
Aliases: []string{"gr"},
@@ -20,10 +26,10 @@ Don't edit it manually, and if you update registry.yaml in the pkgs directory, d
2026
2127
No argument is needed.
2228
`,
23-
Action: r.generateRegistryAction,
29+
Action: r.action,
2430
}
2531
}
2632

27-
func (r *Runner) generateRegistryAction(context.Context, *cli.Command) error {
33+
func (r *runner) action(context.Context, *cli.Command) error {
2834
return genrg.GenerateRegistry() //nolint:wrapcheck
2935
}
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package cli
1+
package initcmd
22

33
import (
44
"context"
@@ -7,7 +7,13 @@ import (
77
"github.com/urfave/cli/v3"
88
)
99

10-
func (r *Runner) newInitCommand() *cli.Command {
10+
type runner struct{}
11+
12+
func Command() *cli.Command {
13+
return (&runner{}).Command()
14+
}
15+
16+
func (r *runner) Command() *cli.Command {
1117
return &cli.Command{
1218
Name: "init",
1319
Usage: `Create configuration files`,
@@ -17,10 +23,10 @@ func (r *Runner) newInitCommand() *cli.Command {
1723
* aqua.yaml
1824
* aqua-dev.yaml
1925
`,
20-
Action: r.initAction,
26+
Action: r.action,
2127
}
2228
}
2329

24-
func (r *Runner) initAction(ctx context.Context, _ *cli.Command) error {
30+
func (r *runner) action(ctx context.Context, _ *cli.Command) error {
2531
return initcmd.Init(ctx) //nolint:wrapcheck
2632
}

pkg/cli/mv.go renamed to pkg/cli/mv/command.go

+10-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package cli
1+
package mv
22

33
import (
44
"context"
@@ -9,17 +9,23 @@ import (
99
"github.com/urfave/cli/v3"
1010
)
1111

12-
func (r *Runner) newMVCommand() *cli.Command {
12+
type runner struct{}
13+
14+
func Command() *cli.Command {
15+
return (&runner{}).Command()
16+
}
17+
18+
func (r *runner) Command() *cli.Command {
1319
return &cli.Command{
1420
Name: "mv",
1521
Usage: `Rename a package`,
1622
UsageText: `$ aqua-registry mv <old package name> <new package name>`,
1723
Description: `Rename a package.`,
18-
Action: r.moveAction,
24+
Action: r.action,
1925
}
2026
}
2127

22-
func (r *Runner) moveAction(ctx context.Context, cmd *cli.Command) error {
28+
func (r *runner) action(ctx context.Context, cmd *cli.Command) error {
2329
args := cmd.Args().Slice()
2430
if len(args) != 2 { //nolint:mnd
2531
return errors.New("invalid arguments")
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
1-
package cli
1+
package patchchecksum
22

33
import (
44
"context"
55

66
"github.com/aquaproj/registry-tool/pkg/patchchecksum"
7+
"github.com/sirupsen/logrus"
78
"github.com/urfave/cli/v3"
89
)
910

10-
func (r *Runner) newPatchChecksumCommand() *cli.Command {
11+
type runner struct {
12+
logE *logrus.Entry
13+
}
14+
15+
func Command(logE *logrus.Entry) *cli.Command {
16+
return (&runner{logE: logE}).Command()
17+
}
18+
19+
func (r *runner) Command() *cli.Command {
1120
return &cli.Command{
1221
Name: "patch-checksum",
1322
Usage: `Patch a checksum configuration`,
@@ -18,10 +27,10 @@ e.g.
1827
1928
$ aqua-registry patch-checksum pkgs/suzuki-shunsuke/tfcmt/registry.yaml
2029
`,
21-
Action: r.patchChecksumAction,
30+
Action: r.action,
2231
}
2332
}
2433

25-
func (r *Runner) patchChecksumAction(ctx context.Context, cmd *cli.Command) error {
26-
return patchchecksum.PatchChecksum(ctx, r.LogE, cmd.Args().First()) //nolint:wrapcheck
34+
func (r *runner) action(ctx context.Context, cmd *cli.Command) error {
35+
return patchchecksum.PatchChecksum(ctx, r.logE, cmd.Args().First()) //nolint:wrapcheck
2736
}

0 commit comments

Comments
 (0)