github.com/haalcala/mattermost-server-change-repo@v0.0.0-20210713015153-16753fbeee5f/config/memory.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 "fmt" 8 9 "github.com/pkg/errors" 10 11 "github.com/mattermost/mattermost-server/v5/model" 12 ) 13 14 // MemoryStore implements the Store interface. It is meant primarily for testing. 15 // Not to be used directly. Only to be used as a backing store for config.Store 16 type MemoryStore struct { 17 allowEnvironmentOverrides bool 18 validate bool 19 files map[string][]byte 20 savedConfig *model.Config 21 } 22 23 // MemoryStoreOptions makes configuration of the memory store explicit. 24 type MemoryStoreOptions struct { 25 IgnoreEnvironmentOverrides bool 26 SkipValidation bool 27 InitialConfig *model.Config 28 InitialFiles map[string][]byte 29 } 30 31 // NewMemoryStore creates a new MemoryStore instance with default options. 32 func NewMemoryStore() (*MemoryStore, error) { 33 return NewMemoryStoreWithOptions(&MemoryStoreOptions{}) 34 } 35 36 // NewMemoryStoreWithOptions creates a new MemoryStore instance. 37 func NewMemoryStoreWithOptions(options *MemoryStoreOptions) (*MemoryStore, error) { 38 savedConfig := options.InitialConfig 39 if savedConfig == nil { 40 savedConfig = &model.Config{} 41 savedConfig.SetDefaults() 42 } 43 44 initialFiles := options.InitialFiles 45 if initialFiles == nil { 46 initialFiles = make(map[string][]byte) 47 } 48 49 ms := &MemoryStore{ 50 allowEnvironmentOverrides: !options.IgnoreEnvironmentOverrides, 51 validate: !options.SkipValidation, 52 files: initialFiles, 53 savedConfig: savedConfig, 54 } 55 56 return ms, nil 57 } 58 59 // Set replaces the current configuration in its entirety. 60 func (ms *MemoryStore) Set(newCfg *model.Config) error { 61 return ms.persist(newCfg) 62 } 63 64 // persist copies the active config to the saved config. 65 func (ms *MemoryStore) persist(cfg *model.Config) error { 66 ms.savedConfig = cfg.Clone() 67 68 return nil 69 } 70 71 // Load applies environment overrides to the default config as if a re-load had occurred. 72 func (ms *MemoryStore) Load() ([]byte, error) { 73 cfgBytes, err := marshalConfig(ms.savedConfig) 74 if err != nil { 75 return nil, errors.Wrap(err, "failed to serialize config") 76 } 77 78 return cfgBytes, nil 79 80 } 81 82 // GetFile fetches the contents of a previously persisted configuration file. 83 func (ms *MemoryStore) GetFile(name string) ([]byte, error) { 84 data, ok := ms.files[name] 85 if !ok { 86 return nil, fmt.Errorf("file %s not stored", name) 87 } 88 89 return data, nil 90 } 91 92 // SetFile sets or replaces the contents of a configuration file. 93 func (ms *MemoryStore) SetFile(name string, data []byte) error { 94 ms.files[name] = data 95 96 return nil 97 } 98 99 // HasFile returns true if the given file was previously persisted. 100 func (ms *MemoryStore) HasFile(name string) (bool, error) { 101 _, ok := ms.files[name] 102 return ok, nil 103 } 104 105 // RemoveFile removes a previously persisted configuration file. 106 func (ms *MemoryStore) RemoveFile(name string) error { 107 delete(ms.files, name) 108 109 return nil 110 } 111 112 // String returns a hard-coded description, as there is no backing store. 113 func (ms *MemoryStore) String() string { 114 return "memory://" 115 } 116 117 // Close does nothing for a memory store. 118 func (ms *MemoryStore) Close() error { 119 return nil 120 } 121 122 // Watch nothing on memory store 123 func (ms *MemoryStore) Watch(_ func()) error { 124 return nil 125 }