From bc15c598ac05639ac0f58fc4b7450472c00170a8 Mon Sep 17 00:00:00 2001 From: Michael FIG Date: Tue, 6 Oct 2020 11:20:36 -0600 Subject: [PATCH] refactor: inline flags.GetKeyringDir and remove it --- client/cmd.go | 5 ++++- client/flags/flags.go | 15 --------------- client/keys/add.go | 5 ++++- client/keys/delete.go | 5 ++++- client/keys/export.go | 5 ++++- client/keys/import.go | 6 +++++- client/keys/list.go | 6 +++++- client/keys/migrate.go | 5 ++++- client/keys/show.go | 5 ++++- simapp/simd/cmd/genaccounts.go | 6 +++++- x/auth/client/cli/tx_multisign.go | 5 ++++- 11 files changed, 43 insertions(+), 25 deletions(-) diff --git a/client/cmd.go b/client/cmd.go index f4024df0baf..101090d7bb9 100644 --- a/client/cmd.go +++ b/client/cmd.go @@ -103,8 +103,11 @@ func ReadPersistentCommandFlags(clientCtx Context, flagSet *pflag.FlagSet) (Cont } if clientCtx.Keyring == nil || flagSet.Changed(flags.FlagKeyringBackend) { + keyringDir, _ := flagSet.GetString(flags.FlagKeyringDir) + if keyringDir == "" { + keyringDir, _ = flagSet.GetString(flags.FlagHome) + } keyringBackend, _ := flagSet.GetString(flags.FlagKeyringBackend) - keyringDir, _ := flags.GetKeyringDir(flagSet, clientCtx.HomeDir) if keyringBackend != "" { kr, err := newKeyringFromFlags(clientCtx, keyringBackend, keyringDir) diff --git a/client/flags/flags.go b/client/flags/flags.go index 00635d603ee..a8cb32b6138 100644 --- a/client/flags/flags.go +++ b/client/flags/flags.go @@ -5,7 +5,6 @@ import ( "strconv" "github.com/spf13/cobra" - "github.com/spf13/pflag" tmcli "github.com/tendermint/tendermint/libs/cli" "github.com/cosmos/cosmos-sdk/crypto/keyring" @@ -114,20 +113,6 @@ func AddTxFlagsToCmd(cmd *cobra.Command) { cmd.SetOut(cmd.OutOrStdout()) } -// GetKeyringDir finds the directory for the keyring storage -func GetKeyringDir(pflags *pflag.FlagSet, homeDir string) (string, error) { - keyringDir, err := pflags.GetString(FlagKeyringDir) - if keyringDir == "" { - // Use the specified home directory. - keyringDir = homeDir - } - if keyringDir == "" { - // Default to the node's home directory. - keyringDir, err = pflags.GetString(FlagHome) - } - return keyringDir, err -} - // AddPaginationFlagsToCmd adds common pagination flags to cmd func AddPaginationFlagsToCmd(cmd *cobra.Command, query string) { cmd.Flags().Uint64(FlagPage, 1, fmt.Sprintf("pagination page of %s to query for. This sets offset to a multiple of limit", query)) diff --git a/client/keys/add.go b/client/keys/add.go index 93fc69976d5..6b94b436604 100644 --- a/client/keys/add.go +++ b/client/keys/add.go @@ -85,7 +85,10 @@ the flag --nosort is set. func runAddCmd(cmd *cobra.Command, args []string) error { buf := bufio.NewReader(cmd.InOrStdin()) - keyringDir, _ := flags.GetKeyringDir(cmd.Flags(), "") + keyringDir, _ := cmd.Flags().GetString(flags.FlagKeyringDir) + if keyringDir == "" { + keyringDir, _ = cmd.Flags().GetString(flags.FlagHome) + } dryRun, _ := cmd.Flags().GetBool(flags.FlagDryRun) var ( diff --git a/client/keys/delete.go b/client/keys/delete.go index 3e79b3dc43b..ef9673b3c2f 100644 --- a/client/keys/delete.go +++ b/client/keys/delete.go @@ -31,8 +31,11 @@ private keys stored in a ledger device cannot be deleted with the CLI. RunE: func(cmd *cobra.Command, args []string) error { buf := bufio.NewReader(cmd.InOrStdin()) + keyringDir, _ := cmd.Flags().GetString(flags.FlagKeyringDir) + if keyringDir == "" { + keyringDir, _ = cmd.Flags().GetString(flags.FlagHome) + } backend, _ := cmd.Flags().GetString(flags.FlagKeyringBackend) - keyringDir, _ := flags.GetKeyringDir(cmd.Flags(), "") kb, err := keyring.New(sdk.KeyringServiceName(), backend, keyringDir, buf) if err != nil { return err diff --git a/client/keys/export.go b/client/keys/export.go index 40b941339eb..e07a6ed9e9f 100644 --- a/client/keys/export.go +++ b/client/keys/export.go @@ -21,8 +21,11 @@ func ExportKeyCommand() *cobra.Command { RunE: func(cmd *cobra.Command, args []string) error { buf := bufio.NewReader(cmd.InOrStdin()) + keyringDir, _ := cmd.Flags().GetString(flags.FlagKeyringDir) + if keyringDir == "" { + keyringDir, _ = cmd.Flags().GetString(flags.FlagHome) + } backend, _ := cmd.Flags().GetString(flags.FlagKeyringBackend) - keyringDir, _ := flags.GetKeyringDir(cmd.Flags(), "") kb, err := keyring.New(sdk.KeyringServiceName(), backend, keyringDir, buf) if err != nil { return err diff --git a/client/keys/import.go b/client/keys/import.go index c3ed9b23089..b7cdb8d6680 100644 --- a/client/keys/import.go +++ b/client/keys/import.go @@ -22,8 +22,12 @@ func ImportKeyCommand() *cobra.Command { RunE: func(cmd *cobra.Command, args []string) error { buf := bufio.NewReader(cmd.InOrStdin()) + keyringDir, _ := cmd.Flags().GetString(flags.FlagKeyringDir) + if keyringDir == "" { + keyringDir, _ = cmd.Flags().GetString(flags.FlagHome) + } backend, _ := cmd.Flags().GetString(flags.FlagKeyringBackend) - keyringDir, _ := flags.GetKeyringDir(cmd.Flags(), "") + kb, err := keyring.New(sdk.KeyringServiceName(), backend, keyringDir, buf) if err != nil { return err diff --git a/client/keys/list.go b/client/keys/list.go index 006d65765f7..2ecab964236 100644 --- a/client/keys/list.go +++ b/client/keys/list.go @@ -26,8 +26,12 @@ along with their associated name and address.`, } func runListCmd(cmd *cobra.Command, _ []string) error { + keyringDir, _ := cmd.Flags().GetString(flags.FlagKeyringDir) + if keyringDir == "" { + keyringDir, _ = cmd.Flags().GetString(flags.FlagHome) + } backend, _ := cmd.Flags().GetString(flags.FlagKeyringBackend) - keyringDir, _ := flags.GetKeyringDir(cmd.Flags(), "") + kb, err := keyring.New(sdk.KeyringServiceName(), backend, keyringDir, cmd.InOrStdin()) if err != nil { return err diff --git a/client/keys/migrate.go b/client/keys/migrate.go index 9e853a372c1..c7192e1893c 100644 --- a/client/keys/migrate.go +++ b/client/keys/migrate.go @@ -40,7 +40,10 @@ It is recommended to run in 'dry-run' mode first to verify all key migration mat } func runMigrateCmd(cmd *cobra.Command, args []string) error { - keyringDir, _ := flags.GetKeyringDir(cmd.Flags(), "") + keyringDir, _ := cmd.Flags().GetString(flags.FlagKeyringDir) + if keyringDir == "" { + keyringDir, _ = cmd.Flags().GetString(flags.FlagHome) + } // instantiate legacy keybase var legacyKb keyring.LegacyKeybase diff --git a/client/keys/show.go b/client/keys/show.go index 3bdca3d4c91..04cfd4c067e 100644 --- a/client/keys/show.go +++ b/client/keys/show.go @@ -54,8 +54,11 @@ consisting of all the keys provided by name and multisig threshold.`, func runShowCmd(cmd *cobra.Command, args []string) (err error) { var info keyring.Info + keyringDir, _ := cmd.Flags().GetString(flags.FlagKeyringDir) + if keyringDir == "" { + keyringDir, _ = cmd.Flags().GetString(flags.FlagHome) + } backend, _ := cmd.Flags().GetString(flags.FlagKeyringBackend) - keyringDir, _ := flags.GetKeyringDir(cmd.Flags(), "") kb, err := keyring.New(sdk.KeyringServiceName(), backend, keyringDir, cmd.InOrStdin()) if err != nil { return err diff --git a/simapp/simd/cmd/genaccounts.go b/simapp/simd/cmd/genaccounts.go index b42d65029d2..36c03414336 100644 --- a/simapp/simd/cmd/genaccounts.go +++ b/simapp/simd/cmd/genaccounts.go @@ -51,7 +51,11 @@ contain valid denominations. Accounts may optionally be supplied with vesting pa addr, err := sdk.AccAddressFromBech32(args[0]) if err != nil { inBuf := bufio.NewReader(cmd.InOrStdin()) - keyringDir, _ := flags.GetKeyringDir(cmd.Flags(), clientCtx.HomeDir) + + keyringDir, _ := cmd.Flags().GetString(flags.FlagKeyringDir) + if keyringDir == "" { + keyringDir = clientCtx.HomeDir + } keyringBackend, _ := cmd.Flags().GetString(flags.FlagKeyringBackend) // attempt to lookup address from Keybase if no address was provided diff --git a/x/auth/client/cli/tx_multisign.go b/x/auth/client/cli/tx_multisign.go index 18254bf2460..2faeb5d03a8 100644 --- a/x/auth/client/cli/tx_multisign.go +++ b/x/auth/client/cli/tx_multisign.go @@ -82,8 +82,11 @@ func makeMultiSignCmd() func(cmd *cobra.Command, args []string) error { return err } + keyringDir, _ := cmd.Flags().GetString(flags.FlagKeyringDir) + if keyringDir == "" { + keyringDir = clientCtx.HomeDir + } backend, _ := cmd.Flags().GetString(flags.FlagKeyringBackend) - keyringDir, _ := flags.GetKeyringDir(cmd.Flags(), clientCtx.HomeDir) inBuf := bufio.NewReader(cmd.InOrStdin()) kb, err := keyring.New(sdk.KeyringServiceName(), backend, keyringDir, inBuf)