From 189507dc89958daab0e04d5674a0b1f69f15056d Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Thu, 23 Jun 2022 17:21:43 +0300 Subject: [PATCH] [#1541] neofs-cli: allow to print `shards list` output in JSON Eventually more parameters will be supported (#1390) and after blobstor configuration refactoring the output will certainly change. Implement the simplest approach now. Marshaling the result directly results in too ugly names and they cannot be easily customized. Marshaling the results via `jsonpb` is better but is not that flexible in terms of what we want to output. Signed-off-by: Evgenii Stratonikov --- cmd/neofs-cli/modules/control/shards_list.go | 59 +++++++++++++++----- 1 file changed, 44 insertions(+), 15 deletions(-) diff --git a/cmd/neofs-cli/modules/control/shards_list.go b/cmd/neofs-cli/modules/control/shards_list.go index 8a4a550e0..7244ebd0d 100644 --- a/cmd/neofs-cli/modules/control/shards_list.go +++ b/cmd/neofs-cli/modules/control/shards_list.go @@ -1,6 +1,8 @@ package control import ( + "bytes" + "encoding/json" "fmt" "github.com/mr-tron/base58" @@ -25,6 +27,7 @@ func initControlShardsListCmd() { flags := listShardsCmd.Flags() flags.String(controlRPC, controlRPCDefault, controlRPCUsage) + flags.Bool(commonflags.JSON, false, "Print shard info as a JSON array") } func listShards(cmd *cobra.Command, _ []string) { @@ -47,24 +50,37 @@ func listShards(cmd *cobra.Command, _ []string) { verifyResponse(cmd, resp.GetSignature(), resp.GetBody()) - prettyPrintShards(cmd, resp.GetBody().GetShards()) + isJSON, _ := cmd.Flags().GetBool(commonflags.JSON) + if isJSON { + prettyPrintShardsJSON(cmd, resp.GetBody().GetShards()) + } else { + prettyPrintShards(cmd, resp.GetBody().GetShards()) + } +} + +func prettyPrintShardsJSON(cmd *cobra.Command, ii []*control.ShardInfo) { + out := make([]map[string]interface{}, 0, len(ii)) + for _, i := range ii { + out = append(out, map[string]interface{}{ + "shard_id": base58.Encode(i.Shard_ID), + "mode": shardModeToString(i.GetMode()), + "metabase": i.GetMetabasePath(), + "blobstor": i.GetBlobstorPath(), + "writecache": i.GetWritecachePath(), + "error_count": i.GetErrorCount(), + }) + } + + buf := bytes.NewBuffer(nil) + enc := json.NewEncoder(buf) + enc.SetIndent("", " ") + common.ExitOnErr(cmd, "cannot shard info to JSON: %w", enc.Encode(out)) + + cmd.Print(buf.String()) // pretty printer emits newline, to no need for Println } func prettyPrintShards(cmd *cobra.Command, ii []*control.ShardInfo) { for _, i := range ii { - var mode string - - switch i.GetMode() { - case control.ShardMode_READ_WRITE: - mode = "read-write" - case control.ShardMode_READ_ONLY: - mode = "read-only" - case control.ShardMode_DEGRADED: - mode = "degraded" - default: - mode = "unknown" - } - pathPrinter := func(name, path string) string { if path == "" { return "" @@ -79,7 +95,20 @@ func prettyPrintShards(cmd *cobra.Command, ii []*control.ShardInfo) { pathPrinter("Write-cache", i.GetWritecachePath())+ fmt.Sprintf("Error count: %d\n", i.GetErrorCount()), base58.Encode(i.Shard_ID), - mode, + shardModeToString(i.GetMode()), ) } } + +func shardModeToString(m control.ShardMode) string { + switch m { + case control.ShardMode_READ_WRITE: + return "read-write" + case control.ShardMode_READ_ONLY: + return "read-only" + case control.ShardMode_DEGRADED: + return "degraded" + default: + return "unknown" + } +}