forked from minio/mc
/
config.go
290 lines (252 loc) · 7.65 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
286
287
288
289
290
/*
* Minio Client (C) 2015 Minio, 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 main
import (
"os"
"os/user"
"path/filepath"
"runtime"
"sync"
"github.com/minio/mc/pkg/console"
"github.com/minio/mc/pkg/quick"
"github.com/minio/minio/pkg/iodine"
)
type configV1 struct {
Version string
Aliases map[string]string
Hosts map[string]*hostConfig
}
// cached variables should *NEVER* be accessed directly from outside this file.
var cache sync.Pool
// customConfigDir contains the whole path to config dir. Only access via get/set functions.
var mcCustomConfigDir string
// setMcConfigDir - construct minio client config folder.
func setMcConfigDir(configDir string) {
mcCustomConfigDir = configDir
}
// getMcConfigDir - construct minio client config folder.
func getMcConfigDir() (string, error) {
if mcCustomConfigDir != "" {
return mcCustomConfigDir, nil
}
u, err := user.Current()
if err != nil {
return "", NewIodine(iodine.New(err, nil))
}
// For windows the path is slightly different
switch runtime.GOOS {
case "windows":
return filepath.Join(u.HomeDir, mcConfigWindowsDir), nil
default:
return filepath.Join(u.HomeDir, mcConfigDir), nil
}
}
// mustGetMcConfigDir - construct minio client config folder or fail
func mustGetMcConfigDir() (configDir string) {
configDir, err := getMcConfigDir()
if err != nil {
console.Fatalf("Unable to determine default configuration folder. %s\n", NewIodine(iodine.New(err, nil)))
}
return configDir
}
// createMcConfigDir - create minio client config folder
func createMcConfigDir() error {
p, err := getMcConfigDir()
if err != nil {
return NewIodine(iodine.New(err, nil))
}
err = os.MkdirAll(p, 0700)
if err != nil {
return NewIodine(iodine.New(err, nil))
}
return nil
}
// getMcConfigPath - construct minio client configuration path
func getMcConfigPath() (string, error) {
dir, err := getMcConfigDir()
if err != nil {
return "", NewIodine(iodine.New(err, nil))
}
return filepath.Join(dir, mcConfigFile), nil
}
// mustGetMcConfigPath - similar to getMcConfigPath, ignores errors
func mustGetMcConfigPath() string {
p, err := getMcConfigPath()
if err != nil {
console.Fatalf("Unable to determine default config path. %s\n", NewIodine(iodine.New(err, nil)))
}
return p
}
// getMcConfig - reads configuration file and returns config
func getMcConfig() (*configV1, error) {
if !isMcConfigExists() {
return nil, NewIodine(iodine.New(errInvalidArgument{}, nil))
}
configFile, err := getMcConfigPath()
if err != nil {
return nil, NewIodine(iodine.New(err, nil))
}
// Cached in private global variable.
if v := cache.Get(); v != nil { // Use previously cached config.
return v.(quick.Config).Data().(*configV1), nil
}
conf := newConfigV1()
qconf, err := quick.New(conf)
if err != nil {
return nil, NewIodine(iodine.New(err, nil))
}
err = qconf.Load(configFile)
if err != nil {
return nil, NewIodine(iodine.New(err, nil))
}
cache.Put(qconf)
return qconf.Data().(*configV1), nil
}
// mustGetMcConfig - reads configuration file and returns configs, exits on error
func mustGetMcConfig() *configV1 {
config, err := getMcConfig()
if err != nil {
console.Fatalf("Unable to retrieve mc configuration. %s\n", err)
}
return config
}
// isMcConfigExists xreturns err if config doesn't exist
func isMcConfigExists() bool {
configFile, err := getMcConfigPath()
if err != nil {
return false
}
_, err = os.Stat(configFile)
if err != nil {
return false
}
return true
}
// writeConfig - write configuration file
func writeConfig(config quick.Config) error {
err := createMcConfigDir()
if err != nil {
return NewIodine(iodine.New(err, nil))
}
configPath, err := getMcConfigPath()
if err != nil {
return NewIodine(iodine.New(err, nil))
}
if err := config.Save(configPath); err != nil {
return NewIodine(iodine.New(err, nil))
}
return nil
}
func migrateConfig() {
// Migrate session V1 to V101
migrateConfigV1ToV101()
}
func migrateConfigV1ToV101() {
if !isMcConfigExists() {
return
}
conf := newConfigV1()
config, err := quick.New(conf)
if err != nil {
console.Fatalln(NewIodine(iodine.New(err, nil)))
}
config.Load(mustGetMcConfigPath())
conf = config.Data().(*configV1)
// version is the same return
if conf.Version == mcCurrentConfigVersion {
return
}
conf.Version = mcCurrentConfigVersion
localHostConfig := new(hostConfig)
localHostConfig.AccessKeyID = ""
localHostConfig.SecretAccessKey = ""
if _, ok := conf.Hosts["localhost:*"]; !ok {
conf.Hosts["localhost:*"] = localHostConfig
}
if _, ok := conf.Hosts["127.0.0.1:*"]; !ok {
conf.Hosts["127.0.0.1:*"] = localHostConfig
}
newConfig, err := quick.New(conf)
if err := newConfig.Save(mustGetMcConfigPath()); err != nil {
console.Fatalln(NewIodine(iodine.New(err, nil)))
}
console.Infof("Successfully migrated %s from version: %s to version: %s\n", mustGetMcConfigPath(), mcPreviousConfigVersion, mcCurrentConfigVersion)
}
// newConfigV1() - get new config version 1.0.0
func newConfigV1() *configV1 {
conf := new(configV1)
conf.Version = mcPreviousConfigVersion
// make sure to allocate map's otherwise Golang
// exits silently without providing any errors
conf.Hosts = make(map[string]*hostConfig)
conf.Aliases = make(map[string]string)
return conf
}
// newConfigV101() - get new config version 1.0.1
func newConfigV101() *configV1 {
conf := new(configV1)
conf.Version = mcCurrentConfigVersion
// make sure to allocate map's otherwise Golang
// exits silently without providing any errors
conf.Hosts = make(map[string]*hostConfig)
conf.Aliases = make(map[string]string)
localHostConfig := new(hostConfig)
localHostConfig.AccessKeyID = ""
localHostConfig.SecretAccessKey = ""
s3HostConf := new(hostConfig)
s3HostConf.AccessKeyID = globalAccessKeyID
s3HostConf.SecretAccessKey = globalSecretAccessKey
// Your example host config
exampleHostConf := new(hostConfig)
exampleHostConf.AccessKeyID = globalAccessKeyID
exampleHostConf.SecretAccessKey = globalSecretAccessKey
playHostConfig := new(hostConfig)
playHostConfig.AccessKeyID = ""
playHostConfig.SecretAccessKey = ""
dlHostConfig := new(hostConfig)
dlHostConfig.AccessKeyID = ""
dlHostConfig.SecretAccessKey = ""
conf.Hosts[exampleHostURL] = exampleHostConf
conf.Hosts["localhost:*"] = localHostConfig
conf.Hosts["127.0.0.1:*"] = localHostConfig
conf.Hosts["s3*.amazonaws.com"] = s3HostConf
conf.Hosts["play.minio.io:9000"] = playHostConfig
conf.Hosts["dl.minio.io:9000"] = dlHostConfig
aliases := make(map[string]string)
aliases["s3"] = "https://s3.amazonaws.com"
aliases["play"] = "https://play.minio.io:9000"
aliases["dl"] = "https://dl.minio.io:9000"
aliases["localhost"] = "http://localhost:9000"
conf.Aliases = aliases
return conf
}
// newConfig - get new config interface
func newConfig() (config quick.Config, err error) {
conf := newConfigV101()
config, err = quick.New(conf)
if err != nil {
return nil, NewIodine(iodine.New(err, nil))
}
return config, nil
}
// mustNewConfig instantiates a new config handler, exists upon error
func mustNewConfig() quick.Config {
config, err := newConfig()
if err != nil {
console.Fatalf("Unable to instantiate a new config handler. %s\n", NewIodine(iodine.New(err, nil)))
}
return config
}