Skip to content

Commit

Permalink
refactor(keys): remove duplicate --home flag (#17197)
Browse files Browse the repository at this point in the history
  • Loading branch information
zakir-code authored Jul 31, 2023
1 parent bf1803b commit edba186
Show file tree
Hide file tree
Showing 13 changed files with 42 additions and 43 deletions.
6 changes: 3 additions & 3 deletions client/keys/add_ledger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func Test_runAddCmdLedgerWithCustomCoinType(t *testing.T) {
config.SetBech32PrefixForConsensusNode(bech32PrefixConsAddr, bech32PrefixConsPub)

cmd := AddKeyCommand()
cmd.Flags().AddFlagSet(Commands("home").PersistentFlags())
cmd.Flags().AddFlagSet(Commands().PersistentFlags())

// Prepare a keybase
kbHome := t.TempDir()
Expand Down Expand Up @@ -91,7 +91,7 @@ func Test_runAddCmdLedgerWithCustomCoinType(t *testing.T) {

func Test_runAddCmdLedger(t *testing.T) {
cmd := AddKeyCommand()
cmd.Flags().AddFlagSet(Commands("home").PersistentFlags())
cmd.Flags().AddFlagSet(Commands().PersistentFlags())

mockIn := testutil.ApplyMockIODiscardOutErr(cmd)
kbHome := t.TempDir()
Expand Down Expand Up @@ -166,7 +166,7 @@ func Test_runAddCmdLedgerDryRun(t *testing.T) {
tt := tt
t.Run(tt.name, func(t *testing.T) {
cmd := AddKeyCommand()
cmd.Flags().AddFlagSet(Commands("home").PersistentFlags())
cmd.Flags().AddFlagSet(Commands().PersistentFlags())

kbHome := t.TempDir()
mockIn := testutil.ApplyMockIODiscardOutErr(cmd)
Expand Down
16 changes: 8 additions & 8 deletions client/keys/add_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (

func Test_runAddCmdBasic(t *testing.T) {
cmd := AddKeyCommand()
cmd.Flags().AddFlagSet(Commands("home").PersistentFlags())
cmd.Flags().AddFlagSet(Commands().PersistentFlags())

mockIn := testutil.ApplyMockIODiscardOutErr(cmd)
kbHome := t.TempDir()
Expand All @@ -41,7 +41,7 @@ func Test_runAddCmdBasic(t *testing.T) {

cmd.SetArgs([]string{
"keyname1",
fmt.Sprintf("--%s=%s", flags.FlagHome, kbHome),
fmt.Sprintf("--%s=%s", flags.FlagKeyringDir, kbHome),
fmt.Sprintf("--%s=%s", flags.FlagOutput, flags.OutputFormatText),
fmt.Sprintf("--%s=%s", flags.FlagKeyType, hd.Secp256k1Type),
fmt.Sprintf("--%s=%s", flags.FlagKeyringBackend, keyring.BackendTest),
Expand All @@ -54,7 +54,7 @@ func Test_runAddCmdBasic(t *testing.T) {

cmd.SetArgs([]string{
"keyname2",
fmt.Sprintf("--%s=%s", flags.FlagHome, kbHome),
fmt.Sprintf("--%s=%s", flags.FlagKeyringDir, kbHome),
fmt.Sprintf("--%s=%s", flags.FlagOutput, flags.OutputFormatText),
fmt.Sprintf("--%s=%s", flags.FlagKeyType, hd.Secp256k1Type),
fmt.Sprintf("--%s=%s", flags.FlagKeyringBackend, keyring.BackendTest),
Expand All @@ -68,7 +68,7 @@ func Test_runAddCmdBasic(t *testing.T) {

cmd.SetArgs([]string{
"keyname4",
fmt.Sprintf("--%s=%s", flags.FlagHome, kbHome),
fmt.Sprintf("--%s=%s", flags.FlagKeyringDir, kbHome),
fmt.Sprintf("--%s=%s", flags.FlagOutput, flags.OutputFormatText),
fmt.Sprintf("--%s=%s", flags.FlagKeyType, hd.Secp256k1Type),
fmt.Sprintf("--%s=%s", flags.FlagKeyringBackend, keyring.BackendTest),
Expand All @@ -79,7 +79,7 @@ func Test_runAddCmdBasic(t *testing.T) {

cmd.SetArgs([]string{
"keyname5",
fmt.Sprintf("--%s=%s", flags.FlagHome, kbHome),
fmt.Sprintf("--%s=%s", flags.FlagKeyringDir, kbHome),
fmt.Sprintf("--%s=true", flags.FlagDryRun),
fmt.Sprintf("--%s=%s", flags.FlagOutput, flags.OutputFormatText),
fmt.Sprintf("--%s=%s", flags.FlagKeyType, hd.Secp256k1Type),
Expand Down Expand Up @@ -186,7 +186,7 @@ func Test_runAddCmdDryRun(t *testing.T) {
tt := tt
t.Run(tt.name, func(t *testing.T) {
cmd := AddKeyCommand()
cmd.Flags().AddFlagSet(Commands("home").PersistentFlags())
cmd.Flags().AddFlagSet(Commands().PersistentFlags())

kbHome := t.TempDir()
mockIn := testutil.ApplyMockIODiscardOutErr(cmd)
Expand Down Expand Up @@ -232,7 +232,7 @@ func Test_runAddCmdDryRun(t *testing.T) {

func TestAddRecoverFileBackend(t *testing.T) {
cmd := AddKeyCommand()
cmd.Flags().AddFlagSet(Commands("home").PersistentFlags())
cmd.Flags().AddFlagSet(Commands().PersistentFlags())
cdc := moduletestutil.MakeTestEncodingConfig().Codec

mockIn := testutil.ApplyMockIODiscardOutErr(cmd)
Expand All @@ -243,7 +243,7 @@ func TestAddRecoverFileBackend(t *testing.T) {

cmd.SetArgs([]string{
"keyname1",
fmt.Sprintf("--%s=%s", flags.FlagHome, kbHome),
fmt.Sprintf("--%s=%s", flags.FlagKeyringDir, kbHome),
fmt.Sprintf("--%s=%s", flags.FlagOutput, flags.OutputFormatText),
fmt.Sprintf("--%s=%s", flags.FlagKeyType, hd.Secp256k1Type),
fmt.Sprintf("--%s=%s", flags.FlagKeyringBackend, keyring.BackendFile),
Expand Down
10 changes: 5 additions & 5 deletions client/keys/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func Test_runDeleteCmd(t *testing.T) {
// Now add a temporary keybase
kbHome := t.TempDir()
cmd := DeleteKeyCommand()
cmd.Flags().AddFlagSet(Commands(kbHome).PersistentFlags())
cmd.Flags().AddFlagSet(Commands().PersistentFlags())
mockIn := testutil.ApplyMockIODiscardOutErr(cmd)

yesF, _ := cmd.Flags().GetBool(flagYes)
Expand All @@ -36,7 +36,7 @@ func Test_runDeleteCmd(t *testing.T) {
path := sdk.GetConfig().GetFullBIP44Path()
cdc := moduletestutil.MakeTestEncodingConfig().Codec

cmd.SetArgs([]string{"blah", fmt.Sprintf("--%s=%s", flags.FlagHome, kbHome)})
cmd.SetArgs([]string{"blah", fmt.Sprintf("--%s=%s", flags.FlagKeyringDir, kbHome)})
kb, err := keyring.New(sdk.KeyringServiceName(), keyring.BackendTest, kbHome, mockIn, cdc)
require.NoError(t, err)

Expand All @@ -59,7 +59,7 @@ func Test_runDeleteCmd(t *testing.T) {
// User confirmation missing
cmd.SetArgs([]string{
fakeKeyName1,
fmt.Sprintf("--%s=%s", flags.FlagHome, kbHome),
fmt.Sprintf("--%s=%s", flags.FlagKeyringDir, kbHome),
fmt.Sprintf("--%s=%s", flags.FlagKeyringBackend, keyring.BackendTest),
})
err = cmd.Execute()
Expand All @@ -72,7 +72,7 @@ func Test_runDeleteCmd(t *testing.T) {
// Now there is a confirmation
cmd.SetArgs([]string{
fakeKeyName1,
fmt.Sprintf("--%s=%s", flags.FlagHome, kbHome),
fmt.Sprintf("--%s=%s", flags.FlagKeyringDir, kbHome),
fmt.Sprintf("--%s=true", flagYes),
fmt.Sprintf("--%s=%s", flags.FlagKeyringBackend, keyring.BackendTest),
})
Expand All @@ -86,7 +86,7 @@ func Test_runDeleteCmd(t *testing.T) {

cmd.SetArgs([]string{
fakeKeyName2,
fmt.Sprintf("--%s=%s", flags.FlagHome, kbHome),
fmt.Sprintf("--%s=%s", flags.FlagKeyringDir, kbHome),
fmt.Sprintf("--%s=true", flagYes),
fmt.Sprintf("--%s=%s", flags.FlagKeyringBackend, keyring.BackendTest),
})
Expand Down
4 changes: 2 additions & 2 deletions client/keys/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,12 @@ func Test_runExportCmd(t *testing.T) {
kbHome := t.TempDir()
defaultArgs := []string{
"keyname1",
fmt.Sprintf("--%s=%s", flags.FlagHome, kbHome),
fmt.Sprintf("--%s=%s", flags.FlagKeyringDir, kbHome),
fmt.Sprintf("--%s=%s", flags.FlagKeyringBackend, tc.keyringBackend),
}

cmd := ExportKeyCommand()
cmd.Flags().AddFlagSet(Commands("home").PersistentFlags())
cmd.Flags().AddFlagSet(Commands().PersistentFlags())

cmd.SetArgs(append(defaultArgs, tc.extraArgs...))
mockIn, mockOut := testutil.ApplyMockIO(cmd)
Expand Down
2 changes: 1 addition & 1 deletion client/keys/import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ HbP+c6JmeJy9JXe2rbbF1QtCX1gLqGcDQPBXiCtFvP7/8wTZtVOPj8vREzhZ9ElO
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
cmd := ImportKeyCommand()
cmd.Flags().AddFlagSet(Commands("home").PersistentFlags())
cmd.Flags().AddFlagSet(Commands().PersistentFlags())
mockIn := testutil.ApplyMockIODiscardOutErr(cmd)

// Now add a temporary keybase
Expand Down
6 changes: 3 additions & 3 deletions client/keys/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func cleanupKeys(t *testing.T, kb keyring.Keyring, keys ...string) func() {

func Test_runListCmd(t *testing.T) {
cmd := ListKeysCmd()
cmd.Flags().AddFlagSet(Commands("home").PersistentFlags())
cmd.Flags().AddFlagSet(Commands().PersistentFlags())

kbHome1 := t.TempDir()
kbHome2 := t.TempDir()
Expand Down Expand Up @@ -63,7 +63,7 @@ func Test_runListCmd(t *testing.T) {
tt := tt
t.Run(tt.name, func(t *testing.T) {
cmd.SetArgs([]string{
fmt.Sprintf("--%s=%s", flags.FlagHome, tt.kbDir),
fmt.Sprintf("--%s=%s", flags.FlagKeyringDir, tt.kbDir),
fmt.Sprintf("--%s=false", flagListNames),
})

Expand All @@ -72,7 +72,7 @@ func Test_runListCmd(t *testing.T) {
}

cmd.SetArgs([]string{
fmt.Sprintf("--%s=%s", flags.FlagHome, tt.kbDir),
fmt.Sprintf("--%s=%s", flags.FlagKeyringDir, tt.kbDir),
fmt.Sprintf("--%s=true", flagListNames),
})

Expand Down
4 changes: 2 additions & 2 deletions client/keys/migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (s *MigrateTestSuite) Test_runListAndShowCmd() {

// run test simd keys list - to see that the migrated key is there
cmd := ListKeysCmd()
cmd.Flags().AddFlagSet(Commands("home").PersistentFlags())
cmd.Flags().AddFlagSet(Commands().PersistentFlags())

mockIn := testutil.ApplyMockIODiscardOutErr(cmd)
kb, err := keyring.New(s.appName, keyring.BackendTest, s.dir, mockIn, s.cdc)
Expand All @@ -75,7 +75,7 @@ func (s *MigrateTestSuite) Test_runListAndShowCmd() {
ctx := context.WithValue(context.Background(), client.ClientContextKey, &clientCtx)

cmd.SetArgs([]string{
fmt.Sprintf("--%s=%s", flags.FlagHome, s.dir),
fmt.Sprintf("--%s=%s", flags.FlagKeyringDir, s.dir),
fmt.Sprintf("--%s=false", flagListNames),
})

Expand Down
10 changes: 5 additions & 5 deletions client/keys/rename_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func Test_runRenameCmd(t *testing.T) {
// temp keybase
kbHome := t.TempDir()
cmd := RenameKeyCommand()
cmd.Flags().AddFlagSet(Commands(kbHome).PersistentFlags())
cmd.Flags().AddFlagSet(Commands().PersistentFlags())
mockIn := testutil.ApplyMockIODiscardOutErr(cmd)

yesF, _ := cmd.Flags().GetBool(flagYes)
Expand All @@ -47,7 +47,7 @@ func Test_runRenameCmd(t *testing.T) {
ctx := context.WithValue(context.Background(), client.ClientContextKey, &clientCtx)

// rename a key 'blah' which doesnt exist
cmd.SetArgs([]string{"blah", "blaah", fmt.Sprintf("--%s=%s", flags.FlagHome, kbHome)})
cmd.SetArgs([]string{"blah", "blaah", fmt.Sprintf("--%s=%s", flags.FlagKeyringDir, kbHome)})
err = cmd.ExecuteContext(ctx)
require.Error(t, err)
require.EqualError(t, err, "blah.info: key not found")
Expand All @@ -56,7 +56,7 @@ func Test_runRenameCmd(t *testing.T) {
cmd.SetArgs([]string{
fakeKeyName1,
"nokey",
fmt.Sprintf("--%s=%s", flags.FlagHome, kbHome),
fmt.Sprintf("--%s=%s", flags.FlagKeyringDir, kbHome),
fmt.Sprintf("--%s=%s", flags.FlagKeyringBackend, keyring.BackendTest),
})
err = cmd.Execute()
Expand All @@ -70,7 +70,7 @@ func Test_runRenameCmd(t *testing.T) {
cmd.SetArgs([]string{
fakeKeyName1,
fakeKeyName2,
fmt.Sprintf("--%s=%s", flags.FlagHome, kbHome),
fmt.Sprintf("--%s=%s", flags.FlagKeyringDir, kbHome),
fmt.Sprintf("--%s=true", flagYes),
fmt.Sprintf("--%s=%s", flags.FlagKeyringBackend, keyring.BackendTest),
})
Expand Down Expand Up @@ -100,7 +100,7 @@ func Test_runRenameCmd(t *testing.T) {
cmd.SetArgs([]string{
fakeKeyName1,
fakeKeyName2,
fmt.Sprintf("--%s=%s", flags.FlagHome, kbHome),
fmt.Sprintf("--%s=%s", flags.FlagKeyringDir, kbHome),
fmt.Sprintf("--%s=true", flagYes),
fmt.Sprintf("--%s=%s", flags.FlagKeyringBackend, keyring.BackendTest),
})
Expand Down
3 changes: 1 addition & 2 deletions client/keys/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

// Commands registers a sub-tree of commands to interact with
// local private key storage.
func Commands(defaultNodeHome string) *cobra.Command {
func Commands() *cobra.Command {
cmd := &cobra.Command{
Use: "keys",
Short: "Manage your application's keys",
Expand Down Expand Up @@ -50,7 +50,6 @@ The pass backend requires GnuPG: https://gnupg.org/
MigrateCommand(),
)

cmd.PersistentFlags().String(flags.FlagHome, defaultNodeHome, "The application home directory")
cmd.PersistentFlags().String(flags.FlagOutput, "text", "Output format (text|json)")
flags.AddKeyringFlags(cmd.PersistentFlags())

Expand Down
2 changes: 1 addition & 1 deletion client/keys/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

func TestCommands(t *testing.T) {
rootCommands := Commands("home")
rootCommands := Commands()
assert.Assert(t, rootCommands != nil)

// Commands are registered
Expand Down
18 changes: 9 additions & 9 deletions client/keys/show_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func Test_showKeysCmd(t *testing.T) {

func Test_runShowCmd(t *testing.T) {
cmd := ShowKeysCmd()
cmd.Flags().AddFlagSet(Commands("home").PersistentFlags())
cmd.Flags().AddFlagSet(Commands().PersistentFlags())
mockIn := testutil.ApplyMockIODiscardOutErr(cmd)

kbHome := t.TempDir()
Expand Down Expand Up @@ -87,15 +87,15 @@ func Test_runShowCmd(t *testing.T) {
// Now try single key
cmd.SetArgs([]string{
fakeKeyName1,
fmt.Sprintf("--%s=%s", flags.FlagHome, kbHome),
fmt.Sprintf("--%s=%s", flags.FlagKeyringDir, kbHome),
fmt.Sprintf("--%s=", FlagBechPrefix),
fmt.Sprintf("--%s=%s", flags.FlagKeyringBackend, keyring.BackendTest),
})
require.EqualError(t, cmd.ExecuteContext(ctx), "invalid Bech32 prefix encoding provided: ")

cmd.SetArgs([]string{
fakeKeyName1,
fmt.Sprintf("--%s=%s", flags.FlagHome, kbHome),
fmt.Sprintf("--%s=%s", flags.FlagKeyringDir, kbHome),
fmt.Sprintf("--%s=%s", FlagBechPrefix, sdk.PrefixAccount),
fmt.Sprintf("--%s=%s", flags.FlagKeyringBackend, keyring.BackendTest),
})
Expand All @@ -110,7 +110,7 @@ func Test_runShowCmd(t *testing.T) {
require.NoError(t, err)
cmd.SetArgs([]string{
addr.String(),
fmt.Sprintf("--%s=%s", flags.FlagHome, kbHome),
fmt.Sprintf("--%s=%s", flags.FlagKeyringDir, kbHome),
fmt.Sprintf("--%s=%s", FlagBechPrefix, sdk.PrefixAccount),
fmt.Sprintf("--%s=%s", flags.FlagKeyringBackend, keyring.BackendTest),
})
Expand All @@ -120,7 +120,7 @@ func Test_runShowCmd(t *testing.T) {
// Now try multisig key - set bech to acc
cmd.SetArgs([]string{
fakeKeyName1, fakeKeyName2,
fmt.Sprintf("--%s=%s", flags.FlagHome, kbHome),
fmt.Sprintf("--%s=%s", flags.FlagKeyringDir, kbHome),
fmt.Sprintf("--%s=%s", FlagBechPrefix, sdk.PrefixAccount),
fmt.Sprintf("--%s=0", flagMultiSigThreshold),
fmt.Sprintf("--%s=%s", flags.FlagKeyringBackend, keyring.BackendTest),
Expand All @@ -129,7 +129,7 @@ func Test_runShowCmd(t *testing.T) {

cmd.SetArgs([]string{
fakeKeyName1, fakeKeyName2,
fmt.Sprintf("--%s=%s", flags.FlagHome, kbHome),
fmt.Sprintf("--%s=%s", flags.FlagKeyringDir, kbHome),
fmt.Sprintf("--%s=%s", FlagBechPrefix, sdk.PrefixAccount),
fmt.Sprintf("--%s=2", flagMultiSigThreshold),
fmt.Sprintf("--%s=%s", flags.FlagKeyringBackend, keyring.BackendTest),
Expand All @@ -139,7 +139,7 @@ func Test_runShowCmd(t *testing.T) {
// Now try multisig key - set bech to acc + threshold=2
cmd.SetArgs([]string{
fakeKeyName1, fakeKeyName2,
fmt.Sprintf("--%s=%s", flags.FlagHome, kbHome),
fmt.Sprintf("--%s=%s", flags.FlagKeyringDir, kbHome),
fmt.Sprintf("--%s=acc", FlagBechPrefix),
fmt.Sprintf("--%s=true", FlagDevice),
fmt.Sprintf("--%s=2", flagMultiSigThreshold),
Expand All @@ -149,7 +149,7 @@ func Test_runShowCmd(t *testing.T) {

cmd.SetArgs([]string{
fakeKeyName1, fakeKeyName2,
fmt.Sprintf("--%s=%s", flags.FlagHome, kbHome),
fmt.Sprintf("--%s=%s", flags.FlagKeyringDir, kbHome),
fmt.Sprintf("--%s=val", FlagBechPrefix),
fmt.Sprintf("--%s=true", FlagDevice),
fmt.Sprintf("--%s=2", flagMultiSigThreshold),
Expand All @@ -159,7 +159,7 @@ func Test_runShowCmd(t *testing.T) {

cmd.SetArgs([]string{
fakeKeyName1, fakeKeyName2,
fmt.Sprintf("--%s=%s", flags.FlagHome, kbHome),
fmt.Sprintf("--%s=%s", flags.FlagKeyringDir, kbHome),
fmt.Sprintf("--%s=val", FlagBechPrefix),
fmt.Sprintf("--%s=true", FlagDevice),
fmt.Sprintf("--%s=2", flagMultiSigThreshold),
Expand Down
2 changes: 1 addition & 1 deletion simapp/simd/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ func initRootCmd(rootCmd *cobra.Command, encodingConfig params.EncodingConfig, b
genesisCommand(encodingConfig, basicManager),
txCommand(),
queryCommand(),
keys.Commands(simapp.DefaultNodeHome),
keys.Commands(),
)
}

Expand Down
2 changes: 1 addition & 1 deletion simapp/simd/cmd/root_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ func initRootCmd(
genesisCommand(txConfig, basicManager),
queryCommand(),
txCommand(),
keys.Commands(simapp.DefaultNodeHome),
keys.Commands(),
)
}

Expand Down

0 comments on commit edba186

Please sign in to comment.