-
Notifications
You must be signed in to change notification settings - Fork 18
/
configuration.go
94 lines (78 loc) · 2.63 KB
/
configuration.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package pluginapi
import (
"bytes"
"encoding/json"
"github.com/mattermost/mattermost-server/v6/model"
"github.com/mattermost/mattermost-server/v6/plugin"
"github.com/mattermost/mattermost-server/v6/utils"
"github.com/pkg/errors"
)
// ConfigurationService exposes methods to manipulate the server and plugin configuration.
type ConfigurationService struct {
api plugin.API
}
// LoadPluginConfiguration loads the plugin's configuration. dest should be a pointer to a
// struct to which the configuration JSON can be unmarshalled.
//
// Minimum server version: 5.2
func (c *ConfigurationService) LoadPluginConfiguration(dest interface{}) error {
// TODO: Isn't this method redundant given GetPluginConfig() and even GetConfig()?
return c.api.LoadPluginConfiguration(dest)
}
// GetConfig fetches the currently persisted config.
//
// Minimum server version: 5.2
func (c *ConfigurationService) GetConfig() *model.Config {
return c.api.GetConfig()
}
// GetUnsanitizedConfig fetches the currently persisted config without removing secrets.
//
// Minimum server version: 5.16
func (c *ConfigurationService) GetUnsanitizedConfig() *model.Config {
return c.api.GetUnsanitizedConfig()
}
// SaveConfig sets the given config and persists the changes
//
// Minimum server version: 5.2
func (c *ConfigurationService) SaveConfig(config *model.Config) error {
return normalizeAppErr(c.api.SaveConfig(config))
}
// GetPluginConfig fetches the currently persisted config of plugin
//
// Minimum server version: 5.6
func (c *ConfigurationService) GetPluginConfig() map[string]interface{} {
return c.api.GetPluginConfig()
}
// SavePluginConfig sets the given config for plugin and persists the changes
//
// Minimum server version: 5.6
func (c *ConfigurationService) SavePluginConfig(config map[string]interface{}) error {
return normalizeAppErr(c.api.SavePluginConfig(config))
}
// CheckRequiredServerConfiguration checks if the server is configured according to
// plugin requirements.
//
// Minimum server version: 5.2
func (c *ConfigurationService) CheckRequiredServerConfiguration(req *model.Config) (bool, error) {
if req == nil {
return true, nil
}
cfg := c.api.GetConfig()
mc, err := utils.Merge(cfg, req, nil)
if err != nil {
return false, errors.Wrap(err, "could not merge configurations")
}
mergedCfg := mc.(model.Config)
mergedCfgJSON, err := json.Marshal(mergedCfg)
if err != nil {
return false, errors.Wrap(err, "failed to marshal merged config")
}
cjfJSON, err := json.Marshal(cfg)
if err != nil {
return false, errors.Wrap(err, "failed to marshal config")
}
if !bytes.Equal(mergedCfgJSON, cjfJSON) {
return false, nil
}
return true, nil
}