forked from TrueCloudLab/frostfs-node
[#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 <leonard@nspcc.ru>
This commit is contained in:
parent
2740bf7ee4
commit
7a26b2a57a
1 changed files with 39 additions and 9 deletions
|
@ -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",
|
||||
)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue