From 2740bf7ee44e8b7805e656150ee3804190b0c5b3 Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Thu, 11 Aug 2022 10:02:44 +0400 Subject: [PATCH] [#1649] cli: Add option to print attributes in `list-objects` Define `--with-attr` flag of `container list-objects` which makes the command to request and print user attributes for each object from the container. Signed-off-by: Leonard Lyubich --- .../modules/container/list_objects.go | 58 +++++++++++++++++-- cmd/neofs-cli/modules/session/util.go | 1 + 2 files changed, 53 insertions(+), 6 deletions(-) diff --git a/cmd/neofs-cli/modules/container/list_objects.go b/cmd/neofs-cli/modules/container/list_objects.go index 1a42e1830..c8db5f843 100644 --- a/cmd/neofs-cli/modules/container/list_objects.go +++ b/cmd/neofs-cli/modules/container/list_objects.go @@ -1,6 +1,9 @@ package container import ( + "strings" + + v2object "github.com/nspcc-dev/neofs-api-go/v2/object" 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" @@ -8,9 +11,20 @@ import ( objectCli "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/modules/object" sessionCli "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/modules/session" "github.com/nspcc-dev/neofs-sdk-go/object" + oid "github.com/nspcc-dev/neofs-sdk-go/object/id" "github.com/spf13/cobra" ) +// flags of list-object command +const ( + flagListObjectPrintAttr = "with-attr" +) + +// flag vars of list-objects command +var ( + flagVarListObjectsPrintAttr bool +) + var listContainerObjectsCmd = &cobra.Command{ Use: "list-objects", Short: "List existing objects in container", @@ -23,19 +37,48 @@ var listContainerObjectsCmd = &cobra.Command{ pk := key.GetOrGenerate(cmd) - var prm internalclient.SearchObjectsPrm - sessionCli.Prepare(cmd, id, nil, pk, &prm) - objectCli.Prepare(cmd, &prm) - prm.SetContainerID(id) - prm.SetFilters(*filters) + var prmSearch internalclient.SearchObjectsPrm + var prmHead internalclient.HeadObjectPrm - res, err := internalclient.SearchObjects(prm) + if flagVarListObjectsPrintAttr { + sessionCli.Prepare(cmd, id, nil, pk, &prmSearch, &prmHead) + objectCli.Prepare(cmd, &prmSearch, &prmHead) + } else { + sessionCli.Prepare(cmd, id, nil, pk, &prmSearch) + objectCli.Prepare(cmd, &prmSearch) + } + + prmSearch.SetContainerID(id) + prmSearch.SetFilters(*filters) + + res, err := internalclient.SearchObjects(prmSearch) common.ExitOnErr(cmd, "rpc error: %w", err) objectIDs := res.IDList() for i := range objectIDs { cmd.Println(objectIDs[i].String()) + + if flagVarListObjectsPrintAttr { + var addr oid.Address + addr.SetContainer(id) + addr.SetObject(objectIDs[i]) + prmHead.SetAddress(addr) + + resHead, err := internalclient.HeadObject(prmHead) + if err == nil { + attrs := resHead.Header().Attributes() + for i := range attrs { + attrKey := attrs[i].Key() + if !strings.HasPrefix(attrKey, v2object.SysAttributePrefix) { + // FIXME(@cthulhu-rider): neofs-sdk-go#226 use dedicated method to skip system attributes + cmd.Printf(" %s: %s\n", attrKey, attrs[i].Value()) + } + } + } else { + cmd.Printf(" failed to read attributes: %v\n", err) + } + } } }, } @@ -47,4 +90,7 @@ func initContainerListObjectsCmd() { flags := listContainerObjectsCmd.Flags() flags.StringVar(&containerID, "cid", "", "container ID") + flags.BoolVar(&flagVarListObjectsPrintAttr, flagListObjectPrintAttr, false, + "request and print user attributes of each object", + ) } diff --git a/cmd/neofs-cli/modules/session/util.go b/cmd/neofs-cli/modules/session/util.go index 636c1bdd8..d9cda5f5a 100644 --- a/cmd/neofs-cli/modules/session/util.go +++ b/cmd/neofs-cli/modules/session/util.go @@ -41,6 +41,7 @@ func Prepare(cmd *cobra.Command, cnr cid.ID, obj *oid.ID, key *ecdsa.PrivateKey, } for i := range prms { + tok := tok switch prms[i].(type) { case *internalclient.GetObjectPrm: tok.ForVerb(session.VerbObjectGet)