From e288a0188da5773a97aa4da16ca8f1814f115d79 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Tue, 8 Feb 2022 13:41:49 +0300 Subject: [PATCH] [#749] neofs-adm: refactor password reading into a separate function Signed-off-by: Evgenii Stratonikov --- .../internal/modules/morph/generate_test.go | 2 +- cmd/neofs-adm/internal/modules/morph/group.go | 48 +++++++++---------- 2 files changed, 23 insertions(+), 27 deletions(-) diff --git a/cmd/neofs-adm/internal/modules/morph/generate_test.go b/cmd/neofs-adm/internal/modules/morph/generate_test.go index 8299a3588..7ff6e9caa 100644 --- a/cmd/neofs-adm/internal/modules/morph/generate_test.go +++ b/cmd/neofs-adm/internal/modules/morph/generate_test.go @@ -89,7 +89,7 @@ func TestGenerateAlphabet(t *testing.T) { } t.Run("check contract group wallet", func(t *testing.T) { - p := filepath.Join(walletDir, contractWalletName) + p := filepath.Join(walletDir, contractWalletFilename) w, err := wallet.NewWalletFromFile(p) require.NoError(t, err, "contract wallet doesn't exist") require.Equal(t, 1, len(w.Accounts), "contract wallet must have 1 accout") diff --git a/cmd/neofs-adm/internal/modules/morph/group.go b/cmd/neofs-adm/internal/modules/morph/group.go index c86883c49..59c6f6c56 100644 --- a/cmd/neofs-adm/internal/modules/morph/group.go +++ b/cmd/neofs-adm/internal/modules/morph/group.go @@ -16,25 +16,18 @@ import ( "github.com/spf13/viper" ) -const contractWalletName = "contract.json" +const ( + contractWalletFilename = "contract.json" + contractWalletPasswordKey = "credentials.contract" +) func initializeContractWallet(walletDir string) (*wallet.Wallet, error) { - var ( - password string - err error - ) - - if key := "credentials.contract"; viper.IsSet(key) { - password = viper.GetString(key) - } else { - prompt := "Password for contract wallet > " - password, err = input.ReadPassword(prompt) - if err != nil { - return nil, err - } + password, err := getPassword(contractWalletPasswordKey, "Password for contract wallet > ") + if err != nil { + return nil, err } - w, err := wallet.NewWallet(path.Join(walletDir, contractWalletName)) + w, err := wallet.NewWallet(path.Join(walletDir, contractWalletFilename)) if err != nil { return nil, err } @@ -58,7 +51,7 @@ func initializeContractWallet(walletDir string) (*wallet.Wallet, error) { } func openContractWallet(cmd *cobra.Command, walletDir string) (*wallet.Wallet, error) { - p := path.Join(walletDir, contractWalletName) + p := path.Join(walletDir, contractWalletFilename) w, err := wallet.NewWalletFromFile(p) if err != nil { if !os.IsNotExist(err) { @@ -66,22 +59,16 @@ func openContractWallet(cmd *cobra.Command, walletDir string) (*wallet.Wallet, e } cmd.Printf("Contract group wallet is missing, initialize at %s\n", - filepath.Join(walletDir, contractWalletName)) + filepath.Join(walletDir, contractWalletFilename)) w, err = initializeContractWallet(walletDir) if err != nil { return nil, err } } - var password string - if key := "credentials.contract"; viper.IsSet(key) { - password = viper.GetString(key) - } else { - prompt := "Password for contract wallet > " - password, err = input.ReadPassword(prompt) - if err != nil { - return nil, fmt.Errorf("can't fetch password: %w", err) - } + password, err := getPassword(contractWalletPasswordKey, "Password for contract wallet > ") + if err != nil { + return nil, err } for i := range w.Accounts { @@ -93,6 +80,15 @@ func openContractWallet(cmd *cobra.Command, walletDir string) (*wallet.Wallet, e return w, nil } +func getPassword(key string, promps string) (string, error) { + if viper.IsSet(key) { + return viper.GetString(key), nil + } + + prompt := "Password for contract wallet > " + return input.ReadPassword(prompt) +} + func (c *initializeContext) addManifestGroup(h util.Uint160, cs *contractState) error { priv := c.ContractWallet.Accounts[0].PrivateKey() pub := priv.PublicKey()