From c57c3ddbf683c12299f15bc882c4d01d14d45b52 Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Sun, 26 Apr 2015 19:11:38 -0700 Subject: [PATCH 1/2] Make cmd-config simpler, context processing happens only by one function --- cmd-access.go | 6 +-- cmd-cat.go | 8 +-- cmd-config.go | 141 +++++++++++++++++++++++++++---------------------- cmd-options.go | 2 +- cmd-update.go | 10 ++-- 5 files changed, 90 insertions(+), 77 deletions(-) diff --git a/cmd-access.go b/cmd-access.go index 877d6dd9..37b8c012 100644 --- a/cmd-access.go +++ b/cmd-access.go @@ -35,7 +35,7 @@ func runAccessCmd(ctx *cli.Context) { config, err := getMcConfig() if err != nil { log.Debug.Println(iodine.New(err, nil)) - console.Fatalf("mc: loading config file failed with following reason: [%s]\n", iodine.ToError(err)) + console.Fatalf("loading config file failed with following reason: [%s]\n", iodine.ToError(err)) } targetURLConfigMap := make(map[string]*hostConfig) @@ -44,10 +44,10 @@ func runAccessCmd(ctx *cli.Context) { switch e := iodine.ToError(err).(type) { case errUnsupportedScheme: log.Debug.Println(iodine.New(err, nil)) - console.Fatalf("mc: reading URL [%s] failed with following reason: [%s]\n", e.url, e) + console.Fatalf("reading URL [%s] failed with following reason: [%s]\n", e.url, e) default: log.Debug.Println(iodine.New(err, nil)) - console.Fatalf("mc: reading URLs failed with following reason: [%s]\n", e) + console.Fatalf("reading URLs failed with following reason: [%s]\n", e) } } acl := bucketACL(ctx.Args().First()) diff --git a/cmd-cat.go b/cmd-cat.go index cbe022e7..10bb5e82 100644 --- a/cmd-cat.go +++ b/cmd-cat.go @@ -33,7 +33,7 @@ func runCatCmd(ctx *cli.Context) { config, err := getMcConfig() if err != nil { log.Debug.Println(iodine.New(err, nil)) - console.Fatalf("mc: loading config file failed with following reason: [%s]\n", iodine.ToError(err)) + console.Fatalf("loading config file failed with following reason: [%s]\n", iodine.ToError(err)) } // Convert arguments to URLs: expand alias, fix format... @@ -42,10 +42,10 @@ func runCatCmd(ctx *cli.Context) { switch e := iodine.ToError(err).(type) { case errUnsupportedScheme: log.Debug.Println(iodine.New(err, nil)) - console.Fatalf("mc: reading URL [%s] failed with following reason: [%s]\n", e.url, e) + console.Fatalf("reading URL [%s] failed with following reason: [%s]\n", e.url, e) default: log.Debug.Println(iodine.New(err, nil)) - console.Fatalf("mc: reading URLs failed with following reason: [%s]\n", e) + console.Fatalf("reading URLs failed with following reason: [%s]\n", e) } } @@ -53,7 +53,7 @@ func runCatCmd(ctx *cli.Context) { sourceURLConfigMap, err := getHostConfigs(sourceURLs) if err != nil { log.Debug.Println(iodine.New(err, nil)) - console.Fatalf("mc: reading host config for URL [%s] failed with following reason: [%s]\n", sourceURLs, iodine.ToError(err)) + console.Fatalf("reading host config for URL [%s] failed with following reason: [%s]\n", sourceURLs, iodine.ToError(err)) } humanReadable, err := doCatCmd(mcClientMethods{}, sourceURLConfigMap, "/dev/stdout", globalDebugFlag) if err != nil { diff --git a/cmd-config.go b/cmd-config.go index 602a5cda..ab7ffd5e 100644 --- a/cmd-config.go +++ b/cmd-config.go @@ -132,27 +132,6 @@ func writeConfig(config quick.Config) error { return nil } -// saveConfig writes configuration data in json format to config file. -func saveConfig(ctx *cli.Context) error { - switch ctx.Args().Get(0) { - case "generate": - if isMcConfigExist() { - return iodine.New(errConfigExists{}, nil) - } - err := writeConfig(newConfig()) - if err != nil { - return iodine.New(err, nil) - } - return nil - default: - config, err := parseConfigInput(ctx) - if err != nil { - return iodine.New(err, nil) - } - return writeConfig(config) - } -} - func newConfigV1() *configV1 { conf := new(configV1) conf.Version = mcCurrentConfigVersion @@ -186,38 +165,6 @@ func newConfig() (config quick.Config) { return config } -func parseConfigInput(ctx *cli.Context) (config quick.Config, err error) { - conf := newConfigV1() - config = quick.New(&conf) - config.Load(mcConfigFile) - - alias := strings.Fields(ctx.String("alias")) - switch true { - case len(alias) == 2: - aliasName := alias[0] - url := strings.TrimSuffix(alias[1], "/") - if strings.HasPrefix(aliasName, "http") { - return nil, iodine.New(errInvalidAliasName{name: aliasName}, nil) - } - if !strings.HasPrefix(url, "http") { - return nil, iodine.New(errInvalidURL{url: url}, nil) - } - if !isValidAliasName(aliasName) { - return nil, iodine.New(errInvalidAliasName{name: aliasName}, nil) - } - // convert interface{} back to its original struct - newConf := config.Data().(configV1) - if _, ok := newConf.Aliases[aliasName]; ok { - return nil, iodine.New(errAliasExists{name: aliasName}, nil) - } - newConf.Aliases[aliasName] = url - newConfig := quick.New(newConf) - return newConfig, nil - default: - return nil, iodine.New(errInvalidArgument{}, nil) - } -} - // getHostURL - func getHostURL(u *url.URL) string { return u.Scheme + "://" + u.Host @@ -282,28 +229,94 @@ func getHostConfig(requestURL string) (*hostConfig, error) { return nil, iodine.New(errNoMatchingHost{}, nil) } -// doConfigCmd is the handler for "mc config" sub-command. -func doConfigCmd(ctx *cli.Context) { +// saveConfig writes configuration data in json format to config file. +func saveConfig(arg string, alias []string) error { + switch arg { + case "generate": + if isMcConfigExist() { + return iodine.New(errConfigExists{}, nil) + } + err := writeConfig(newConfig()) + if err != nil { + return iodine.New(err, nil) + } + return nil + default: + config, err := addAlias(alias) + if err != nil { + return iodine.New(err, nil) + } + return writeConfig(config) + } +} + +func addAlias(alias []string) (quick.Config, error) { + if len(alias) < 2 { + return nil, iodine.New(errInvalidArgument{}, nil) + } + conf := newConfigV1() + config := quick.New(conf) + config.Load(mustGetMcConfigPath()) + + aliasName := alias[0] + url := strings.TrimSuffix(alias[1], "/") + if strings.HasPrefix(aliasName, "http") { + return nil, iodine.New(errInvalidAliasName{name: aliasName}, nil) + } + if !strings.HasPrefix(url, "http") { + return nil, iodine.New(errInvalidURL{url: url}, nil) + } + if !isValidAliasName(aliasName) { + return nil, iodine.New(errInvalidAliasName{name: aliasName}, nil) + } + // convert interface{} back to its original struct + newConf := config.Data().(*configV1) + if _, ok := newConf.Aliases[aliasName]; ok { + return nil, iodine.New(errAliasExists{name: aliasName}, nil) + } + newConf.Aliases[aliasName] = url + newConfig := quick.New(newConf) + return newConfig, nil +} + +// runConfigCmd is the handle for "mc config" sub-command +func runConfigCmd(ctx *cli.Context) { // show help if nothing is set - if len(ctx.Args()) < 1 && !ctx.IsSet("completion") && !ctx.IsSet("alias") { + if !ctx.Args().Present() && !ctx.IsSet("alias") || ctx.Args().First() == "help" { cli.ShowCommandHelpAndExit(ctx, "config", 1) // last argument is exit code } - configPath, err := getMcConfigPath() + arg := ctx.Args().First() + alias := strings.Fields(ctx.String("alias")) + msg, err := doConfig(arg, alias) if err != nil { log.Debug.Println(iodine.New(err, nil)) - console.Fatalln("Unable to determine config file path.") + console.Fatalln(msg) } - err = saveConfig(ctx) +} + +// doConfig is the handler for "mc config" sub-command. +func doConfig(arg string, alias []string) (string, error) { + configPath, err := getMcConfigPath() + if err != nil { + return "Unable to determine config file path.", iodine.New(err, nil) + } + err = saveConfig(arg, alias) if err != nil { switch iodine.ToError(err).(type) { case errConfigExists: - log.Debug.Println(iodine.New(err, nil)) - console.Fatalln("Configuration file " + configPath + " already exists.") + return "Configuration file [" + configPath + "] already exists.", iodine.New(err, nil) + case errInvalidArgument: + return "Incorrect usage, please use \"help\" ", iodine.New(err, nil) + case errAliasExists: + return "Alias [" + alias[0] + "] already exists", iodine.New(err, nil) + case errInvalidAliasName: + return "Alias [" + alias[0] + "] is reserved word or invalid", iodine.New(err, nil) + case errInvalidURL: + return "Alias [" + alias[1] + "] is invalid URL", iodine.New(err, nil) default: // unexpected error - log.Debug.Println(iodine.New(err, nil)) - console.Fatalln("Unable to generate config file", configPath, ".") + return "Unable to generate config file [" + configPath + "].", iodine.New(err, nil) } } - console.Infoln("Configuration written to " + configPath + ". Please update your access credentials.") + return "Configuration written to [" + configPath + "]. Please update your access credentials.", nil } diff --git a/cmd-options.go b/cmd-options.go index 454a26e5..e9be3d6d 100644 --- a/cmd-options.go +++ b/cmd-options.go @@ -202,7 +202,7 @@ EXAMPLES: configCmd = cli.Command{ Name: "config", Usage: "Generate configuration \"" + mustGetMcConfigPath() + "\" file.", - Action: doConfigCmd, + Action: runConfigCmd, Flags: []cli.Flag{ cli.StringFlag{ Name: "alias", diff --git a/cmd-update.go b/cmd-update.go index d3d2dcbf..798c71b5 100644 --- a/cmd-update.go +++ b/cmd-update.go @@ -54,28 +54,28 @@ func doUpdateCmd(ctx *cli.Context) { req, err := getReq(mcUpdateURL) if err != nil { log.Debug.Println(iodine.New(err, nil)) - console.Fatalln("mc: Unable to update:", mcUpdateURL) + console.Fatalln("Unable to update:", mcUpdateURL) } res, err := http.DefaultTransport.RoundTrip(req) if err != nil { log.Debug.Println(iodine.New(err, nil)) - console.Fatalln("mc: Unable to retrieve:", mcUpdateURL) + console.Fatalln("Unable to retrieve:", mcUpdateURL) } if res.StatusCode != http.StatusOK { msg := fmt.Sprint("Received invalid HTTP status: ", res.StatusCode) log.Debug.Println(iodine.New(errors.New(msg), nil)) - console.Fatalln("mc: " + msg) + console.Fatalln(msg) } ures := updateResults{} err = json.NewDecoder(res.Body).Decode(&ures) if err != nil { log.Debug.Println(iodine.New(err, nil)) - console.Fatalln("mc: Unable to parse JSON:", mcUpdateURL) + console.Fatalln("Unable to parse JSON:", mcUpdateURL) } latest, err := time.Parse(time.RFC3339Nano, ures.LatestBuild) if err != nil { log.Debug.Println(iodine.New(err, nil)) - console.Fatalln("mc: Unable to parse update time:", ures.LatestBuild) + console.Fatalln("Unable to parse update time:", ures.LatestBuild) } if latest.After(ctx.App.Compiled) { // FIXME : find some proper versioning scheme here From 9711642f1de5fdee0c713fcc0e8ca0261cb153ea Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Sun, 26 Apr 2015 19:16:12 -0700 Subject: [PATCH 2/2] Fix for #439 - simplify config handling --- cmd-config.go | 109 ++++++++++---------------------------------------- copy.go | 16 ++++++++ hostconfig.go | 90 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 127 insertions(+), 88 deletions(-) create mode 100644 hostconfig.go diff --git a/cmd-config.go b/cmd-config.go index ab7ffd5e..52d7827a 100644 --- a/cmd-config.go +++ b/cmd-config.go @@ -17,17 +17,14 @@ package main import ( - "net/url" "os" "path" "runtime" "strings" "os/user" - "path/filepath" "github.com/minio-io/cli" - "github.com/minio-io/mc/pkg/client" "github.com/minio-io/mc/pkg/console" "github.com/minio-io/mc/pkg/quick" "github.com/minio-io/minio/pkg/iodine" @@ -165,91 +162,6 @@ func newConfig() (config quick.Config) { return config } -// getHostURL - -func getHostURL(u *url.URL) string { - return u.Scheme + "://" + u.Host -} - -func getHostConfigs(requestURLs []string) (hostConfigs map[string]*hostConfig, err error) { - hostConfigs = make(map[string]*hostConfig) - for _, requestURL := range requestURLs { - hostConfigs[requestURL], err = getHostConfig(requestURL) - if err != nil { - return nil, iodine.New(err, nil) - } - } - return hostConfigs, nil -} - -// getHostConfig retrieves host specific configuration such as access keys, certs. -func getHostConfig(requestURL string) (*hostConfig, error) { - config, err := getMcConfig() - if err != nil { - return nil, iodine.New(err, nil) - } - u, err := url.Parse(requestURL) - if err != nil { - return nil, iodine.New(errInvalidURL{url: requestURL}, nil) - } - // No host matching or keys needed for filesystem requests - if client.GetType(requestURL) == client.Filesystem { - hostCfg := &hostConfig{ - AccessKeyID: "", - SecretAccessKey: "", - } - return hostCfg, nil - } - - // No host matching or keys needed for localhost and 127.0.0.1 URL's skip them - if strings.Contains(getHostURL(u), "localhost") || strings.Contains(getHostURL(u), "127.0.0.1") { - hostCfg := &hostConfig{ - AccessKeyID: "", - SecretAccessKey: "", - } - return hostCfg, nil - } - for globURL, hostCfg := range config.Hosts { - match, err := filepath.Match(globURL, getHostURL(u)) - if err != nil { - return nil, iodine.New(errInvalidGlobURL{glob: globURL, request: requestURL}, nil) - } - if match { - if hostCfg == nil { - return nil, iodine.New(errInvalidAuth{}, nil) - } - // verify Auth key validity for all hosts - if hostCfg.AccessKeyID != globalAccessKeyID && hostCfg.SecretAccessKey != globalSecretAccessKey { - if !client.IsValidAccessKey(hostCfg.AccessKeyID) || !client.IsValidSecretKey(hostCfg.SecretAccessKey) { - return nil, iodine.New(errInvalidAuthKeys{}, nil) - } - } - return hostCfg, nil - } - } - return nil, iodine.New(errNoMatchingHost{}, nil) -} - -// saveConfig writes configuration data in json format to config file. -func saveConfig(arg string, alias []string) error { - switch arg { - case "generate": - if isMcConfigExist() { - return iodine.New(errConfigExists{}, nil) - } - err := writeConfig(newConfig()) - if err != nil { - return iodine.New(err, nil) - } - return nil - default: - config, err := addAlias(alias) - if err != nil { - return iodine.New(err, nil) - } - return writeConfig(config) - } -} - func addAlias(alias []string) (quick.Config, error) { if len(alias) < 2 { return nil, iodine.New(errInvalidArgument{}, nil) @@ -294,6 +206,27 @@ func runConfigCmd(ctx *cli.Context) { } } +// saveConfig writes configuration data in json format to config file. +func saveConfig(arg string, alias []string) error { + switch arg { + case "generate": + if isMcConfigExist() { + return iodine.New(errConfigExists{}, nil) + } + err := writeConfig(newConfig()) + if err != nil { + return iodine.New(err, nil) + } + return nil + default: + config, err := addAlias(alias) + if err != nil { + return iodine.New(err, nil) + } + return writeConfig(config) + } +} + // doConfig is the handler for "mc config" sub-command. func doConfig(arg string, alias []string) (string, error) { configPath, err := getMcConfigPath() diff --git a/copy.go b/copy.go index 6124d326..c4a81a62 100644 --- a/copy.go +++ b/copy.go @@ -1,3 +1,19 @@ +/* + * Mini Copy (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 ( diff --git a/hostconfig.go b/hostconfig.go new file mode 100644 index 00000000..faa8e4a4 --- /dev/null +++ b/hostconfig.go @@ -0,0 +1,90 @@ +/* + * Mini Copy (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 ( + "net/url" + "path/filepath" + "strings" + + "github.com/minio-io/mc/pkg/client" + "github.com/minio-io/minio/pkg/iodine" +) + +// getHostURL - +func getHostURL(u *url.URL) string { + return u.Scheme + "://" + u.Host +} + +func getHostConfigs(requestURLs []string) (hostConfigs map[string]*hostConfig, err error) { + hostConfigs = make(map[string]*hostConfig) + for _, requestURL := range requestURLs { + hostConfigs[requestURL], err = getHostConfig(requestURL) + if err != nil { + return nil, iodine.New(err, nil) + } + } + return hostConfigs, nil +} + +// getHostConfig retrieves host specific configuration such as access keys, certs. +func getHostConfig(requestURL string) (*hostConfig, error) { + config, err := getMcConfig() + if err != nil { + return nil, iodine.New(err, nil) + } + u, err := url.Parse(requestURL) + if err != nil { + return nil, iodine.New(errInvalidURL{url: requestURL}, nil) + } + // No host matching or keys needed for filesystem requests + if client.GetType(requestURL) == client.Filesystem { + hostCfg := &hostConfig{ + AccessKeyID: "", + SecretAccessKey: "", + } + return hostCfg, nil + } + + // No host matching or keys needed for localhost and 127.0.0.1 URL's skip them + if strings.Contains(getHostURL(u), "localhost") || strings.Contains(getHostURL(u), "127.0.0.1") { + hostCfg := &hostConfig{ + AccessKeyID: "", + SecretAccessKey: "", + } + return hostCfg, nil + } + for globURL, hostCfg := range config.Hosts { + match, err := filepath.Match(globURL, getHostURL(u)) + if err != nil { + return nil, iodine.New(errInvalidGlobURL{glob: globURL, request: requestURL}, nil) + } + if match { + if hostCfg == nil { + return nil, iodine.New(errInvalidAuth{}, nil) + } + // verify Auth key validity for all hosts + if hostCfg.AccessKeyID != globalAccessKeyID && hostCfg.SecretAccessKey != globalSecretAccessKey { + if !client.IsValidAccessKey(hostCfg.AccessKeyID) || !client.IsValidSecretKey(hostCfg.SecretAccessKey) { + return nil, iodine.New(errInvalidAuthKeys{}, nil) + } + } + return hostCfg, nil + } + } + return nil, iodine.New(errNoMatchingHost{}, nil) +}