[#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"
"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-sdk-go/client"
"github.com/spf13/cobra"
"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) {
// 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
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))
}
cli, err := internalclient.GetSDKClientByFlag(pk, commonflags.RPC)
common.ExitOnErr(cmd, "create API client: %w", err)
cli := internalclient.GetSDKClientByFlag(cmd, pk, commonflags.RPC)
var prm internalclient.BalanceOfPrm
prm.SetClient(cli)

View file

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

View file

@ -338,8 +338,7 @@ func prepareSessionPrmWithOwner(
ownerID *user.ID,
prms ...clientKeySession,
) {
cli, err := internalclient.GetSDKClientByFlag(key, commonflags.RPC)
common.ExitOnErr(cmd, "create API client: %w", err)
cli := internalclient.GetSDKClientByFlag(cmd, key, commonflags.RPC)
var sessionToken *session.Token
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)
}
} else {
var err error
sessionToken, err = sessionCli.CreateSession(cli, ownerID, sessionTokenLifetime)
common.ExitOnErr(cmd, "", err)
}
@ -387,7 +387,7 @@ func prepareSessionPrmWithOwner(
tok.SetIat(sessionToken.Iat())
tok.SetNbf(sessionToken.Nbf())
err = tok.Sign(key)
err := tok.Sign(key)
common.ExitOnErr(cmd, "session token signing: %w", err)
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.
func prepareAPIClientWithKey(cmd *cobra.Command, key *ecdsa.PrivateKey, dst ...clientWithKey) {
cli, err := internalclient.GetSDKClientByFlag(key, commonflags.RPC)
common.ExitOnErr(cmd, "create API client: %w", err)
cli := internalclient.GetSDKClientByFlag(cmd, key, commonflags.RPC)
for _, d := range dst {
d.SetClient(cli)