forked from mattermost/mattermost
-
Notifications
You must be signed in to change notification settings - Fork 0
/
memory.go
154 lines (119 loc) · 3.75 KB
/
memory.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
package config
import (
"bytes"
"fmt"
"io/ioutil"
"github.com/pkg/errors"
"github.com/mattermost/mattermost-server/model"
)
// memoryStore implements the Store interface. It is meant primarily for testing.
type memoryStore struct {
commonStore
allowEnvironmentOverrides bool
validate bool
files map[string][]byte
savedConfig *model.Config
}
// MemoryStoreOptions makes configuration of the memory store explicit.
type MemoryStoreOptions struct {
IgnoreEnvironmentOverrides bool
SkipValidation bool
InitialConfig *model.Config
InitialFiles map[string][]byte
}
// NewMemoryStore creates a new memoryStore instance with default options.
func NewMemoryStore() (*memoryStore, error) {
return NewMemoryStoreWithOptions(&MemoryStoreOptions{})
}
// NewMemoryStoreWithOptions creates a new memoryStore instance.
func NewMemoryStoreWithOptions(options *MemoryStoreOptions) (*memoryStore, error) {
savedConfig := options.InitialConfig
if savedConfig == nil {
savedConfig = &model.Config{}
savedConfig.SetDefaults()
}
initialFiles := options.InitialFiles
if initialFiles == nil {
initialFiles = make(map[string][]byte)
}
ms := &memoryStore{
allowEnvironmentOverrides: !options.IgnoreEnvironmentOverrides,
validate: !options.SkipValidation,
files: initialFiles,
savedConfig: savedConfig,
}
ms.commonStore.config = &model.Config{}
ms.commonStore.config.SetDefaults()
if err := ms.Load(); err != nil {
return nil, err
}
return ms, nil
}
// Set replaces the current configuration in its entirety.
func (ms *memoryStore) Set(newCfg *model.Config) (*model.Config, error) {
validate := ms.commonStore.validate
if !ms.validate {
validate = nil
}
return ms.commonStore.set(newCfg, validate, ms.persist)
}
// persist copies the active config to the saved config.
func (ms *memoryStore) persist(cfg *model.Config) error {
ms.savedConfig = cfg.Clone()
return nil
}
// Load applies environment overrides to the default config as if a re-load had occurred.
func (ms *memoryStore) Load() (err error) {
var cfgBytes []byte
cfgBytes, err = marshalConfig(ms.savedConfig)
if err != nil {
return errors.Wrap(err, "failed to serialize config")
}
f := ioutil.NopCloser(bytes.NewReader(cfgBytes))
validate := ms.commonStore.validate
if !ms.validate {
validate = nil
}
return ms.commonStore.load(f, false, validate, ms.persist)
}
// GetFile fetches the contents of a previously persisted configuration file.
func (ms *memoryStore) GetFile(name string) ([]byte, error) {
ms.configLock.RLock()
defer ms.configLock.RUnlock()
data, ok := ms.files[name]
if !ok {
return nil, fmt.Errorf("file %s not stored", name)
}
return data, nil
}
// SetFile sets or replaces the contents of a configuration file.
func (ms *memoryStore) SetFile(name string, data []byte) error {
ms.configLock.Lock()
defer ms.configLock.Unlock()
ms.files[name] = data
return nil
}
// HasFile returns true if the given file was previously persisted.
func (ms *memoryStore) HasFile(name string) (bool, error) {
ms.configLock.RLock()
defer ms.configLock.RUnlock()
_, ok := ms.files[name]
return ok, nil
}
// RemoveFile removes a previously persisted configuration file.
func (ms *memoryStore) RemoveFile(name string) error {
ms.configLock.Lock()
defer ms.configLock.Unlock()
delete(ms.files, name)
return nil
}
// String returns a hard-coded description, as there is no backing store.
func (ms *memoryStore) String() string {
return "memory://"
}
// Close does nothing for a memory store.
func (ms *memoryStore) Close() error {
return nil
}