2022-06-02 12:24:31 +00:00
|
|
|
package container
|
|
|
|
|
|
|
|
import (
|
2022-08-11 06:02:44 +00:00
|
|
|
"strings"
|
|
|
|
|
2023-03-07 13:38:26 +00:00
|
|
|
v2object "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/object"
|
|
|
|
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"
|
|
|
|
objectCli "git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-cli/modules/object"
|
|
|
|
commonCmd "git.frostfs.info/TrueCloudLab/frostfs-node/cmd/internal/common"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object"
|
|
|
|
oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
|
2022-06-02 12:24:31 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
2022-10-17 12:03:55 +00:00
|
|
|
// flags of list-object command.
|
2022-08-11 06:02:44 +00:00
|
|
|
const (
|
|
|
|
flagListObjectPrintAttr = "with-attr"
|
|
|
|
)
|
|
|
|
|
2022-10-17 12:03:55 +00:00
|
|
|
// flag vars of list-objects command.
|
2022-08-11 06:02:44 +00:00
|
|
|
var (
|
|
|
|
flagVarListObjectsPrintAttr bool
|
|
|
|
)
|
|
|
|
|
2022-06-02 12:24:31 +00:00
|
|
|
var listContainerObjectsCmd = &cobra.Command{
|
|
|
|
Use: "list-objects",
|
|
|
|
Short: "List existing objects in container",
|
|
|
|
Long: `List existing objects in container`,
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
id := parseContainerID(cmd)
|
|
|
|
|
|
|
|
filters := new(object.SearchFilters)
|
|
|
|
filters.AddRootFilter() // search only user created objects
|
|
|
|
|
2022-10-20 09:40:33 +00:00
|
|
|
cli := internalclient.GetSDKClientByFlag(cmd, key.GetOrGenerate(cmd), commonflags.RPC)
|
2022-06-02 12:24:31 +00:00
|
|
|
|
2022-08-11 06:02:44 +00:00
|
|
|
var prmSearch internalclient.SearchObjectsPrm
|
|
|
|
var prmHead internalclient.HeadObjectPrm
|
2022-06-02 12:24:31 +00:00
|
|
|
|
2022-10-20 09:40:33 +00:00
|
|
|
prmSearch.SetClient(cli)
|
|
|
|
|
2022-08-11 06:02:44 +00:00
|
|
|
if flagVarListObjectsPrintAttr {
|
2022-10-20 09:40:33 +00:00
|
|
|
prmHead.SetClient(cli)
|
2022-08-11 06:02:44 +00:00
|
|
|
objectCli.Prepare(cmd, &prmSearch, &prmHead)
|
|
|
|
} else {
|
|
|
|
objectCli.Prepare(cmd, &prmSearch)
|
|
|
|
}
|
|
|
|
|
|
|
|
prmSearch.SetContainerID(id)
|
|
|
|
prmSearch.SetFilters(*filters)
|
|
|
|
|
2023-05-24 13:51:57 +00:00
|
|
|
res, err := internalclient.SearchObjects(cmd.Context(), prmSearch)
|
2023-01-16 09:20:16 +00:00
|
|
|
commonCmd.ExitOnErr(cmd, "rpc error: %w", err)
|
2022-06-02 12:24:31 +00:00
|
|
|
|
|
|
|
objectIDs := res.IDList()
|
|
|
|
|
|
|
|
for i := range objectIDs {
|
|
|
|
cmd.Println(objectIDs[i].String())
|
2022-08-11 06:02:44 +00:00
|
|
|
|
|
|
|
if flagVarListObjectsPrintAttr {
|
|
|
|
var addr oid.Address
|
|
|
|
addr.SetContainer(id)
|
|
|
|
addr.SetObject(objectIDs[i])
|
|
|
|
prmHead.SetAddress(addr)
|
|
|
|
|
2023-05-24 13:51:57 +00:00
|
|
|
resHead, err := internalclient.HeadObject(cmd.Context(), prmHead)
|
2022-08-11 06:02:44 +00:00
|
|
|
if err == nil {
|
|
|
|
attrs := resHead.Header().Attributes()
|
|
|
|
for i := range attrs {
|
|
|
|
attrKey := attrs[i].Key()
|
2023-03-06 13:11:42 +00:00
|
|
|
if !strings.HasPrefix(attrKey, v2object.SysAttributePrefix) && !strings.HasPrefix(attrKey, v2object.SysAttributePrefixNeoFS) {
|
2022-08-11 06:02:44 +00:00
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
}
|
2022-06-02 12:24:31 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func initContainerListObjectsCmd() {
|
|
|
|
commonflags.Init(listContainerObjectsCmd)
|
2022-06-24 09:25:17 +00:00
|
|
|
objectCli.InitBearer(listContainerObjectsCmd)
|
2022-06-02 12:24:31 +00:00
|
|
|
|
|
|
|
flags := listContainerObjectsCmd.Flags()
|
|
|
|
|
2022-10-18 11:43:04 +00:00
|
|
|
flags.StringVar(&containerID, commonflags.CIDFlag, "", commonflags.CIDFlagUsage)
|
2022-08-11 06:02:44 +00:00
|
|
|
flags.BoolVar(&flagVarListObjectsPrintAttr, flagListObjectPrintAttr, false,
|
2022-10-11 14:02:03 +00:00
|
|
|
"Request and print user attributes of each object",
|
2022-08-11 06:02:44 +00:00
|
|
|
)
|
2022-06-02 12:24:31 +00:00
|
|
|
}
|