Fix config file flags in subcommands (#7475)

This commit is contained in:
bidlocode 2020-10-11 21:26:59 +06:00 committed by GitHub
parent b5a913d862
commit 0214553415
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 115 additions and 40 deletions

View File

@ -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",
],
)
@ -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",
],

View File

@ -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"
)
@ -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)

View File

@ -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",

View File

@ -3,6 +3,7 @@ package cmd
import (
"github.com/urfave/cli/v2"
"github.com/urfave/cli/v2/altsrc"
)
var (
@ -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
shared/cmd/flags_test.go Normal file
View File

@ -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"))
}

View File

@ -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",
],
)
@ -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",
],
)

View File

@ -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"
)
@ -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)

View File

@ -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",
],
@ -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",

View File

@ -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,
@ -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)
@ -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,
@ -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)
@ -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,
@ -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)
@ -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,
@ -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)
@ -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,
@ -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)
@ -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,
@ -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)

View File

@ -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,

View File

@ -28,7 +28,6 @@ import (
"github.com/prysmaticlabs/prysm/validator/node"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
"github.com/urfave/cli/v2/altsrc"
prefixed "github.com/x-cray/logrus-prefixed-formatter"
"google.golang.org/grpc"
)
@ -131,12 +130,15 @@ func main() {
Description: `creates a new validator account keystore containing private keys for Ethereum 2.0 -
this command outputs a deposit data string which can be used to deposit Ether into the ETH1.0 deposit
contract in order to activate the validator client`,
Flags: append(featureconfig.ActiveFlags(featureconfig.ValidatorFlags),
Flags: cmd.WrapFlags(append(featureconfig.ActiveFlags(featureconfig.ValidatorFlags),
[]cli.Flag{
flags.KeystorePathFlag,
flags.PasswordFlag,
cmd.ChainConfigFileFlag,
}...),
}...)),
Before: func(cliCtx *cli.Context) error {
return cmd.LoadFlagsFromConfig(cliCtx, cliCtx.Command.Flags)
},
Action: func(cliCtx *cli.Context) error {
featureconfig.ConfigureValidator(cliCtx)
@ -159,9 +161,12 @@ contract in order to activate the validator client`,
{
Name: "keys",
Description: `lists the private keys for 'keystore' keymanager keys`,
Flags: []cli.Flag{
Flags: cmd.WrapFlags([]cli.Flag{
flags.KeystorePathFlag,
flags.PasswordFlag,
}),
Before: func(cliCtx *cli.Context) error {
return cmd.LoadFlagsFromConfig(cliCtx, cliCtx.Command.Flags)
},
Action: func(cliCtx *cli.Context) error {
keystorePath, passphrase, err := v1.HandleEmptyKeystoreFlags(cliCtx, false /*confirmPassword*/)
@ -177,7 +182,7 @@ contract in order to activate the validator client`,
{
Name: "status",
Description: `list the validator status for existing validator keys`,
Flags: []cli.Flag{
Flags: cmd.WrapFlags([]cli.Flag{
cmd.GrpcMaxCallRecvMsgSizeFlag,
flags.BeaconRPCProviderFlag,
flags.CertFlag,
@ -186,6 +191,9 @@ contract in order to activate the validator client`,
flags.GrpcRetryDelayFlag,
flags.KeyManager,
flags.KeyManagerOpts,
}),
Before: func(cliCtx *cli.Context) error {
return cmd.LoadFlagsFromConfig(cliCtx, cliCtx.Command.Flags)
},
Action: func(cliCtx *cli.Context) error {
var err error
@ -225,9 +233,12 @@ contract in order to activate the validator client`,
{
Name: "change-password",
Description: "changes password for all keys located in a keystore",
Flags: []cli.Flag{
Flags: cmd.WrapFlags([]cli.Flag{
flags.KeystorePathFlag,
flags.PasswordFlag,
}),
Before: func(cliCtx *cli.Context) error {
return cmd.LoadFlagsFromConfig(cliCtx, cliCtx.Command.Flags)
},
Action: func(cliCtx *cli.Context) error {
keystorePath, oldPassword, err := v1.HandleEmptyKeystoreFlags(cliCtx, false /*confirmPassword*/)
@ -254,9 +265,12 @@ contract in order to activate the validator client`,
{
Name: "merge",
Description: "merges data from several validator databases into a new validator database",
Flags: []cli.Flag{
Flags: cmd.WrapFlags([]cli.Flag{
flags.SourceDirectories,
flags.TargetDirectory,
}),
Before: func(cliCtx *cli.Context) error {
return cmd.LoadFlagsFromConfig(cliCtx, cliCtx.Command.Flags)
},
Action: func(cliCtx *cli.Context) error {
passedSources := cliCtx.String(flags.SourceDirectories.Name)
@ -275,9 +289,12 @@ contract in order to activate the validator client`,
{
Name: "split",
Description: "splits one validator database into several databases - one for each public key",
Flags: []cli.Flag{
Flags: cmd.WrapFlags([]cli.Flag{
flags.SourceDirectory,
flags.TargetDirectory,
}),
Before: func(cliCtx *cli.Context) error {
return cmd.LoadFlagsFromConfig(cliCtx, cliCtx.Command.Flags)
},
Action: func(cliCtx *cli.Context) error {
source := cliCtx.String(flags.SourceDirectory.Name)
@ -299,11 +316,11 @@ contract in order to activate the validator client`,
app.Flags = appFlags
app.Before = func(ctx *cli.Context) error {
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
}
flags.ComplainOnDeprecatedFlags(ctx)
format := ctx.String(cmd.LogFormat.Name)