Skip to content

Commit e4c9775

Browse files
authored
Upgrade to cli/v3 (#140)
1 parent 134b0b6 commit e4c9775

File tree

15 files changed

+234
-224
lines changed

15 files changed

+234
-224
lines changed

cmd/ipni/ipni.go

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

33
import (
4+
"context"
45
"encoding/json"
56
"fmt"
67
"io"
@@ -15,15 +16,15 @@ import (
1516
"github.com/ipni/ipni-cli/pkg/random"
1617
"github.com/ipni/ipni-cli/pkg/spaddr"
1718
"github.com/ipni/ipni-cli/pkg/verify"
18-
"github.com/urfave/cli/v2"
19+
"github.com/urfave/cli/v3"
1920
"golang.org/x/mod/semver"
2021
)
2122

2223
func main() {
2324
// Disable logging that happens in packages such as data-transfer.
2425
_ = logging.SetLogLevel("*", "fatal")
2526

26-
app := &cli.App{
27+
cmd := &cli.Command{
2728
Name: "ipni",
2829
Usage: "Commands to interact with IPNI indexers and index providers",
2930
Version: ipnicli.Version,
@@ -38,7 +39,7 @@ func main() {
3839
},
3940
}
4041

41-
if err := app.Run(os.Args); err != nil {
42+
if err := cmd.Run(context.Background(), os.Args); err != nil {
4243
fmt.Fprintln(os.Stderr, err)
4344
os.Exit(1)
4445
}
@@ -50,9 +51,9 @@ var versionCmd = &cli.Command{
5051
Action: versionAction,
5152
}
5253

53-
func versionAction(cctx *cli.Context) error {
54+
func versionAction(ctx context.Context, cmd *cli.Command) error {
5455
const githubURL = "https://api.github.com/repos/ipni/ipni-cli/releases/latest"
55-
fmt.Println(cctx.App.Version)
56+
fmt.Println(cmd.Version)
5657
resp, err := http.Get(githubURL)
5758
if err != nil {
5859
return fmt.Errorf("cannot check for newer version: %s", err)
@@ -72,11 +73,11 @@ func versionAction(cctx *cli.Context) error {
7273
}
7374
switch semver.Compare(ipnicli.Release, relName) {
7475
case 0:
75-
fmt.Fprintln(os.Stderr, cctx.App.Name, "is up to date")
76+
fmt.Fprintln(os.Stderr, cmd.Name, "is up to date")
7677
case -1:
77-
fmt.Fprintln(os.Stderr, "a newer version of", cctx.App.Name, "is available:", relName)
78+
fmt.Fprintln(os.Stderr, "a newer version of", cmd.Name, "is available:", relName)
7879
case 1:
79-
fmt.Fprintln(os.Stderr, "this is the newest version of", cctx.App.Name)
80+
fmt.Fprintln(os.Stderr, "this is the newest version of", cmd.Name)
8081
}
8182
return nil
8283
}

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ require (
1717
github.com/multiformats/go-multicodec v0.9.0
1818
github.com/multiformats/go-multihash v0.2.3
1919
github.com/stretchr/testify v1.10.0
20-
github.com/urfave/cli/v2 v2.27.5
20+
github.com/urfave/cli/v3 v3.3.2
2121
github.com/ybbus/jsonrpc/v2 v2.1.7
2222
golang.org/x/mod v0.23.0
2323
)
@@ -139,6 +139,7 @@ require (
139139
github.com/raulk/go-watchdog v1.3.0 // indirect
140140
github.com/russross/blackfriday/v2 v2.1.0 // indirect
141141
github.com/spaolacci/murmur3 v1.1.0 // indirect
142+
github.com/urfave/cli/v2 v2.27.5 // indirect
142143
github.com/whyrusleeping/cbor v0.0.0-20171005072247-63513f603b11 // indirect
143144
github.com/whyrusleeping/cbor-gen v0.2.0 // indirect
144145
github.com/wlynxg/anet v0.0.5 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -577,6 +577,8 @@ github.com/urfave/cli v1.22.10/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60Nt
577577
github.com/urfave/cli/v2 v2.0.0/go.mod h1:SE9GqnLQmjVa0iPEY0f1w3ygNIYcIJ0OKPMoW2caLfQ=
578578
github.com/urfave/cli/v2 v2.27.5 h1:WoHEJLdsXr6dDWoJgMq/CboDmyY/8HMMH1fTECbih+w=
579579
github.com/urfave/cli/v2 v2.27.5/go.mod h1:3Sevf16NykTbInEnD0yKkjDAeZDS0A6bzhBH5hrMvTQ=
580+
github.com/urfave/cli/v3 v3.3.2 h1:BYFVnhhZ8RqT38DxEYVFPPmGFTEf7tJwySTXsVRrS/o=
581+
github.com/urfave/cli/v3 v3.3.2/go.mod h1:FJSKtM/9AiiTOJL4fJ6TbMUkxBXn7GO9guZqoZtpYpo=
580582
github.com/viant/assertly v0.4.8/go.mod h1:aGifi++jvCrUaklKEKT0BU95igDNaqkvz+49uaYMPRU=
581583
github.com/viant/toolbox v0.24.0/go.mod h1:OxMCG57V0PXuIP2HNQrtJf2CjqdmbrOx5EkMILuUhzM=
582584
github.com/warpfork/go-testmark v0.12.1 h1:rMgCpJfwy1sJ50x0M0NgyphxYYPMOODIJHhsXyEHU0s=

pkg/ads/ads.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package ads
22

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

77
var AdsCmd = &cli.Command{
88
Name: "ads",
99
Usage: "Show advertisements on a chain from a specified publisher",
10-
Subcommands: []*cli.Command{
10+
Commands: []*cli.Command{
1111
adsGetSubCmd,
1212
adsListSubCmd,
1313
adsCrawlSubCmd,

pkg/ads/crawl.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
"github.com/ipni/go-libipni/metadata"
1414
"github.com/ipni/ipni-cli/pkg/adpub"
1515
"github.com/libp2p/go-libp2p/core/peer"
16-
"github.com/urfave/cli/v2"
16+
"github.com/urfave/cli/v3"
1717
)
1818

1919
var adsCrawlSubCmd = &cli.Command{
@@ -65,46 +65,46 @@ var adsCrawlFlags = []cli.Flag{
6565
topicFlag,
6666
}
6767

68-
func adsCrawlAction(cctx *cli.Context) error {
69-
addrInfo, err := peer.AddrInfoFromString(cctx.String("addr-info"))
68+
func adsCrawlAction(ctx context.Context, cmd *cli.Command) error {
69+
addrInfo, err := peer.AddrInfoFromString(cmd.String("addr-info"))
7070
if err != nil {
7171
return fmt.Errorf("bad pub-addr-info: %w", err)
7272
}
7373

7474
provClient, err := adpub.NewClient(*addrInfo,
7575
adpub.WithDeleteAfterRead(true),
7676
adpub.WithEntriesDepthLimit(0),
77-
adpub.WithTopicName(cctx.String("topic")),
78-
adpub.WithHttpTimeout(cctx.Duration("timeout")))
77+
adpub.WithTopicName(cmd.String("topic")),
78+
adpub.WithHttpTimeout(cmd.Duration("timeout")))
7979
if err != nil {
8080
return err
8181
}
8282

8383
var latestCid cid.Cid
84-
if cctx.String("latest") != "" {
85-
latestCid, err = cid.Decode(cctx.String("latest"))
84+
if cmd.String("latest") != "" {
85+
latestCid, err = cid.Decode(cmd.String("latest"))
8686
if err != nil {
8787
return fmt.Errorf("bad cid: %w", err)
8888
}
8989
}
9090

91-
quiet := cctx.Bool("quiet")
92-
skipEntries := cctx.Bool("skip-entries")
93-
showMetadata := cctx.Bool("show-metadata")
94-
showExtProviders := cctx.Bool("show-ext-providers")
95-
stopMhs := cctx.Int("stop-mhs")
91+
quiet := cmd.Bool("quiet")
92+
skipEntries := cmd.Bool("skip-entries")
93+
showMetadata := cmd.Bool("show-metadata")
94+
showExtProviders := cmd.Bool("show-ext-providers")
95+
stopMhs := cmd.Int("stop-mhs")
9696

9797
if skipEntries && stopMhs != 0 {
9898
return errors.New("cannot use flag --skip-entries with --stop-mhs")
9999
}
100100

101-
ctx, cancel := context.WithCancel(cctx.Context)
101+
ctx, cancel := context.WithCancel(ctx)
102102
defer cancel()
103103

104104
ads := make(chan *adpub.Advertisement, 1)
105105
errCh := make(chan error, 1)
106106
go func() {
107-
errCh <- provClient.Crawl(ctx, latestCid, cctx.Int("number"), ads)
107+
errCh <- provClient.Crawl(ctx, latestCid, cmd.Int("number"), ads)
108108
close(ads)
109109
}()
110110

@@ -187,7 +187,7 @@ func adsCrawlAction(cctx *cli.Context) error {
187187
continue
188188
}
189189

190-
err = provClient.SyncEntriesWithRetry(cctx.Context, ad.Entries.Root())
190+
err = provClient.SyncEntriesWithRetry(ctx, ad.Entries.Root())
191191
if err != nil {
192192
fmt.Fprintf(os.Stderr, "Failed to sync entries for advertisement %s: %s\n", ad.ID, err)
193193
continue

pkg/ads/dist.go

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

33
import (
4+
"context"
45
"fmt"
56

67
"github.com/ipfs/go-cid"
78
"github.com/ipni/ipni-cli/pkg/dtrack"
89
"github.com/libp2p/go-libp2p/core/peer"
9-
"github.com/urfave/cli/v2"
10+
"github.com/urfave/cli/v3"
1011
)
1112

1213
var adsDistSubCmd = &cli.Command{
@@ -42,28 +43,28 @@ var adsDistFlags = []cli.Flag{
4243
topicFlag,
4344
}
4445

45-
func adsDistAction(cctx *cli.Context) error {
46-
addrInfo, err := peer.AddrInfoFromString(cctx.String("addr-info"))
46+
func adsDistAction(ctx context.Context, cmd *cli.Command) error {
47+
addrInfo, err := peer.AddrInfoFromString(cmd.String("addr-info"))
4748
if err != nil {
4849
return fmt.Errorf("bad pub-addr-info: %w", err)
4950
}
5051

51-
startCid, err := cid.Decode(cctx.String("start"))
52+
startCid, err := cid.Decode(cmd.String("start"))
5253
if err != nil {
5354
return fmt.Errorf("bad start cid: %w", err)
5455
}
5556

5657
adDist, err := dtrack.NewAdDistance(
57-
dtrack.WithTopic(cctx.String("topic")),
58-
dtrack.WithDepthLimit(cctx.Int64("dist-limit")))
58+
dtrack.WithTopic(cmd.String("topic")),
59+
dtrack.WithDepthLimit(cmd.Int64("dist-limit")))
5960
if err != nil {
6061
return err
6162
}
6263

6364
var endStr string
6465
var endCid cid.Cid
65-
if cctx.String("end") != "" {
66-
endCid, err = cid.Decode(cctx.String("end"))
66+
if cmd.String("end") != "" {
67+
endCid, err = cid.Decode(cmd.String("end"))
6768
if err != nil {
6869
return fmt.Errorf("bad end cid: %w", err)
6970
}
@@ -72,12 +73,12 @@ func adsDistAction(cctx *cli.Context) error {
7273
endStr = "head"
7374
}
7475

75-
adCount, _, err := adDist.Get(cctx.Context, *addrInfo, startCid, endCid)
76+
adCount, _, err := adDist.Get(ctx, *addrInfo, startCid, endCid)
7677
if err != nil {
7778
return err
7879
}
7980

80-
if cctx.Bool("quiet") {
81+
if cmd.Bool("quiet") {
8182
fmt.Println(adCount)
8283
} else {
8384
fmt.Printf("Distance from %s to %s is %d\n", startCid, endStr, adCount)

pkg/ads/flags.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package ads
33
import (
44
"time"
55

6-
"github.com/urfave/cli/v2"
6+
"github.com/urfave/cli/v3"
77
)
88

99
var addrInfoFlag = &cli.StringFlag{

pkg/ads/get.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package ads
22

33
import (
44
"bufio"
5+
"context"
56
"encoding/base64"
67
"errors"
78
"fmt"
@@ -14,7 +15,7 @@ import (
1415
"github.com/ipni/ipni-cli/pkg/adpub"
1516
"github.com/libp2p/go-libp2p/core/peer"
1617
"github.com/mattn/go-isatty"
17-
"github.com/urfave/cli/v2"
18+
"github.com/urfave/cli/v3"
1819
)
1920

2021
var adsGetSubCmd = &cli.Command{
@@ -68,14 +69,14 @@ var adsGetFlags = []cli.Flag{
6869
topicFlag,
6970
}
7071

71-
func adsGetAction(cctx *cli.Context) error {
72-
addrInfo, err := peer.AddrInfoFromString(cctx.String("addr-info"))
72+
func adsGetAction(ctx context.Context, cmd *cli.Command) error {
73+
addrInfo, err := peer.AddrInfoFromString(cmd.String("addr-info"))
7374
if err != nil {
7475
return fmt.Errorf("bad pub-addr-info: %w", err)
7576
}
7677

7778
var adCids []cid.Cid
78-
cidArgs := cctx.StringSlice("cid")
79+
cidArgs := cmd.StringSlice("cid")
7980
if len(cidArgs) != 0 {
8081
seen := make(map[string]struct{}, len(cidArgs))
8182
adCids = make([]cid.Cid, 0, len(cidArgs))
@@ -91,7 +92,7 @@ func adsGetAction(cctx *cli.Context) error {
9192
adCids = append(adCids, cid)
9293
}
9394
}
94-
if cctx.Bool("head") {
95+
if cmd.Bool("head") {
9596
// Fetch latest advertisement
9697
adCids = append(adCids, cid.Undef)
9798
}
@@ -125,17 +126,17 @@ func adsGetAction(cctx *cli.Context) error {
125126
}
126127

127128
pubClient, err := adpub.NewClient(*addrInfo,
128-
adpub.WithTopicName(cctx.String("topic")),
129-
adpub.WithEntriesDepthLimit(cctx.Int64("entries-depth-limit")),
130-
adpub.WithHttpTimeout(cctx.Duration("timeout")))
129+
adpub.WithTopicName(cmd.String("topic")),
130+
adpub.WithEntriesDepthLimit(cmd.Int64("entries-depth-limit")),
131+
adpub.WithHttpTimeout(cmd.Duration("timeout")))
131132
if err != nil {
132133
return err
133134
}
134135

135136
for _, adCid := range adCids {
136137
fmt.Println()
137138

138-
ad, err := pubClient.GetAdvertisement(cctx.Context, adCid)
139+
ad, err := pubClient.GetAdvertisement(ctx, adCid)
139140
if err != nil {
140141
if ad == nil {
141142
if errors.Is(err, adpub.ErrContentNotFound) {
@@ -223,12 +224,12 @@ func adsGetAction(cctx *cli.Context) error {
223224
continue
224225
}
225226

226-
if cctx.Bool("skip-entries") {
227+
if cmd.Bool("skip-entries") {
227228
continue
228229
}
229230

230231
// Sync entries if not a removal advertisement and has entries.
231-
err = pubClient.SyncEntriesWithRetry(cctx.Context, ad.Entries.Root())
232+
err = pubClient.SyncEntriesWithRetry(ctx, ad.Entries.Root())
232233
if err != nil {
233234
fmt.Fprintf(os.Stderr, "⚠️ Failed to sync entries for advertisement %s: %s\n", ad.ID, err)
234235
continue
@@ -244,7 +245,7 @@ func adsGetAction(cctx *cli.Context) error {
244245
entriesOutput = "⚠️ Note: More entries were available but not synced due to the configured entries recursion limit or error during traversal."
245246
}
246247

247-
if cctx.Bool("print-entries") {
248+
if cmd.Bool("print-entries") {
248249
for _, mh := range entries {
249250
fmt.Printf(" %s\n", mh.B58String())
250251
}

pkg/ads/list.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
package ads
22

33
import (
4+
"context"
45
"fmt"
56
"os"
67

78
"github.com/ipfs/go-cid"
89
"github.com/ipni/ipni-cli/pkg/adpub"
910
"github.com/libp2p/go-libp2p/core/peer"
10-
"github.com/urfave/cli/v2"
11+
"github.com/urfave/cli/v3"
1112
)
1213

1314
var adsListSubCmd = &cli.Command{
@@ -38,27 +39,27 @@ var adsListFlags = []cli.Flag{
3839
topicFlag,
3940
}
4041

41-
func adsListAction(cctx *cli.Context) error {
42-
addrInfo, err := peer.AddrInfoFromString(cctx.String("addr-info"))
42+
func adsListAction(ctx context.Context, cmd *cli.Command) error {
43+
addrInfo, err := peer.AddrInfoFromString(cmd.String("addr-info"))
4344
if err != nil {
4445
return fmt.Errorf("bad pub-addr-info: %w", err)
4546
}
4647

4748
provClient, err := adpub.NewClient(*addrInfo,
4849
adpub.WithDeleteAfterRead(true),
49-
adpub.WithTopicName(cctx.String("topic")),
50-
adpub.WithHttpTimeout(cctx.Duration("timeout")))
50+
adpub.WithTopicName(cmd.String("topic")),
51+
adpub.WithHttpTimeout(cmd.Duration("timeout")))
5152
if err != nil {
5253
return err
5354
}
5455

5556
var latestCid cid.Cid
56-
if cctx.String("latest") != "" {
57-
latestCid, err = cid.Decode(cctx.String("latest"))
57+
if cmd.String("latest") != "" {
58+
latestCid, err = cid.Decode(cmd.String("latest"))
5859
if err != nil {
5960
return fmt.Errorf("bad cid: %w", err)
6061
}
6162
}
6263

63-
return provClient.List(cctx.Context, latestCid, cctx.Int("number"), os.Stdout)
64+
return provClient.List(ctx, latestCid, cmd.Int("number"), os.Stdout)
6465
}

0 commit comments

Comments
 (0)