/
config.go
285 lines (224 loc) · 8.04 KB
/
config.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
// Copyright 2019 Dolthub, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package env
import (
"errors"
"path/filepath"
"strings"
"github.com/dolthub/dolt/go/libraries/doltcore/dbfactory"
"github.com/dolthub/dolt/go/libraries/utils/config"
"github.com/dolthub/dolt/go/libraries/utils/filesys"
"github.com/dolthub/dolt/go/libraries/utils/set"
"github.com/dolthub/dolt/go/store/datas"
)
const (
localConfigName = "local"
globalConfigName = "global"
UserEmailKey = "user.email"
UserNameKey = "user.name"
// should be able to have remote specific creds?
UserCreds = "user.creds"
DoltEditor = "core.editor"
InitBranchName = "init.defaultbranch"
RemotesApiHostKey = "remotes.default_host"
RemotesApiHostPortKey = "remotes.default_port"
AddCredsUrlKey = "creds.add_url"
DoltLabInsecureKey = "doltlab.insecure"
MetricsDisabled = "metrics.disabled"
MetricsHost = "metrics.host"
MetricsPort = "metrics.port"
MetricsInsecure = "metrics.insecure"
)
var LocalConfigWhitelist = set.NewStrSet([]string{UserNameKey, UserEmailKey})
var GlobalConfigWhitelist = set.NewStrSet([]string{UserNameKey, UserEmailKey})
// ConfigScope is an enum representing the elements that make up the ConfigHierarchy
type ConfigScope int
const (
// LocalConfig is the repository's local config portion of the ConfigHierarchy
LocalConfig ConfigScope = iota
// GlobalConfig is the user's global config portion of the ConfigHierarchy
GlobalConfig
)
const (
// SqlServerGlobalsPrefix is config namespace accessible by the SQL engine (ex: sqlserver.global.key)
SqlServerGlobalsPrefix = "sqlserver.global"
)
// String gives the string name of an element that was used when it was added to the ConfigHierarchy, which is the
// same name that is used to retrieve that element of the string hierarchy.
func (ce ConfigScope) String() string {
switch ce {
case LocalConfig:
return localConfigName
case GlobalConfig:
return globalConfigName
}
return ""
}
// DoltCliConfig is the config for the cli
type DoltCliConfig struct {
config.ReadableConfig
ch *config.ConfigHierarchy
fs filesys.ReadWriteFS
}
var _ config.ReadableConfig = &DoltCliConfig{}
func LoadDoltCliConfig(hdp HomeDirProvider, fs filesys.ReadWriteFS) (*DoltCliConfig, error) {
ch := config.NewConfigHierarchy()
lPath := getLocalConfigPath()
if exists, _ := fs.Exists(lPath); exists {
lCfg, err := config.FromFile(lPath, fs)
if err == nil {
ch.AddConfig(localConfigName, lCfg)
}
}
gPath, err := getGlobalCfgPath(hdp)
if err != nil {
return nil, err
}
gCfg, err := ensureGlobalConfig(gPath, fs)
if err != nil {
return nil, err
}
ch.AddConfig(globalConfigName, gCfg)
return &DoltCliConfig{ch, ch, fs}, nil
}
func ensureGlobalConfig(path string, fs filesys.ReadWriteFS) (config.ReadWriteConfig, error) {
if exists, isDir := fs.Exists(path); exists {
if isDir {
return nil, errors.New("A directory exists where this file should be. path: " + path)
}
return config.FromFile(path, fs)
}
return config.NewFileConfig(path, fs, map[string]string{})
}
// CreateLocalConfig creates a new repository local config file. The current directory must have already been initialized
// as a data repository before a local config can be created.
func (dcc *DoltCliConfig) CreateLocalConfig(vals map[string]string) error {
return dcc.createLocalConfigAt(".", vals)
}
func (dcc *DoltCliConfig) createLocalConfigAt(dir string, vals map[string]string) error {
doltDir := filepath.Join(dir, dbfactory.DoltDir)
if exists, isDir := dcc.fs.Exists(doltDir); !exists {
return errors.New(dbfactory.DoltDir + " directory not found. Is the current directory a repository directory?")
} else if !isDir {
return errors.New("A file exists with the name \"" + dbfactory.DoltDir + "\". This is not a valid file within a data repository directory.")
}
path := filepath.Join(dir, getLocalConfigPath())
cfg, err := config.NewFileConfig(path, dcc.fs, vals)
if err != nil {
return err
}
dcc.ch.AddConfig(localConfigName, cfg)
return nil
}
// GetConfig retrieves a specific element of the config hierarchy.
func (dcc *DoltCliConfig) GetConfig(element ConfigScope) (config.ReadWriteConfig, bool) {
switch element {
case LocalConfig, GlobalConfig:
return dcc.ch.GetConfig(element.String())
default:
return nil, false
}
}
// GetStringOrDefault retrieves a string from the config hierarchy and returns it if available. Otherwise it returns
// the default string value
func (dcc *DoltCliConfig) GetStringOrDefault(key, defStr string) string {
return GetStringOrDefault(dcc.ch, key, defStr)
}
// IfEmptyUseConfig looks at a strings value and if it is an empty string will try to return a value from the config
// hierarchy. If it is missing in the config a pointer to an empty string will be returned.
func (dcc *DoltCliConfig) IfEmptyUseConfig(val, key string) string {
if len(strings.TrimSpace(val)) > 0 {
return val
}
cfgVal, err := dcc.ch.GetString(key)
if err != nil {
s := ""
return s
}
return cfgVal
}
func GetStringOrDefault(cfg config.ReadableConfig, key, defStr string) string {
val, err := cfg.GetString(key)
if err != nil {
return defStr
}
return val
}
// GetNameAndEmail returns the name and email from the supplied config
func GetNameAndEmail(cfg config.ReadableConfig) (string, string, error) {
name, err := cfg.GetString(UserNameKey)
if err == config.ErrConfigParamNotFound {
return "", "", datas.ErrNameNotConfigured
} else if err != nil {
return "", "", err
}
email, err := cfg.GetString(UserEmailKey)
if err == config.ErrConfigParamNotFound {
return "", "", datas.ErrEmailNotConfigured
} else if err != nil {
return "", "", err
}
return name, email, nil
}
// writeableLocalDoltCliConfig is an extension to DoltCliConfig that reads values from the hierarchy but writes to
// local config.
type writeableLocalDoltCliConfig struct {
*DoltCliConfig
}
// WriteableConfig returns a ReadWriteConfig reading from this config hierarchy. The config will read from the hierarchy
// and write to the local config if it's available, or the global config otherwise.
func (dcc *DoltCliConfig) WriteableConfig() config.ReadWriteConfig {
return writeableLocalDoltCliConfig{dcc}
}
// SetFailsafes sets the config values given as failsafes, i.e. values that will be returned as a last resort if they
// are not found elsewhere in the config hierarchy. The "failsafe" config can be written to in order to conform to the
// interface of ConfigHierarchy, but values will not persist beyond this session.
// Calling SetFailsafes more than once will overwrite any previous values.
// Should only be called after primary configuration of the config hierarchy has been completed.
func (dcc DoltCliConfig) SetFailsafes(cfg map[string]string) {
existing, ok := dcc.ch.GetConfig("failsafe")
if !ok {
existing = config.NewEmptyMapConfig()
dcc.ch.AddConfig("failsafe", existing)
}
_ = existing.SetStrings(cfg)
}
const (
DefaultEmail = "doltuser@dolthub.com"
DefaultName = "Dolt System Account"
)
var DefaultFailsafeConfig = map[string]string{
UserEmailKey: DefaultEmail,
UserNameKey: DefaultName,
}
func (w writeableLocalDoltCliConfig) SetStrings(updates map[string]string) error {
cfg, ok := w.GetConfig(LocalConfig)
if !ok {
cfg, ok = w.GetConfig(GlobalConfig)
if !ok {
return errors.New("no local or global config found")
}
}
return cfg.SetStrings(updates)
}
func (w writeableLocalDoltCliConfig) Unset(params []string) error {
cfg, ok := w.GetConfig(LocalConfig)
if !ok {
cfg, ok = w.GetConfig(GlobalConfig)
if !ok {
return errors.New("no local or global config found")
}
}
return cfg.Unset(params)
}