Skip to content

Commit 775ccfb

Browse files
committed
feat: migrate to github.com/urfave/cli v3
1 parent e8a666a commit 775ccfb

File tree

122 files changed

+6960
-19118
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

122 files changed

+6960
-19118
lines changed

cmd/aoc-cli/commands.go

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

33
import (
4-
"context"
5-
6-
"github.com/urfave/cli/v2"
4+
"github.com/urfave/cli/v3"
75
)
86

9-
func commands(ctx context.Context) []*cli.Command {
7+
func commands() []*cli.Command {
108
const (
119
cmdRun = "run"
1210
)
1311

1412
cmds := []*cli.Command{
1513
{
16-
Name: cmdRun,
17-
Aliases: nil,
18-
Usage: "Runs advent-of-code application",
19-
UsageText: "",
20-
Description: "",
21-
ArgsUsage: "",
22-
Category: "",
23-
BashComplete: nil,
24-
Before: nil,
25-
After: nil,
26-
Action: menu(ctx),
27-
OnUsageError: nil,
28-
Subcommands: nil,
29-
Flags: cmdRunFlags(),
30-
SkipFlagParsing: false,
31-
HideHelp: false,
32-
HideHelpCommand: false,
33-
Hidden: false,
34-
UseShortOptionHandling: false,
35-
HelpName: "",
36-
CustomHelpTemplate: "",
14+
Name: cmdRun,
15+
Usage: "Runs advent-of-code application",
16+
Action: menu,
3717
},
3818
}
3919

cmd/aoc-cli/flags.go

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

33
import (
4-
"github.com/urfave/cli/v2"
4+
"github.com/urfave/cli/v3"
55

66
"github.com/obalunenko/advent-of-code/internal/puzzles"
77
)
@@ -19,46 +19,23 @@ func cmdRunFlags() []cli.Flag {
1919
var res []cli.Flag
2020

2121
elapsed := cli.BoolFlag{
22-
Name: flagElapsed,
23-
Aliases: []string{flagShortElapsed},
24-
Usage: "Enables elapsed time metric",
25-
EnvVars: nil,
26-
FilePath: "",
27-
Required: false,
28-
Hidden: false,
29-
Value: false,
30-
DefaultText: "",
31-
Destination: nil,
32-
HasBeenSet: false,
22+
Name: flagElapsed,
23+
Usage: "Enables elapsed time metric",
24+
Aliases: []string{flagShortElapsed},
3325
}
3426

3527
benchmark := cli.BoolFlag{
36-
Name: flagBenchmark,
37-
Aliases: []string{flagShortBenchmark},
38-
Usage: "Enables benchmark metric",
39-
EnvVars: nil,
40-
FilePath: "",
41-
Required: false,
42-
Hidden: false,
43-
Value: false,
44-
DefaultText: "",
45-
Destination: nil,
46-
HasBeenSet: false,
28+
Name: flagBenchmark,
29+
Usage: "Enables benchmark metric",
30+
Aliases: []string{flagShortBenchmark},
4731
}
4832

4933
session := cli.StringFlag{
50-
Name: flagSession,
51-
Aliases: []string{flagShortSession},
52-
Usage: "AOC auth session to get inputs",
53-
EnvVars: []string{puzzles.AOCSession},
54-
FilePath: "",
55-
Required: true,
56-
Hidden: false,
57-
TakesFile: false,
58-
Value: "",
59-
DefaultText: "",
60-
Destination: nil,
61-
HasBeenSet: false,
34+
Name: flagSession,
35+
Usage: "AOC auth session to get inputs",
36+
Sources: cli.EnvVars(puzzles.AOCSession),
37+
Required: true,
38+
Aliases: []string{flagShortSession},
6239
}
6340

6441
res = append(res, &elapsed, &benchmark, &session)

cmd/aoc-cli/handlers.go

Lines changed: 44 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -14,32 +14,29 @@ import (
1414
promptlist "github.com/manifoldco/promptui/list"
1515
log "github.com/obalunenko/logger"
1616
"github.com/savioxavier/termlink"
17-
"github.com/urfave/cli/v2"
17+
"github.com/urfave/cli/v3"
1818

1919
"github.com/obalunenko/advent-of-code/internal/command"
2020
"github.com/obalunenko/advent-of-code/internal/puzzles"
2121
)
2222

23-
func onExit(_ context.Context) cli.AfterFunc {
24-
return func(_ *cli.Context) error {
25-
fmt.Println("Exit...")
23+
func onExit(_ context.Context, _ *cli.Command) error {
24+
fmt.Println("Exit...")
2625

27-
return nil
28-
}
26+
return nil
2927
}
3028

31-
func printHeader(_ context.Context) cli.BeforeFunc {
29+
func printHeader(ctx context.Context, cmd *cli.Command) (context.Context, error) {
3230
const (
3331
padding int = 1
3432
minWidth int = 0
3533
tabWidth int = 0
3634
padChar byte = ' '
3735
)
3836

39-
return func(c *cli.Context) error {
40-
w := tabwriter.NewWriter(c.App.Writer, minWidth, tabWidth, padding, padChar, tabwriter.TabIndent)
37+
w := tabwriter.NewWriter(cmd.Writer, minWidth, tabWidth, padding, padChar, tabwriter.TabIndent)
4138

42-
_, err := fmt.Fprintf(w, `
39+
_, err := fmt.Fprintf(w, `
4340

4441
█████╗ ██████╗ ██╗ ██╗███████╗███╗ ██╗████████╗ ██████╗ ███████╗ ██████╗ ██████╗ ██████╗ ███████╗
4542
██╔══██╗██╔══██╗██║ ██║██╔════╝████╗ ██║╚══██╔══╝ ██╔═══██╗██╔════╝ ██╔════╝██╔═══██╗██╔══██╗██╔════╝
@@ -49,54 +46,49 @@ func printHeader(_ context.Context) cli.BeforeFunc {
4946
╚═╝ ╚═╝╚═════╝ ╚═══╝ ╚══════╝╚═╝ ╚═══╝ ╚═╝ ╚═════╝ ╚═╝ ╚═════╝ ╚═════╝ ╚═════╝ ╚══════╝
5047

5148
`)
52-
if err != nil {
53-
return fmt.Errorf("print version: %w", err)
54-
}
55-
56-
return nil
49+
if err != nil {
50+
return ctx, fmt.Errorf("print version: %w", err)
5751
}
52+
53+
return ctx, nil
5854
}
5955

60-
func notFound(ctx context.Context) cli.CommandNotFoundFunc {
61-
return func(c *cli.Context, command string) {
62-
if _, err := fmt.Fprintf(
63-
c.App.Writer,
64-
"Command [%s] not supported.\nTry --help flag to see how to use it\n",
65-
command,
66-
); err != nil {
67-
log.WithError(ctx, err).Fatal("Failed to print not found message")
68-
}
56+
func notFound(ctx context.Context, cmd *cli.Command, command string) {
57+
if _, err := fmt.Fprintf(
58+
cmd.Writer,
59+
"Command [%s] not supported.\nTry --help flag to see how to use it\n",
60+
command,
61+
); err != nil {
62+
log.WithError(ctx, err).Fatal("Failed to print not found message")
6963
}
7064
}
7165

72-
func menu(ctx context.Context) cli.ActionFunc {
73-
return func(c *cli.Context) error {
74-
ctx = command.ContextWithOptions(ctx, optionsFromCli(c)...)
75-
ctx = command.ContextWithSession(ctx, sessionFromCli(c))
76-
77-
years := puzzles.GetYears()
78-
79-
items := makeMenuItemsList(years, exit)
80-
81-
prompt := promptui.Select{
82-
Label: "Years menu (exit' for exit)",
83-
Items: items,
84-
Size: pageSize,
85-
CursorPos: 0,
86-
IsVimMode: false,
87-
HideHelp: false,
88-
HideSelected: false,
89-
Templates: nil,
90-
Keys: nil,
91-
Searcher: searcher(items),
92-
StartInSearchMode: false,
93-
Pointer: promptui.DefaultCursor,
94-
Stdin: nil,
95-
Stdout: nil,
96-
}
66+
func menu(ctx context.Context, c *cli.Command) error {
67+
ctx = command.ContextWithOptions(ctx, optionsFromCli(c)...)
68+
ctx = command.ContextWithSession(ctx, sessionFromCli(c))
69+
70+
years := puzzles.GetYears()
71+
72+
items := makeMenuItemsList(years, exit)
9773

98-
return handleYearChoices(ctx, prompt)
74+
prompt := promptui.Select{
75+
Label: "Years menu (exit' for exit)",
76+
Items: items,
77+
Size: pageSize,
78+
CursorPos: 0,
79+
IsVimMode: false,
80+
HideHelp: false,
81+
HideSelected: false,
82+
Templates: nil,
83+
Keys: nil,
84+
Searcher: searcher(items),
85+
StartInSearchMode: false,
86+
Pointer: promptui.DefaultCursor,
87+
Stdin: nil,
88+
Stdout: nil,
9989
}
90+
91+
return handleYearChoices(ctx, prompt)
10092
}
10193

10294
func menuPuzzle(ctx context.Context, year string) error {
@@ -238,7 +230,7 @@ func isBack(in string) bool {
238230
return strings.EqualFold(back, in)
239231
}
240232

241-
func optionsFromCli(c *cli.Context) []puzzles.RunOption {
233+
func optionsFromCli(c *cli.Command) []puzzles.RunOption {
242234
const optsNum = 2
243235

244236
options := make([]puzzles.RunOption, 0, optsNum)
@@ -254,7 +246,7 @@ func optionsFromCli(c *cli.Context) []puzzles.RunOption {
254246
return options
255247
}
256248

257-
func sessionFromCli(c *cli.Context) string {
249+
func sessionFromCli(c *cli.Command) string {
258250
var sess string
259251

260252
sess = c.String(flagSession)

cmd/aoc-cli/main.go

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"os"
88

99
log "github.com/obalunenko/logger"
10-
"github.com/urfave/cli/v2"
10+
"github.com/urfave/cli/v3"
1111

1212
_ "github.com/obalunenko/advent-of-code/internal/puzzles/solutions" // register all solutions.
1313
)
@@ -24,29 +24,25 @@ var errExit = errors.New("exit is chosen")
2424
func main() {
2525
ctx := context.Background()
2626

27-
app := cli.NewApp()
27+
ctx = log.ContextWithLogger(ctx, log.FromContext(ctx))
28+
29+
app := cli.Command{}
2830
app.Name = "aoc-cli"
2931
app.Description = "Solutions of puzzles for Advent Of Code (https://adventofcode.com/)\n" +
3032
"This command line tool contains solutions for puzzles and cli tool to run solutions to get " +
3133
"answers for input on site."
32-
app.Usage = `a command line tool for get solution for Advent of Code puzzles`
33-
app.Authors = []*cli.Author{
34-
{
35-
Name: "Oleg Balunenko",
36-
37-
},
34+
app.Usage = `A command line tool for get solution for Advent of Code puzzles`
35+
app.Authors = []any{
36+
"Oleg Balunenko <[email protected]>",
3837
}
39-
app.CommandNotFound = notFound(ctx)
40-
app.Commands = commands(ctx)
41-
app.Version = printVersion(ctx)
42-
app.Before = printHeader(ctx)
43-
app.After = onExit(ctx)
4438

45-
if err := app.Run(os.Args); err != nil {
46-
if errors.Is(err, errExit) {
47-
return
48-
}
39+
app.CommandNotFound = notFound
40+
app.Commands = commands()
41+
app.Version = printVersion(ctx)
42+
app.Before = printHeader
43+
app.After = onExit
4944

45+
if err := app.Run(ctx, os.Args); err != nil {
5046
log.WithError(ctx, err).Fatal("Run failed")
5147
}
5248
}

go.mod

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,16 @@ require (
1212
github.com/obalunenko/version v1.3.1
1313
github.com/savioxavier/termlink v1.4.3
1414
github.com/stretchr/testify v1.10.0
15-
github.com/urfave/cli/v2 v2.27.7
15+
github.com/urfave/cli/v3 v3.3.8
1616
)
1717

1818
require (
1919
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e // indirect
20-
github.com/cpuguy83/go-md2man/v2 v2.0.7 // indirect
2120
github.com/davecgh/go-spew v1.1.1 // indirect
2221
github.com/fatih/color v1.13.0 // indirect
2322
github.com/mattn/go-colorable v0.1.9 // indirect
2423
github.com/mattn/go-isatty v0.0.14 // indirect
2524
github.com/pmezard/go-difflib v1.0.0 // indirect
26-
github.com/russross/blackfriday/v2 v2.1.0 // indirect
27-
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 // indirect
2825
golang.org/x/exp v0.0.0-20250218142911-aa4b98e5adaa // indirect
2926
golang.org/x/sys v0.6.0 // indirect
3027
golang.org/x/term v0.6.0 // indirect

go.sum

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e h1:fY5BOSpyZCqRo5O
66
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
77
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1 h1:q763qf9huN11kDQavWsoZXJNW3xEE4JJyHa5Q25/sd8=
88
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
9-
github.com/cpuguy83/go-md2man/v2 v2.0.7 h1:zbFlGlXEAKlwXpmvle3d8Oe3YnkKIK4xSRTd3sHPnBo=
10-
github.com/cpuguy83/go-md2man/v2 v2.0.7/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g=
119
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
1210
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
1311
github.com/fatih/color v1.13.0 h1:8LOYc1KYPPmyKMuN8QV2DNRWNbLo6LZ0iLs8+mlH53w=
@@ -27,16 +25,12 @@ github.com/obalunenko/version v1.3.1 h1:NN+YSOrti8mEyJSnu+7//YSvGrOhLivh60hJXhIr
2725
github.com/obalunenko/version v1.3.1/go.mod h1:56ydLXefFem3sEJ2iyguuZ7dwJ25VoIRTev/JfxFIa4=
2826
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
2927
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
30-
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
31-
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
3228
github.com/savioxavier/termlink v1.4.3 h1:Gh6vrG7jSn21cRiYdQqFXYcdXfM+Fg14aG487JTfKpA=
3329
github.com/savioxavier/termlink v1.4.3/go.mod h1:5T5ePUlWbxCHIwyF8/Ez1qufOoGM89RCg9NvG+3G3gc=
3430
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
3531
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
36-
github.com/urfave/cli/v2 v2.27.7 h1:bH59vdhbjLv3LAvIu6gd0usJHgoTTPhCFib8qqOwXYU=
37-
github.com/urfave/cli/v2 v2.27.7/go.mod h1:CyNAG/xg+iAOg0N4MPGZqVmv2rCoP267496AOXUZjA4=
38-
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 h1:gEOO8jv9F4OT7lGCjxCBTO/36wtF6j2nSip77qHd4x4=
39-
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1/go.mod h1:Ohn+xnUBiLI6FVj/9LpzZWtj1/D6lUovWYBkxHVV3aM=
32+
github.com/urfave/cli/v3 v3.3.8 h1:BzolUExliMdet9NlJ/u4m5vHSotJ3PzEqSAZ1oPMa/E=
33+
github.com/urfave/cli/v3 v3.3.8/go.mod h1:FJSKtM/9AiiTOJL4fJ6TbMUkxBXn7GO9guZqoZtpYpo=
4034
golang.org/x/exp v0.0.0-20250218142911-aa4b98e5adaa h1:t2QcU6V556bFjYgu4L6C+6VrCPyJZ+eyRsABUPs1mz4=
4135
golang.org/x/exp v0.0.0-20250218142911-aa4b98e5adaa/go.mod h1:BHOTPb3L19zxehTsLoJXVaTktb06DFgmdW6Wb9s8jqk=
4236
golang.org/x/sys v0.0.0-20181122145206-62eef0e2fa9b/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=

vendor/github.com/cpuguy83/go-md2man/v2/LICENSE.md

Lines changed: 0 additions & 21 deletions
This file was deleted.

0 commit comments

Comments
 (0)