[MM-18121] Fix config set panic (#12083)
* Fix config set panic * Change error message * Add a test case * Update error string
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
e236eb74fa
Коммит
32bc585446
@@ -21,6 +21,8 @@ import (
|
|||||||
"github.com/mattermost/viper"
|
"github.com/mattermost/viper"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const noSettingsNamed = "unable to find a setting named: %s"
|
||||||
|
|
||||||
var ConfigCmd = &cobra.Command{
|
var ConfigCmd = &cobra.Command{
|
||||||
Use: "config",
|
Use: "config",
|
||||||
Short: "Configuration",
|
Short: "Configuration",
|
||||||
@@ -296,7 +298,7 @@ func updateConfigValue(configSetting string, newVal []string, oldConfig, newConf
|
|||||||
func UpdateMap(configMap map[string]interface{}, configSettings []string, newVal []string) error {
|
func UpdateMap(configMap map[string]interface{}, configSettings []string, newVal []string) error {
|
||||||
res, ok := configMap[configSettings[0]]
|
res, ok := configMap[configSettings[0]]
|
||||||
if !ok {
|
if !ok {
|
||||||
return fmt.Errorf("unable to find a setting with that name %s", configSettings[0])
|
return fmt.Errorf(noSettingsNamed, configSettings[0])
|
||||||
}
|
}
|
||||||
|
|
||||||
value := reflect.ValueOf(res)
|
value := reflect.ValueOf(res)
|
||||||
@@ -308,6 +310,9 @@ func UpdateMap(configMap map[string]interface{}, configSettings []string, newVal
|
|||||||
if len(configSettings) == 1 {
|
if len(configSettings) == 1 {
|
||||||
return errors.New("unable to set multiple settings at once")
|
return errors.New("unable to set multiple settings at once")
|
||||||
}
|
}
|
||||||
|
if value.Len() == 0 {
|
||||||
|
return fmt.Errorf(noSettingsNamed, configSettings[1])
|
||||||
|
}
|
||||||
return UpdateMap(res.(map[string]interface{}), configSettings[1:], newVal)
|
return UpdateMap(res.(map[string]interface{}), configSettings[1:], newVal)
|
||||||
|
|
||||||
case reflect.Int:
|
case reflect.Int:
|
||||||
@@ -319,7 +324,7 @@ func UpdateMap(configMap map[string]interface{}, configSettings []string, newVal
|
|||||||
configMap[configSettings[0]] = val
|
configMap[configSettings[0]] = val
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return fmt.Errorf("unable to find a setting with that name %s", configSettings[0])
|
return fmt.Errorf(noSettingsNamed, configSettings[0])
|
||||||
|
|
||||||
case reflect.Int64:
|
case reflect.Int64:
|
||||||
if len(configSettings) == 1 {
|
if len(configSettings) == 1 {
|
||||||
@@ -330,7 +335,7 @@ func UpdateMap(configMap map[string]interface{}, configSettings []string, newVal
|
|||||||
configMap[configSettings[0]] = int64(val)
|
configMap[configSettings[0]] = int64(val)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return fmt.Errorf("unable to find a setting with that name %s", configSettings[0])
|
return fmt.Errorf(noSettingsNamed, configSettings[0])
|
||||||
|
|
||||||
case reflect.Bool:
|
case reflect.Bool:
|
||||||
if len(configSettings) == 1 {
|
if len(configSettings) == 1 {
|
||||||
@@ -341,21 +346,21 @@ func UpdateMap(configMap map[string]interface{}, configSettings []string, newVal
|
|||||||
configMap[configSettings[0]] = val
|
configMap[configSettings[0]] = val
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return fmt.Errorf("unable to find a setting with that name %s", configSettings[0])
|
return fmt.Errorf(noSettingsNamed, configSettings[0])
|
||||||
|
|
||||||
case reflect.String:
|
case reflect.String:
|
||||||
if len(configSettings) == 1 {
|
if len(configSettings) == 1 {
|
||||||
configMap[configSettings[0]] = newVal[0]
|
configMap[configSettings[0]] = newVal[0]
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return fmt.Errorf("unable to find a setting with that name %s", configSettings[0])
|
return fmt.Errorf(noSettingsNamed, configSettings[0])
|
||||||
|
|
||||||
case reflect.Slice:
|
case reflect.Slice:
|
||||||
if len(configSettings) == 1 {
|
if len(configSettings) == 1 {
|
||||||
configMap[configSettings[0]] = newVal
|
configMap[configSettings[0]] = newVal
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return fmt.Errorf("unable to find a setting with that name %s", configSettings[0])
|
return fmt.Errorf(noSettingsNamed, configSettings[0])
|
||||||
|
|
||||||
default:
|
default:
|
||||||
return errors.New("type not supported yet")
|
return errors.New("type not supported yet")
|
||||||
|
|||||||
@@ -141,6 +141,12 @@ func TestConfigSet(t *testing.T) {
|
|||||||
assert.NotContains(t, string(output), "invalid-key")
|
assert.NotContains(t, string(output), "invalid-key")
|
||||||
})
|
})
|
||||||
|
|
||||||
|
t.Run("Error when the parameter of an unknown plugin is set", func(t *testing.T) {
|
||||||
|
output, err := th.RunCommandWithOutput(t, "config", "set", "PluginSettings.Plugins.someplugin", "true")
|
||||||
|
assert.Error(t, err)
|
||||||
|
assert.NotContains(t, string(output), "panic")
|
||||||
|
})
|
||||||
|
|
||||||
t.Run("Error when the wrong locale is set", func(t *testing.T) {
|
t.Run("Error when the wrong locale is set", func(t *testing.T) {
|
||||||
th.CheckCommand(t, "config", "set", "LocalizationSettings.DefaultServerLocale", "es")
|
th.CheckCommand(t, "config", "set", "LocalizationSettings.DefaultServerLocale", "es")
|
||||||
assert.Error(t, th.RunCommand(t, "config", "set", "LocalizationSettings.DefaultServerLocale", "invalid-key"))
|
assert.Error(t, th.RunCommand(t, "config", "set", "LocalizationSettings.DefaultServerLocale", "invalid-key"))
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user