From 1805c6606dd681ee243582aa07038c291ea591dd Mon Sep 17 00:00:00 2001 From: Pavel Karpy Date: Tue, 18 Oct 2022 16:41:10 +0300 Subject: [PATCH] [#1332] cli: Add `tree list` Signed-off-by: Pavel Karpy --- cmd/neofs-cli/modules/tree/list.go | 63 ++++++++++++++++++++++++++++++ cmd/neofs-cli/modules/tree/root.go | 2 + 2 files changed, 65 insertions(+) create mode 100644 cmd/neofs-cli/modules/tree/list.go diff --git a/cmd/neofs-cli/modules/tree/list.go b/cmd/neofs-cli/modules/tree/list.go new file mode 100644 index 00000000..03500d15 --- /dev/null +++ b/cmd/neofs-cli/modules/tree/list.go @@ -0,0 +1,63 @@ +package tree + +import ( + "crypto/sha256" + + "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/spf13/cobra" +) + +var listCmd = &cobra.Command{ + Use: "list", + Short: "Get tree IDs", + Run: list, + PersistentPreRun: func(cmd *cobra.Command, _ []string) { + commonflags.Bind(cmd) + }, +} + +func initListCmd() { + commonflags.Init(listCmd) + + ff := listCmd.Flags() + ff.String(containerIDFlagKey, "", "Container ID") + _ = listCmd.MarkFlagRequired(containerIDFlagKey) + + _ = cobra.MarkFlagRequired(ff, commonflags.RPC) +} + +func list(cmd *cobra.Command, _ []string) { + pk := key.GetOrGenerate(cmd) + cidString, _ := cmd.Flags().GetString(containerIDFlagKey) + + var cnr cid.ID + err := cnr.DecodeString(cidString) + common.ExitOnErr(cmd, "decode container ID string: %w", err) + + ctx := cmd.Context() + + cli, err := _client(ctx) + common.ExitOnErr(cmd, "client: %w", err) + + rawCID := make([]byte, sha256.Size) + cnr.Encode(rawCID) + + req := &tree.TreeListRequest{ + Body: &tree.TreeListRequest_Body{ + ContainerId: rawCID, + }, + } + + common.ExitOnErr(cmd, "message signing: %w", tree.SignMessage(req, pk)) + + resp, err := cli.TreeList(ctx, req) + common.ExitOnErr(cmd, "rpc call: %w", err) + + for _, treeID := range resp.GetBody().GetIds() { + cmd.Println(treeID) + } +} diff --git a/cmd/neofs-cli/modules/tree/root.go b/cmd/neofs-cli/modules/tree/root.go index 70e9b326..dbd2a5ec 100644 --- a/cmd/neofs-cli/modules/tree/root.go +++ b/cmd/neofs-cli/modules/tree/root.go @@ -13,10 +13,12 @@ func init() { Cmd.AddCommand(addCmd) Cmd.AddCommand(getByPathCmd) Cmd.AddCommand(addByPathCmd) + Cmd.AddCommand(listCmd) initAddCmd() initGetByPathCmd() initAddByPathCmd() + initListCmd() } const (