forked from minio/mc
/
config-host-main.go
285 lines (243 loc) · 7.79 KB
/
config-host-main.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
/*
* 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 (
"encoding/json"
"fmt"
"strings"
"github.com/fatih/color"
"github.com/minio/cli"
"github.com/minio/mc/pkg/console"
"github.com/minio/minio/pkg/probe"
)
var (
configHostFlags = []cli.Flag{
cli.BoolFlag{
Name: "help, h",
Usage: "Help of config host",
},
}
)
var configHostCmd = cli.Command{
Name: "host",
Usage: "List, modify and remove hosts in configuration file.",
Flags: append(configHostFlags, globalFlags...),
Action: mainConfigHost,
CustomHelpTemplate: `NAME:
mc config {{.Name}} - {{.Usage}}
USAGE:
mc config {{.Name}} OPERATION
OPERATION:
add ALIAS URL ACCESS-KEY SECRET-KEY [API]
remove ALIAS
list
FLAGS:
{{range .Flags}}{{.}}
{{end}}
EXAMPLES:
1. Add Amazon S3 storage service under "myphotos" alias. For security reasons turn off bash history momentarily.
$ set +o history
$ mc config {{.Name}} add myphotos https://s3.amazonaws.com BKIKJAA5BMMU2RHO6IBB V7f1CwQqAcwo80UEIJEjc5gVQUSSx5ohQ9GSrr12
$ set -o history
2. Add Google Cloud Storage service under "goodisk" alias.
$ mc config {{.Name}} add goodisk https://storage.googleapis.com BKIKJAA5BMMU2RHO6IBB V7f1CwQqAcwo80UEIJEjc5gVQUSSx5ohQ9GSrr12 S3v2
3. List all hosts.
$ mc config {{.Name}} list
4. Remove "goodisk" config.
$ mc config {{.Name}} remove goodisk
`,
}
// hostMessage container for content message structure
type hostMessage struct {
op string
Status string `json:"status"`
Alias string `json:"alias"`
URL string `json:"URL"`
AccessKey string `json:"accessKey,omitempty"`
SecretKey string `json:"secretKey,omitempty"`
API string `json:"api,omitempty"`
}
// String colorized host message
func (h hostMessage) String() string {
switch h.op {
case "list":
message := console.Colorize("Alias", fmt.Sprintf("%s: ", h.Alias))
message += console.Colorize("URL", fmt.Sprintf("%s", h.URL))
if h.AccessKey != "" || h.SecretKey != "" {
message += " | " + console.Colorize("AccessKey", fmt.Sprintf("<- %s,", h.AccessKey))
message += " | " + console.Colorize("SecretKey", fmt.Sprintf(" %s,", h.SecretKey))
message += " | " + console.Colorize("API", fmt.Sprintf(" %s", h.API))
}
return message
case "remove":
return console.Colorize("HostMessage", "Removed ‘"+h.Alias+"’ successfully.")
case "add":
return console.Colorize("HostMessage", "Added ‘"+h.Alias+"’ successfully.")
default:
return ""
}
}
// JSON jsonified host message
func (h hostMessage) JSON() string {
h.Status = "success"
jsonMessageBytes, e := json.Marshal(h)
fatalIf(probe.NewError(e), "Unable to marshal into JSON.")
return string(jsonMessageBytes)
}
// Validate command-line input args.
func checkConfigHostSyntax(ctx *cli.Context) {
// show help if nothing is set
if !ctx.Args().Present() {
cli.ShowCommandHelpAndExit(ctx, "host", 1) // last argument is exit code
}
switch strings.TrimSpace(ctx.Args().First()) {
case "add":
checkConfigHostAddSyntax(ctx)
case "remove":
checkConfigHostRemoveSyntax(ctx)
case "list":
default:
cli.ShowCommandHelpAndExit(ctx, "host", 1) // last argument is exit code
}
}
// checkConfigHostAddSyntax - verifies input arguments to 'config host add'.
func checkConfigHostAddSyntax(ctx *cli.Context) {
tailArgs := ctx.Args().Tail()
tailsArgsNr := len(tailArgs)
if tailsArgsNr < 4 || tailsArgsNr > 5 {
fatalIf(errInvalidArgument().Trace(ctx.Args().Tail()...),
"Incorrect number of arguments for host add command.")
}
alias := tailArgs.Get(0)
url := tailArgs.Get(1)
accessKey := tailArgs.Get(2)
secretKey := tailArgs.Get(3)
api := tailArgs.Get(4)
if !isValidAlias(alias) {
fatalIf(errDummy().Trace(alias), "Invalid alias ‘"+alias+"’.")
}
if !isValidHostURL(url) {
fatalIf(errDummy().Trace(url),
"Invalid URL ‘"+url+"’.")
}
if !isValidAccessKey(accessKey) {
fatalIf(errInvalidArgument().Trace(accessKey),
"Invalid access key ‘"+accessKey+"’.")
}
if !isValidSecretKey(secretKey) {
fatalIf(errInvalidArgument().Trace(secretKey),
"Invalid secret key ‘"+secretKey+"’.")
}
switch api {
case "S3v2", "S3v4", "":
default:
fatalIf(errInvalidArgument().Trace(api),
"Unrecognized API signature. Valid options are ‘[ S3v4, S3v2 ]’.")
}
}
// checkConfigHostRemoveSyntax - verifies input arguments to 'config host remove'.
func checkConfigHostRemoveSyntax(ctx *cli.Context) {
tailArgs := ctx.Args().Tail()
if len(ctx.Args().Tail()) != 1 {
fatalIf(errInvalidArgument().Trace(tailArgs...),
"Incorrect number of arguments for remove host command.")
}
if !isValidAlias(tailArgs.Get(0)) {
fatalIf(errDummy().Trace(tailArgs.Get(0)),
"Invalid alias ‘"+tailArgs.Get(0)+"’.")
}
}
func mainConfigHost(ctx *cli.Context) {
// Set global flags from context.
setGlobalsFromContext(ctx)
// check 'config host' cli arguments.
checkConfigHostSyntax(ctx)
// Additional command speific theme customization.
console.SetColor("HostMessage", color.New(color.FgGreen))
console.SetColor("Alias", color.New(color.FgCyan, color.Bold))
console.SetColor("URL", color.New(color.FgCyan))
console.SetColor("AccessKey", color.New(color.FgBlue))
console.SetColor("SecretKey", color.New(color.FgBlue))
console.SetColor("API", color.New(color.FgYellow))
cmd := ctx.Args().First()
args := ctx.Args().Tail()
// Switch case through commands.
switch strings.TrimSpace(cmd) {
case "add":
alias := args.Get(0)
url := args.Get(1)
accessKey := args.Get(2)
secretKey := args.Get(3)
api := args.Get(4)
if strings.TrimSpace(api) == "" {
api = "S3v4"
}
hostCfg := hostConfigV7{
URL: url,
AccessKey: accessKey,
SecretKey: secretKey,
API: api,
}
addHost(alias, hostCfg) // Add a host with specified credentials.
case "remove":
alias := args.Get(0)
removeHost(alias) // Remove a host.
case "list":
listHosts() // List all configured hosts.
}
}
// addHost - add a host config.
func addHost(alias string, hostCfgV7 hostConfigV7) {
mcCfgV7, err := loadMcConfig()
fatalIf(err.Trace(globalMCConfigVersion), "Unable to load config ‘"+mustGetMcConfigPath()+"’.")
// Add new host.
mcCfgV7.Hosts[alias] = hostCfgV7
err = saveMcConfig(mcCfgV7)
fatalIf(err.Trace(alias), "Unable to update hosts in config version ‘"+mustGetMcConfigPath()+"’.")
printMsg(hostMessage{
op: "add",
Alias: alias,
URL: hostCfgV7.URL,
AccessKey: hostCfgV7.AccessKey,
SecretKey: hostCfgV7.SecretKey,
API: hostCfgV7.API,
})
}
// removeHost - removes a host.
func removeHost(alias string) {
conf, err := loadMcConfig()
fatalIf(err.Trace(globalMCConfigVersion), "Unable to load config version ‘"+globalMCConfigVersion+"’.")
// Remove host.
delete(conf.Hosts, alias)
err = saveMcConfig(conf)
fatalIf(err.Trace(alias), "Unable to save deleted hosts in config version ‘"+globalMCConfigVersion+"’.")
printMsg(hostMessage{op: "remove", Alias: alias})
}
// listHosts - list all host URLs.
func listHosts() {
conf, err := loadMcConfig()
fatalIf(err.Trace(globalMCConfigVersion), "Unable to load config version ‘"+globalMCConfigVersion+"’.")
for k, v := range conf.Hosts {
printMsg(hostMessage{
op: "list",
Alias: k,
URL: v.URL,
AccessKey: v.AccessKey,
SecretKey: v.SecretKey,
API: v.API,
})
}
}