diff --git a/cmd/neofs-cli/internal/client/sdk.go b/cmd/neofs-cli/internal/client/sdk.go index 08991d0835..e3acac6556 100644 --- a/cmd/neofs-cli/internal/client/sdk.go +++ b/cmd/neofs-cli/internal/client/sdk.go @@ -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)) diff --git a/cmd/neofs-cli/modules/accounting/balance.go b/cmd/neofs-cli/modules/accounting/balance.go index 8a4b25e143..e4048b7f3f 100644 --- a/cmd/neofs-cli/modules/accounting/balance.go +++ b/cmd/neofs-cli/modules/accounting/balance.go @@ -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) diff --git a/cmd/neofs-cli/modules/control/util.go b/cmd/neofs-cli/modules/control/util.go index 724c10f572..5f22ae8fde 100644 --- a/cmd/neofs-cli/modules/control/util.go +++ b/cmd/neofs-cli/modules/control/util.go @@ -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) } diff --git a/cmd/neofs-cli/modules/object.go b/cmd/neofs-cli/modules/object.go index b509e174fb..9f81a41062 100644 --- a/cmd/neofs-cli/modules/object.go +++ b/cmd/neofs-cli/modules/object.go @@ -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) diff --git a/cmd/neofs-cli/modules/root.go b/cmd/neofs-cli/modules/root.go index 36e9421a5c..e0fce317b1 100644 --- a/cmd/neofs-cli/modules/root.go +++ b/cmd/neofs-cli/modules/root.go @@ -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)