Skip to content

Commit f6ac80c

Browse files
authored
cmd/geth, cmd/devp2p: fix some cli parsing issues (#25234)
* cmd/geth: add some missing argument count checks * internal/flags: skip cmds with no action func in MigrateGlobalFlags * internal/flags: add Merge * cmd/devp2p: re-add listener config flags in discv4 commands
1 parent 55f914a commit f6ac80c

File tree

4 files changed

+34
-5
lines changed

4 files changed

+34
-5
lines changed

cmd/devp2p/discv4cmd.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"github.com/ethereum/go-ethereum/cmd/devp2p/internal/v4test"
2626
"github.com/ethereum/go-ethereum/common"
2727
"github.com/ethereum/go-ethereum/crypto"
28+
"github.com/ethereum/go-ethereum/internal/flags"
2829
"github.com/ethereum/go-ethereum/p2p/discover"
2930
"github.com/ethereum/go-ethereum/p2p/enode"
3031
"github.com/ethereum/go-ethereum/params"
@@ -49,32 +50,34 @@ var (
4950
Usage: "Sends ping to a node",
5051
Action: discv4Ping,
5152
ArgsUsage: "<node>",
53+
Flags: v4NodeFlags,
5254
}
5355
discv4RequestRecordCommand = &cli.Command{
5456
Name: "requestenr",
5557
Usage: "Requests a node record using EIP-868 enrRequest",
5658
Action: discv4RequestRecord,
5759
ArgsUsage: "<node>",
60+
Flags: v4NodeFlags,
5861
}
5962
discv4ResolveCommand = &cli.Command{
6063
Name: "resolve",
6164
Usage: "Finds a node in the DHT",
6265
Action: discv4Resolve,
6366
ArgsUsage: "<node>",
64-
Flags: []cli.Flag{bootnodesFlag},
67+
Flags: v4NodeFlags,
6568
}
6669
discv4ResolveJSONCommand = &cli.Command{
6770
Name: "resolve-json",
6871
Usage: "Re-resolves nodes in a nodes.json file",
6972
Action: discv4ResolveJSON,
70-
Flags: []cli.Flag{bootnodesFlag},
73+
Flags: v4NodeFlags,
7174
ArgsUsage: "<nodes.json file>",
7275
}
7376
discv4CrawlCommand = &cli.Command{
7477
Name: "crawl",
7578
Usage: "Updates a nodes.json file with random nodes found in the DHT",
7679
Action: discv4Crawl,
77-
Flags: []cli.Flag{bootnodesFlag, crawlTimeoutFlag},
80+
Flags: flags.Merge(v4NodeFlags, []cli.Flag{crawlTimeoutFlag}),
7881
}
7982
discv4TestCommand = &cli.Command{
8083
Name: "test",
@@ -119,6 +122,13 @@ var (
119122
}
120123
)
121124

125+
var v4NodeFlags = []cli.Flag{
126+
bootnodesFlag,
127+
nodekeyFlag,
128+
nodedbFlag,
129+
listenAddrFlag,
130+
}
131+
122132
func discv4Ping(ctx *cli.Context) error {
123133
n := getNodeArg(ctx)
124134
disc := startV4(ctx)

cmd/geth/chaincmd.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,10 +166,12 @@ This command dumps out the state for a given block (or latest, if none provided)
166166
// initGenesis will initialise the given JSON format genesis file and writes it as
167167
// the zero'd block (i.e. genesis) or will fail hard if it can't succeed.
168168
func initGenesis(ctx *cli.Context) error {
169-
// Make sure we have a valid genesis JSON
169+
if ctx.Args().Len() != 1 {
170+
utils.Fatalf("need genesis.json file as the only argument")
171+
}
170172
genesisPath := ctx.Args().First()
171173
if len(genesisPath) == 0 {
172-
utils.Fatalf("Must supply path to genesis JSON file")
174+
utils.Fatalf("invalid path to genesis file")
173175
}
174176
file, err := os.Open(genesisPath)
175177
if err != nil {

cmd/geth/consolecmd.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,10 @@ func localConsole(ctx *cli.Context) error {
114114
// remoteConsole will connect to a remote geth instance, attaching a JavaScript
115115
// console to it.
116116
func remoteConsole(ctx *cli.Context) error {
117+
if ctx.Args().Len() > 1 {
118+
utils.Fatalf("invalid command-line: too many arguments")
119+
}
120+
117121
endpoint := ctx.Args().First()
118122
if endpoint == "" {
119123
cfg := defaultNodeConfig()

internal/flags/helpers.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,15 @@ func NewApp(gitCommit, gitDate, usage string) *cli.App {
3838
return app
3939
}
4040

41+
// Merge merges the given flag slices.
42+
func Merge(groups ...[]cli.Flag) []cli.Flag {
43+
var ret []cli.Flag
44+
for _, group := range groups {
45+
ret = append(ret, group...)
46+
}
47+
return ret
48+
}
49+
4150
var migrationApplied = map[*cli.Command]struct{}{}
4251

4352
// MigrateGlobalFlags makes all global flag values available in the
@@ -70,6 +79,10 @@ func MigrateGlobalFlags(ctx *cli.Context) {
7079

7180
// This iterates over all commands and wraps their action function.
7281
iterate(ctx.App.Commands, func(cmd *cli.Command) {
82+
if cmd.Action == nil {
83+
return
84+
}
85+
7386
action := cmd.Action
7487
cmd.Action = func(ctx *cli.Context) error {
7588
doMigrateFlags(ctx)

0 commit comments

Comments
 (0)