[MM-51054] Allow enabling/disabling plugins through env override (#22426)
* Allow enabling/disabling plugins through env override * Improve map value setting
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
a9d9f95b24
Коммит
cb96d30fc7
@@ -26,6 +26,43 @@ func GetEnvironment() map[string]string {
|
||||
return mmenv
|
||||
}
|
||||
|
||||
func applyPluginStateOverride(m reflect.Value, key, value string) bool {
|
||||
keyParts := strings.SplitN(key, "_", 2)
|
||||
if len(keyParts) != 2 {
|
||||
return false
|
||||
}
|
||||
|
||||
// supporting legacy pluginID format with dots in it (e.g. com.mattermost.plugin)
|
||||
pluginID := strings.ReplaceAll(strings.ToLower(keyParts[1]), "_", ".")
|
||||
|
||||
for _, key := range m.MapKeys() {
|
||||
if key.String() == pluginID {
|
||||
enable, err := strconv.ParseBool(value)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
mapVal := m.MapIndex(key)
|
||||
if !mapVal.CanInterface() {
|
||||
return false
|
||||
}
|
||||
|
||||
stateVal := mapVal.Interface()
|
||||
state, ok := stateVal.(*model.PluginState)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
|
||||
state.Enable = enable
|
||||
m.SetMapIndex(key, reflect.ValueOf(state))
|
||||
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func applyEnvKey(key, value string, rValueSubject reflect.Value) {
|
||||
keyParts := strings.SplitN(key, "_", 2)
|
||||
if len(keyParts) < 1 {
|
||||
@@ -75,6 +112,12 @@ func applyEnvKey(key, value string, rValueSubject reflect.Value) {
|
||||
case reflect.SliceOf(reflect.TypeOf("")).Kind():
|
||||
rFieldValue.Set(reflect.ValueOf(strings.Split(value, " ")))
|
||||
case reflect.Map:
|
||||
if rFieldValue.Type().String() == "map[string]*model.PluginState" {
|
||||
if applied := applyPluginStateOverride(rFieldValue, key, value); applied {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
target := reflect.New(rFieldValue.Type()).Interface()
|
||||
if err := json.Unmarshal([]byte(value), target); err == nil {
|
||||
rFieldValue.Set(reflect.ValueOf(target).Elem())
|
||||
|
||||
Ссылка в новой задаче
Block a user