From edba186aa3175a69566d7ccb0bba50964f68a21d Mon Sep 17 00:00:00 2001 From: zakir <80246097+zakir-code@users.noreply.github.com> Date: Mon, 31 Jul 2023 17:48:58 +0800 Subject: [PATCH] refactor(keys): remove duplicate `--home` flag (#17197) --- client/keys/add_ledger_test.go | 6 +++--- client/keys/add_test.go | 16 ++++++++-------- client/keys/delete_test.go | 10 +++++----- client/keys/export_test.go | 4 ++-- client/keys/import_test.go | 2 +- client/keys/list_test.go | 6 +++--- client/keys/migrate_test.go | 4 ++-- client/keys/rename_test.go | 10 +++++----- client/keys/root.go | 3 +-- client/keys/root_test.go | 2 +- client/keys/show_test.go | 18 +++++++++--------- simapp/simd/cmd/root.go | 2 +- simapp/simd/cmd/root_v2.go | 2 +- 13 files changed, 42 insertions(+), 43 deletions(-) diff --git a/client/keys/add_ledger_test.go b/client/keys/add_ledger_test.go index 289200e3fe29..797c0452dfbb 100644 --- a/client/keys/add_ledger_test.go +++ b/client/keys/add_ledger_test.go @@ -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() @@ -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() @@ -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) diff --git a/client/keys/add_test.go b/client/keys/add_test.go index c69c1b81bec0..719473aa88e8 100644 --- a/client/keys/add_test.go +++ b/client/keys/add_test.go @@ -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() @@ -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), @@ -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), @@ -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), @@ -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), @@ -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) @@ -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) @@ -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), diff --git a/client/keys/delete_test.go b/client/keys/delete_test.go index 49f897aece41..29246b1095dd 100644 --- a/client/keys/delete_test.go +++ b/client/keys/delete_test.go @@ -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) @@ -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) @@ -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() @@ -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), }) @@ -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), }) diff --git a/client/keys/export_test.go b/client/keys/export_test.go index 9edd5aedf39a..d7e4126f6a7c 100644 --- a/client/keys/export_test.go +++ b/client/keys/export_test.go @@ -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) diff --git a/client/keys/import_test.go b/client/keys/import_test.go index d9b9d0fee79f..a64d0ee898f1 100644 --- a/client/keys/import_test.go +++ b/client/keys/import_test.go @@ -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 diff --git a/client/keys/list_test.go b/client/keys/list_test.go index 354ea1b12a29..38602db2d267 100644 --- a/client/keys/list_test.go +++ b/client/keys/list_test.go @@ -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() @@ -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), }) @@ -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), }) diff --git a/client/keys/migrate_test.go b/client/keys/migrate_test.go index 8878f61755c0..bacbbacc7419 100644 --- a/client/keys/migrate_test.go +++ b/client/keys/migrate_test.go @@ -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) @@ -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), }) diff --git a/client/keys/rename_test.go b/client/keys/rename_test.go index 40d7d579330b..8df5d641ba05 100644 --- a/client/keys/rename_test.go +++ b/client/keys/rename_test.go @@ -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) @@ -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") @@ -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() @@ -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), }) @@ -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), }) diff --git a/client/keys/root.go b/client/keys/root.go index 92fd3ae2e339..4cadecbc1815 100644 --- a/client/keys/root.go +++ b/client/keys/root.go @@ -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", @@ -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()) diff --git a/client/keys/root_test.go b/client/keys/root_test.go index 20b3f1a23472..bb204a474994 100644 --- a/client/keys/root_test.go +++ b/client/keys/root_test.go @@ -7,7 +7,7 @@ import ( ) func TestCommands(t *testing.T) { - rootCommands := Commands("home") + rootCommands := Commands() assert.Assert(t, rootCommands != nil) // Commands are registered diff --git a/client/keys/show_test.go b/client/keys/show_test.go index 7a180c1a10c5..59d3b2a3247c 100644 --- a/client/keys/show_test.go +++ b/client/keys/show_test.go @@ -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() @@ -87,7 +87,7 @@ 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), }) @@ -95,7 +95,7 @@ func Test_runShowCmd(t *testing.T) { 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), }) @@ -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), }) @@ -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), @@ -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), @@ -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), @@ -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), @@ -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), diff --git a/simapp/simd/cmd/root.go b/simapp/simd/cmd/root.go index a392ec87216f..70f1cf9f2af1 100644 --- a/simapp/simd/cmd/root.go +++ b/simapp/simd/cmd/root.go @@ -209,7 +209,7 @@ func initRootCmd(rootCmd *cobra.Command, encodingConfig params.EncodingConfig, b genesisCommand(encodingConfig, basicManager), txCommand(), queryCommand(), - keys.Commands(simapp.DefaultNodeHome), + keys.Commands(), ) } diff --git a/simapp/simd/cmd/root_v2.go b/simapp/simd/cmd/root_v2.go index 72f5dff29d73..bdad9390bc23 100644 --- a/simapp/simd/cmd/root_v2.go +++ b/simapp/simd/cmd/root_v2.go @@ -225,7 +225,7 @@ func initRootCmd( genesisCommand(txConfig, basicManager), queryCommand(), txCommand(), - keys.Commands(simapp.DefaultNodeHome), + keys.Commands(), ) }