[#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 <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2022-08-11 10:02:44 +04:00 committed by fyrchik
parent e07921fc46
commit 2740bf7ee4
2 changed files with 53 additions and 6 deletions

View file

@ -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",
)
}

View file

@ -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)