From d9b93b12c1536953cb3286753f115b4162f88c65 Mon Sep 17 00:00:00 2001 From: Ekaterina Lebedeva Date: Mon, 18 Sep 2023 10:05:17 +0300 Subject: [PATCH] [#333] Sort shards by shard_ID in cli output Signed-off-by: Ekaterina Lebedeva --- cmd/frostfs-cli/modules/control/shards_list.go | 14 ++++++++++++-- cmd/frostfs-cli/modules/control/shards_set_mode.go | 6 ++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/cmd/frostfs-cli/modules/control/shards_list.go b/cmd/frostfs-cli/modules/control/shards_list.go index ec515e6b..4d09667b 100644 --- a/cmd/frostfs-cli/modules/control/shards_list.go +++ b/cmd/frostfs-cli/modules/control/shards_list.go @@ -4,6 +4,7 @@ import ( "bytes" "encoding/json" "fmt" + "sort" "strings" rawclient "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/rpc/client" @@ -49,11 +50,14 @@ func listShards(cmd *cobra.Command, _ []string) { verifyResponse(cmd, resp.GetSignature(), resp.GetBody()) + shards := resp.GetBody().GetShards() + sortShardsByID(shards) + isJSON, _ := cmd.Flags().GetBool(commonflags.JSON) if isJSON { - prettyPrintShardsJSON(cmd, resp.GetBody().GetShards()) + prettyPrintShardsJSON(cmd, shards) } else { - prettyPrintShards(cmd, resp.GetBody().GetShards()) + prettyPrintShards(cmd, shards) } } @@ -115,3 +119,9 @@ func shardModeToString(m control.ShardMode) string { return "unknown" } + +func sortShardsByID(ii []*control.ShardInfo) { + sort.Slice(ii, func(i, j int) bool { + return bytes.Compare(ii[i].Shard_ID, ii[j].Shard_ID) < 0 + }) +} diff --git a/cmd/frostfs-cli/modules/control/shards_set_mode.go b/cmd/frostfs-cli/modules/control/shards_set_mode.go index 135c0efa..78f76896 100644 --- a/cmd/frostfs-cli/modules/control/shards_set_mode.go +++ b/cmd/frostfs-cli/modules/control/shards_set_mode.go @@ -1,7 +1,9 @@ package control import ( + "bytes" "fmt" + "sort" "strings" rawclient "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/rpc/client" @@ -167,5 +169,9 @@ func getShardIDList(cmd *cobra.Command) [][]byte { res = append(res, raw) } + sort.Slice(res, func(i, j int) bool { + return bytes.Compare(res[i], res[j]) < 0 + }) + return res }