From c4a2a283ae75af9a615ec9712568e0b3471ac1f4 Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Fri, 14 Oct 2022 12:36:54 +0400 Subject: [PATCH] [#1883] cli/shards: Make supporting new modes easier Add new shard modes as a map entry to automatically parse them in `set-mode` command. The change also automatically adds new modes to help message. Signed-off-by: Leonard Lyubich --- cmd/neofs-cli/modules/control/shards_list.go | 17 +++----- .../modules/control/shards_set_mode.go | 43 +++++++++---------- 2 files changed, 27 insertions(+), 33 deletions(-) diff --git a/cmd/neofs-cli/modules/control/shards_list.go b/cmd/neofs-cli/modules/control/shards_list.go index 03243aac..fdb9c0e2 100644 --- a/cmd/neofs-cli/modules/control/shards_list.go +++ b/cmd/neofs-cli/modules/control/shards_list.go @@ -108,16 +108,11 @@ func prettyPrintShards(cmd *cobra.Command, ii []*control.ShardInfo) { } 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-read-write" - case control.ShardMode_DEGRADED_READ_ONLY: - return "degraded-read-only" - default: - return "unknown" + for strMode, mode := range mShardModes { + if mode == m { + return strMode + } } + + return "unknown" } diff --git a/cmd/neofs-cli/modules/control/shards_set_mode.go b/cmd/neofs-cli/modules/control/shards_set_mode.go index a39027e2..2d279fad 100644 --- a/cmd/neofs-cli/modules/control/shards_set_mode.go +++ b/cmd/neofs-cli/modules/control/shards_set_mode.go @@ -2,6 +2,7 @@ package control import ( "fmt" + "strings" "github.com/mr-tron/base58" rawclient "github.com/nspcc-dev/neofs-api-go/v2/rpc/client" @@ -16,13 +17,17 @@ const ( shardIDFlag = "id" shardAllFlag = "all" shardClearErrorsFlag = "clear-errors" - - shardModeReadOnly = "read-only" - shardModeReadWrite = "read-write" - shardModeDegraded = "degraded-read-write" - shardModeDegradedReadOnly = "degraded-read-only" ) +// maps string command input to control.ShardMode. To support new mode, it's +// enough to add the map entry. +var mShardModes = map[string]control.ShardMode{ + "read-only": control.ShardMode_READ_ONLY, + "read-write": control.ShardMode_READ_WRITE, + "degraded-read-write": control.ShardMode_DEGRADED, + "degraded-read-only": control.ShardMode_DEGRADED_READ_ONLY, +} + var setShardModeCmd = &cobra.Command{ Use: "set-mode", Short: "Set work mode of the shard", @@ -36,12 +41,14 @@ func initControlSetShardModeCmd() { flags := setShardModeCmd.Flags() flags.StringSlice(shardIDFlag, nil, "List of shard IDs in base58 encoding") flags.Bool(shardAllFlag, false, "Process all shards") + + modes := make([]string, 0, len(mShardModes)) + for strMode := range mShardModes { + modes = append(modes, "'"+strMode+"'") + } + flags.String(shardModeFlag, "", - fmt.Sprintf("New shard mode keyword ('%s', '%s', '%s')", - shardModeReadWrite, - shardModeReadOnly, - shardModeDegraded, - ), + fmt.Sprintf("New shard mode keyword (%s)", strings.Join(modes, ",")), ) flags.Bool(shardClearErrorsFlag, false, "Set shard error count to 0") @@ -51,19 +58,11 @@ func initControlSetShardModeCmd() { func setShardMode(cmd *cobra.Command, _ []string) { pk := key.Get(cmd) - var mode control.ShardMode + strMode, _ := cmd.Flags().GetString(shardModeFlag) - switch shardMode, _ := cmd.Flags().GetString(shardModeFlag); shardMode { - default: - common.ExitOnErr(cmd, "", fmt.Errorf("unsupported mode %s", shardMode)) - case shardModeReadWrite: - mode = control.ShardMode_READ_WRITE - case shardModeReadOnly: - mode = control.ShardMode_READ_ONLY - case shardModeDegraded: - mode = control.ShardMode_DEGRADED - case shardModeDegradedReadOnly: - mode = control.ShardMode_DEGRADED_READ_ONLY + mode, ok := mShardModes[strMode] + if !ok { + common.ExitOnErr(cmd, "", fmt.Errorf("unsupported mode %s", strMode)) } req := new(control.SetShardModeRequest)