Merge pull request #1073 from harshavardhana/add-alias-host-command
Move primitives for add, list, remove into their own subcommands as a…
This commit is contained in:
+2
-2
@@ -124,7 +124,7 @@ func mainAccess(ctx *cli.Context) {
|
||||
case "set":
|
||||
perms := bucketPerms(ctx.Args().Tail().Get(0))
|
||||
for _, arg := range ctx.Args().Tail().Tail() {
|
||||
targetURL := aliasExpand(arg, config.Aliases)
|
||||
targetURL := getAliasURL(arg, config.Aliases)
|
||||
|
||||
fatalIf(doSetAccess(targetURL, perms).Trace(targetURL, string(perms)), "Unable to set access permission ‘"+string(perms)+"’ for ‘"+targetURL+"’.")
|
||||
|
||||
@@ -137,7 +137,7 @@ func mainAccess(ctx *cli.Context) {
|
||||
}
|
||||
case "get":
|
||||
for _, arg := range ctx.Args().Tail() {
|
||||
targetURL := aliasExpand(arg, config.Aliases)
|
||||
targetURL := getAliasURL(arg, config.Aliases)
|
||||
perms, err := doGetAccess(targetURL)
|
||||
fatalIf(err.Trace(targetURL), "Unable to get access permission for ‘"+targetURL+"’.")
|
||||
|
||||
|
||||
@@ -31,8 +31,8 @@ func isValidAliasName(aliasName string) bool {
|
||||
return validAliasName.MatchString(aliasName)
|
||||
}
|
||||
|
||||
// aliasExpand expands aliased (name:/path) to full URL, used by url-parser.
|
||||
func aliasExpand(aliasedURL string, aliases map[string]string) string {
|
||||
// getAliasURL expands aliased (name:/path) to full URL, used by url-parser.
|
||||
func getAliasURL(aliasedURL string, aliases map[string]string) string {
|
||||
for aliasName, aliasValue := range aliases {
|
||||
if strings.HasPrefix(aliasedURL, aliasName) {
|
||||
// Match found. Expand it.
|
||||
+1
-1
@@ -89,7 +89,7 @@ func mainCat(ctx *cli.Context) {
|
||||
|
||||
func catURL(sourceURL string) *probe.Error {
|
||||
config := mustGetMcConfig()
|
||||
URL := aliasExpand(sourceURL, config.Aliases)
|
||||
URL := getAliasURL(sourceURL, config.Aliases)
|
||||
|
||||
sourceClnt, err := source2Client(URL)
|
||||
if err != nil {
|
||||
|
||||
+7
-7
@@ -252,25 +252,25 @@ func (s *CmdTestSuite) TestIsvalidAliasName(c *C) {
|
||||
}
|
||||
|
||||
func (s *CmdTestSuite) TestEmptyExpansions(c *C) {
|
||||
url := aliasExpand("hello", nil)
|
||||
url := getAliasURL("hello", nil)
|
||||
c.Assert(url, Equals, "hello")
|
||||
|
||||
url = aliasExpand("minio://hello", nil)
|
||||
url = getAliasURL("minio://hello", nil)
|
||||
c.Assert(url, Equals, "minio://hello")
|
||||
|
||||
url = aliasExpand("$#\\", nil)
|
||||
url = getAliasURL("$#\\", nil)
|
||||
c.Assert(url, Equals, "$#\\")
|
||||
|
||||
url = aliasExpand("foo/bar", map[string]string{"foo": "http://foo"})
|
||||
url = getAliasURL("foo/bar", map[string]string{"foo": "http://foo"})
|
||||
c.Assert(url, Equals, "http://foo/bar")
|
||||
|
||||
url = aliasExpand("myfoo/bar", nil)
|
||||
url = getAliasURL("myfoo/bar", nil)
|
||||
c.Assert(url, Equals, "myfoo/bar")
|
||||
|
||||
url = aliasExpand("", nil)
|
||||
url = getAliasURL("", nil)
|
||||
c.Assert(url, Equals, "")
|
||||
|
||||
url = aliasExpand("hello", nil)
|
||||
url = getAliasURL("hello", nil)
|
||||
c.Assert(url, Equals, "hello")
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,224 @@
|
||||
/*
|
||||
* 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
|
||||
|
||||
// listAliases - list alias
|
||||
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"
|
||||
"github.com/minio/minio/pkg/quick"
|
||||
)
|
||||
|
||||
var configAliasCmd = cli.Command{
|
||||
Name: "alias",
|
||||
Usage: "Manage aliases in configuration file.",
|
||||
Action: mainConfigAlias,
|
||||
CustomHelpTemplate: `NAME:
|
||||
mc config {{.Name}} - {{.Usage}}
|
||||
|
||||
USAGE:
|
||||
mc config {{.Name}} OPERATION [ARGS...]
|
||||
|
||||
OPERATION = add | list | remove
|
||||
|
||||
EXAMPLES:
|
||||
1. Add aliases for a URL
|
||||
$ mc config {{.Name}} add mcloud https://s3.amazonaws.com/miniocloud
|
||||
$ mc ls mcloud
|
||||
$ mc cp /bin/true mcloud/true
|
||||
|
||||
2. List all aliased URLs.
|
||||
$ mc config {{.Name}} list
|
||||
|
||||
3. Remove an alias
|
||||
$ mc config {{.Name}} remove zek
|
||||
|
||||
`,
|
||||
}
|
||||
|
||||
// AliasMessage container for content message structure
|
||||
type AliasMessage struct {
|
||||
op string
|
||||
Alias string `json:"alias"`
|
||||
URL string `json:"url,omitempty"`
|
||||
}
|
||||
|
||||
// String string printer for Content metadata
|
||||
func (a AliasMessage) String() string {
|
||||
if !globalJSONFlag {
|
||||
if a.op == "list" {
|
||||
message := console.Colorize("Alias", fmt.Sprintf("[%s] <- ", a.Alias))
|
||||
message += console.Colorize("URL", fmt.Sprintf("%s", a.URL))
|
||||
return message
|
||||
}
|
||||
if a.op == "remove" {
|
||||
return console.Colorize("AliasMessage", "Removed alias ‘"+a.Alias+"’ successfully.")
|
||||
}
|
||||
if a.op == "add" {
|
||||
return console.Colorize("AliasMessage", "Added alias ‘"+a.Alias+"’ successfully.")
|
||||
}
|
||||
}
|
||||
jsonMessageBytes, e := json.Marshal(a)
|
||||
fatalIf(probe.NewError(e), "Unable to marshal into JSON.")
|
||||
|
||||
return string(jsonMessageBytes)
|
||||
}
|
||||
|
||||
func checkConfigAliasSyntax(ctx *cli.Context) {
|
||||
// show help if nothing is set
|
||||
if !ctx.Args().Present() || ctx.Args().First() == "help" {
|
||||
cli.ShowCommandHelpAndExit(ctx, "alias", 1) // last argument is exit code
|
||||
}
|
||||
if strings.TrimSpace(ctx.Args().First()) == "" {
|
||||
cli.ShowCommandHelpAndExit(ctx, "alias", 1) // last argument is exit code
|
||||
}
|
||||
if len(ctx.Args().Tail()) > 2 {
|
||||
fatalIf(errDummy().Trace(), "Incorrect number of arguments to alias command")
|
||||
}
|
||||
switch strings.TrimSpace(ctx.Args().Get(0)) {
|
||||
case "add":
|
||||
if len(ctx.Args().Tail()) != 2 {
|
||||
fatalIf(errInvalidArgument().Trace(), "Incorrect number of arguments for add alias command.")
|
||||
}
|
||||
case "remove":
|
||||
if len(ctx.Args().Tail()) != 1 {
|
||||
fatalIf(errInvalidArgument().Trace(), "Incorrect number of arguments for remove alias command.")
|
||||
}
|
||||
case "list":
|
||||
default:
|
||||
cli.ShowCommandHelpAndExit(ctx, "alias", 1) // last argument is exit code
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
func mainConfigAlias(ctx *cli.Context) {
|
||||
checkConfigAliasSyntax(ctx)
|
||||
|
||||
// set new custom coloring
|
||||
console.SetCustomTheme(map[string]*color.Color{
|
||||
"Alias": color.New(color.FgCyan, color.Bold),
|
||||
"AliasMessage": color.New(color.FgGreen, color.Bold),
|
||||
"URL": color.New(color.FgWhite, color.Bold),
|
||||
})
|
||||
|
||||
arg := ctx.Args().First()
|
||||
tailArgs := ctx.Args().Tail()
|
||||
|
||||
switch strings.TrimSpace(arg) {
|
||||
case "add":
|
||||
addAlias(tailArgs.Get(0), tailArgs.Get(1))
|
||||
case "remove":
|
||||
removeAlias(tailArgs.Get(0))
|
||||
case "list":
|
||||
listAliases()
|
||||
}
|
||||
}
|
||||
|
||||
func listAliases() {
|
||||
config, err := newConfig()
|
||||
fatalIf(err.Trace(globalMCConfigVersion), "Failed to initialize ‘quick’ configuration data structure.")
|
||||
|
||||
configPath := mustGetMcConfigPath()
|
||||
err = config.Load(configPath)
|
||||
fatalIf(err.Trace(configPath), "Unable to load config path")
|
||||
|
||||
// convert interface{} back to its original struct
|
||||
newConf := config.Data().(*configV3)
|
||||
for k, v := range newConf.Aliases {
|
||||
console.Println(AliasMessage{
|
||||
op: "list",
|
||||
Alias: k,
|
||||
URL: v,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// removeAlias - remove alias
|
||||
func removeAlias(alias string) {
|
||||
if strings.TrimSpace(alias) == "" {
|
||||
fatalIf(errDummy().Trace(), "Alias or URL cannot be empty.")
|
||||
}
|
||||
config, err := newConfig()
|
||||
fatalIf(err.Trace(), "Failed to initialize ‘quick’ configuration data structure.")
|
||||
|
||||
configPath := mustGetMcConfigPath()
|
||||
err = config.Load(configPath)
|
||||
fatalIf(err.Trace(configPath), "Unable to load config path")
|
||||
if !isValidAliasName(alias) {
|
||||
fatalIf(errDummy().Trace(), fmt.Sprintf("Alias name ‘%s’ is invalid, valid examples are: mybucket, Area51, Grand-Nagus", alias))
|
||||
}
|
||||
|
||||
// convert interface{} back to its original struct
|
||||
newConf := config.Data().(*configV3)
|
||||
if _, ok := newConf.Aliases[alias]; !ok {
|
||||
fatalIf(errDummy().Trace(), fmt.Sprintf("Alias ‘%s’ does not exist.", alias))
|
||||
}
|
||||
delete(newConf.Aliases, alias)
|
||||
|
||||
newConfig, err := quick.New(newConf)
|
||||
fatalIf(err.Trace(globalMCConfigVersion), "Failed to initialize ‘quick’ configuration data structure.")
|
||||
err = writeConfig(newConfig)
|
||||
fatalIf(err.Trace(alias), "Unable to save alias ‘"+alias+"’.")
|
||||
|
||||
console.Println(AliasMessage{
|
||||
op: "remove",
|
||||
Alias: alias,
|
||||
})
|
||||
}
|
||||
|
||||
// addAlias - add new aliases
|
||||
func addAlias(alias, url string) {
|
||||
if alias == "" || url == "" {
|
||||
fatalIf(errDummy().Trace(), "Alias or URL cannot be empty.")
|
||||
}
|
||||
config, err := newConfig()
|
||||
fatalIf(err.Trace(globalMCConfigVersion), "Failed to initialize ‘quick’ configuration data structure.")
|
||||
|
||||
err = config.Load(mustGetMcConfigPath())
|
||||
fatalIf(err.Trace(), "Unable to load config path")
|
||||
|
||||
url = strings.TrimSuffix(url, "/")
|
||||
if !strings.HasPrefix(url, "http") {
|
||||
fatalIf(errDummy().Trace(), fmt.Sprintf("Invalid alias URL ‘%s’. Valid examples are: http://s3.amazonaws.com, https://yourbucket.example.com.", url))
|
||||
}
|
||||
if !isValidAliasName(alias) {
|
||||
fatalIf(errDummy().Trace(), fmt.Sprintf("Alias name ‘%s’ is invalid, valid examples are: mybucket, Area51, Grand-Nagus", alias))
|
||||
}
|
||||
// convert interface{} back to its original struct
|
||||
newConf := config.Data().(*configV3)
|
||||
if oldURL, ok := newConf.Aliases[alias]; ok {
|
||||
fatalIf(errDummy().Trace(), fmt.Sprintf("Alias ‘%s’ already exists for ‘%s’.", alias, oldURL))
|
||||
}
|
||||
newConf.Aliases[alias] = url
|
||||
newConfig, err := quick.New(newConf)
|
||||
fatalIf(err.Trace(globalMCConfigVersion), "Failed to initialize ‘quick’ configuration data structure.")
|
||||
|
||||
err = writeConfig(newConfig)
|
||||
fatalIf(err.Trace(alias, url), "Unable to save alias ‘"+alias+"’.")
|
||||
|
||||
console.Println(AliasMessage{
|
||||
op: "add",
|
||||
Alias: alias,
|
||||
URL: url,
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,234 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/fatih/color"
|
||||
"github.com/minio/cli"
|
||||
"github.com/minio/mc/pkg/console"
|
||||
"github.com/minio/minio/pkg/probe"
|
||||
"github.com/minio/minio/pkg/quick"
|
||||
)
|
||||
|
||||
var configHostCmd = cli.Command{
|
||||
Name: "host",
|
||||
Usage: "Manage hosts in configuration file.",
|
||||
Action: mainConfigHost,
|
||||
CustomHelpTemplate: `NAME:
|
||||
mc config {{.Name}} - {{.Usage}}
|
||||
|
||||
USAGE:
|
||||
mc config {{.Name}} OPERATION [ARGS...]
|
||||
|
||||
OPERATION = add | list | remove
|
||||
|
||||
EXAMPLES:
|
||||
1. Add host configuration for a URL.
|
||||
$ mc config {{.Name}} add s3.amazonaws.com BKIKJAA5BMMU2RHO6IBB V7f1C\wQqAcwo80UEIJEjc5gVQUSSx5ohQ9GSrr1
|
||||
|
||||
2. List all hosts.
|
||||
$ mc config {{.Name}} list
|
||||
|
||||
3. Remove host config.
|
||||
$ mc config {{.Name}} remove s3.amazonaws.com
|
||||
|
||||
`,
|
||||
}
|
||||
|
||||
// HostMessage container for content message structure
|
||||
type HostMessage struct {
|
||||
op string
|
||||
Host string `json:"host"`
|
||||
AccessKeyID string `json:"access-key-id,omitempty"`
|
||||
SecretAccessKey string `json:"secret-access-key,omitempty"`
|
||||
}
|
||||
|
||||
// String string printer for Content metadata
|
||||
func (a HostMessage) String() string {
|
||||
if !globalJSONFlag {
|
||||
if a.op == "list" {
|
||||
message := console.Colorize("Host", fmt.Sprintf("[%s] ", a.Host))
|
||||
if a.AccessKeyID != "" || a.SecretAccessKey != "" {
|
||||
message += console.Colorize("AccessKeyID", fmt.Sprintf("<- %s,", a.AccessKeyID))
|
||||
message += console.Colorize("SecretAccessKey", fmt.Sprintf(" %s", a.SecretAccessKey))
|
||||
}
|
||||
return message
|
||||
}
|
||||
if a.op == "remove" {
|
||||
return console.Colorize("HostMessage", "Removed host ‘"+a.Host+"’ successfully.")
|
||||
}
|
||||
if a.op == "add" {
|
||||
return console.Colorize("HostMessage", "Added host ‘"+a.Host+"’ successfully.")
|
||||
}
|
||||
}
|
||||
jsonMessageBytes, e := json.Marshal(a)
|
||||
fatalIf(probe.NewError(e), "Unable to marshal into JSON.")
|
||||
|
||||
return string(jsonMessageBytes)
|
||||
}
|
||||
|
||||
func checkConfigHostSyntax(ctx *cli.Context) {
|
||||
// show help if nothing is set
|
||||
if !ctx.Args().Present() || ctx.Args().First() == "help" {
|
||||
cli.ShowCommandHelpAndExit(ctx, "host", 1) // last argument is exit code
|
||||
}
|
||||
if strings.TrimSpace(ctx.Args().First()) == "" {
|
||||
cli.ShowCommandHelpAndExit(ctx, "host", 1) // last argument is exit code
|
||||
}
|
||||
if len(ctx.Args().Tail()) > 3 {
|
||||
fatalIf(errDummy().Trace(), "Incorrect number of arguments to host command")
|
||||
}
|
||||
switch strings.TrimSpace(ctx.Args().First()) {
|
||||
case "add":
|
||||
if len(ctx.Args().Tail()) != 3 {
|
||||
fatalIf(errInvalidArgument().Trace(), "Incorrect number of arguments for add host command.")
|
||||
}
|
||||
case "remove":
|
||||
if len(ctx.Args().Tail()) != 1 {
|
||||
fatalIf(errInvalidArgument().Trace(), "Incorrect number of arguments for remove host command.")
|
||||
}
|
||||
case "list":
|
||||
default:
|
||||
cli.ShowCommandHelpAndExit(ctx, "host", 1) // last argument is exit code
|
||||
}
|
||||
}
|
||||
|
||||
func mainConfigHost(ctx *cli.Context) {
|
||||
checkConfigHostSyntax(ctx)
|
||||
|
||||
// set new custom coloring
|
||||
console.SetCustomTheme(map[string]*color.Color{
|
||||
"Host": color.New(color.FgCyan, color.Bold),
|
||||
"AccessKeyID": color.New(color.FgBlue, color.Bold),
|
||||
"SecretAccessKey": color.New(color.FgRed, color.Bold),
|
||||
"HostMessage": color.New(color.FgGreen, color.Bold),
|
||||
})
|
||||
|
||||
arg := ctx.Args().First()
|
||||
tailArgs := ctx.Args().Tail()
|
||||
|
||||
switch strings.TrimSpace(arg) {
|
||||
case "add":
|
||||
addHost(tailArgs.Get(0), tailArgs.Get(1), tailArgs.Get(2))
|
||||
case "remove":
|
||||
removeHost(tailArgs.Get(0))
|
||||
case "list":
|
||||
listHosts()
|
||||
}
|
||||
}
|
||||
|
||||
func listHosts() {
|
||||
config, err := newConfig()
|
||||
fatalIf(err.Trace(), "Failed to initialize ‘quick’ configuration data structure.")
|
||||
|
||||
configPath := mustGetMcConfigPath()
|
||||
err = config.Load(configPath)
|
||||
fatalIf(err.Trace(configPath), "Unable to load config path")
|
||||
|
||||
// convert interface{} back to its original struct
|
||||
newConf := config.Data().(*configV3)
|
||||
for k, v := range newConf.Hosts {
|
||||
console.Println(HostMessage{
|
||||
op: "list",
|
||||
Host: k,
|
||||
AccessKeyID: v.AccessKeyID,
|
||||
SecretAccessKey: v.SecretAccessKey,
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func removeHost(hostGlob string) {
|
||||
if strings.TrimSpace(hostGlob) == "" {
|
||||
fatalIf(errDummy().Trace(), "Alias or URL cannot be empty.")
|
||||
}
|
||||
if strings.TrimSpace(hostGlob) == "dl.minio.io:9000" {
|
||||
fatalIf(errDummy().Trace(), "‘"+hostGlob+"’ is reserved hostname and cannot be removed.")
|
||||
}
|
||||
config, err := newConfig()
|
||||
fatalIf(err.Trace(globalMCConfigVersion), "Failed to initialize ‘quick’ configuration data structure.")
|
||||
|
||||
configPath := mustGetMcConfigPath()
|
||||
err = config.Load(configPath)
|
||||
fatalIf(err.Trace(configPath), "Unable to load config path")
|
||||
|
||||
// convert interface{} back to its original struct
|
||||
newConf := config.Data().(*configV3)
|
||||
if _, ok := newConf.Hosts[hostGlob]; !ok {
|
||||
fatalIf(errDummy().Trace(), fmt.Sprintf("Host glob ‘%s’ does not exist.", hostGlob))
|
||||
}
|
||||
delete(newConf.Hosts, hostGlob)
|
||||
|
||||
newConfig, err := quick.New(newConf)
|
||||
fatalIf(err.Trace(globalMCConfigVersion), "Failed to initialize ‘quick’ configuration data structure.")
|
||||
err = writeConfig(newConfig)
|
||||
fatalIf(err.Trace(hostGlob), "Unable to save host glob ‘"+hostGlob+"’.")
|
||||
|
||||
console.Println(HostMessage{
|
||||
op: "remove",
|
||||
Host: hostGlob,
|
||||
})
|
||||
}
|
||||
|
||||
// isValidSecretKey - validate secret key
|
||||
func isValidSecretKey(secretAccessKey string) bool {
|
||||
if secretAccessKey == "" {
|
||||
return true
|
||||
}
|
||||
regex := regexp.MustCompile("^.{40}$")
|
||||
return regex.MatchString(secretAccessKey)
|
||||
}
|
||||
|
||||
// isValidAccessKey - validate access key
|
||||
func isValidAccessKey(accessKeyID string) bool {
|
||||
if accessKeyID == "" {
|
||||
return true
|
||||
}
|
||||
regex := regexp.MustCompile("^[A-Z0-9\\-\\.\\_\\~]{20}$")
|
||||
return regex.MatchString(accessKeyID)
|
||||
}
|
||||
|
||||
// addHosts - add new hosts
|
||||
func addHost(hostGlob, accessKeyID, secretAccessKey string) {
|
||||
if strings.TrimSpace(hostGlob) == "" {
|
||||
fatalIf(errDummy().Trace(), "Unable to proceed, empty arguments provided.")
|
||||
}
|
||||
config, err := newConfig()
|
||||
fatalIf(err.Trace(globalMCConfigVersion), "Failed to initialize ‘quick’ configuration data structure.")
|
||||
|
||||
configPath := mustGetMcConfigPath()
|
||||
err = config.Load(configPath)
|
||||
fatalIf(err.Trace(configPath), "Unable to load config path")
|
||||
|
||||
if len(accessKeyID) != 0 {
|
||||
if !isValidAccessKey(accessKeyID) {
|
||||
fatalIf(errInvalidArgument().Trace(), "Invalid access key id provided.")
|
||||
}
|
||||
}
|
||||
if len(secretAccessKey) != 0 {
|
||||
if !isValidSecretKey(secretAccessKey) {
|
||||
fatalIf(errInvalidArgument().Trace(), "Invalid secret access key provided.")
|
||||
}
|
||||
}
|
||||
// convert interface{} back to its original struct
|
||||
newConf := config.Data().(*configV3)
|
||||
newConf.Hosts[hostGlob] = hostConfig{
|
||||
AccessKeyID: accessKeyID,
|
||||
SecretAccessKey: secretAccessKey,
|
||||
}
|
||||
newConfig, err := quick.New(newConf)
|
||||
fatalIf(err.Trace(globalMCConfigVersion), "Failed to initialize ‘quick’ configuration data structure.")
|
||||
|
||||
err = writeConfig(newConfig)
|
||||
fatalIf(err.Trace(hostGlob), "Unable to save host glob ‘"+hostGlob+"’.")
|
||||
|
||||
console.Println(HostMessage{
|
||||
op: "add",
|
||||
Host: hostGlob,
|
||||
AccessKeyID: accessKeyID,
|
||||
SecretAccessKey: secretAccessKey,
|
||||
})
|
||||
}
|
||||
+14
-215
@@ -16,17 +16,7 @@
|
||||
|
||||
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"
|
||||
"github.com/minio/minio/pkg/quick"
|
||||
)
|
||||
import "github.com/minio/cli"
|
||||
|
||||
// Configure minio client
|
||||
//
|
||||
@@ -40,218 +30,27 @@ import (
|
||||
//
|
||||
var configCmd = cli.Command{
|
||||
Name: "config",
|
||||
Usage: "Modify, add, remove alias from default configuration file [~/.mc/config.json].",
|
||||
Usage: "Collection of commands for managing configuration file.",
|
||||
Action: mainConfig,
|
||||
Subcommands: []cli.Command{
|
||||
configAliasCmd,
|
||||
configHostCmd,
|
||||
},
|
||||
CustomHelpTemplate: `NAME:
|
||||
mc {{.Name}} - {{.Usage}}
|
||||
{{.Name}} - {{.Usage}}
|
||||
|
||||
USAGE:
|
||||
mc {{.Name}} add OPTION [ARGS...]
|
||||
mc {{.Name}} list OPTION [ARGS...]
|
||||
mc {{.Name}} remove OPTION [ARGS...]
|
||||
{{.Name}} {{if .Flags}}[global flags] {{end}}command{{if .Flags}} [command flags]{{end}} [arguments...]
|
||||
|
||||
OPTION = alias
|
||||
|
||||
EXAMPLES:
|
||||
1. Add aliases for a URL
|
||||
$ mc {{.Name}} add alias mcloud https://s3.amazonaws.com/miniocloud
|
||||
$ mc ls mcloud
|
||||
$ mc cp /bin/true mcloud/true
|
||||
|
||||
2. List all aliased URLs.
|
||||
$ mc {{.Name}} list alias
|
||||
|
||||
3. Remove an alias
|
||||
$ mc {{.Name}} remove alias zek
|
||||
COMMANDS:
|
||||
{{range .Commands}}{{ .Name }}{{ "\t" }}{{.Usage}}
|
||||
{{end}}
|
||||
`,
|
||||
}
|
||||
|
||||
// AliasMessage container for content message structure
|
||||
type AliasMessage struct {
|
||||
op string
|
||||
Alias string `json:"alias"`
|
||||
URL string `json:"url,omitempty"`
|
||||
}
|
||||
|
||||
// String string printer for Content metadata
|
||||
func (a AliasMessage) String() string {
|
||||
if !globalJSONFlag {
|
||||
if a.op == "list" {
|
||||
message := console.Colorize("Alias", fmt.Sprintf("[%s] <- ", a.Alias))
|
||||
message += console.Colorize("URL", fmt.Sprintf("%s", a.URL))
|
||||
return message
|
||||
}
|
||||
if a.op == "remove" {
|
||||
return console.Colorize("AliasMessage", "Removed alias ‘"+a.Alias+"’ successfully.")
|
||||
}
|
||||
if a.op == "add" {
|
||||
return console.Colorize("AliasMessage", "Added alias ‘"+a.Alias+"’ successfully.")
|
||||
}
|
||||
}
|
||||
jsonMessageBytes, e := json.Marshal(a)
|
||||
fatalIf(probe.NewError(e), "Unable to marshal into JSON.")
|
||||
|
||||
return string(jsonMessageBytes)
|
||||
}
|
||||
|
||||
func checkConfigSyntax(ctx *cli.Context) {
|
||||
// show help if nothing is set
|
||||
if !ctx.Args().Present() || ctx.Args().First() == "help" {
|
||||
cli.ShowCommandHelpAndExit(ctx, "config", 1) // last argument is exit code
|
||||
}
|
||||
if strings.TrimSpace(ctx.Args().First()) == "" {
|
||||
cli.ShowCommandHelpAndExit(ctx, "config", 1) // last argument is exit code
|
||||
}
|
||||
if len(ctx.Args().Tail()) > 3 {
|
||||
fatalIf(errDummy().Trace(), "Incorrect number of arguments to config command")
|
||||
}
|
||||
switch strings.TrimSpace(ctx.Args().First()) {
|
||||
case "add":
|
||||
if strings.TrimSpace(ctx.Args().Tail().First()) != "alias" {
|
||||
cli.ShowCommandHelpAndExit(ctx, "config", 1) // last argument is exit code
|
||||
}
|
||||
if strings.TrimSpace(ctx.Args().Tail().First()) == "alias" {
|
||||
if len(ctx.Args().Tail().Tail()) != 2 {
|
||||
fatalIf(errInvalidArgument().Trace(), "Incorrect number of arguments for add alias command.")
|
||||
}
|
||||
}
|
||||
case "remove":
|
||||
if strings.TrimSpace(ctx.Args().Tail().First()) != "alias" {
|
||||
cli.ShowCommandHelpAndExit(ctx, "config", 1) // last argument is exit code
|
||||
}
|
||||
if strings.TrimSpace(ctx.Args().Tail().First()) == "alias" {
|
||||
if len(ctx.Args().Tail().Tail()) != 1 {
|
||||
fatalIf(errInvalidArgument().Trace(), "Incorrect number of arguments for remove alias command.")
|
||||
}
|
||||
}
|
||||
case "list":
|
||||
if strings.TrimSpace(ctx.Args().Tail().First()) != "alias" {
|
||||
cli.ShowCommandHelpAndExit(ctx, "config", 1) // last argument is exit code
|
||||
}
|
||||
default:
|
||||
cli.ShowCommandHelpAndExit(ctx, "config", 1) // last argument is exit code
|
||||
}
|
||||
}
|
||||
|
||||
// mainConfig is the handle for "mc config" sub-command. writes configuration data in json format to config file.
|
||||
// mainConfig is the handle for "mc config" command. provides sub-commands which write configuration data in json format to config file.
|
||||
func mainConfig(ctx *cli.Context) {
|
||||
checkConfigSyntax(ctx)
|
||||
|
||||
// set new custom coloring
|
||||
console.SetCustomTheme(map[string]*color.Color{
|
||||
"Alias": color.New(color.FgCyan, color.Bold),
|
||||
"AliasMessage": color.New(color.FgGreen, color.Bold),
|
||||
"URL": color.New(color.FgWhite),
|
||||
})
|
||||
|
||||
arg := ctx.Args().First()
|
||||
tailArgs := ctx.Args().Tail()
|
||||
|
||||
switch strings.TrimSpace(arg) {
|
||||
case "add":
|
||||
if strings.TrimSpace(tailArgs.First()) == "alias" {
|
||||
addAlias(tailArgs.Get(1), tailArgs.Get(2))
|
||||
}
|
||||
case "remove":
|
||||
if strings.TrimSpace(tailArgs.First()) == "alias" {
|
||||
removeAlias(tailArgs.Get(1))
|
||||
}
|
||||
case "list":
|
||||
if strings.TrimSpace(tailArgs.First()) == "alias" {
|
||||
listAliases()
|
||||
}
|
||||
if !ctx.Args().Present() || ctx.Args().First() == "help" {
|
||||
cli.ShowAppHelp(ctx)
|
||||
}
|
||||
}
|
||||
|
||||
// listAliases - list alias
|
||||
func listAliases() {
|
||||
conf := newConfigV3()
|
||||
config, err := quick.New(conf)
|
||||
fatalIf(err.Trace(conf.Version), "Failed to initialize ‘quick’ configuration data structure.")
|
||||
|
||||
configPath := mustGetMcConfigPath()
|
||||
err = config.Load(configPath)
|
||||
fatalIf(err.Trace(configPath), "Unable to load config path")
|
||||
|
||||
// convert interface{} back to its original struct
|
||||
newConf := config.Data().(*configV3)
|
||||
for k, v := range newConf.Aliases {
|
||||
console.Println(AliasMessage{
|
||||
op: "list",
|
||||
Alias: k,
|
||||
URL: v,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// removeAlias - remove alias
|
||||
func removeAlias(alias string) {
|
||||
if alias == "" {
|
||||
fatalIf(errDummy().Trace(), "Alias or URL cannot be empty.")
|
||||
}
|
||||
conf := newConfigV3()
|
||||
config, err := quick.New(conf)
|
||||
fatalIf(err.Trace(conf.Version), "Failed to initialize ‘quick’ configuration data structure.")
|
||||
|
||||
err = config.Load(mustGetMcConfigPath())
|
||||
fatalIf(err.Trace(), "Unable to load config path")
|
||||
if !isValidAliasName(alias) {
|
||||
fatalIf(errDummy().Trace(), fmt.Sprintf("Alias name ‘%s’ is invalid, valid examples are: mybucket, Area51, Grand-Nagus", alias))
|
||||
}
|
||||
// convert interface{} back to its original struct
|
||||
newConf := config.Data().(*configV3)
|
||||
|
||||
if _, ok := newConf.Aliases[alias]; !ok {
|
||||
fatalIf(errDummy().Trace(), fmt.Sprintf("Alias ‘%s’ does not exist.", alias))
|
||||
}
|
||||
delete(newConf.Aliases, alias)
|
||||
|
||||
newConfig, err := quick.New(newConf)
|
||||
fatalIf(err.Trace(conf.Version), "Failed to initialize ‘quick’ configuration data structure.")
|
||||
|
||||
err = writeConfig(newConfig)
|
||||
fatalIf(err.Trace(alias), "Unable to save alias ‘"+alias+"’.")
|
||||
|
||||
console.Println(AliasMessage{
|
||||
op: "remove",
|
||||
Alias: alias,
|
||||
})
|
||||
}
|
||||
|
||||
// addAlias - add new aliases
|
||||
func addAlias(alias, url string) {
|
||||
if alias == "" || url == "" {
|
||||
fatalIf(errDummy().Trace(), "Alias or URL cannot be empty.")
|
||||
}
|
||||
conf := newConfigV3()
|
||||
config, err := quick.New(conf)
|
||||
fatalIf(err.Trace(conf.Version), "Failed to initialize ‘quick’ configuration data structure.")
|
||||
|
||||
err = config.Load(mustGetMcConfigPath())
|
||||
fatalIf(err.Trace(), "Unable to load config path")
|
||||
|
||||
url = strings.TrimSuffix(url, "/")
|
||||
if !strings.HasPrefix(url, "http") {
|
||||
fatalIf(errDummy().Trace(), fmt.Sprintf("Invalid alias URL ‘%s’. Valid examples are: http://s3.amazonaws.com, https://yourbucket.example.com.", url))
|
||||
}
|
||||
if !isValidAliasName(alias) {
|
||||
fatalIf(errDummy().Trace(), fmt.Sprintf("Alias name ‘%s’ is invalid, valid examples are: mybucket, Area51, Grand-Nagus", alias))
|
||||
}
|
||||
// convert interface{} back to its original struct
|
||||
newConf := config.Data().(*configV3)
|
||||
if oldURL, ok := newConf.Aliases[alias]; ok {
|
||||
fatalIf(errDummy().Trace(), fmt.Sprintf("Alias ‘%s’ already exists for ‘%s’.", alias, oldURL))
|
||||
}
|
||||
newConf.Aliases[alias] = url
|
||||
newConfig, err := quick.New(newConf)
|
||||
fatalIf(err.Trace(conf.Version), "Failed to initialize ‘quick’ configuration data structure.")
|
||||
|
||||
err = writeConfig(newConfig)
|
||||
fatalIf(err.Trace(alias, url), "Unable to save alias ‘"+alias+"’.")
|
||||
|
||||
console.Println(AliasMessage{
|
||||
op: "add",
|
||||
Alias: alias,
|
||||
URL: url,
|
||||
})
|
||||
}
|
||||
|
||||
+35
-3
@@ -23,20 +23,52 @@ import (
|
||||
. "gopkg.in/check.v1"
|
||||
)
|
||||
|
||||
func (s *CmdTestSuite) TestConfigContext(c *C) {
|
||||
func (s *CmdTestSuite) TestConfigAliasContext(c *C) {
|
||||
console.IsExited = false
|
||||
|
||||
err := app.Run([]string{os.Args[0], "config", "add", "alias", "test", "htt://test.io"})
|
||||
err := app.Run([]string{os.Args[0], "config", "alias", "add", "test", "htt://test.io"})
|
||||
c.Assert(err, IsNil)
|
||||
c.Assert(console.IsExited, Equals, true)
|
||||
|
||||
// reset back
|
||||
console.IsExited = false
|
||||
|
||||
err = app.Run([]string{os.Args[0], "config", "add", "alias", "new", "http://test.io"})
|
||||
err = app.Run([]string{os.Args[0], "config", "alias", "add", "new", "http://test.io"})
|
||||
c.Assert(err, IsNil)
|
||||
c.Assert(console.IsExited, Equals, false)
|
||||
|
||||
// reset back
|
||||
console.IsExited = false
|
||||
}
|
||||
|
||||
func (s *CmdTestSuite) TestConfigHostContext(c *C) {
|
||||
console.IsExited = false
|
||||
|
||||
err := app.Run([]string{os.Args[0], "config", "host", "add", "*test.io", "invalid", "invalid"})
|
||||
c.Assert(err, IsNil)
|
||||
c.Assert(console.IsExited, Equals, true)
|
||||
|
||||
// reset back
|
||||
console.IsExited = false
|
||||
|
||||
err = app.Run([]string{os.Args[0], "config", "host", "add", "*my-example.com", "AKIKJAA5BMMU2RHO6IBB", "V7f1CCwQqAcwo80UEIJEjc5gVQUSSx5ohQ9GSrr9"})
|
||||
c.Assert(err, IsNil)
|
||||
c.Assert(console.IsExited, Equals, false)
|
||||
|
||||
// reset back
|
||||
console.IsExited = false
|
||||
|
||||
err = app.Run([]string{os.Args[0], "config", "host", "remove", "*my-example.com"})
|
||||
c.Assert(err, IsNil)
|
||||
c.Assert(console.IsExited, Equals, false)
|
||||
|
||||
// reset back
|
||||
console.IsExited = false
|
||||
|
||||
err = app.Run([]string{os.Args[0], "config", "host", "remove", "dl.minio.io:9000"})
|
||||
c.Assert(err, IsNil)
|
||||
c.Assert(console.IsExited, Equals, true)
|
||||
|
||||
// reset back
|
||||
console.IsExited = false
|
||||
}
|
||||
|
||||
+2
-2
@@ -74,8 +74,8 @@ func mainDiff(ctx *cli.Context) {
|
||||
firstArg := ctx.Args().First()
|
||||
secondArg := ctx.Args().Last()
|
||||
|
||||
firstURL := aliasExpand(firstArg, config.Aliases)
|
||||
secondURL := aliasExpand(secondArg, config.Aliases)
|
||||
firstURL := getAliasURL(firstArg, config.Aliases)
|
||||
secondURL := getAliasURL(secondArg, config.Aliases)
|
||||
|
||||
newFirstURL := stripRecursiveURL(firstURL)
|
||||
for diff := range doDiffCmd(newFirstURL, secondURL, isURLRecursive(firstURL)) {
|
||||
|
||||
+1
-1
@@ -109,7 +109,7 @@ func mainList(ctx *cli.Context) {
|
||||
|
||||
config := mustGetMcConfig()
|
||||
for _, arg := range args {
|
||||
targetURL := aliasExpand(arg, config.Aliases)
|
||||
targetURL := getAliasURL(arg, config.Aliases)
|
||||
|
||||
// if recursive strip off the "..."
|
||||
err := doListCmd(stripRecursiveURL(targetURL), isURLRecursive(targetURL))
|
||||
|
||||
+1
-1
@@ -86,7 +86,7 @@ func mainMakeBucket(ctx *cli.Context) {
|
||||
|
||||
config := mustGetMcConfig()
|
||||
for _, arg := range ctx.Args() {
|
||||
targetURL := aliasExpand(arg, config.Aliases)
|
||||
targetURL := getAliasURL(arg, config.Aliases)
|
||||
|
||||
fatalIf(doMakeBucketCmd(targetURL).Trace(targetURL), "Unable to make bucket ‘"+targetURL+"’.")
|
||||
console.Println(MakeBucketMessage{
|
||||
|
||||
+1
-1
@@ -88,7 +88,7 @@ func mainShare(ctx *cli.Context) {
|
||||
fatalIf(probe.NewError(err), "Unable to parse time argument.")
|
||||
}
|
||||
|
||||
targetURL := aliasExpand(url, config.Aliases)
|
||||
targetURL := getAliasURL(url, config.Aliases)
|
||||
|
||||
// if recursive strip off the "..."
|
||||
err := doShareCmd(stripRecursiveURL(targetURL), isURLRecursive(targetURL), shareDuration{duration: expires})
|
||||
|
||||
@@ -64,7 +64,7 @@ func args2URLs(args []string) ([]string, *probe.Error) {
|
||||
// Convert arguments to URLs: expand alias, fix format...
|
||||
URLs := []string{}
|
||||
for _, arg := range args {
|
||||
URLs = append(URLs, aliasExpand(arg, config.Aliases))
|
||||
URLs = append(URLs, getAliasURL(arg, config.Aliases))
|
||||
}
|
||||
return URLs, nil
|
||||
}
|
||||
|
||||
+2
-2
@@ -34,8 +34,8 @@
|
||||
"canonical": "github.com/minio/cli",
|
||||
"comment": "",
|
||||
"local": "vendor/github.com/minio/cli",
|
||||
"revision": "ee386baecc113eef2b8945df429120a5aec319ef",
|
||||
"revisionTime": "2015-08-19T11:23:55-07:00"
|
||||
"revision": "8d4728a947c06097835fb3e2858b7754580a1d2f",
|
||||
"revisionTime": "2015-09-01T18:40:37-07:00"
|
||||
},
|
||||
{
|
||||
"canonical": "github.com/minio/minio-go",
|
||||
|
||||
+3
@@ -163,6 +163,9 @@ func (c Command) startApp(ctx *Context) error {
|
||||
// set CommandNotFound
|
||||
app.CommandNotFound = ctx.App.CommandNotFound
|
||||
|
||||
app.CustomAppHelpTemplate = c.CustomHelpTemplate
|
||||
app.HideVersion = ctx.App.HideVersion
|
||||
|
||||
// set the flags and commands
|
||||
app.Commands = c.Subcommands
|
||||
app.Flags = c.Flags
|
||||
|
||||
Reference in New Issue
Block a user