Skip to content

Commit

Permalink
cmd/*: refactor get flag value (#24761)
Browse files Browse the repository at this point in the history
  • Loading branch information
s7v7nislands authored Apr 26, 2022
1 parent 0914234 commit 195c2d3
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 59 deletions.
6 changes: 4 additions & 2 deletions cmd/devp2p/enrcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,20 @@ import (
"gopkg.in/urfave/cli.v1"
)

var fileFlag = cli.StringFlag{Name: "file"}

var enrdumpCommand = cli.Command{
Name: "enrdump",
Usage: "Pretty-prints node records",
Action: enrdump,
Flags: []cli.Flag{
cli.StringFlag{Name: "file"},
fileFlag,
},
}

func enrdump(ctx *cli.Context) error {
var source string
if file := ctx.String("file"); file != "" {
if file := ctx.String(fileFlag.Name); file != "" {
if ctx.NArg() != 0 {
return fmt.Errorf("can't dump record from command-line argument in -file mode")
}
Expand Down
25 changes: 15 additions & 10 deletions cmd/ethkey/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,17 @@ type outputGenerate struct {
AddressEIP55 string
}

var (
privateKeyFlag = cli.StringFlag{
Name: "privatekey",
Usage: "file containing a raw private key to encrypt",
}
lightKDFFlag = cli.BoolFlag{
Name: "lightkdf",
Usage: "use less secure scrypt parameters",
}
)

var commandGenerate = cli.Command{
Name: "generate",
Usage: "generate new keyfile",
Expand All @@ -48,14 +59,8 @@ If you want to encrypt an existing private key, it can be specified by setting
Flags: []cli.Flag{
passphraseFlag,
jsonFlag,
cli.StringFlag{
Name: "privatekey",
Usage: "file containing a raw private key to encrypt",
},
cli.BoolFlag{
Name: "lightkdf",
Usage: "use less secure scrypt parameters",
},
privateKeyFlag,
lightKDFFlag,
},
Action: func(ctx *cli.Context) error {
// Check if keyfile path given and make sure it doesn't already exist.
Expand All @@ -71,7 +76,7 @@ If you want to encrypt an existing private key, it can be specified by setting

var privateKey *ecdsa.PrivateKey
var err error
if file := ctx.String("privatekey"); file != "" {
if file := ctx.String(privateKeyFlag.Name); file != "" {
// Load private key from file.
privateKey, err = crypto.LoadECDSA(file)
if err != nil {
Expand Down Expand Up @@ -99,7 +104,7 @@ If you want to encrypt an existing private key, it can be specified by setting
// Encrypt key with passphrase.
passphrase := getPassphrase(ctx, true)
scryptN, scryptP := keystore.StandardScryptN, keystore.StandardScryptP
if ctx.Bool("lightkdf") {
if ctx.Bool(lightKDFFlag.Name) {
scryptN, scryptP = keystore.LightScryptN, keystore.LightScryptP
}
keyjson, err := keystore.EncryptKey(key, passphrase, scryptN, scryptP)
Expand Down
14 changes: 9 additions & 5 deletions cmd/ethkey/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ type outputInspect struct {
PrivateKey string
}

var (
privateFlag = cli.BoolFlag{
Name: "private",
Usage: "include the private key in the output",
}
)

var commandInspect = cli.Command{
Name: "inspect",
Usage: "inspect a keyfile",
Expand All @@ -45,10 +52,7 @@ make sure to use this feature with great caution!`,
Flags: []cli.Flag{
passphraseFlag,
jsonFlag,
cli.BoolFlag{
Name: "private",
Usage: "include the private key in the output",
},
privateFlag,
},
Action: func(ctx *cli.Context) error {
keyfilepath := ctx.Args().First()
Expand All @@ -67,7 +71,7 @@ make sure to use this feature with great caution!`,
}

// Output all relevant information we can retrieve.
showPrivate := ctx.Bool("private")
showPrivate := ctx.Bool(privateFlag.Name)
out := outputInspect{
Address: key.Address.Hex(),
PublicKey: hex.EncodeToString(
Expand Down
2 changes: 1 addition & 1 deletion cmd/ethkey/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ It is possible to refer to a file containing the message.`,
}

func getMessage(ctx *cli.Context, msgarg int) []byte {
if file := ctx.String("msgfile"); file != "" {
if file := ctx.String(msgfileFlag.Name); file != "" {
if len(ctx.Args()) > msgarg {
utils.Fatalf("Can't use --msgfile and message argument at the same time.")
}
Expand Down
99 changes: 58 additions & 41 deletions cmd/p2psim/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,58 @@ import (

var client *simulations.Client

var (
// global command flags
apiFlag = cli.StringFlag{
Name: "api",
Value: "http://localhost:8888",
Usage: "simulation API URL",
EnvVar: "P2PSIM_API_URL",
}

// events subcommand flags
currentFlag = cli.BoolFlag{
Name: "current",
Usage: "get existing nodes and conns first",
}
filterFlag = cli.StringFlag{
Name: "filter",
Value: "",
Usage: "message filter",
}

// node create subcommand flags
nameFlag = cli.StringFlag{
Name: "name",
Value: "",
Usage: "node name",
}
servicesFlag = cli.StringFlag{
Name: "services",
Value: "",
Usage: "node services (comma separated)",
}
keyFlag = cli.StringFlag{
Name: "key",
Value: "",
Usage: "node private key (hex encoded)",
}

// node rpc subcommand flags
subscribeFlag = cli.BoolFlag{
Name: "subscribe",
Usage: "method is a subscription",
}
)

func main() {
app := cli.NewApp()
app.Usage = "devp2p simulation command-line client"
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "api",
Value: "http://localhost:8888",
Usage: "simulation API URL",
EnvVar: "P2PSIM_API_URL",
},
apiFlag,
}
app.Before = func(ctx *cli.Context) error {
client = simulations.NewClient(ctx.GlobalString("api"))
client = simulations.NewClient(ctx.GlobalString(apiFlag.Name))
return nil
}
app.Commands = []cli.Command{
Expand All @@ -82,15 +121,8 @@ func main() {
Usage: "stream network events",
Action: streamNetwork,
Flags: []cli.Flag{
cli.BoolFlag{
Name: "current",
Usage: "get existing nodes and conns first",
},
cli.StringFlag{
Name: "filter",
Value: "",
Usage: "message filter",
},
currentFlag,
filterFlag,
},
},
{
Expand Down Expand Up @@ -118,21 +150,9 @@ func main() {
Usage: "create a node",
Action: createNode,
Flags: []cli.Flag{
cli.StringFlag{
Name: "name",
Value: "",
Usage: "node name",
},
cli.StringFlag{
Name: "services",
Value: "",
Usage: "node services (comma separated)",
},
cli.StringFlag{
Name: "key",
Value: "",
Usage: "node private key (hex encoded)",
},
nameFlag,
servicesFlag,
keyFlag,
},
},
{
Expand Down Expand Up @@ -171,10 +191,7 @@ func main() {
Usage: "call a node RPC method",
Action: rpcNode,
Flags: []cli.Flag{
cli.BoolFlag{
Name: "subscribe",
Usage: "method is a subscription",
},
subscribeFlag,
},
},
},
Expand Down Expand Up @@ -207,8 +224,8 @@ func streamNetwork(ctx *cli.Context) error {
}
events := make(chan *simulations.Event)
sub, err := client.SubscribeNetwork(events, simulations.SubscribeOpts{
Current: ctx.Bool("current"),
Filter: ctx.String("filter"),
Current: ctx.Bool(currentFlag.Name),
Filter: ctx.String(filterFlag.Name),
})
if err != nil {
return err
Expand Down Expand Up @@ -279,16 +296,16 @@ func createNode(ctx *cli.Context) error {
return cli.ShowCommandHelp(ctx, ctx.Command.Name)
}
config := adapters.RandomNodeConfig()
config.Name = ctx.String("name")
if key := ctx.String("key"); key != "" {
config.Name = ctx.String(nameFlag.Name)
if key := ctx.String(keyFlag.Name); key != "" {
privKey, err := crypto.HexToECDSA(key)
if err != nil {
return err
}
config.ID = enode.PubkeyToIDV4(&privKey.PublicKey)
config.PrivateKey = privKey
}
if services := ctx.String("services"); services != "" {
if services := ctx.String(servicesFlag.Name); services != "" {
config.Lifecycles = strings.Split(services, ",")
}
node, err := client.CreateNode(config)
Expand Down Expand Up @@ -389,7 +406,7 @@ func rpcNode(ctx *cli.Context) error {
if err != nil {
return err
}
if ctx.Bool("subscribe") {
if ctx.Bool(subscribeFlag.Name) {
return rpcSubscribe(rpcClient, ctx.App.Writer, method, args[3:]...)
}
var result interface{}
Expand Down

0 comments on commit 195c2d3

Please sign in to comment.