Split config-host into remove, list, add commands (#2069)
This PR also adds convenient aliases for - remove as `rm` - list as `ls` - add as `a` Also brings much needed cleanup.
This commit is contained in:
@@ -0,0 +1,140 @@
|
||||
/*
|
||||
* Minio Client (C) 2017 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 cmd
|
||||
|
||||
import (
|
||||
"github.com/fatih/color"
|
||||
"github.com/minio/cli"
|
||||
"github.com/minio/mc/pkg/console"
|
||||
)
|
||||
|
||||
var configHostAddCmd = cli.Command{
|
||||
Name: "add",
|
||||
ShortName: "a",
|
||||
Usage: "Add a new host to configuration file.",
|
||||
Action: mainConfigHostAdd,
|
||||
Before: setGlobalsFromContext,
|
||||
Flags: globalFlags,
|
||||
HideHelpCommand: true,
|
||||
CustomHelpTemplate: `NAME:
|
||||
{{.HelpName}} - {{.Usage}}
|
||||
|
||||
USAGE:
|
||||
{{.HelpName}} ALIAS URL ACCESS-KEY SECRET-KEY [API]
|
||||
|
||||
FLAGS:
|
||||
{{range .VisibleFlags}}{{.}}
|
||||
{{end}}
|
||||
EXAMPLES:
|
||||
1. Add Amazon S3 storage service under "mys3" alias. For security reasons turn off bash history momentarily.
|
||||
$ set +o history
|
||||
$ {{.HelpName}} mys3 https://s3.amazonaws.com \
|
||||
BKIKJAA5BMMU2RHO6IBB V8f1CwQqAcwo80UEIJEjc5gVQUSSx5ohQ9GSrr12
|
||||
$ set -o history
|
||||
|
||||
2. Add Amazon S3 accelerated storage service under "mys3-accel" alias. For security reasons turn off bash history momentarily.
|
||||
$ set +o history
|
||||
$ {{.HelpName}} mys3-accel https://s3-accelerate.amazonaws.com \
|
||||
BKIKJAA5BMMU2RHO6IBB V8f1CwQqAcwo80UEIJEjc5gVQUSSx5ohQ9GSrr12
|
||||
$ set -o history
|
||||
`,
|
||||
}
|
||||
|
||||
// checkConfigHostAddSyntax - verifies input arguments to 'config host add'.
|
||||
func checkConfigHostAddSyntax(ctx *cli.Context) {
|
||||
args := ctx.Args()
|
||||
argsNr := len(args)
|
||||
if argsNr < 4 || argsNr > 5 {
|
||||
fatalIf(errInvalidArgument().Trace(ctx.Args().Tail()...),
|
||||
"Incorrect number of arguments for host add command.")
|
||||
}
|
||||
|
||||
alias := args.Get(0)
|
||||
url := args.Get(1)
|
||||
accessKey := args.Get(2)
|
||||
secretKey := args.Get(3)
|
||||
api := args.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+"`.")
|
||||
}
|
||||
|
||||
if api != "" && !isValidAPI(api) { // Empty value set to default "S3v4".
|
||||
fatalIf(errInvalidArgument().Trace(api),
|
||||
"Unrecognized API signature. Valid options are `[S3v4, S3v2]`.")
|
||||
}
|
||||
}
|
||||
|
||||
// addHost - add a host config.
|
||||
func addHost(alias string, hostCfgV8 hostConfigV8) {
|
||||
mcCfgV8, err := loadMcConfig()
|
||||
fatalIf(err.Trace(globalMCConfigVersion), "Unable to load config `"+mustGetMcConfigPath()+"`.")
|
||||
|
||||
// Add new host.
|
||||
mcCfgV8.Hosts[alias] = hostCfgV8
|
||||
|
||||
err = saveMcConfig(mcCfgV8)
|
||||
fatalIf(err.Trace(alias), "Unable to update hosts in config version `"+mustGetMcConfigPath()+"`.")
|
||||
|
||||
printMsg(hostMessage{
|
||||
op: "add",
|
||||
Alias: alias,
|
||||
URL: hostCfgV8.URL,
|
||||
AccessKey: hostCfgV8.AccessKey,
|
||||
SecretKey: hostCfgV8.SecretKey,
|
||||
API: hostCfgV8.API,
|
||||
})
|
||||
}
|
||||
|
||||
func mainConfigHostAdd(ctx *cli.Context) error {
|
||||
checkConfigHostAddSyntax(ctx)
|
||||
|
||||
console.SetColor("HostMessage", color.New(color.FgGreen))
|
||||
|
||||
args := ctx.Args()
|
||||
alias := args.Get(0)
|
||||
url := args.Get(1)
|
||||
accessKey := args.Get(2)
|
||||
secretKey := args.Get(3)
|
||||
api := args.Get(4)
|
||||
if api == "" {
|
||||
api = "S3v4"
|
||||
}
|
||||
hostCfg := hostConfigV8{
|
||||
URL: url,
|
||||
AccessKey: accessKey,
|
||||
SecretKey: secretKey,
|
||||
API: api,
|
||||
}
|
||||
addHost(alias, hostCfg) // Add a host with specified credentials.
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,147 @@
|
||||
/*
|
||||
* Minio Client (C) 2017 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 cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
|
||||
"github.com/fatih/color"
|
||||
"github.com/minio/cli"
|
||||
"github.com/minio/mc/pkg/console"
|
||||
)
|
||||
|
||||
var configHostListCmd = cli.Command{
|
||||
Name: "list",
|
||||
ShortName: "ls",
|
||||
Usage: "Lists hosts in configuration file.",
|
||||
Action: mainConfigHostList,
|
||||
Before: setGlobalsFromContext,
|
||||
Flags: globalFlags,
|
||||
HideHelpCommand: true,
|
||||
CustomHelpTemplate: `NAME:
|
||||
{{.HelpName}} - {{.Usage}}
|
||||
|
||||
USAGE:
|
||||
{{.HelpName}} [ALIAS]
|
||||
|
||||
FLAGS:
|
||||
{{range .VisibleFlags}}{{.}}
|
||||
{{end}}
|
||||
EXAMPLES:
|
||||
1. List all hosts.
|
||||
$ {{.HelpName}}
|
||||
|
||||
2. List a specific host.
|
||||
$ {{.HelpName}} s3
|
||||
`,
|
||||
}
|
||||
|
||||
// Input argument validator..
|
||||
func checkConfigHostListSyntax(ctx *cli.Context) {
|
||||
args := ctx.Args()
|
||||
|
||||
if len(ctx.Args()) > 1 {
|
||||
fatalIf(errInvalidArgument().Trace(args...),
|
||||
"Incorrect number of arguments to list hosts.")
|
||||
}
|
||||
|
||||
if args.Get(0) != "" {
|
||||
if !isValidAlias(args.Get(0)) {
|
||||
fatalIf(errDummy().Trace(args.Get(0)),
|
||||
"Invalid alias `"+args.Get(0)+"`.")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func mainConfigHostList(ctx *cli.Context) error {
|
||||
checkConfigHostListSyntax(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))
|
||||
|
||||
args := ctx.Args()
|
||||
listHosts(args.Get(0)) // List all configured hosts.
|
||||
return nil
|
||||
}
|
||||
|
||||
// Prints all the hosts.
|
||||
func printHosts(hosts ...hostMessage) {
|
||||
var maxAlias = 0
|
||||
for _, host := range hosts {
|
||||
if len(host.Alias) > maxAlias {
|
||||
maxAlias = len(host.Alias)
|
||||
}
|
||||
}
|
||||
for _, host := range hosts {
|
||||
// Format properly for alignment based on alias length.
|
||||
host.Alias = fmt.Sprintf("%-*.*s", maxAlias, maxAlias, host.Alias)
|
||||
printMsg(host)
|
||||
}
|
||||
}
|
||||
|
||||
// byAlias is a collection satisfying sort.Interface
|
||||
type byAlias []hostMessage
|
||||
|
||||
func (d byAlias) Len() int { return len(d) }
|
||||
func (d byAlias) Swap(i, j int) { d[i], d[j] = d[j], d[i] }
|
||||
func (d byAlias) Less(i, j int) bool { return d[i].Alias < d[j].Alias }
|
||||
|
||||
// listHosts - list all host URLs or a requested host.
|
||||
func listHosts(alias string) {
|
||||
conf, err := loadMcConfig()
|
||||
fatalIf(err.Trace(globalMCConfigVersion), "Unable to load config version `"+globalMCConfigVersion+"`.")
|
||||
|
||||
// If specific alias is requested, look for it and print.
|
||||
if alias != "" {
|
||||
if v, ok := conf.Hosts[alias]; ok {
|
||||
printHosts(hostMessage{
|
||||
op: "list",
|
||||
Alias: alias,
|
||||
URL: v.URL,
|
||||
AccessKey: v.AccessKey,
|
||||
SecretKey: v.SecretKey,
|
||||
API: v.API,
|
||||
})
|
||||
return
|
||||
}
|
||||
fatalIf(errInvalidAliasedURL(alias), "No such alias `"+alias+"` found")
|
||||
}
|
||||
|
||||
var hosts []hostMessage
|
||||
for k, v := range conf.Hosts {
|
||||
hosts = append(hosts, hostMessage{
|
||||
op: "list",
|
||||
Alias: k,
|
||||
URL: v.URL,
|
||||
AccessKey: v.AccessKey,
|
||||
SecretKey: v.SecretKey,
|
||||
API: v.API,
|
||||
})
|
||||
}
|
||||
|
||||
// Sort hosts by alias names lexically.
|
||||
sort.Sort(byAlias(hosts))
|
||||
|
||||
// Display all the hosts.
|
||||
printHosts(hosts...)
|
||||
}
|
||||
@@ -1,309 +0,0 @@
|
||||
/*
|
||||
* 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 cmd
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"sort"
|
||||
"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{}
|
||||
)
|
||||
|
||||
var configHostCmd = cli.Command{
|
||||
Name: "host",
|
||||
Usage: "List, modify and remove hosts in configuration file.",
|
||||
Flags: append(configHostFlags, globalFlags...),
|
||||
Action: mainConfigHost,
|
||||
Before: setGlobalsFromContext,
|
||||
CustomHelpTemplate: `NAME:
|
||||
{{.HelpName}} - {{.Usage}}
|
||||
|
||||
USAGE:
|
||||
{{.HelpName}} OPERATION
|
||||
|
||||
OPERATION:
|
||||
add ALIAS URL ACCESS-KEY SECRET-KEY [API]
|
||||
remove ALIAS
|
||||
list
|
||||
|
||||
FLAGS:
|
||||
{{range .VisibleFlags}}{{.}}
|
||||
{{end}}
|
||||
EXAMPLES:
|
||||
1. Add Amazon S3 storage service under "myphotos" alias. For security reasons turn off bash history momentarily.
|
||||
$ set +o history
|
||||
$ {{.HelpName}} add myphotos https://s3.amazonaws.com \
|
||||
BKIKJAA5BMMU2RHO6IBB V8f1CwQqAcwo80UEIJEjc5gVQUSSx5ohQ9GSrr12
|
||||
$ set -o history
|
||||
|
||||
2. Add Amazon S3 accelerated storage service under "accel" alias. For security reasons turn off bash history momentarily.
|
||||
$ set +o history
|
||||
$ {{.HelpName}} add accel https://s3-accelerate.amazonaws.com \
|
||||
BKIKJAA5BMMU2RHO6IBB V8f1CwQqAcwo80UEIJEjc5gVQUSSx5ohQ9GSrr12
|
||||
$ set -o history
|
||||
|
||||
3. List all hosts.
|
||||
$ {{.HelpName}} list
|
||||
|
||||
4. Remove "goodisk" config.
|
||||
$ {{.HelpName}} 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("%-30.30s", h.URL))
|
||||
if h.AccessKey != "" || h.SecretKey != "" {
|
||||
message += console.Colorize("AccessKey", fmt.Sprintf(" %-20.20s", h.AccessKey))
|
||||
message += console.Colorize("SecretKey", fmt.Sprintf(" %-40.40s", h.SecretKey))
|
||||
message += console.Colorize("API", fmt.Sprintf(" %.20s", 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+"`.")
|
||||
}
|
||||
|
||||
if api != "" && !isValidAPI(api) { // Empty value set to default "S3v4".
|
||||
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) error {
|
||||
|
||||
// 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 api == "" {
|
||||
api = "S3v4"
|
||||
}
|
||||
hostCfg := hostConfigV8{
|
||||
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.
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// addHost - add a host config.
|
||||
func addHost(alias string, hostCfgV8 hostConfigV8) {
|
||||
mcCfgV8, err := loadMcConfig()
|
||||
fatalIf(err.Trace(globalMCConfigVersion), "Unable to load config `"+mustGetMcConfigPath()+"`.")
|
||||
|
||||
// Add new host.
|
||||
mcCfgV8.Hosts[alias] = hostCfgV8
|
||||
|
||||
err = saveMcConfig(mcCfgV8)
|
||||
fatalIf(err.Trace(alias), "Unable to update hosts in config version `"+mustGetMcConfigPath()+"`.")
|
||||
|
||||
printMsg(hostMessage{
|
||||
op: "add",
|
||||
Alias: alias,
|
||||
URL: hostCfgV8.URL,
|
||||
AccessKey: hostCfgV8.AccessKey,
|
||||
SecretKey: hostCfgV8.SecretKey,
|
||||
API: hostCfgV8.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})
|
||||
}
|
||||
|
||||
//byAlias is a collection satisfying sort.Interface
|
||||
type byAlias []hostMessage
|
||||
|
||||
func (d byAlias) Len() int { return len(d) }
|
||||
func (d byAlias) Swap(i, j int) { d[i], d[j] = d[j], d[i] }
|
||||
func (d byAlias) Less(i, j int) bool { return d[i].Alias < d[j].Alias }
|
||||
|
||||
// listHosts - list all host URLs.
|
||||
func listHosts() {
|
||||
conf, err := loadMcConfig()
|
||||
fatalIf(err.Trace(globalMCConfigVersion), "Unable to load config version `"+globalMCConfigVersion+"`.")
|
||||
|
||||
var maxAlias = 0
|
||||
for k := range conf.Hosts {
|
||||
if len(k) > maxAlias {
|
||||
maxAlias = len(k)
|
||||
}
|
||||
}
|
||||
|
||||
var hosts []hostMessage
|
||||
for k, v := range conf.Hosts {
|
||||
hosts = append(hosts, hostMessage{
|
||||
op: "list",
|
||||
Alias: k,
|
||||
URL: v.URL,
|
||||
AccessKey: v.AccessKey,
|
||||
SecretKey: v.SecretKey,
|
||||
API: v.API,
|
||||
})
|
||||
}
|
||||
|
||||
// Sort hosts by alias names lexically.
|
||||
sort.Sort(byAlias(hosts))
|
||||
|
||||
// Display all the hosts.
|
||||
for _, host := range hosts {
|
||||
// Format properly for alignment based on alias length.
|
||||
host.Alias = fmt.Sprintf("%-*.*s", maxAlias, maxAlias, host.Alias)
|
||||
printMsg(host)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
/*
|
||||
* Minio Client (C) 2017 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 cmd
|
||||
|
||||
import (
|
||||
"github.com/fatih/color"
|
||||
"github.com/minio/cli"
|
||||
"github.com/minio/mc/pkg/console"
|
||||
)
|
||||
|
||||
var configHostRemoveCmd = cli.Command{
|
||||
Name: "remove",
|
||||
ShortName: "rm",
|
||||
Usage: "Remove a host from configuration file.",
|
||||
Action: mainConfigHostRemove,
|
||||
Before: setGlobalsFromContext,
|
||||
Flags: globalFlags,
|
||||
HideHelpCommand: true,
|
||||
CustomHelpTemplate: `NAME:
|
||||
{{.HelpName}} - {{.Usage}}
|
||||
|
||||
USAGE:
|
||||
{{.HelpName}} ALIAS
|
||||
|
||||
FLAGS:
|
||||
{{range .VisibleFlags}}{{.}}
|
||||
{{end}}
|
||||
EXAMPLES:
|
||||
1. Remove "goodisk" from config.
|
||||
$ {{.HelpName}} goodisk
|
||||
|
||||
`,
|
||||
}
|
||||
|
||||
// checkConfigHostRemoveSyntax - verifies input arguments to 'config host remove'.
|
||||
func checkConfigHostRemoveSyntax(ctx *cli.Context) {
|
||||
args := ctx.Args()
|
||||
|
||||
if len(ctx.Args()) != 1 {
|
||||
fatalIf(errInvalidArgument().Trace(args...),
|
||||
"Incorrect number of arguments for remove host command.")
|
||||
}
|
||||
|
||||
if !isValidAlias(args.Get(0)) {
|
||||
fatalIf(errDummy().Trace(args.Get(0)),
|
||||
"Invalid alias `"+args.Get(0)+"`.")
|
||||
}
|
||||
}
|
||||
|
||||
// mainConfigHost is the handle for "mc config host rm" command.
|
||||
func mainConfigHostRemove(ctx *cli.Context) error {
|
||||
checkConfigHostRemoveSyntax(ctx)
|
||||
|
||||
console.SetColor("HostMessage", color.New(color.FgGreen))
|
||||
|
||||
args := ctx.Args()
|
||||
alias := args.Get(0)
|
||||
removeHost(alias) // Remove a host.
|
||||
return nil
|
||||
}
|
||||
|
||||
// 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})
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
/*
|
||||
* Minio Client (C) 2017 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 cmd
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"github.com/minio/cli"
|
||||
"github.com/minio/mc/pkg/console"
|
||||
"github.com/minio/minio/pkg/probe"
|
||||
)
|
||||
|
||||
var configHostCmd = cli.Command{
|
||||
Name: "host",
|
||||
Usage: "List, modify and remove hosts in configuration file.",
|
||||
Action: mainConfigHost,
|
||||
Before: setGlobalsFromContext,
|
||||
Flags: globalFlags,
|
||||
Subcommands: []cli.Command{
|
||||
configHostAddCmd,
|
||||
configHostRemoveCmd,
|
||||
configHostListCmd,
|
||||
},
|
||||
HideHelpCommand: true,
|
||||
}
|
||||
|
||||
// mainConfigHost is the handle for "mc config host" command.
|
||||
func mainConfigHost(ctx *cli.Context) error {
|
||||
cli.ShowCommandHelp(ctx, ctx.Args().First())
|
||||
return nil
|
||||
// Sub-commands like "remove", "list" have their own main.
|
||||
}
|
||||
|
||||
// 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("%-30.30s", h.URL))
|
||||
if h.AccessKey != "" || h.SecretKey != "" {
|
||||
message += console.Colorize("AccessKey", fmt.Sprintf(" %-20.20s", h.AccessKey))
|
||||
message += console.Colorize("SecretKey", fmt.Sprintf(" %-40.40s", h.SecretKey))
|
||||
message += console.Colorize("API", fmt.Sprintf(" %.20s", 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)
|
||||
}
|
||||
Reference in New Issue
Block a user