From cd46a7478ef4000e9748ee378eb13256293554be Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Wed, 18 May 2022 13:38:45 +0300 Subject: [PATCH] [#1379] neofs-cli: Bind key-related arguments to viper Signed-off-by: Evgenii Stratonikov --- cmd/neofs-cli/internal/key/raw.go | 10 +++++++--- cmd/neofs-cli/modules/root.go | 2 +- cmd/neofs-cli/modules/session/create.go | 9 ++++++--- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/cmd/neofs-cli/internal/key/raw.go b/cmd/neofs-cli/internal/key/raw.go index 52f72ba56..15ee4b255 100644 --- a/cmd/neofs-cli/internal/key/raw.go +++ b/cmd/neofs-cli/internal/key/raw.go @@ -7,16 +7,20 @@ import ( "github.com/nspcc-dev/neo-go/pkg/crypto/keys" "github.com/nspcc-dev/neo-go/pkg/wallet" + "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/commonflags" + "github.com/spf13/viper" ) -// Get returns private key from the followind sources: +// Get returns private key from the following sources: // 1. WIF // 2. Raw binary key // 3. Wallet file // 4. NEP-2 encrypted WIF. // Ideally we want to touch file-system on the last step. // However, asking for NEP-2 password seems to be confusing if we provide a wallet. -func Get(keyDesc string, address string) (*ecdsa.PrivateKey, error) { +// This function assumes that all flags were bind to viper in a `PersistentPreRun`. +func Get() (*ecdsa.PrivateKey, error) { + keyDesc := viper.GetString(commonflags.WalletPath) priv, err := keys.NewPrivateKeyFromWIF(keyDesc) if err == nil { return &priv.PrivateKey, nil @@ -29,7 +33,7 @@ func Get(keyDesc string, address string) (*ecdsa.PrivateKey, error) { w, err := wallet.NewWalletFromFile(keyDesc) if err == nil { - return FromWallet(w, address) + return FromWallet(w, viper.GetString(commonflags.Account)) } if len(keyDesc) == nep2Base58Length { diff --git a/cmd/neofs-cli/modules/root.go b/cmd/neofs-cli/modules/root.go index eeea81d9d..7bbc45c9a 100644 --- a/cmd/neofs-cli/modules/root.go +++ b/cmd/neofs-cli/modules/root.go @@ -151,7 +151,7 @@ func getKey() (*ecdsa.PrivateKey, error) { } func getKeyNoGenerate() (*ecdsa.PrivateKey, error) { - return key.Get(viper.GetString(commonflags.WalletPath), viper.GetString(commonflags.Account)) + return key.Get() } type clientWithKey interface { diff --git a/cmd/neofs-cli/modules/session/create.go b/cmd/neofs-cli/modules/session/create.go index 6db71a013..27d2923fa 100644 --- a/cmd/neofs-cli/modules/session/create.go +++ b/cmd/neofs-cli/modules/session/create.go @@ -12,6 +12,7 @@ import ( "github.com/nspcc-dev/neofs-sdk-go/owner" "github.com/nspcc-dev/neofs-sdk-go/session" "github.com/spf13/cobra" + "github.com/spf13/viper" ) const ( @@ -26,6 +27,10 @@ var createCmd = &cobra.Command{ Use: "create", Short: "Create session token", RunE: createSession, + PersistentPreRun: func(cmd *cobra.Command, _ []string) { + _ = viper.BindPFlag(commonflags.WalletPath, cmd.Flags().Lookup(commonflags.WalletPath)) + _ = viper.BindPFlag(commonflags.Account, cmd.Flags().Lookup(commonflags.Account)) + }, } func init() { @@ -43,9 +48,7 @@ func init() { } func createSession(cmd *cobra.Command, _ []string) error { - walletPath, _ := cmd.Flags().GetString(commonflags.WalletPath) - accPath, _ := cmd.Flags().GetString(commonflags.Account) - privKey, err := key.Get(walletPath, accPath) + privKey, err := key.Get() if err != nil { return err }