From 006d6e8b48b947c0383de6fa0de9b69976a0e783 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Mon, 28 Mar 2022 12:36:04 +0300 Subject: [PATCH] [#1216] neofs-cli: Reuse `GetSDKClient` between CLI modules Signed-off-by: Evgenii Stratonikov --- cmd/neofs-cli/internal/client/sdk.go | 35 ++++++++++++++++++++++++++ cmd/neofs-cli/modules/bearer/create.go | 23 ++++------------- cmd/neofs-cli/modules/root.go | 25 ++---------------- 3 files changed, 42 insertions(+), 41 deletions(-) create mode 100644 cmd/neofs-cli/internal/client/sdk.go diff --git a/cmd/neofs-cli/internal/client/sdk.go b/cmd/neofs-cli/internal/client/sdk.go new file mode 100644 index 000000000..ab254a7d4 --- /dev/null +++ b/cmd/neofs-cli/internal/client/sdk.go @@ -0,0 +1,35 @@ +package internal + +import ( + "crypto/ecdsa" + "crypto/tls" + "fmt" + + "github.com/nspcc-dev/neofs-node/pkg/network" + "github.com/nspcc-dev/neofs-sdk-go/client" +) + +// GetSDKClient returns default neofs-sdk-go client. +func GetSDKClient(key *ecdsa.PrivateKey, addr network.Address) (*client.Client, error) { + var ( + c client.Client + prmInit client.PrmInit + prmDial client.PrmDial + ) + + prmInit.SetDefaultPrivateKey(*key) + prmInit.ResolveNeoFSFailures() + prmDial.SetServerURI(addr.HostAddr()) + + if addr.TLSEnabled() { + prmDial.SetTLSConfig(&tls.Config{}) + } + + c.Init(prmInit) + + if err := c.Dial(prmDial); err != nil { + return nil, fmt.Errorf("can't init SDK client: %w", err) + } + + return &c, nil +} diff --git a/cmd/neofs-cli/modules/bearer/create.go b/cmd/neofs-cli/modules/bearer/create.go index 472da3170..4f9f8a69a 100644 --- a/cmd/neofs-cli/modules/bearer/create.go +++ b/cmd/neofs-cli/modules/bearer/create.go @@ -5,13 +5,13 @@ import ( "crypto/ecdsa" "crypto/elliptic" "crypto/rand" - "crypto/tls" "encoding/json" "fmt" "io/ioutil" "strconv" "time" + internalclient "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/client" "github.com/nspcc-dev/neofs-node/pkg/network" "github.com/nspcc-dev/neofs-sdk-go/client" eaclSDK "github.com/nspcc-dev/neofs-sdk-go/eacl" @@ -160,12 +160,7 @@ func parseEpoch(cmd *cobra.Command, flag string) (uint64, bool, error) { // getCurrentEpoch returns current epoch. func getCurrentEpoch(endpoint string) (uint64, error) { - var ( - c client.Client - prmInit client.PrmInit - prmDial client.PrmDial - addr network.Address - ) + var addr network.Address if err := addr.FromString(endpoint); err != nil { return 0, fmt.Errorf("can't parse RPC endpoint: %w", err) @@ -176,17 +171,9 @@ func getCurrentEpoch(endpoint string) (uint64, error) { return 0, fmt.Errorf("can't generate key to sign query: %w", err) } - prmInit.SetDefaultPrivateKey(*key) - prmInit.ResolveNeoFSFailures() - c.Init(prmInit) - - prmDial.SetServerURI(addr.HostAddr()) - if addr.TLSEnabled() { - prmDial.SetTLSConfig(&tls.Config{}) - } - - if err := c.Dial(prmDial); err != nil { - return 0, fmt.Errorf("can't initialize client: %w", err) + c, err := internalclient.GetSDKClient(key, addr) + if err != nil { + return 0, err } ctx, cancel := context.WithTimeout(context.Background(), time.Second*30) diff --git a/cmd/neofs-cli/modules/root.go b/cmd/neofs-cli/modules/root.go index e1307a845..e87b3dfdc 100644 --- a/cmd/neofs-cli/modules/root.go +++ b/cmd/neofs-cli/modules/root.go @@ -2,7 +2,6 @@ package cmd import ( "crypto/ecdsa" - "crypto/tls" "errors" "fmt" "os" @@ -15,6 +14,7 @@ import ( "github.com/nspcc-dev/neo-go/pkg/crypto/keys" "github.com/nspcc-dev/neo-go/pkg/util" "github.com/nspcc-dev/neo-go/pkg/wallet" + internalclient "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/client" "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/modules/acl" bearerCli "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/modules/bearer" "github.com/nspcc-dev/neofs-node/misc" @@ -341,32 +341,11 @@ func prepareBearerPrm(cmd *cobra.Command, prm bearerPrm) { // getSDKClient returns default neofs-api-go sdk client. Consider using // opts... to provide TTL or other global configuration flags. func getSDKClient(key *ecdsa.PrivateKey) (*client.Client, error) { - var ( - c client.Client - prmInit client.PrmInit - prmDial client.PrmDial - ) - netAddr, err := getEndpointAddress(rpc) if err != nil { return nil, err } - - prmInit.SetDefaultPrivateKey(*key) - prmInit.ResolveNeoFSFailures() - prmDial.SetServerURI(netAddr.HostAddr()) - - if netAddr.TLSEnabled() { - prmDial.SetTLSConfig(&tls.Config{}) - } - - c.Init(prmInit) - err = c.Dial(prmDial) - if err != nil { - return nil, fmt.Errorf("coult not init api client:%w", err) - } - - return &c, err + return internalclient.GetSDKClient(key, netAddr) } func getTTL() uint32 {