From 7a26b2a57a26cbdcbe1608fc2d947f117db2c861 Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Thu, 11 Aug 2022 10:28:32 +0400 Subject: [PATCH] [#1649] cli: Add option to print attributes in `list` Define `--with-attr` flag of `container list` which makes the command to request and print user attributes for each found element. Signed-off-by: Leonard Lyubich --- cmd/neofs-cli/modules/container/list.go | 48 ++++++++++++++++++++----- 1 file changed, 39 insertions(+), 9 deletions(-) diff --git a/cmd/neofs-cli/modules/container/list.go b/cmd/neofs-cli/modules/container/list.go index f73abd44d..fbb80aace 100644 --- a/cmd/neofs-cli/modules/container/list.go +++ b/cmd/neofs-cli/modules/container/list.go @@ -1,15 +1,27 @@ package container import ( + "strings" + + "github.com/nspcc-dev/neofs-api-go/v2/container" internalclient "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/client" "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/common" "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/commonflags" "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/key" - cid "github.com/nspcc-dev/neofs-sdk-go/container/id" "github.com/nspcc-dev/neofs-sdk-go/user" "github.com/spf13/cobra" ) +// flags of list command +const ( + flagListPrintAttr = "with-attr" +) + +// flag vars of list command +var ( + flagVarListPrintAttr bool +) + var containerOwner string var listContainersCmd = &cobra.Command{ @@ -37,8 +49,29 @@ var listContainersCmd = &cobra.Command{ res, err := internalclient.ListContainers(prm) common.ExitOnErr(cmd, "rpc error: %w", err) - // print to stdout - prettyPrintContainerList(cmd, res.IDList()) + var prmGet internalclient.GetContainerPrm + prmGet.SetClient(cli) + + list := res.IDList() + for i := range list { + cmd.Println(list[i].String()) + + if flagVarListPrintAttr { + prmGet.SetContainer(list[i]) + + res, err := internalclient.GetContainer(prmGet) + if err == nil { + res.Container().IterateAttributes(func(key, val string) { + if !strings.HasPrefix(key, container.SysAttributePrefix) { + // FIXME(@cthulhu-rider): neofs-sdk-go#314 use dedicated method to skip system attributes + cmd.Printf(" %s: %s\n", key, val) + } + }) + } else { + cmd.Printf(" failed to read attributes: %v\n", err) + } + } + } }, } @@ -48,10 +81,7 @@ func initContainerListContainersCmd() { flags := listContainersCmd.Flags() flags.StringVar(&containerOwner, "owner", "", "owner of containers (omit to use owner from private key)") -} - -func prettyPrintContainerList(cmd *cobra.Command, list []cid.ID) { - for i := range list { - cmd.Println(list[i].String()) - } + flags.BoolVar(&flagVarListPrintAttr, flagListPrintAttr, false, + "request and print attributes of each container", + ) }