2022-10-10 18:55:48 +00:00
|
|
|
package tree
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/sha256"
|
|
|
|
"strings"
|
|
|
|
|
2022-12-23 17:35:35 +00:00
|
|
|
"github.com/TrueCloudLab/frostfs-node/cmd/frostfs-cli/internal/common"
|
|
|
|
"github.com/TrueCloudLab/frostfs-node/cmd/frostfs-cli/internal/commonflags"
|
|
|
|
"github.com/TrueCloudLab/frostfs-node/cmd/frostfs-cli/internal/key"
|
2023-01-16 09:20:16 +00:00
|
|
|
commonCmd "github.com/TrueCloudLab/frostfs-node/cmd/internal/common"
|
2022-12-23 17:35:35 +00:00
|
|
|
"github.com/TrueCloudLab/frostfs-node/pkg/services/tree"
|
|
|
|
cid "github.com/TrueCloudLab/frostfs-sdk-go/container/id"
|
|
|
|
"github.com/TrueCloudLab/frostfs-sdk-go/object"
|
2022-10-10 18:55:48 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
var getByPathCmd = &cobra.Command{
|
|
|
|
Use: "get-by-path",
|
|
|
|
Short: "Get a node by its path",
|
|
|
|
Run: getByPath,
|
|
|
|
PersistentPreRun: func(cmd *cobra.Command, _ []string) {
|
|
|
|
commonflags.Bind(cmd)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func initGetByPathCmd() {
|
|
|
|
commonflags.Init(getByPathCmd)
|
|
|
|
initCTID(getByPathCmd)
|
|
|
|
|
|
|
|
ff := getByPathCmd.Flags()
|
|
|
|
|
|
|
|
// tree service does not allow any attribute except
|
|
|
|
// the 'FileName' but that's a limitation of the
|
|
|
|
// current implementation, not the rule
|
|
|
|
//ff.String(pathAttributeFlagKey, "", "Path attribute")
|
|
|
|
ff.String(pathFlagKey, "", "Path to a node")
|
|
|
|
|
|
|
|
ff.Bool(latestOnlyFlagKey, false, "Look only for the latest version of a node")
|
|
|
|
|
|
|
|
_ = cobra.MarkFlagRequired(ff, commonflags.RPC)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getByPath(cmd *cobra.Command, _ []string) {
|
|
|
|
pk := key.GetOrGenerate(cmd)
|
|
|
|
|
2022-10-18 11:43:04 +00:00
|
|
|
cidRaw, _ := cmd.Flags().GetString(commonflags.CIDFlag)
|
2022-10-10 18:55:48 +00:00
|
|
|
|
|
|
|
var cnr cid.ID
|
|
|
|
err := cnr.DecodeString(cidRaw)
|
2023-01-16 09:20:16 +00:00
|
|
|
commonCmd.ExitOnErr(cmd, "decode container ID string: %w", err)
|
2022-10-10 18:55:48 +00:00
|
|
|
|
|
|
|
tid, _ := cmd.Flags().GetString(treeIDFlagKey)
|
|
|
|
ctx := cmd.Context()
|
|
|
|
|
|
|
|
cli, err := _client(ctx)
|
2023-01-16 09:20:16 +00:00
|
|
|
commonCmd.ExitOnErr(cmd, "client: %w", err)
|
2022-10-10 18:55:48 +00:00
|
|
|
|
|
|
|
rawCID := make([]byte, sha256.Size)
|
|
|
|
cnr.Encode(rawCID)
|
|
|
|
|
|
|
|
latestOnly, _ := cmd.Flags().GetBool(latestOnlyFlagKey)
|
|
|
|
path, _ := cmd.Flags().GetString(pathFlagKey)
|
|
|
|
//pAttr, _ := cmd.Flags().GetString(pathAttributeFlagKey)
|
|
|
|
|
|
|
|
req := new(tree.GetNodeByPathRequest)
|
|
|
|
req.Body = &tree.GetNodeByPathRequest_Body{
|
|
|
|
ContainerId: rawCID,
|
|
|
|
TreeId: tid,
|
|
|
|
PathAttribute: object.AttributeFileName,
|
|
|
|
//PathAttribute: pAttr,
|
|
|
|
Path: strings.Split(path, "/"),
|
|
|
|
LatestOnly: latestOnly,
|
|
|
|
AllAttributes: true,
|
|
|
|
BearerToken: nil, // TODO: #1891 add token handling
|
|
|
|
}
|
|
|
|
|
2023-01-16 09:20:16 +00:00
|
|
|
commonCmd.ExitOnErr(cmd, "message signing: %w", tree.SignMessage(req, pk))
|
2022-10-10 18:55:48 +00:00
|
|
|
|
|
|
|
resp, err := cli.GetNodeByPath(ctx, req)
|
2023-01-16 09:20:16 +00:00
|
|
|
commonCmd.ExitOnErr(cmd, "rpc call: %w", err)
|
2022-10-10 18:55:48 +00:00
|
|
|
|
|
|
|
nn := resp.GetBody().GetNodes()
|
|
|
|
if len(nn) == 0 {
|
2022-12-27 09:36:30 +00:00
|
|
|
common.PrintVerbose(cmd, "The node is not found")
|
2022-10-10 18:55:48 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, n := range nn {
|
|
|
|
cmd.Printf("%d:\n", n.GetNodeId())
|
|
|
|
|
|
|
|
cmd.Println("\tParent ID: ", n.GetParentId())
|
|
|
|
cmd.Println("\tTimestamp: ", n.GetTimestamp())
|
|
|
|
|
|
|
|
cmd.Println("\tMeta pairs: ")
|
|
|
|
for _, kv := range n.GetMeta() {
|
|
|
|
cmd.Printf("\t\t%s: %s\n", kv.GetKey(), string(kv.GetValue()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|