[#1379] neofs-cli: Move client creation to internal/client

Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
This commit is contained in:
Evgenii Stratonikov 2022-05-18 13:16:04 +03:00 committed by fyrchik
parent cbc4ca800d
commit a198a4858c
4 changed files with 19 additions and 34 deletions

View file

@ -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 (

View file

@ -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) {

View file

@ -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

View file

@ -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)