[#1883] cli/shards: Exclude degraded-read-write mode from help

The mode is not expected to be used by the user. It is supported only
for developers.

Signed-off-by: Leonard Lyubich <ctulhurider@gmail.com>
This commit is contained in:
Leonard Lyubich 2022-10-15 11:57:06 +04:00 committed by fyrchik
parent eb206d6e59
commit 3c6daa2995
2 changed files with 57 additions and 15 deletions

View file

@ -108,11 +108,10 @@ func prettyPrintShards(cmd *cobra.Command, ii []*control.ShardInfo) {
} }
func shardModeToString(m control.ShardMode) string { func shardModeToString(m control.ShardMode) string {
for strMode, mode := range mShardModes { strMode, ok := lookUpShardModeString(m)
if mode == m { if ok {
return strMode return strMode
} }
}
return "unknown" return "unknown"
} }

View file

@ -20,12 +20,54 @@ const (
) )
// maps string command input to control.ShardMode. To support new mode, it's // maps string command input to control.ShardMode. To support new mode, it's
// enough to add the map entry. // enough to add the map entry. Modes are automatically printed in command help
var mShardModes = map[string]control.ShardMode{ // messages.
"read-only": control.ShardMode_READ_ONLY, var mShardModes = map[string]struct {
"read-write": control.ShardMode_READ_WRITE, val control.ShardMode
"degraded-read-write": control.ShardMode_DEGRADED,
"degraded-read-only": control.ShardMode_DEGRADED_READ_ONLY, // flag to support shard mode implicitly without help message. The flag is set
// for values which are not expected to be set by users but still supported
// for developers.
unsafe bool
}{
"read-only": {val: control.ShardMode_READ_ONLY},
"read-write": {val: control.ShardMode_READ_WRITE},
"degraded-read-write": {val: control.ShardMode_DEGRADED, unsafe: true},
"degraded-read-only": {val: control.ShardMode_DEGRADED_READ_ONLY},
}
// iterates over string representations of safe supported shard modes. Safe means
// modes which are expected to be used by any user. All other supported modes
// are for developers only.
func iterateSafeShardModes(f func(string)) {
for strMode, mode := range mShardModes {
if !mode.unsafe {
f(strMode)
}
}
}
// looks up for supported control.ShardMode represented by the given string.
// Returns false if no corresponding mode exists.
func lookUpShardModeFromString(str string) (control.ShardMode, bool) {
mode, ok := mShardModes[str]
if !ok {
return control.ShardMode_SHARD_MODE_UNDEFINED, false
}
return mode.val, true
}
// looks up for string representation of supported shard mode. Returns false
// if mode is not supported.
func lookUpShardModeString(m control.ShardMode) (string, bool) {
for strMode, mode := range mShardModes {
if mode.val == m {
return strMode, true
}
}
return "", false
} }
var setShardModeCmd = &cobra.Command{ var setShardModeCmd = &cobra.Command{
@ -42,13 +84,14 @@ func initControlSetShardModeCmd() {
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)) modes := make([]string, 0)
for strMode := range mShardModes {
iterateSafeShardModes(func(strMode string) {
modes = append(modes, "'"+strMode+"'") modes = append(modes, "'"+strMode+"'")
} })
flags.String(shardModeFlag, "", flags.String(shardModeFlag, "",
fmt.Sprintf("New shard mode keyword (%s)", strings.Join(modes, ",")), fmt.Sprintf("New shard mode (%s)", strings.Join(modes, ", ")),
) )
flags.Bool(shardClearErrorsFlag, false, "Set shard error count to 0") flags.Bool(shardClearErrorsFlag, false, "Set shard error count to 0")
@ -60,7 +103,7 @@ func setShardMode(cmd *cobra.Command, _ []string) {
strMode, _ := cmd.Flags().GetString(shardModeFlag) strMode, _ := cmd.Flags().GetString(shardModeFlag)
mode, ok := mShardModes[strMode] mode, ok := lookUpShardModeFromString(strMode)
if !ok { if !ok {
common.ExitOnErr(cmd, "", fmt.Errorf("unsupported mode %s", strMode)) common.ExitOnErr(cmd, "", fmt.Errorf("unsupported mode %s", strMode))
} }