From a46f585fb3b928efebfd68908a475e901e53c86c Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Mon, 30 May 2022 16:46:54 +0300 Subject: [PATCH] [#1074] neofs-cli: Inline some of the functions They are quite small, however will produce problems during subsequent refactoring. Signed-off-by: Evgenii Stratonikov --- cmd/neofs-cli/modules/container.go | 18 +++++++++-------- cmd/neofs-cli/modules/object.go | 6 ++++-- cmd/neofs-cli/modules/root.go | 31 +----------------------------- 3 files changed, 15 insertions(+), 40 deletions(-) diff --git a/cmd/neofs-cli/modules/container.go b/cmd/neofs-cli/modules/container.go index 1341d8ffb..142b454c5 100644 --- a/cmd/neofs-cli/modules/container.go +++ b/cmd/neofs-cli/modules/container.go @@ -114,12 +114,14 @@ var listContainersCmd = &cobra.Command{ if containerOwner == "" { user.IDFromKey(&idUser, key.PublicKey) } else { - common.ExitOnErr(cmd, "", userFromString(&idUser, containerOwner)) + err := idUser.DecodeString(containerOwner) + common.ExitOnErr(cmd, "invalid user ID: %w", err) } - var prm internalclient.ListContainersPrm + cli := internalclient.GetSDKClientByFlag(cmd, key, commonflags.RPC) - prepareAPIClientWithKey(cmd, key, &prm) + var prm internalclient.ListContainersPrm + prm.SetClient(cli) prm.SetAccount(idUser) res, err := internalclient.ListContainers(prm) @@ -182,12 +184,10 @@ It will be stored in sidechain when inner ring will accepts it.`, cnr.SetNonceUUID(nonce) cnr.SetSessionToken(tok) - var ( - putPrm internalclient.PutContainerPrm - getPrm internalclient.GetContainerPrm - ) + cli := internalclient.GetSDKClientByFlag(cmd, key, commonflags.RPC) - prepareAPIClientWithKey(cmd, key, &putPrm, &getPrm) + var putPrm internalclient.PutContainerPrm + putPrm.SetClient(cli) putPrm.SetContainer(*cnr) res, err := internalclient.PutContainer(putPrm) @@ -200,6 +200,8 @@ It will be stored in sidechain when inner ring will accepts it.`, if containerAwait { cmd.Println("awaiting...") + var getPrm internalclient.GetContainerPrm + getPrm.SetClient(cli) getPrm.SetContainer(id) for i := 0; i < awaitTimeout; i++ { diff --git a/cmd/neofs-cli/modules/object.go b/cmd/neofs-cli/modules/object.go index 885c73318..313f27317 100644 --- a/cmd/neofs-cli/modules/object.go +++ b/cmd/neofs-cli/modules/object.go @@ -21,6 +21,7 @@ import ( "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/commonflags" "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/key" sessionCli "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/modules/session" + "github.com/nspcc-dev/neofs-sdk-go/bearer" "github.com/nspcc-dev/neofs-sdk-go/checksum" cid "github.com/nspcc-dev/neofs-sdk-go/container/id" "github.com/nspcc-dev/neofs-sdk-go/object" @@ -313,15 +314,16 @@ func prepareSessionPrm(cmd *cobra.Command, cnr cid.ID, obj *oid.ID, prms ...sess } type objectPrm interface { - bearerPrm + SetBearerToken(prm *bearer.Token) SetTTL(uint32) SetXHeaders([]string) } func prepareObjectPrm(cmd *cobra.Command, prms ...objectPrm) { for i := range prms { - prepareBearerPrm(cmd, prms[i]) + btok := common.ReadBearerToken(cmd, bearerTokenFlag) + prms[i].SetBearerToken(btok) prms[i].SetTTL(getTTL()) prms[i].SetXHeaders(parseXHeaders(cmd)) } diff --git a/cmd/neofs-cli/modules/root.go b/cmd/neofs-cli/modules/root.go index 4ec7ae0cc..39a83214c 100644 --- a/cmd/neofs-cli/modules/root.go +++ b/cmd/neofs-cli/modules/root.go @@ -1,7 +1,6 @@ package cmd import ( - "crypto/ecdsa" "fmt" "os" "path/filepath" @@ -20,9 +19,7 @@ import ( utilCli "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/modules/util" "github.com/nspcc-dev/neofs-node/misc" "github.com/nspcc-dev/neofs-node/pkg/util/gendoc" - "github.com/nspcc-dev/neofs-sdk-go/bearer" "github.com/nspcc-dev/neofs-sdk-go/client" - "github.com/nspcc-dev/neofs-sdk-go/user" "github.com/spf13/cobra" "github.com/spf13/viper" ) @@ -132,29 +129,13 @@ type clientWithKey interface { // reads private key from command args and call prepareAPIClientWithKey with it. func prepareAPIClient(cmd *cobra.Command, dst ...clientWithKey) { p := key.GetOrGenerate(cmd) - - prepareAPIClientWithKey(cmd, p, dst...) -} - -// 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 := internalclient.GetSDKClientByFlag(cmd, key, commonflags.RPC) + cli := internalclient.GetSDKClientByFlag(cmd, p, commonflags.RPC) for _, d := range dst { d.SetClient(cli) } } -type bearerPrm interface { - SetBearerToken(prm *bearer.Token) -} - -func prepareBearerPrm(cmd *cobra.Command, prm bearerPrm) { - btok := common.ReadBearerToken(cmd, bearerTokenFlag) - - prm.SetBearerToken(btok) -} - func getTTL() uint32 { ttl := viper.GetUint32(commonflags.TTL) common.PrintVerbose("TTL: %d", ttl) @@ -162,16 +143,6 @@ func getTTL() uint32 { return ttl } -// userFromString decodes user ID from string input. -func userFromString(id *user.ID, s string) error { - err := id.DecodeString(s) - if err != nil { - return fmt.Errorf("invalid user ID: %w", err) - } - - return nil -} - func parseXHeaders(cmd *cobra.Command) []string { xHeaders, _ := cmd.Flags().GetStringSlice(commonflags.XHeadersKey) xs := make([]string, 0, 2*len(xHeaders))