Skip to content

Commit 52ed357

Browse files
cmd: migrate to urfave/cli/v2 (#24751)
This change updates our urfave/cli dependency to the v2 branch of the library. There are some Go API changes in cli v2: - Flag values can now be accessed using the methods ctx.Bool, ctx.Int, ctx.String, ... regardless of whether the flag is 'local' or 'global'. - v2 has built-in support for flag categories. Our home-grown category system is removed and the categories of flags are assigned as part of the flag definition. For users, there is only one observable difference with cli v2: flags must now strictly appear before regular arguments. For example, the following command is now invalid: geth account import mykey.json --password file.txt Instead, the command must be invoked as follows: geth account import --password file.txt mykey.json
1 parent 119f955 commit 52ed357

Some content is hidden

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

61 files changed

+2478
-2445
lines changed

build/ci.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -132,12 +132,12 @@ var (
132132
// Note: the following Ubuntu releases have been officially deprecated on Launchpad:
133133
// wily, yakkety, zesty, artful, cosmic, disco, eoan, groovy, hirsuite
134134
debDistroGoBoots = map[string]string{
135-
"trusty": "golang-1.11", // EOL: 04/2024
136-
"xenial": "golang-go", // EOL: 04/2026
137-
"bionic": "golang-go", // EOL: 04/2028
138-
"focal": "golang-go", // EOL: 04/2030
139-
"impish": "golang-go", // EOL: 07/2022
140-
"jammy": "golang-go", // EOL: 04/2032
135+
"trusty": "golang-1.11", // EOL: 04/2024
136+
"xenial": "golang-go", // EOL: 04/2026
137+
"bionic": "golang-go", // EOL: 04/2028
138+
"focal": "golang-go", // EOL: 04/2030
139+
"impish": "golang-go", // EOL: 07/2022
140+
"jammy": "golang-go", // EOL: 04/2032
141141
//"kinetic": "golang-go", // EOL: 07/2023
142142
}
143143

@@ -224,6 +224,9 @@ func doInstall(cmdline []string) {
224224
gobuild.Args = append(gobuild.Args, "-p", "1")
225225
}
226226

227+
// Disable CLI markdown doc generation in release builds.
228+
gobuild.Args = append(gobuild.Args, "-tags", "urfave_cli_no_docs")
229+
227230
// We use -trimpath to avoid leaking local paths into the built executables.
228231
gobuild.Args = append(gobuild.Args, "-trimpath")
229232

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#! /bin/bash
2+
3+
: ${PROG:=$(basename ${BASH_SOURCE})}
4+
5+
_cli_bash_autocomplete() {
6+
if [[ "${COMP_WORDS[0]}" != "source" ]]; then
7+
local cur opts base
8+
COMPREPLY=()
9+
cur="${COMP_WORDS[COMP_CWORD]}"
10+
if [[ "$cur" == "-"* ]]; then
11+
opts=$( ${COMP_WORDS[@]:0:$COMP_CWORD} ${cur} --generate-bash-completion )
12+
else
13+
opts=$( ${COMP_WORDS[@]:0:$COMP_CWORD} --generate-bash-completion )
14+
fi
15+
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
16+
return 0
17+
fi
18+
}
19+
20+
complete -o bashdefault -o default -o nospace -F _cli_bash_autocomplete $PROG
21+
unset PROG
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#compdef $PROG
2+
3+
_cli_zsh_autocomplete() {
4+
local -a opts
5+
local cur
6+
cur=${words[-1]}
7+
if [[ "$cur" == "-"* ]]; then
8+
opts=("${(@f)$(${words[@]:0:#words[@]-1} ${cur} --generate-bash-completion)}")
9+
else
10+
opts=("${(@f)$(${words[@]:0:#words[@]-1} --generate-bash-completion)}")
11+
fi
12+
13+
if [[ "${opts[1]}" != "" ]]; then
14+
_describe 'values' opts
15+
else
16+
_files
17+
fi
18+
}
19+
20+
compdef _cli_zsh_autocomplete $PROG

cmd/abigen/main.go

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import (
3030
"github.com/ethereum/go-ethereum/crypto"
3131
"github.com/ethereum/go-ethereum/internal/flags"
3232
"github.com/ethereum/go-ethereum/log"
33-
"gopkg.in/urfave/cli.v1"
33+
"github.com/urfave/cli/v2"
3434
)
3535

3636
var (
@@ -39,49 +39,52 @@ var (
3939
gitDate = ""
4040

4141
app *cli.App
42+
)
4243

44+
var (
4345
// Flags needed by abigen
44-
abiFlag = cli.StringFlag{
46+
abiFlag = &cli.StringFlag{
4547
Name: "abi",
4648
Usage: "Path to the Ethereum contract ABI json to bind, - for STDIN",
4749
}
48-
binFlag = cli.StringFlag{
50+
binFlag = &cli.StringFlag{
4951
Name: "bin",
5052
Usage: "Path to the Ethereum contract bytecode (generate deploy method)",
5153
}
52-
typeFlag = cli.StringFlag{
54+
typeFlag = &cli.StringFlag{
5355
Name: "type",
5456
Usage: "Struct name for the binding (default = package name)",
5557
}
56-
jsonFlag = cli.StringFlag{
58+
jsonFlag = &cli.StringFlag{
5759
Name: "combined-json",
5860
Usage: "Path to the combined-json file generated by compiler, - for STDIN",
5961
}
60-
excFlag = cli.StringFlag{
62+
excFlag = &cli.StringFlag{
6163
Name: "exc",
6264
Usage: "Comma separated types to exclude from binding",
6365
}
64-
pkgFlag = cli.StringFlag{
66+
pkgFlag = &cli.StringFlag{
6567
Name: "pkg",
6668
Usage: "Package name to generate the binding into",
6769
}
68-
outFlag = cli.StringFlag{
70+
outFlag = &cli.StringFlag{
6971
Name: "out",
7072
Usage: "Output file for the generated binding (default = stdout)",
7173
}
72-
langFlag = cli.StringFlag{
74+
langFlag = &cli.StringFlag{
7375
Name: "lang",
7476
Usage: "Destination language for the bindings (go, java, objc)",
7577
Value: "go",
7678
}
77-
aliasFlag = cli.StringFlag{
79+
aliasFlag = &cli.StringFlag{
7880
Name: "alias",
7981
Usage: "Comma separated aliases for function and event renaming, e.g. original1=alias1, original2=alias2",
8082
}
8183
)
8284

8385
func init() {
8486
app = flags.NewApp(gitCommit, gitDate, "ethereum checkpoint helper tool")
87+
app.Name = "abigen"
8588
app.Flags = []cli.Flag{
8689
abiFlag,
8790
binFlag,
@@ -93,17 +96,17 @@ func init() {
9396
langFlag,
9497
aliasFlag,
9598
}
96-
app.Action = utils.MigrateFlags(abigen)
97-
cli.CommandHelpTemplate = flags.OriginCommandHelpTemplate
99+
app.Action = abigen
98100
}
99101

100102
func abigen(c *cli.Context) error {
101103
utils.CheckExclusive(c, abiFlag, jsonFlag) // Only one source can be selected.
102-
if c.GlobalString(pkgFlag.Name) == "" {
104+
105+
if c.String(pkgFlag.Name) == "" {
103106
utils.Fatalf("No destination package specified (--pkg)")
104107
}
105108
var lang bind.Lang
106-
switch c.GlobalString(langFlag.Name) {
109+
switch c.String(langFlag.Name) {
107110
case "go":
108111
lang = bind.LangGo
109112
case "java":
@@ -112,7 +115,7 @@ func abigen(c *cli.Context) error {
112115
lang = bind.LangObjC
113116
utils.Fatalf("Objc binding generation is uncompleted")
114117
default:
115-
utils.Fatalf("Unsupported destination language \"%s\" (--lang)", c.GlobalString(langFlag.Name))
118+
utils.Fatalf("Unsupported destination language \"%s\" (--lang)", c.String(langFlag.Name))
116119
}
117120
// If the entire solidity code was specified, build and bind based on that
118121
var (
@@ -123,13 +126,13 @@ func abigen(c *cli.Context) error {
123126
libs = make(map[string]string)
124127
aliases = make(map[string]string)
125128
)
126-
if c.GlobalString(abiFlag.Name) != "" {
129+
if c.String(abiFlag.Name) != "" {
127130
// Load up the ABI, optional bytecode and type name from the parameters
128131
var (
129132
abi []byte
130133
err error
131134
)
132-
input := c.GlobalString(abiFlag.Name)
135+
input := c.String(abiFlag.Name)
133136
if input == "-" {
134137
abi, err = io.ReadAll(os.Stdin)
135138
} else {
@@ -141,7 +144,7 @@ func abigen(c *cli.Context) error {
141144
abis = append(abis, string(abi))
142145

143146
var bin []byte
144-
if binFile := c.GlobalString(binFlag.Name); binFile != "" {
147+
if binFile := c.String(binFlag.Name); binFile != "" {
145148
if bin, err = os.ReadFile(binFile); err != nil {
146149
utils.Fatalf("Failed to read input bytecode: %v", err)
147150
}
@@ -151,22 +154,22 @@ func abigen(c *cli.Context) error {
151154
}
152155
bins = append(bins, string(bin))
153156

154-
kind := c.GlobalString(typeFlag.Name)
157+
kind := c.String(typeFlag.Name)
155158
if kind == "" {
156-
kind = c.GlobalString(pkgFlag.Name)
159+
kind = c.String(pkgFlag.Name)
157160
}
158161
types = append(types, kind)
159162
} else {
160163
// Generate the list of types to exclude from binding
161164
exclude := make(map[string]bool)
162-
for _, kind := range strings.Split(c.GlobalString(excFlag.Name), ",") {
165+
for _, kind := range strings.Split(c.String(excFlag.Name), ",") {
163166
exclude[strings.ToLower(kind)] = true
164167
}
165168
var contracts map[string]*compiler.Contract
166169

167-
if c.GlobalIsSet(jsonFlag.Name) {
170+
if c.IsSet(jsonFlag.Name) {
168171
var (
169-
input = c.GlobalString(jsonFlag.Name)
172+
input = c.String(jsonFlag.Name)
170173
jsonOutput []byte
171174
err error
172175
)
@@ -207,28 +210,28 @@ func abigen(c *cli.Context) error {
207210
}
208211
}
209212
// Extract all aliases from the flags
210-
if c.GlobalIsSet(aliasFlag.Name) {
213+
if c.IsSet(aliasFlag.Name) {
211214
// We support multi-versions for aliasing
212215
// e.g.
213216
// foo=bar,foo2=bar2
214217
// foo:bar,foo2:bar2
215218
re := regexp.MustCompile(`(?:(\w+)[:=](\w+))`)
216-
submatches := re.FindAllStringSubmatch(c.GlobalString(aliasFlag.Name), -1)
219+
submatches := re.FindAllStringSubmatch(c.String(aliasFlag.Name), -1)
217220
for _, match := range submatches {
218221
aliases[match[1]] = match[2]
219222
}
220223
}
221224
// Generate the contract binding
222-
code, err := bind.Bind(types, abis, bins, sigs, c.GlobalString(pkgFlag.Name), lang, libs, aliases)
225+
code, err := bind.Bind(types, abis, bins, sigs, c.String(pkgFlag.Name), lang, libs, aliases)
223226
if err != nil {
224227
utils.Fatalf("Failed to generate ABI binding: %v", err)
225228
}
226229
// Either flush it out to a file or display on the standard output
227-
if !c.GlobalIsSet(outFlag.Name) {
230+
if !c.IsSet(outFlag.Name) {
228231
fmt.Printf("%s\n", code)
229232
return nil
230233
}
231-
if err := os.WriteFile(c.GlobalString(outFlag.Name), []byte(code), 0600); err != nil {
234+
if err := os.WriteFile(c.String(outFlag.Name), []byte(code), 0600); err != nil {
232235
utils.Fatalf("Failed to write ABI binding: %v", err)
233236
}
234237
return nil

cmd/checkpoint-admin/common.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@ import (
2828
"github.com/ethereum/go-ethereum/ethclient"
2929
"github.com/ethereum/go-ethereum/params"
3030
"github.com/ethereum/go-ethereum/rpc"
31-
"gopkg.in/urfave/cli.v1"
31+
"github.com/urfave/cli/v2"
3232
)
3333

3434
// newClient creates a client with specified remote URL.
3535
func newClient(ctx *cli.Context) *ethclient.Client {
36-
client, err := ethclient.Dial(ctx.GlobalString(nodeURLFlag.Name))
36+
client, err := ethclient.Dial(ctx.String(nodeURLFlag.Name))
3737
if err != nil {
3838
utils.Fatalf("Failed to connect to Ethereum node: %v", err)
3939
}
@@ -64,9 +64,9 @@ func getContractAddr(client *rpc.Client) common.Address {
6464
func getCheckpoint(ctx *cli.Context, client *rpc.Client) *params.TrustedCheckpoint {
6565
var checkpoint *params.TrustedCheckpoint
6666

67-
if ctx.GlobalIsSet(indexFlag.Name) {
67+
if ctx.IsSet(indexFlag.Name) {
6868
var result [3]string
69-
index := uint64(ctx.GlobalInt64(indexFlag.Name))
69+
index := uint64(ctx.Int64(indexFlag.Name))
7070
if err := client.Call(&result, "les_getCheckpoint", index); err != nil {
7171
utils.Fatalf("Failed to get local checkpoint %v, please ensure the les API is exposed", err)
7272
}

cmd/checkpoint-admin/exec.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ import (
3636
"github.com/ethereum/go-ethereum/log"
3737
"github.com/ethereum/go-ethereum/params"
3838
"github.com/ethereum/go-ethereum/rpc"
39-
"gopkg.in/urfave/cli.v1"
39+
"github.com/urfave/cli/v2"
4040
)
4141

42-
var commandDeploy = cli.Command{
42+
var commandDeploy = &cli.Command{
4343
Name: "deploy",
4444
Usage: "Deploy a new checkpoint oracle contract",
4545
Flags: []cli.Flag{
@@ -49,10 +49,10 @@ var commandDeploy = cli.Command{
4949
signersFlag,
5050
thresholdFlag,
5151
},
52-
Action: utils.MigrateFlags(deploy),
52+
Action: deploy,
5353
}
5454

55-
var commandSign = cli.Command{
55+
var commandSign = &cli.Command{
5656
Name: "sign",
5757
Usage: "Sign the checkpoint with the specified key",
5858
Flags: []cli.Flag{
@@ -63,10 +63,10 @@ var commandSign = cli.Command{
6363
hashFlag,
6464
oracleFlag,
6565
},
66-
Action: utils.MigrateFlags(sign),
66+
Action: sign,
6767
}
6868

69-
var commandPublish = cli.Command{
69+
var commandPublish = &cli.Command{
7070
Name: "publish",
7171
Usage: "Publish a checkpoint into the oracle",
7272
Flags: []cli.Flag{
@@ -76,7 +76,7 @@ var commandPublish = cli.Command{
7676
indexFlag,
7777
signaturesFlag,
7878
},
79-
Action: utils.MigrateFlags(publish),
79+
Action: publish,
8080
}
8181

8282
// deploy deploys the checkpoint registrar contract.
@@ -132,7 +132,7 @@ func sign(ctx *cli.Context) error {
132132
node *rpc.Client
133133
oracle *checkpointoracle.CheckpointOracle
134134
)
135-
if !ctx.GlobalIsSet(nodeURLFlag.Name) {
135+
if !ctx.IsSet(nodeURLFlag.Name) {
136136
// Offline mode signing
137137
offline = true
138138
if !ctx.IsSet(hashFlag.Name) {
@@ -151,7 +151,7 @@ func sign(ctx *cli.Context) error {
151151
address = common.HexToAddress(ctx.String(oracleFlag.Name))
152152
} else {
153153
// Interactive mode signing, retrieve the data from the remote node
154-
node = newRPCClient(ctx.GlobalString(nodeURLFlag.Name))
154+
node = newRPCClient(ctx.String(nodeURLFlag.Name))
155155

156156
checkpoint := getCheckpoint(ctx, node)
157157
chash, cindex, address = checkpoint.Hash(), checkpoint.SectionIndex, getContractAddr(node)
@@ -265,7 +265,7 @@ func publish(ctx *cli.Context) error {
265265
}
266266
// Retrieve the checkpoint we want to sign to sort the signatures
267267
var (
268-
client = newRPCClient(ctx.GlobalString(nodeURLFlag.Name))
268+
client = newRPCClient(ctx.String(nodeURLFlag.Name))
269269
addr, oracle = newContract(client)
270270
checkpoint = getCheckpoint(ctx, client)
271271
sighash = sighash(checkpoint.SectionIndex, addr, checkpoint.Hash())

0 commit comments

Comments
 (0)