From 2b5998b8202548c5152d0cb971a4d26c0da212e8 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Tue, 15 Jun 2021 18:43:09 +0300 Subject: [PATCH] [#610] neofs-cli: add `--generate-key` flag Signed-off-by: Evgenii Stratonikov --- cmd/neofs-cli/modules/root.go | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/cmd/neofs-cli/modules/root.go b/cmd/neofs-cli/modules/root.go index 446828824..b4f28a058 100644 --- a/cmd/neofs-cli/modules/root.go +++ b/cmd/neofs-cli/modules/root.go @@ -24,8 +24,6 @@ import ( const ( envPrefix = "NEOFS_CLI" - generateKeyConst = "new" - ttlDefaultValue = 2 ) @@ -79,7 +77,11 @@ func init() { rootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "config file (default is $HOME/.config/neofs-cli/config.yaml)") - rootCmd.PersistentFlags().StringP("key", "k", "", "private key in hex, WIF, NEP-2 or filepath (use `--key new` to generate key for request)") + // Key options. + rootCmd.PersistentFlags().BoolP("generate-key", "", false, "generate new private key") + _ = viper.BindPFlag("generate-key", rootCmd.PersistentFlags().Lookup("generate-key")) + + rootCmd.PersistentFlags().StringP("key", "k", "", "private key in hex, WIF, NEP-2 or filepath") _ = viper.BindPFlag("key", rootCmd.PersistentFlags().Lookup("key")) rootCmd.PersistentFlags().StringP("address", "", "", "address of wallet account") @@ -133,8 +135,7 @@ const nep2Base58Length = 58 // getKey returns private key that was provided in global arguments. func getKey() (*ecdsa.PrivateKey, error) { - privateKey := viper.GetString("key") - if privateKey == generateKeyConst { + if viper.GetBool("generate-key") { priv, err := keys.NewPrivateKey() if err != nil { return nil, errCantGenerateKey @@ -142,6 +143,7 @@ func getKey() (*ecdsa.PrivateKey, error) { return &priv.PrivateKey, nil } + privateKey := viper.GetString("key") w, err := wallet.NewWalletFromFile(privateKey) if err == nil { return getKeyFromWallet(w, viper.GetString("address"))