Skip to content

Commit

Permalink
Fix config file flags in subcommands (#7475)
Browse files Browse the repository at this point in the history
  • Loading branch information
bidlocode committed Oct 11, 2020
1 parent b5a913d commit 0214553
Show file tree
Hide file tree
Showing 11 changed files with 115 additions and 40 deletions.
2 changes: 0 additions & 2 deletions beacon-chain/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ go_library(
"@com_github_joonix_log//:go_default_library",
"@com_github_sirupsen_logrus//:go_default_library",
"@com_github_urfave_cli_v2//:go_default_library",
"@com_github_urfave_cli_v2//altsrc:go_default_library",
"@com_github_x_cray_logrus_prefixed_formatter//:go_default_library",
],
)
Expand Down Expand Up @@ -63,7 +62,6 @@ go_image(
"@com_github_joonix_log//:go_default_library",
"@com_github_sirupsen_logrus//:go_default_library",
"@com_github_urfave_cli_v2//:go_default_library",
"@com_github_urfave_cli_v2//altsrc:go_default_library",
"@com_github_x_cray_logrus_prefixed_formatter//:go_default_library",
"//shared/maxprocs:go_default_library",
],
Expand Down
9 changes: 3 additions & 6 deletions beacon-chain/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"github.com/prysmaticlabs/prysm/shared/version"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
"github.com/urfave/cli/v2/altsrc"
prefixed "github.com/x-cray/logrus-prefixed-formatter"
)

