From a198a4858ccb59db1b9509c82b81b74b7fec9885 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Wed, 18 May 2022 13:16:04 +0300 Subject: [PATCH] [#1379] neofs-cli: Move client creation to `internal/client` Signed-off-by: Evgenii Stratonikov --- cmd/neofs-cli/internal/client/sdk.go | 15 +++++++++++++ cmd/neofs-cli/modules/control.go | 3 ++- cmd/neofs-cli/modules/object.go | 2 +- cmd/neofs-cli/modules/root.go | 33 +--------------------------- 4 files changed, 19 insertions(+), 34 deletions(-) diff --git a/cmd/neofs-cli/internal/client/sdk.go b/cmd/neofs-cli/internal/client/sdk.go index 9f7457820..08991d083 100644 --- a/cmd/neofs-cli/internal/client/sdk.go +++ b/cmd/neofs-cli/internal/client/sdk.go @@ -2,12 +2,27 @@ package internal import ( "crypto/ecdsa" + "errors" "fmt" "github.com/nspcc-dev/neofs-node/pkg/network" "github.com/nspcc-dev/neofs-sdk-go/client" + "github.com/spf13/viper" ) +var errInvalidEndpoint = errors.New("provided RPC endpoint is incorrect") + +// GetSDKClientByFlag returns default neofs-sdk-go client using the specified flag for the address. +func GetSDKClientByFlag(key *ecdsa.PrivateKey, endpointFlag string) (*client.Client, error) { + var addr network.Address + + err := addr.FromString(viper.GetString(endpointFlag)) + if err != nil { + return nil, fmt.Errorf("%v: %w", errInvalidEndpoint, err) + } + return GetSDKClient(key, addr) +} + // GetSDKClient returns default neofs-sdk-go client. func GetSDKClient(key *ecdsa.PrivateKey, addr network.Address) (*client.Client, error) { var ( diff --git a/cmd/neofs-cli/modules/control.go b/cmd/neofs-cli/modules/control.go index d0ba89808..8426b224a 100644 --- a/cmd/neofs-cli/modules/control.go +++ b/cmd/neofs-cli/modules/control.go @@ -8,6 +8,7 @@ import ( "github.com/mr-tron/base58" "github.com/nspcc-dev/neofs-api-go/v2/refs" rawclient "github.com/nspcc-dev/neofs-api-go/v2/rpc/client" + internalclient "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/client" "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/commonflags" "github.com/nspcc-dev/neofs-node/pkg/services/control" ircontrol "github.com/nspcc-dev/neofs-node/pkg/services/control/ir" @@ -444,7 +445,7 @@ func listShards(cmd *cobra.Command, _ []string) { // getControlSDKClient calls getSDKClientFlag with "endpoint" flag. func getControlSDKClient(key *ecdsa.PrivateKey) (*client.Client, error) { - return getSDKClientFlag(key, controlRPC) + return internalclient.GetSDKClientByFlag(key, controlRPC) } func prettyPrintShards(cmd *cobra.Command, ii []*control.ShardInfo) { diff --git a/cmd/neofs-cli/modules/object.go b/cmd/neofs-cli/modules/object.go index ad53cb0c7..e4a2175f0 100644 --- a/cmd/neofs-cli/modules/object.go +++ b/cmd/neofs-cli/modules/object.go @@ -337,7 +337,7 @@ func prepareSessionPrmWithOwner( ownerID *owner.ID, prms ...clientKeySession, ) { - cli, err := getSDKClient(key) + cli, err := internalclient.GetSDKClientByFlag(key, commonflags.RPC) exitOnErr(cmd, errf("create API client: %w", err)) var sessionToken *session.Token diff --git a/cmd/neofs-cli/modules/root.go b/cmd/neofs-cli/modules/root.go index 016e2c468..eeea81d9d 100644 --- a/cmd/neofs-cli/modules/root.go +++ b/cmd/neofs-cli/modules/root.go @@ -17,7 +17,6 @@ import ( bearerCli "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/modules/bearer" sessionCli "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/modules/session" "github.com/nspcc-dev/neofs-node/misc" - "github.com/nspcc-dev/neofs-node/pkg/network" "github.com/nspcc-dev/neofs-sdk-go/bearer" "github.com/nspcc-dev/neofs-sdk-go/client" "github.com/nspcc-dev/neofs-sdk-go/owner" @@ -64,7 +63,6 @@ and much more!`, } var ( - errInvalidEndpoint = errors.New("provided RPC endpoint is incorrect") errCantGenerateKey = errors.New("can't generate new private key") ) @@ -156,19 +154,6 @@ func getKeyNoGenerate() (*ecdsa.PrivateKey, error) { return key.Get(viper.GetString(commonflags.WalletPath), viper.GetString(commonflags.Account)) } -// getEndpointAddress returns network address structure that stores multiaddr -// inside, parsed from global arguments. -func getEndpointAddress(endpointFlag string) (addr network.Address, err error) { - endpoint := viper.GetString(endpointFlag) - - err = addr.FromString(endpoint) - if err != nil { - err = fmt.Errorf("%v: %w", errInvalidEndpoint, err) - } - - return -} - type clientWithKey interface { SetClient(*client.Client) } @@ -183,7 +168,7 @@ func prepareAPIClient(cmd *cobra.Command, dst ...clientWithKey) { // creates NeoFS API client and writes it to target along with the private key. func prepareAPIClientWithKey(cmd *cobra.Command, key *ecdsa.PrivateKey, dst ...clientWithKey) { - cli, err := getSDKClient(key) + cli, err := internalclient.GetSDKClientByFlag(key, commonflags.RPC) exitOnErr(cmd, errf("create API client: %w", err)) for _, d := range dst { @@ -202,22 +187,6 @@ func prepareBearerPrm(cmd *cobra.Command, prm bearerPrm) { prm.SetBearerToken(btok) } -// getSDKClient calls getSDKGClientFlag with "rpc-endpoint" flag. -func getSDKClient(key *ecdsa.PrivateKey) (*client.Client, error) { - return getSDKClientFlag(key, commonflags.RPC) -} - -// getSDKClientFlag returns NeoFS API client connection to the network address -// set by the given flag. -func getSDKClientFlag(key *ecdsa.PrivateKey, endpointFlag string) (*client.Client, error) { - netAddr, err := getEndpointAddress(endpointFlag) - if err != nil { - return nil, err - } - - return internalclient.GetSDKClient(key, netAddr) -} - func getTTL() uint32 { ttl := viper.GetUint32(ttl) printVerbose("TTL: %d", ttl)