[#1424] neofs-cli: Fail immediately if a client can't be created

Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
This commit is contained in:
Evgenii Stratonikov 2022-05-24 11:38:45 +03:00 committed by LeL
parent 295ec3700a
commit 6cb9c13c5e
5 changed files with 18 additions and 11 deletions

View file

@ -5,15 +5,26 @@ import (
"errors" "errors"
"fmt" "fmt"
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/common"
"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"
"github.com/spf13/cobra"
"github.com/spf13/viper" "github.com/spf13/viper"
) )
var errInvalidEndpoint = errors.New("provided RPC endpoint is incorrect") var errInvalidEndpoint = errors.New("provided RPC endpoint is incorrect")
// GetSDKClientByFlag returns default neofs-sdk-go client using the specified flag for the address. // GetSDKClientByFlag returns default neofs-sdk-go client using the specified flag for the address.
func GetSDKClientByFlag(key *ecdsa.PrivateKey, endpointFlag string) (*client.Client, error) { // On error, outputs to stderr of cmd and exits with non-zero code.
func GetSDKClientByFlag(cmd *cobra.Command, key *ecdsa.PrivateKey, endpointFlag string) *client.Client {
cli, err := getSDKClientByFlag(key, endpointFlag)
if err != nil {
common.ExitOnErr(cmd, "can't create API client: %w", err)
}
return cli
}
func getSDKClientByFlag(key *ecdsa.PrivateKey, endpointFlag string) (*client.Client, error) {
var addr network.Address var addr network.Address
err := addr.FromString(viper.GetString(endpointFlag)) err := addr.FromString(viper.GetString(endpointFlag))

View file

@ -35,8 +35,7 @@ var accountingBalanceCmd = &cobra.Command{
common.ExitOnErr(cmd, "can't decode owner ID wallet address: %w", oid.DecodeString(balanceOwner)) common.ExitOnErr(cmd, "can't decode owner ID wallet address: %w", oid.DecodeString(balanceOwner))
} }
cli, err := internalclient.GetSDKClientByFlag(pk, commonflags.RPC) cli := internalclient.GetSDKClientByFlag(cmd, pk, commonflags.RPC)
common.ExitOnErr(cmd, "create API client: %w", err)
var prm internalclient.BalanceOfPrm var prm internalclient.BalanceOfPrm
prm.SetClient(cli) prm.SetClient(cli)

View file

@ -49,7 +49,5 @@ func verifyResponse(cmd *cobra.Command,
} }
func getClient(cmd *cobra.Command, pk *ecdsa.PrivateKey) *client.Client { func getClient(cmd *cobra.Command, pk *ecdsa.PrivateKey) *client.Client {
cli, err := internalclient.GetSDKClientByFlag(pk, controlRPC) return internalclient.GetSDKClientByFlag(cmd, pk, controlRPC)
common.ExitOnErr(cmd, "", err)
return cli
} }

View file

@ -338,8 +338,7 @@ func prepareSessionPrmWithOwner(
ownerID *user.ID, ownerID *user.ID,
prms ...clientKeySession, prms ...clientKeySession,
) { ) {
cli, err := internalclient.GetSDKClientByFlag(key, commonflags.RPC) cli := internalclient.GetSDKClientByFlag(cmd, key, commonflags.RPC)
common.ExitOnErr(cmd, "create API client: %w", err)
var sessionToken *session.Token var sessionToken *session.Token
if tokenPath, _ := cmd.Flags().GetString(sessionTokenFlag); len(tokenPath) != 0 { if tokenPath, _ := cmd.Flags().GetString(sessionTokenFlag); len(tokenPath) != 0 {
@ -352,6 +351,7 @@ func prepareSessionPrmWithOwner(
common.ExitOnErr(cmd, "can't unmarshal session token: %w", err) common.ExitOnErr(cmd, "can't unmarshal session token: %w", err)
} }
} else { } else {
var err error
sessionToken, err = sessionCli.CreateSession(cli, ownerID, sessionTokenLifetime) sessionToken, err = sessionCli.CreateSession(cli, ownerID, sessionTokenLifetime)
common.ExitOnErr(cmd, "", err) common.ExitOnErr(cmd, "", err)
} }
@ -387,7 +387,7 @@ func prepareSessionPrmWithOwner(
tok.SetIat(sessionToken.Iat()) tok.SetIat(sessionToken.Iat())
tok.SetNbf(sessionToken.Nbf()) tok.SetNbf(sessionToken.Nbf())
err = tok.Sign(key) err := tok.Sign(key)
common.ExitOnErr(cmd, "session token signing: %w", err) common.ExitOnErr(cmd, "session token signing: %w", err)
prms[i].SetClient(cli) prms[i].SetClient(cli)

View file

@ -152,8 +152,7 @@ func prepareAPIClient(cmd *cobra.Command, dst ...clientWithKey) {
// creates NeoFS API client and writes it to target along with the private key. // creates NeoFS API client and writes it to target along with the private key.
func prepareAPIClientWithKey(cmd *cobra.Command, key *ecdsa.PrivateKey, dst ...clientWithKey) { func prepareAPIClientWithKey(cmd *cobra.Command, key *ecdsa.PrivateKey, dst ...clientWithKey) {
cli, err := internalclient.GetSDKClientByFlag(key, commonflags.RPC) cli := internalclient.GetSDKClientByFlag(cmd, key, commonflags.RPC)
common.ExitOnErr(cmd, "create API client: %w", err)
for _, d := range dst { for _, d := range dst {
d.SetClient(cli) d.SetClient(cli)