Expand Down Expand Up @@ -111,11 +110,9 @@ func main() {
app.Flags = appFlags

app.Before = func(ctx *cli.Context) error {
// Load any flags from file, if specified.
if ctx.IsSet(cmd.ConfigFileFlag.Name) {
if err := altsrc.InitInputSourceWithContext(appFlags, altsrc.NewYamlSourceFromFlagFunc(cmd.ConfigFileFlag.Name))(ctx); err != nil {
return err
}
// Load flags from config file, if specified.
if err := cmd.LoadFlagsFromConfig(ctx, app.Flags); err != nil {
return err
}

format := ctx.String(cmd.LogFormat.Name)
Expand Down
2 changes: 2 additions & 0 deletions shared/cmd/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,14 @@ go_test(
size = "small",
srcs = [
"config_test.go",
"flags_test.go",
"helpers_test.go",
],
embed = [":go_default_library"],
deps = [
"//shared/params:go_default_library",
"//shared/testutil/assert:go_default_library",
"//shared/testutil/require:go_default_library",
"@com_github_golang_mock//gomock:go_default_library",
"@com_github_pkg_errors//:go_default_library",
"@com_github_urfave_cli_v2//:go_default_library",
Expand Down
11 changes: 11 additions & 0 deletions shared/cmd/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd

import (
"github.com/urfave/cli/v2"
"github.com/urfave/cli/v2/altsrc"
)

var (
Expand Down Expand Up @@ -203,3 +204,13 @@ var (
Value: 1 << 22,
}
)

// LoadFlagsFromConfig sets flags values from config file if ConfigFileFlag is set.
func LoadFlagsFromConfig(cliCtx *cli.Context, flags []cli.Flag) error {
if cliCtx.IsSet(ConfigFileFlag.Name) {
if err := altsrc.InitInputSourceWithContext(flags, altsrc.NewYamlSourceFromFlagFunc(ConfigFileFlag.Name))(cliCtx); err != nil {
return err
}
}
return nil
}
42 changes: 42 additions & 0 deletions shared/cmd/flags_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package cmd

import (
"flag"
"io/ioutil"
"os"
"testing"

"github.com/prysmaticlabs/prysm/shared/testutil/require"
"github.com/urfave/cli/v2"
)

func TestLoadFlagsFromConfig(t *testing.T) {
app := cli.App{}
set := flag.NewFlagSet("test", 0)
context := cli.NewContext(&app, set, nil)

require.NoError(t, ioutil.WriteFile("flags_test.yaml", []byte("testflag: 100"), 0666))

require.NoError(t, set.Parse([]string{"test-command", "--" + ConfigFileFlag.Name, "flags_test.yaml"}))
command := &cli.Command{
Name: "test-command",
Flags: WrapFlags([]cli.Flag{
&cli.StringFlag{
Name: ConfigFileFlag.Name,
},
&cli.IntFlag{
Name: "testflag",
Value: 0,
},
}),
Before: func(cliCtx *cli.Context) error {
return LoadFlagsFromConfig(cliCtx, cliCtx.Command.Flags)
},
Action: func(cliCtx *cli.Context) error {
require.Equal(t, 100, cliCtx.Int("testflag"))
return nil
},
}
require.NoError(t, command.Run(context))
require.NoError(t, os.Remove("flags_test.yaml"))
}
2 changes: 0 additions & 2 deletions slasher/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ go_library(
"@com_github_joonix_log//:go_default_library",
"@com_github_sirupsen_logrus//:go_default_library",
"@com_github_urfave_cli_v2//:go_default_library",
"@com_github_urfave_cli_v2//altsrc:go_default_library",
"@com_github_x_cray_logrus_prefixed_formatter//:go_default_library",
],
)
Expand Down Expand Up @@ -65,7 +64,6 @@ go_image(
"@com_github_joonix_log//:go_default_library",
"@com_github_sirupsen_logrus//:go_default_library",
"@com_github_urfave_cli_v2//:go_default_library",
"@com_github_urfave_cli_v2//altsrc:go_default_library",
"@com_github_x_cray_logrus_prefixed_formatter//:go_default_library",
],
)
Expand Down
12 changes: 3 additions & 9 deletions slasher/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"github.com/prysmaticlabs/prysm/slasher/node"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
"github.com/urfave/cli/v2/altsrc"
prefixed "github.com/x-cray/logrus-prefixed-formatter"
)

Expand Down Expand Up @@ -85,14 +84,9 @@ func main() {
app.Flags = appFlags
app.Action = startSlasher
app.Before = func(ctx *cli.Context) error {
// Load any flags from file, if specified.
if ctx.IsSet(cmd.ConfigFileFlag.Name) {
if err := altsrc.InitInputSourceWithContext(
appFlags,
altsrc.NewYamlSourceFromFlagFunc(
cmd.ConfigFileFlag.Name))(ctx); err != nil {
return err
}
// Load flags from config file, if specified.
if err := cmd.LoadFlagsFromConfig(ctx, app.Flags); err != nil {
return err
}

format := ctx.String(cmd.LogFormat.Name)
Expand Down
2 changes: 0 additions & 2 deletions validator/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ go_library(
"@com_github_prysmaticlabs_ethereumapis//eth/v1alpha1:go_default_library",
"@com_github_sirupsen_logrus//:go_default_library",
"@com_github_urfave_cli_v2//:go_default_library",
"@com_github_urfave_cli_v2//altsrc:go_default_library",
"@com_github_x_cray_logrus_prefixed_formatter//:go_default_library",
"@org_golang_google_grpc//:go_default_library",
],
Expand Down Expand Up @@ -71,7 +70,6 @@ go_image(
"@com_github_prysmaticlabs_ethereumapis//eth/v1alpha1:go_default_library",
"@com_github_sirupsen_logrus//:go_default_library",
"@com_github_urfave_cli_v2//:go_default_library",
"@com_github_urfave_cli_v2//altsrc:go_default_library",
"@com_github_x_cray_logrus_prefixed_formatter//:go_default_library",
"@org_golang_google_grpc//:go_default_library",
"//shared/maxprocs:go_default_library",
Expand Down
30 changes: 24 additions & 6 deletions validator/accounts/v2/cmd_accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ var AccountCommands = &cli.Command{
Description: `creates a new validator account for eth2. If no wallet exists at the given wallet path, creates a new wallet for a user based on
specified input, capable of creating a direct, derived, or remote wallet.
this command outputs a deposit data string which is required to become a validator in eth2.`,
Flags: []cli.Flag{
Flags: cmd.WrapFlags([]cli.Flag{
flags.WalletDirFlag,
flags.WalletPasswordFileFlag,
flags.AccountPasswordFileFlag,
Expand All @@ -32,6 +32,9 @@ this command outputs a deposit data string which is required to become a validat
featureconfig.MedallaTestnet,
featureconfig.SpadinaTestnet,
featureconfig.ZinkenTestnet,
}),
Before: func(cliCtx *cli.Context) error {
return cmd.LoadFlagsFromConfig(cliCtx, cliCtx.Command.Flags)
},
Action: func(cliCtx *cli.Context) error {
featureconfig.ConfigureValidator(cliCtx)
Expand All @@ -44,7 +47,7 @@ this command outputs a deposit data string which is required to become a validat
{
Name: "delete",
Description: `deletes the selected accounts from a users wallet.`,
Flags: []cli.Flag{
Flags: cmd.WrapFlags([]cli.Flag{
flags.WalletDirFlag,
flags.WalletPasswordFileFlag,
flags.AccountPasswordFileFlag,
Expand All @@ -54,6 +57,9 @@ this command outputs a deposit data string which is required to become a validat
featureconfig.MedallaTestnet,
featureconfig.SpadinaTestnet,
featureconfig.ZinkenTestnet,
}),
Before: func(cliCtx *cli.Context) error {
return cmd.LoadFlagsFromConfig(cliCtx, cliCtx.Command.Flags)
},
Action: func(cliCtx *cli.Context) error {
featureconfig.ConfigureValidator(cliCtx)
Expand All @@ -66,7 +72,7 @@ this command outputs a deposit data string which is required to become a validat
{
Name: "list",
Description: "Lists all validator accounts in a user's wallet directory",
Flags: []cli.Flag{
Flags: cmd.WrapFlags([]cli.Flag{
flags.WalletDirFlag,
flags.WalletPasswordFileFlag,
flags.ShowDepositDataFlag,
Expand All @@ -76,6 +82,9 @@ this command outputs a deposit data string which is required to become a validat
featureconfig.SpadinaTestnet,
featureconfig.ZinkenTestnet,
flags.DeprecatedPasswordsDirFlag,
}),
Before: func(cliCtx *cli.Context) error {
return cmd.LoadFlagsFromConfig(cliCtx, cliCtx.Command.Flags)
},
Action: func(cliCtx *cli.Context) error {
featureconfig.ConfigureValidator(cliCtx)
Expand All @@ -91,7 +100,7 @@ this command outputs a deposit data string which is required to become a validat
"at a desired output directory. Accounts to backup can also " +
"be specified programmatically via a --backup-for-public-keys flag which specifies a comma-separated " +
"list of hex string public keys",
Flags: []cli.Flag{
Flags: cmd.WrapFlags([]cli.Flag{
flags.WalletDirFlag,
flags.WalletPasswordFileFlag,
flags.BackupDirFlag,
Expand All @@ -102,6 +111,9 @@ this command outputs a deposit data string which is required to become a validat
featureconfig.MedallaTestnet,
featureconfig.SpadinaTestnet,
featureconfig.ZinkenTestnet,
}),
Before: func(cliCtx *cli.Context) error {
return cmd.LoadFlagsFromConfig(cliCtx, cliCtx.Command.Flags)
},
Action: func(cliCtx *cli.Context) error {
featureconfig.ConfigureValidator(cliCtx)
Expand All @@ -114,7 +126,7 @@ this command outputs a deposit data string which is required to become a validat
{
Name: "import",
Description: `imports eth2 validator accounts stored in EIP-2335 keystore.json files from an external directory`,
Flags: []cli.Flag{
Flags: cmd.WrapFlags([]cli.Flag{
flags.WalletDirFlag,
flags.KeysDirFlag,
flags.WalletPasswordFileFlag,
Expand All @@ -126,6 +138,9 @@ this command outputs a deposit data string which is required to become a validat
featureconfig.SpadinaTestnet,
featureconfig.ZinkenTestnet,
flags.DeprecatedPasswordsDirFlag,
}),
Before: func(cliCtx *cli.Context) error {
return cmd.LoadFlagsFromConfig(cliCtx, cliCtx.Command.Flags)
},
Action: func(cliCtx *cli.Context) error {
featureconfig.ConfigureValidator(cliCtx)
Expand All @@ -138,7 +153,7 @@ this command outputs a deposit data string which is required to become a validat
{
Name: "voluntary-exit",
Description: "Performs a voluntary exit on selected accounts",
Flags: []cli.Flag{
Flags: cmd.WrapFlags([]cli.Flag{
flags.WalletDirFlag,
flags.WalletPasswordFileFlag,
flags.AccountPasswordFileFlag,
Expand All @@ -154,6 +169,9 @@ this command outputs a deposit data string which is required to become a validat
featureconfig.MedallaTestnet,
featureconfig.SpadinaTestnet,
featureconfig.ZinkenTestnet,
}),
Before: func(cliCtx *cli.Context) error {
return cmd.LoadFlagsFromConfig(cliCtx, cliCtx.Command.Flags)
},
Action: func(cliCtx *cli.Context) error {
featureconfig.ConfigureValidator(cliCtx)
Expand Down
2 changes: 1 addition & 1 deletion validator/flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ var (
Value: false,
}
// NumAccountsFlag defines the amount of accounts to generate for derived wallets.
NumAccountsFlag = &cli.Int64Flag{
NumAccountsFlag = &cli.IntFlag{
Name: "num-accounts",
Usage: "Number of accounts to generate for derived wallets",
Value: 1,
Expand Down

0 comments on commit 0214553

Please sign in to comment.