[#113] cli: add "name" option for "get container" command #224

Merged
fyrchik merged 1 commit from aarifullin/frostfs-node:feature/113-get_cnr_by_name into master 2023-04-12 18:25:38 +00:00

View file

@ -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/commonflags"
"git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-cli/internal/key" "git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-cli/internal/key"
commonCmd "git.frostfs.info/TrueCloudLab/frostfs-node/cmd/internal/common" 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" "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/user"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -16,12 +17,14 @@ import (
const ( const (
flagListPrintAttr = "with-attr" flagListPrintAttr = "with-attr"
flagListContainerOwner = "owner" flagListContainerOwner = "owner"
flagListName = "name"
) )
// flag vars of list command. // flag vars of list command.
var ( var (
flagVarListPrintAttr bool flagVarListPrintAttr bool
flagVarListContainerOwner string flagVarListContainerOwner string
flagVarListName string
) )
var listContainersCmd = &cobra.Command{ var listContainersCmd = &cobra.Command{
@ -52,24 +55,33 @@ var listContainersCmd = &cobra.Command{
var prmGet internalclient.GetContainerPrm var prmGet internalclient.GetContainerPrm
prmGet.SetClient(cli) prmGet.SetClient(cli)
list := res.IDList() containerIDs := res.IDList()
for i := range list { for _, cnrID := range containerIDs {
cmd.Println(list[i].String()) if flagVarListName == "" && !flagVarListPrintAttr {
cmd.Println(cnrID.String())
acid-ant marked this conversation as resolved Outdated

It is not necessary to get container if flagListName is empty. Also it is possible to use result from here when flagVarListPrintAttr set to true.

It is not necessary to get container if `flagListName` is empty. Also it is possible to use result from here when `flagVarListPrintAttr` set to true.

It is not necessary to get container

I fixed this but anyway getting the container doesn't seem to burden the iteration :)

> It is not necessary to get container I fixed this but anyway getting the container doesn't seem to burden the iteration :)
continue
}
prmGet.SetContainer(cnrID)
res, err := internalclient.GetContainer(prmGet)
if err != nil {

I think !strings.Contains is too unexpected, let's stick with the exact name for now.

I think `!strings.Contains` is too unexpected, let's stick with the exact name for now.

OK

OK

This line doesn't match description: List containers only by the whole attribute name or only by the name part. Or did I miss something?

This line doesn't match description: `List containers only by the whole attribute name or only by the name part`. Or did I miss something?

Thanks! Forgot to fix after changes. Fixed

Thanks! Forgot to fix after changes. Fixed
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 { if flagVarListPrintAttr {
prmGet.SetContainer(list[i]) cnr.IterateAttributes(func(key, val string) {
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) { 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 // FIXME(@cthulhu-rider): neofs-sdk-go#314 use dedicated method to skip system attributes
cmd.Printf(" %s: %s\n", key, val) cmd.Printf(" %s: %s\n", key, val)
} }
}) })
} else {
cmd.Printf(" failed to read attributes: %v\n", err)
}
} }
} }
}, },
@ -80,6 +92,9 @@ func initContainerListContainersCmd() {
flags := listContainersCmd.Flags() flags := listContainersCmd.Flags()
flags.StringVar(&flagVarListName, flagListName, "",
"List containers by the attribute name",
)
flags.StringVar(&flagVarListContainerOwner, flagListContainerOwner, "", flags.StringVar(&flagVarListContainerOwner, flagListContainerOwner, "",
"Owner of containers (omit to use owner from private key)", "Owner of containers (omit to use owner from private key)",
) )