github.com/mattermosttest/mattermost-server/v5@v5.0.0-20200917143240-9dfa12e121f9/config/store.go (about) 1 // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. 2 // See LICENSE.txt for license information. 3 4 package config 5 6 import ( 7 "strings" 8 9 "github.com/mattermost/mattermost-server/v5/model" 10 ) 11 12 // Listener is a callback function invoked when the configuration changes. 13 type Listener func(oldConfig *model.Config, newConfig *model.Config) 14 15 // Store abstracts the act of getting and setting the configuration. 16 type Store interface { 17 // Get fetches the current, cached configuration. 18 Get() *model.Config 19 20 // GetEnvironmentOverrides fetches the configuration fields overridden by environment variables. 21 GetEnvironmentOverrides() map[string]interface{} 22 23 // RemoveEnvironmentOverrides returns a new config without the environment 24 // overrides 25 RemoveEnvironmentOverrides(cfg *model.Config) *model.Config 26 27 // Set replaces the current configuration in its entirety and updates the backing store. 28 Set(*model.Config) (*model.Config, error) 29 30 // Load updates the current configuration from the backing store, possibly initializing. 31 Load() (err error) 32 33 // AddListener adds a callback function to invoke when the configuration is modified. 34 AddListener(listener Listener) string 35 36 // RemoveListener removes a callback function using an id returned from AddListener. 37 RemoveListener(id string) 38 39 // GetFile fetches the contents of a previously persisted configuration file. 40 // If no such file exists, an empty byte array will be returned without error. 41 GetFile(name string) ([]byte, error) 42 43 // SetFile sets or replaces the contents of a configuration file. 44 SetFile(name string, data []byte) error 45 46 // HasFile returns true if the given file was previously persisted. 47 HasFile(name string) (bool, error) 48 49 // RemoveFile removes a previously persisted configuration file. 50 RemoveFile(name string) error 51 52 // String describes the backing store for the config. 53 String() string 54 55 // Close cleans up resources associated with the store. 56 Close() error 57 } 58 59 // NewStore creates a database or file store given a data source name by which to connect. 60 func NewStore(dsn string, watch bool) (Store, error) { 61 if strings.HasPrefix(dsn, "mysql://") || strings.HasPrefix(dsn, "postgres://") { 62 return NewDatabaseStore(dsn) 63 } 64 65 return NewFileStore(dsn, watch) 66 }