diff --git a/access-main.go b/access-main.go index cefb4121..06abbf26 100644 --- a/access-main.go +++ b/access-main.go @@ -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+"’.") diff --git a/aliases.go b/aliasconfig.go similarity index 92% rename from aliases.go rename to aliasconfig.go index a73d7332..7645ccdd 100644 --- a/aliases.go +++ b/aliasconfig.go @@ -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. diff --git a/cat-main.go b/cat-main.go index 6f0e1981..a0b754a7 100644 --- a/cat-main.go +++ b/cat-main.go @@ -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 { diff --git a/cmd_test.go b/cmd_test.go index 246afe25..2103113f 100644 --- a/cmd_test.go +++ b/cmd_test.go @@ -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") } diff --git a/config-alias-main.go b/config-alias-main.go new file mode 100644 index 00000000..76bfd239 --- /dev/null +++ b/config-alias-main.go @@ -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, + }) +} diff --git a/config-host-main.go b/config-host-main.go new file mode 100644 index 00000000..11a00eb3 --- /dev/null +++ b/config-host-main.go @@ -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, + }) +} diff --git a/config-main.go b/config-main.go index df20ca28..4256b7a4 100644 --- a/config-main.go +++ b/config-main.go @@ -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, - }) -} diff --git a/config_test.go b/config_test.go index 215c8142..6204eefd 100644 --- a/config_test.go +++ b/config_test.go @@ -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 +} diff --git a/diff-main.go b/diff-main.go index 748c7ec4..ad642f84 100644 --- a/diff-main.go +++ b/diff-main.go @@ -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)) { diff --git a/ls-main.go b/ls-main.go index fcda1e41..86389e84 100644 --- a/ls-main.go +++ b/ls-main.go @@ -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)) diff --git a/mb-main.go b/mb-main.go index d69950be..1b5734dc 100644 --- a/mb-main.go +++ b/mb-main.go @@ -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{ diff --git a/share-main.go b/share-main.go index a6c77ae7..911ba71b 100644 --- a/share-main.go +++ b/share-main.go @@ -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}) diff --git a/url.go b/url.go index ef761c76..23d81149 100644 --- a/url.go +++ b/url.go @@ -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 } diff --git a/vendor.json b/vendor.json index a45d4401..e9625d3d 100755 --- a/vendor.json +++ b/vendor.json @@ -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", diff --git a/vendor/github.com/minio/cli/command.go b/vendor/github.com/minio/cli/command.go index a0955af3..9f5e6a57 100644 --- a/vendor/github.com/minio/cli/command.go +++ b/vendor/github.com/minio/cli/command.go @@ -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