[#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/ecdsa"
"crypto/elliptic" "crypto/elliptic"
"crypto/rand" "crypto/rand"
"crypto/tls"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"strconv" "strconv"
"time" "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-node/pkg/network"
"github.com/nspcc-dev/neofs-sdk-go/client" "github.com/nspcc-dev/neofs-sdk-go/client"
eaclSDK "github.com/nspcc-dev/neofs-sdk-go/eacl" 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. // getCurrentEpoch returns current epoch.
func getCurrentEpoch(endpoint string) (uint64, error) { func getCurrentEpoch(endpoint string) (uint64, error) {
var ( var addr network.Address
c client.Client
prmInit client.PrmInit
prmDial client.PrmDial
addr network.Address
)
if err := addr.FromString(endpoint); err != nil { if err := addr.FromString(endpoint); err != nil {
return 0, fmt.Errorf("can't parse RPC endpoint: %w", err) 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) return 0, fmt.Errorf("can't generate key to sign query: %w", err)
} }
prmInit.SetDefaultPrivateKey(*key) c, err := internalclient.GetSDKClient(key, addr)
prmInit.ResolveNeoFSFailures() if err != nil {
c.Init(prmInit) return 0, err
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)
} }
ctx, cancel := context.WithTimeout(context.Background(), time.Second*30) ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)

View file

@ -2,7 +2,6 @@ package cmd
import ( import (
"crypto/ecdsa" "crypto/ecdsa"
"crypto/tls"
"errors" "errors"
"fmt" "fmt"
"os" "os"
@ -15,6 +14,7 @@ import (
"github.com/nspcc-dev/neo-go/pkg/crypto/keys" "github.com/nspcc-dev/neo-go/pkg/crypto/keys"
"github.com/nspcc-dev/neo-go/pkg/util" "github.com/nspcc-dev/neo-go/pkg/util"
"github.com/nspcc-dev/neo-go/pkg/wallet" "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" "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/modules/acl"
bearerCli "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/modules/bearer" bearerCli "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/modules/bearer"
"github.com/nspcc-dev/neofs-node/misc" "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 // getSDKClient returns default neofs-api-go sdk client. Consider using
// opts... to provide TTL or other global configuration flags. // opts... to provide TTL or other global configuration flags.
func getSDKClient(key *ecdsa.PrivateKey) (*client.Client, error) { func getSDKClient(key *ecdsa.PrivateKey) (*client.Client, error) {
var (
c client.Client
prmInit client.PrmInit
prmDial client.PrmDial
)
netAddr, err := getEndpointAddress(rpc) netAddr, err := getEndpointAddress(rpc)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return internalclient.GetSDKClient(key, netAddr)
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
} }
func getTTL() uint32 { func getTTL() uint32 {