From 3d8349d7f84eb1ca9aac05840b0a19001f325f9c Mon Sep 17 00:00:00 2001 From: Pavel Karpy Date: Mon, 10 Oct 2022 21:55:48 +0300 Subject: [PATCH] [#1332] cli: Implement `tree get-by-path` command It is `TreeService.GetNodeByPath` method implementation. Signed-off-by: Pavel Karpy --- cmd/neofs-cli/modules/tree/get_by_path.go | 98 +++++++++++++++++++++++ cmd/neofs-cli/modules/tree/root.go | 7 ++ 2 files changed, 105 insertions(+) create mode 100644 cmd/neofs-cli/modules/tree/get_by_path.go diff --git a/cmd/neofs-cli/modules/tree/get_by_path.go b/cmd/neofs-cli/modules/tree/get_by_path.go new file mode 100644 index 00000000..9c54986b --- /dev/null +++ b/cmd/neofs-cli/modules/tree/get_by_path.go @@ -0,0 +1,98 @@ +package tree + +import ( + "crypto/sha256" + "strings" + + "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/common" + "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/commonflags" + "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/key" + "github.com/nspcc-dev/neofs-node/pkg/services/tree" + cid "github.com/nspcc-dev/neofs-sdk-go/container/id" + "github.com/nspcc-dev/neofs-sdk-go/object" + "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) + + cidRaw, _ := cmd.Flags().GetString(containerIDFlagKey) + + var cnr cid.ID + err := cnr.DecodeString(cidRaw) + common.ExitOnErr(cmd, "decode container ID string: %w", err) + + tid, _ := cmd.Flags().GetString(treeIDFlagKey) + ctx := cmd.Context() + + cli, err := _client(ctx) + common.ExitOnErr(cmd, "client: %w", err) + + 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 + } + + common.ExitOnErr(cmd, "message signing: %w", tree.SignMessage(req, pk)) + + resp, err := cli.GetNodeByPath(ctx, req) + common.ExitOnErr(cmd, "rpc call: %w", err) + + nn := resp.GetBody().GetNodes() + if len(nn) == 0 { + cmd.Println("The node is not found") + 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())) + } + } +} diff --git a/cmd/neofs-cli/modules/tree/root.go b/cmd/neofs-cli/modules/tree/root.go index 48ee8c86..81f86e0a 100644 --- a/cmd/neofs-cli/modules/tree/root.go +++ b/cmd/neofs-cli/modules/tree/root.go @@ -11,8 +11,10 @@ var Cmd = &cobra.Command{ func init() { Cmd.AddCommand(addCmd) + Cmd.AddCommand(getByPathCmd) initAddCmd() + initGetByPathCmd() } const ( @@ -21,6 +23,11 @@ const ( parentIDFlagKey = "pid" metaFlagKey = "meta" + + pathFlagKey = "path" + pathAttributeFlagKey = "pattr" + + latestOnlyFlagKey = "latest" ) func initCTID(cmd *cobra.Command) {