[#1216] neofs-cli: Reuse GetSDKClient between CLI modules

Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
This commit is contained in:
Evgenii Stratonikov 2022-03-28 12:36:04 +03:00 committed by LeL
parent fd8c00400d
commit 006d6e8b48
3 changed files with 42 additions and 41 deletions

View file

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

View file

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

View file

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