Skip to content

Commit 567fd5c

Browse files
s7v7nislandsjagdeep sidhu
authored andcommitted
cmd/*: refactor get flag value (ethereum#24761)
1 parent 09c6613 commit 567fd5c

File tree

5 files changed

+87
-59
lines changed

5 files changed

+87
-59
lines changed

cmd/devp2p/enrcmd.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,20 @@ import (
3434
"gopkg.in/urfave/cli.v1"
3535
)
3636

37+
var fileFlag = cli.StringFlag{Name: "file"}
38+
3739
var enrdumpCommand = cli.Command{
3840
Name: "enrdump",
3941
Usage: "Pretty-prints node records",
4042
Action: enrdump,
4143
Flags: []cli.Flag{
42-
cli.StringFlag{Name: "file"},
44+
fileFlag,
4345
},
4446
}
4547

4648
func enrdump(ctx *cli.Context) error {
4749
var source string
48-
if file := ctx.String("file"); file != "" {
50+
if file := ctx.String(fileFlag.Name); file != "" {
4951
if ctx.NArg() != 0 {
5052
return fmt.Errorf("can't dump record from command-line argument in -file mode")
5153
}

cmd/ethkey/generate.go

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,17 @@ type outputGenerate struct {
3535
AddressEIP55 string
3636
}
3737

38+
var (
39+
privateKeyFlag = cli.StringFlag{
40+
Name: "privatekey",
41+
Usage: "file containing a raw private key to encrypt",
42+
}
43+
lightKDFFlag = cli.BoolFlag{
44+
Name: "lightkdf",
45+
Usage: "use less secure scrypt parameters",
46+
}
47+
)
48+
3849
var commandGenerate = cli.Command{
3950
Name: "generate",
4051
Usage: "generate new keyfile",
@@ -48,14 +59,8 @@ If you want to encrypt an existing private key, it can be specified by setting
4859
Flags: []cli.Flag{
4960
passphraseFlag,
5061
jsonFlag,
51-
cli.StringFlag{
52-
Name: "privatekey",
53-
Usage: "file containing a raw private key to encrypt",
54-
},
55-
cli.BoolFlag{
56-
Name: "lightkdf",
57-
Usage: "use less secure scrypt parameters",
58-
},
62+
privateKeyFlag,
63+
lightKDFFlag,
5964
},
6065
Action: func(ctx *cli.Context) error {
6166
// Check if keyfile path given and make sure it doesn't already exist.
@@ -71,7 +76,7 @@ If you want to encrypt an existing private key, it can be specified by setting
7176

7277
var privateKey *ecdsa.PrivateKey
7378
var err error
74-
if file := ctx.String("privatekey"); file != "" {
79+
if file := ctx.String(privateKeyFlag.Name); file != "" {
7580
// Load private key from file.
7681
privateKey, err = crypto.LoadECDSA(file)
7782
if err != nil {
@@ -99,7 +104,7 @@ If you want to encrypt an existing private key, it can be specified by setting
99104
// Encrypt key with passphrase.
100105
passphrase := getPassphrase(ctx, true)
101106
scryptN, scryptP := keystore.StandardScryptN, keystore.StandardScryptP
102-
if ctx.Bool("lightkdf") {
107+
if ctx.Bool(lightKDFFlag.Name) {
103108
scryptN, scryptP = keystore.LightScryptN, keystore.LightScryptP
104109
}
105110
keyjson, err := keystore.EncryptKey(key, passphrase, scryptN, scryptP)

cmd/ethkey/inspect.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@ type outputInspect struct {
3333
PrivateKey string
3434
}
3535

36+
var (
37+
privateFlag = cli.BoolFlag{
38+
Name: "private",
39+
Usage: "include the private key in the output",
40+
}
41+
)
42+
3643
var commandInspect = cli.Command{
3744
Name: "inspect",
3845
Usage: "inspect a keyfile",
@@ -45,10 +52,7 @@ make sure to use this feature with great caution!`,
4552
Flags: []cli.Flag{
4653
passphraseFlag,
4754
jsonFlag,
48-
cli.BoolFlag{
49-
Name: "private",
50-
Usage: "include the private key in the output",
51-
},
55+
privateFlag,
5256
},
5357
Action: func(ctx *cli.Context) error {
5458
keyfilepath := ctx.Args().First()
@@ -67,7 +71,7 @@ make sure to use this feature with great caution!`,
6771
}
6872

6973
// Output all relevant information we can retrieve.
70-
showPrivate := ctx.Bool("private")
74+
showPrivate := ctx.Bool(privateFlag.Name)
7175
out := outputInspect{
7276
Address: key.Address.Hex(),
7377
PublicKey: hex.EncodeToString(

cmd/ethkey/message.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ It is possible to refer to a file containing the message.`,
142142
}
143143

144144
func getMessage(ctx *cli.Context, msgarg int) []byte {
145-
if file := ctx.String("msgfile"); file != "" {
145+
if file := ctx.String(msgfileFlag.Name); file != "" {
146146
if len(ctx.Args()) > msgarg {
147147
utils.Fatalf("Can't use --msgfile and message argument at the same time.")
148148
}

cmd/p2psim/main.go

Lines changed: 58 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -56,19 +56,58 @@ import (
5656

5757
var client *simulations.Client
5858

59+
var (
60+
// global command flags
61+
apiFlag = cli.StringFlag{
62+
Name: "api",
63+
Value: "http://localhost:8888",
64+
Usage: "simulation API URL",
65+
EnvVar: "P2PSIM_API_URL",
66+
}
67+
68+
// events subcommand flags
69+
currentFlag = cli.BoolFlag{
70+
Name: "current",
71+
Usage: "get existing nodes and conns first",
72+
}
73+
filterFlag = cli.StringFlag{
74+
Name: "filter",
75+
Value: "",
76+
Usage: "message filter",
77+
}
78+
79+
// node create subcommand flags
80+
nameFlag = cli.StringFlag{
81+
Name: "name",
82+
Value: "",
83+
Usage: "node name",
84+
}
85+
servicesFlag = cli.StringFlag{
86+
Name: "services",
87+
Value: "",
88+
Usage: "node services (comma separated)",
89+
}
90+
keyFlag = cli.StringFlag{
91+
Name: "key",
92+
Value: "",
93+
Usage: "node private key (hex encoded)",
94+
}
95+
96+
// node rpc subcommand flags
97+
subscribeFlag = cli.BoolFlag{
98+
Name: "subscribe",
99+
Usage: "method is a subscription",
100+
}
101+
)
102+
59103
func main() {
60104
app := cli.NewApp()
61105
app.Usage = "devp2p simulation command-line client"
62106
app.Flags = []cli.Flag{
63-
cli.StringFlag{
64-
Name: "api",
65-
Value: "http://localhost:8888",
66-
Usage: "simulation API URL",
67-
EnvVar: "P2PSIM_API_URL",
68-
},
107+
apiFlag,
69108
}
70109
app.Before = func(ctx *cli.Context) error {
71-
client = simulations.NewClient(ctx.GlobalString("api"))
110+
client = simulations.NewClient(ctx.GlobalString(apiFlag.Name))
72111
return nil
73112
}
74113
app.Commands = []cli.Command{
@@ -82,15 +121,8 @@ func main() {
82121
Usage: "stream network events",
83122
Action: streamNetwork,
84123
Flags: []cli.Flag{
85-
cli.BoolFlag{
86-
Name: "current",
87-
Usage: "get existing nodes and conns first",
88-
},
89-
cli.StringFlag{
90-
Name: "filter",
91-
Value: "",
92-
Usage: "message filter",
93-
},
124+
currentFlag,
125+
filterFlag,
94126
},
95127
},
96128
{
@@ -118,21 +150,9 @@ func main() {
118150
Usage: "create a node",
119151
Action: createNode,
120152
Flags: []cli.Flag{
121-
cli.StringFlag{
122-
Name: "name",
123-
Value: "",
124-
Usage: "node name",
125-
},
126-
cli.StringFlag{
127-
Name: "services",
128-
Value: "",
129-
Usage: "node services (comma separated)",
130-
},
131-
cli.StringFlag{
132-
Name: "key",
133-
Value: "",
134-
Usage: "node private key (hex encoded)",
135-
},
153+
nameFlag,
154+
servicesFlag,
155+
keyFlag,
136156
},
137157
},
138158
{
@@ -171,10 +191,7 @@ func main() {
171191
Usage: "call a node RPC method",
172192
Action: rpcNode,
173193
Flags: []cli.Flag{
174-
cli.BoolFlag{
175-
Name: "subscribe",
176-
Usage: "method is a subscription",
177-
},
194+
subscribeFlag,
178195
},
179196
},
180197
},
@@ -207,8 +224,8 @@ func streamNetwork(ctx *cli.Context) error {
207224
}
208225
events := make(chan *simulations.Event)
209226
sub, err := client.SubscribeNetwork(events, simulations.SubscribeOpts{
210-
Current: ctx.Bool("current"),
211-
Filter: ctx.String("filter"),
227+
Current: ctx.Bool(currentFlag.Name),
228+
Filter: ctx.String(filterFlag.Name),
212229
})
213230
if err != nil {
214231
return err
@@ -279,16 +296,16 @@ func createNode(ctx *cli.Context) error {
279296
return cli.ShowCommandHelp(ctx, ctx.Command.Name)
280297
}
281298
config := adapters.RandomNodeConfig()
282-
config.Name = ctx.String("name")
283-
if key := ctx.String("key"); key != "" {
299+
config.Name = ctx.String(nameFlag.Name)
300+
if key := ctx.String(keyFlag.Name); key != "" {
284301
privKey, err := crypto.HexToECDSA(key)
285302
if err != nil {
286303
return err
287304
}
288305
config.ID = enode.PubkeyToIDV4(&privKey.PublicKey)
289306
config.PrivateKey = privKey
290307
}
291-
if services := ctx.String("services"); services != "" {
308+
if services := ctx.String(servicesFlag.Name); services != "" {
292309
config.Lifecycles = strings.Split(services, ",")
293310
}
294311
node, err := client.CreateNode(config)
@@ -389,7 +406,7 @@ func rpcNode(ctx *cli.Context) error {
389406
if err != nil {
390407
return err
391408
}
392-
if ctx.Bool("subscribe") {
409+
if ctx.Bool(subscribeFlag.Name) {
393410
return rpcSubscribe(rpcClient, ctx.App.Writer, method, args[3:]...)
394411
}
395412
var result interface{}

0 commit comments

Comments
 (0)