Dmitrii Stepanov
c516c7c5f4
All checks were successful
DCO action / DCO (pull_request) Successful in 3m45s
Build / Build Components (1.21) (pull_request) Successful in 5m18s
Build / Build Components (1.20) (pull_request) Successful in 5m28s
Tests and linters / Tests (1.20) (pull_request) Successful in 7m30s
Tests and linters / Tests (1.21) (pull_request) Successful in 7m42s
Tests and linters / Lint (pull_request) Successful in 8m25s
Vulncheck / Vulncheck (pull_request) Successful in 9m22s
Tests and linters / Staticcheck (pull_request) Successful in 10m57s
Tests and linters / Tests with -race (pull_request) Successful in 16m53s
Signed-off-by: Dmitrii Stepanov <d.stepanov@yadro.com>
107 lines
3.1 KiB
Go
107 lines
3.1 KiB
Go
package container
|
|
|
|
import (
|
|
"strings"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/container"
|
|
internalclient "git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-cli/internal/client"
|
|
"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"
|
|
)
|
|
|
|
// flags of list command.
|
|
const (
|
|
flagListPrintAttr = "with-attr"
|
|
flagListContainerOwner = "owner"
|
|
flagListName = "name"
|
|
)
|
|
|
|
// flag vars of list command.
|
|
var (
|
|
flagVarListPrintAttr bool
|
|
flagVarListContainerOwner string
|
|
flagVarListName string
|
|
)
|
|
|
|
var listContainersCmd = &cobra.Command{
|
|
Use: "list",
|
|
Short: "List all created containers",
|
|
Long: "List all created containers",
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
var idUser user.ID
|
|
|
|
key := key.GetOrGenerate(cmd)
|
|
|
|
if flagVarListContainerOwner == "" {
|
|
user.IDFromKey(&idUser, key.PublicKey)
|
|
} else {
|
|
err := idUser.DecodeString(flagVarListContainerOwner)
|
|
commonCmd.ExitOnErr(cmd, "invalid user ID: %w", err)
|
|
}
|
|
|
|
cli := internalclient.GetSDKClientByFlag(cmd, key, commonflags.RPC)
|
|
|
|
var prm internalclient.ListContainersPrm
|
|
prm.SetClient(cli)
|
|
prm.Account = idUser
|
|
|
|
res, err := internalclient.ListContainers(cmd.Context(), prm)
|
|
commonCmd.ExitOnErr(cmd, "rpc error: %w", err)
|
|
|
|
prmGet := internalclient.GetContainerPrm{
|
|
Client: cli,
|
|
}
|
|
|
|
containerIDs := res.SortedIDList()
|
|
for _, cnrID := range containerIDs {
|
|
if flagVarListName == "" && !flagVarListPrintAttr {
|
|
cmd.Println(cnrID.String())
|
|
continue
|
|
}
|
|
|
|
cnrID := cnrID
|
|
prmGet.ClientParams.ContainerID = &cnrID
|
|
res, err := internalclient.GetContainer(cmd.Context(), 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 {
|
|
cnr.IterateAttributes(func(key, val string) {
|
|
if !strings.HasPrefix(key, container.SysAttributePrefix) && !strings.HasPrefix(key, container.SysAttributePrefixNeoFS) {
|
|
// FIXME(@cthulhu-rider): https://git.frostfs.info/TrueCloudLab/frostfs-sdk-go/issues/97
|
|
// Use dedicated method to skip system attributes.
|
|
cmd.Printf(" %s: %s\n", key, val)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
},
|
|
}
|
|
|
|
func initContainerListContainersCmd() {
|
|
commonflags.Init(listContainersCmd)
|
|
|
|
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)",
|
|
)
|
|
flags.BoolVar(&flagVarListPrintAttr, flagListPrintAttr, false,
|
|
"Request and print attributes of each container",
|
|
)
|
|
}
|