From 01c0c90a869ef9f77ed412bd5a26c539579dacf7 Mon Sep 17 00:00:00 2001 From: Airat Arifullin Date: Thu, 6 Apr 2023 18:22:59 +0300 Subject: [PATCH] [#113] cli: add "name" option for "get container" command * Make get container command filter out the container by attribute name Signed-off-by: Airat Arifullin a.arifullin@yadro.com --- cmd/frostfs-cli/modules/container/list.go | 47 +++++++++++++++-------- 1 file changed, 31 insertions(+), 16 deletions(-) diff --git a/cmd/frostfs-cli/modules/container/list.go b/cmd/frostfs-cli/modules/container/list.go index 9565748c3..33dd17943 100644 --- a/cmd/frostfs-cli/modules/container/list.go +++ b/cmd/frostfs-cli/modules/container/list.go @@ -8,6 +8,7 @@ import ( "git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-cli/internal/commonflags" "git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-cli/internal/key" commonCmd "git.frostfs.info/TrueCloudLab/frostfs-node/cmd/internal/common" + containerSDK "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container" "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/user" "github.com/spf13/cobra" ) @@ -16,12 +17,14 @@ import ( const ( flagListPrintAttr = "with-attr" flagListContainerOwner = "owner" + flagListName = "name" ) // flag vars of list command. var ( flagVarListPrintAttr bool flagVarListContainerOwner string + flagVarListName string ) var listContainersCmd = &cobra.Command{ @@ -52,24 +55,33 @@ var listContainersCmd = &cobra.Command{ var prmGet internalclient.GetContainerPrm prmGet.SetClient(cli) - list := res.IDList() - for i := range list { - cmd.Println(list[i].String()) + containerIDs := res.IDList() + for _, cnrID := range containerIDs { + if flagVarListName == "" && !flagVarListPrintAttr { + cmd.Println(cnrID.String()) + continue + } + + prmGet.SetContainer(cnrID) + res, err := internalclient.GetContainer(prmGet) + if err != nil { + cmd.Printf(" failed to read attributes: %v\n", err) + continue + } + + cnr := res.Container() + if cnrName := containerSDK.Name(cnr); flagVarListName != "" && cnrName != flagVarListName { + continue + } + cmd.Println(cnrID.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) && !strings.HasPrefix(key, container.SysAttributePrefixNeoFS) { - // 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) - } + cnr.IterateAttributes(func(key, val string) { + if !strings.HasPrefix(key, container.SysAttributePrefix) && !strings.HasPrefix(key, container.SysAttributePrefixNeoFS) { + // FIXME(@cthulhu-rider): neofs-sdk-go#314 use dedicated method to skip system attributes + cmd.Printf(" %s: %s\n", key, val) + } + }) } } }, @@ -80,6 +92,9 @@ func initContainerListContainersCmd() { flags := listContainersCmd.Flags() + flags.StringVar(&flagVarListName, flagListName, "", + "List containers by the attribute name", + ) flags.StringVar(&flagVarListContainerOwner, flagListContainerOwner, "", "Owner of containers (omit to use owner from private key)", )