[#1074] neofs-cli: Inline some of the functions

They are quite small, however will produce problems during subsequent refactoring.

Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
This commit is contained in:
Evgenii Stratonikov 2022-05-30 16:46:54 +03:00 committed by fyrchik
parent 9efec21d34
commit a46f585fb3
3 changed files with 15 additions and 40 deletions

View file

@ -114,12 +114,14 @@ var listContainersCmd = &cobra.Command{
if containerOwner == "" { if containerOwner == "" {
user.IDFromKey(&idUser, key.PublicKey) user.IDFromKey(&idUser, key.PublicKey)
} else { } 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) prm.SetAccount(idUser)
res, err := internalclient.ListContainers(prm) 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.SetNonceUUID(nonce)
cnr.SetSessionToken(tok) cnr.SetSessionToken(tok)
var ( cli := internalclient.GetSDKClientByFlag(cmd, key, commonflags.RPC)
putPrm internalclient.PutContainerPrm
getPrm internalclient.GetContainerPrm
)
prepareAPIClientWithKey(cmd, key, &putPrm, &getPrm) var putPrm internalclient.PutContainerPrm
putPrm.SetClient(cli)
putPrm.SetContainer(*cnr) putPrm.SetContainer(*cnr)
res, err := internalclient.PutContainer(putPrm) res, err := internalclient.PutContainer(putPrm)
@ -200,6 +200,8 @@ It will be stored in sidechain when inner ring will accepts it.`,
if containerAwait { if containerAwait {
cmd.Println("awaiting...") cmd.Println("awaiting...")
var getPrm internalclient.GetContainerPrm
getPrm.SetClient(cli)
getPrm.SetContainer(id) getPrm.SetContainer(id)
for i := 0; i < awaitTimeout; i++ { for i := 0; i < awaitTimeout; i++ {

View file

@ -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/commonflags"
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/key" "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/key"
sessionCli "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/modules/session" 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" "github.com/nspcc-dev/neofs-sdk-go/checksum"
cid "github.com/nspcc-dev/neofs-sdk-go/container/id" cid "github.com/nspcc-dev/neofs-sdk-go/container/id"
"github.com/nspcc-dev/neofs-sdk-go/object" "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 { type objectPrm interface {
bearerPrm SetBearerToken(prm *bearer.Token)
SetTTL(uint32) SetTTL(uint32)
SetXHeaders([]string) SetXHeaders([]string)
} }
func prepareObjectPrm(cmd *cobra.Command, prms ...objectPrm) { func prepareObjectPrm(cmd *cobra.Command, prms ...objectPrm) {
for i := range prms { for i := range prms {
prepareBearerPrm(cmd, prms[i]) btok := common.ReadBearerToken(cmd, bearerTokenFlag)
prms[i].SetBearerToken(btok)
prms[i].SetTTL(getTTL()) prms[i].SetTTL(getTTL())
prms[i].SetXHeaders(parseXHeaders(cmd)) prms[i].SetXHeaders(parseXHeaders(cmd))
} }

View file

@ -1,7 +1,6 @@
package cmd package cmd
import ( import (
"crypto/ecdsa"
"fmt" "fmt"
"os" "os"
"path/filepath" "path/filepath"
@ -20,9 +19,7 @@ import (
utilCli "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/modules/util" 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/misc"
"github.com/nspcc-dev/neofs-node/pkg/util/gendoc" "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/client"
"github.com/nspcc-dev/neofs-sdk-go/user"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper" "github.com/spf13/viper"
) )
@ -132,29 +129,13 @@ type clientWithKey interface {
// reads private key from command args and call prepareAPIClientWithKey with it. // reads private key from command args and call prepareAPIClientWithKey with it.
func prepareAPIClient(cmd *cobra.Command, dst ...clientWithKey) { func prepareAPIClient(cmd *cobra.Command, dst ...clientWithKey) {
p := key.GetOrGenerate(cmd) p := key.GetOrGenerate(cmd)
cli := internalclient.GetSDKClientByFlag(cmd, p, commonflags.RPC)
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)
for _, d := range dst { for _, d := range dst {
d.SetClient(cli) 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 { func getTTL() uint32 {
ttl := viper.GetUint32(commonflags.TTL) ttl := viper.GetUint32(commonflags.TTL)
common.PrintVerbose("TTL: %d", ttl) common.PrintVerbose("TTL: %d", ttl)
@ -162,16 +143,6 @@ func getTTL() uint32 {
return ttl 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 { func parseXHeaders(cmd *cobra.Command) []string {
xHeaders, _ := cmd.Flags().GetStringSlice(commonflags.XHeadersKey) xHeaders, _ := cmd.Flags().GetStringSlice(commonflags.XHeadersKey)
xs := make([]string, 0, 2*len(xHeaders)) xs := make([]string, 0, 2*len(xHeaders))