[#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 <ctulhurider@gmail.com>
This commit is contained in:
Leonard Lyubich 2022-10-14 12:36:54 +04:00 committed by fyrchik
parent 3b939d190c
commit c4a2a283ae
2 changed files with 27 additions and 33 deletions

View file

@ -108,16 +108,11 @@ func prettyPrintShards(cmd *cobra.Command, ii []*control.ShardInfo) {
} }
func shardModeToString(m control.ShardMode) string { func shardModeToString(m control.ShardMode) string {
switch m { for strMode, mode := range mShardModes {
case control.ShardMode_READ_WRITE: if mode == m {
return "read-write" return strMode
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"
} }
}
return "unknown"
} }

View file

@ -2,6 +2,7 @@ package control
import ( import (
"fmt" "fmt"
"strings"
"github.com/mr-tron/base58" "github.com/mr-tron/base58"
rawclient "github.com/nspcc-dev/neofs-api-go/v2/rpc/client" rawclient "github.com/nspcc-dev/neofs-api-go/v2/rpc/client"
@ -16,13 +17,17 @@ const (
shardIDFlag = "id" shardIDFlag = "id"
shardAllFlag = "all" shardAllFlag = "all"
shardClearErrorsFlag = "clear-errors" 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{ var setShardModeCmd = &cobra.Command{
Use: "set-mode", Use: "set-mode",
Short: "Set work mode of the shard", Short: "Set work mode of the shard",
@ -36,12 +41,14 @@ func initControlSetShardModeCmd() {
flags := setShardModeCmd.Flags() flags := setShardModeCmd.Flags()
flags.StringSlice(shardIDFlag, nil, "List of shard IDs in base58 encoding") flags.StringSlice(shardIDFlag, nil, "List of shard IDs in base58 encoding")
flags.Bool(shardAllFlag, false, "Process all shards") flags.Bool(shardAllFlag, false, "Process all shards")
modes := make([]string, 0, len(mShardModes))
for strMode := range mShardModes {
modes = append(modes, "'"+strMode+"'")
}
flags.String(shardModeFlag, "", flags.String(shardModeFlag, "",
fmt.Sprintf("New shard mode keyword ('%s', '%s', '%s')", fmt.Sprintf("New shard mode keyword (%s)", strings.Join(modes, ",")),
shardModeReadWrite,
shardModeReadOnly,
shardModeDegraded,
),
) )
flags.Bool(shardClearErrorsFlag, false, "Set shard error count to 0") flags.Bool(shardClearErrorsFlag, false, "Set shard error count to 0")
@ -51,19 +58,11 @@ func initControlSetShardModeCmd() {
func setShardMode(cmd *cobra.Command, _ []string) { func setShardMode(cmd *cobra.Command, _ []string) {
pk := key.Get(cmd) pk := key.Get(cmd)
var mode control.ShardMode strMode, _ := cmd.Flags().GetString(shardModeFlag)
switch shardMode, _ := cmd.Flags().GetString(shardModeFlag); shardMode { mode, ok := mShardModes[strMode]
default: if !ok {
common.ExitOnErr(cmd, "", fmt.Errorf("unsupported mode %s", shardMode)) common.ExitOnErr(cmd, "", fmt.Errorf("unsupported mode %s", strMode))
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
} }
req := new(control.SetShardModeRequest) req := new(control.SetShardModeRequest)