From 99240bfb2d7d43499e570c4b83dfe9727354838c Mon Sep 17 00:00:00 2001 From: "Anand Babu (AB) Periasamy" Date: Thu, 9 Apr 2015 02:39:33 -0700 Subject: [PATCH] config bug fixes and removed default-host support --- cmd-common.go | 10 ++--- cmd-config.go | 108 ++++++++++++++++++++++++++++++++++++-------------- cmd-update.go | 1 + main.go | 23 +++++++---- url-parser.go | 13 +----- 5 files changed, 99 insertions(+), 56 deletions(-) diff --git a/cmd-common.go b/cmd-common.go index 411c6ee3..3b43c46f 100644 --- a/cmd-common.go +++ b/cmd-common.go @@ -17,6 +17,7 @@ package main import ( + "fmt" "path" "time" @@ -58,12 +59,7 @@ func getTraceTransport() s3.RoundTripTrace { // NewClient - get new client func getNewClient(debug bool, urlStr string) (clnt client.Client, err error) { - config, err := getMcConfig() - if err != nil { - return nil, iodine.New(err, nil) - } - - hostCfg, err := getHostConfig(config.DefaultHost) + hostCfg, err := getHostConfig(urlStr) if err != nil { return nil, iodine.New(err, nil) } @@ -71,7 +67,7 @@ func getNewClient(debug bool, urlStr string) (clnt client.Client, err error) { var auth s3.Auth auth.AccessKeyID = hostCfg.Auth.AccessKeyID auth.SecretAccessKey = hostCfg.Auth.SecretAccessKey - + fmt.Println(urlStr) uType, err := getURLType(urlStr) if err != nil { return nil, iodine.New(err, nil) diff --git a/cmd-config.go b/cmd-config.go index 17f75812..53ea48e3 100644 --- a/cmd-config.go +++ b/cmd-config.go @@ -36,11 +36,10 @@ type hostConfig struct { } type mcConfig struct { - Version uint - MCVersion string - DefaultHost string - Hosts map[string]hostConfig - Aliases map[string]string + Version uint + MCVersion string + Hosts map[string]hostConfig + Aliases map[string]string } const ( @@ -74,6 +73,8 @@ func getMcConfigFilename() string { return path.Join(getMcConfigDir(), configFile) } +// getMcConfig returns the config data from file. Subsequent calls are +// cached in a private global variable func getMcConfig() (cfg *mcConfig, err error) { if _config != nil { return _config, nil @@ -87,6 +88,17 @@ func getMcConfig() (cfg *mcConfig, err error) { return _config, nil } +// getMcConfig returns the config data from file. Subsequent calls are +// cached in a private global variable +func isMcConfigExist() bool { + configFile := getMcConfigFilename() + _, err := os.Stat(configFile) + if err != nil { + return false + } + return true +} + // chechMcConfig checks for errors in config file func checkMcConfig(config *mcConfig) (err error) { // check for version @@ -177,8 +189,7 @@ func saveConfig(ctx *cli.Context) error { // Reload and cache new config _, err = getMcConfig() - err = iodine.ToError(err) - if os.IsNotExist(err) { + if os.IsNotExist(iodine.ToError(err)) { return iodine.New(err, nil) } @@ -226,9 +237,8 @@ func parseConfigInput(c *cli.Context) (config *mcConfig, err error) { switch true { case len(alias) == 0: config = &mcConfig{ - Version: currentConfigVersion, - MCVersion: "0.9", - DefaultHost: "https://s3.amazonaws.com", + Version: currentConfigVersion, + MCVersion: "0.9", Hosts: map[string]hostConfig{ "http*://s3*.amazonaws.com": { Auth: auth{ @@ -237,8 +247,8 @@ func parseConfigInput(c *cli.Context) (config *mcConfig, err error) { }}, }, Aliases: map[string]string{ - "s3": "https://s3.amazonaws.com/", - "localhost": "http://localhost:9000/", + "s3": "https://s3.amazonaws.com", + "localhost": "http://localhost:9000", }, } return config, nil @@ -252,8 +262,8 @@ func parseConfigInput(c *cli.Context) (config *mcConfig, err error) { return nil, iodine.New(errors.New("invalid url type only supports http{s}"), nil) } config = &mcConfig{ - Version: currentConfigVersion, - DefaultHost: "https://s3.amazonaws.com", + Version: currentConfigVersion, + MCVersion: "0.9", Hosts: map[string]hostConfig{ "http*://s3*.amazonaws.com": { Auth: auth{ @@ -262,8 +272,8 @@ func parseConfigInput(c *cli.Context) (config *mcConfig, err error) { }}, }, Aliases: map[string]string{ - "s3": "https://s3.amazonaws.com/", - "localhost": "http://localhost:9000/", + "s3": "https://s3.amazonaws.com", + "localhost": "http://localhost:9000", aliasName: url, }, } @@ -301,24 +311,62 @@ func getHostConfig(hostURL string) (*hostConfig, error) { return nil, iodine.New(errors.New("No matching host config found"), nil) } +//getBashCompletionCmd generates bash completion file. +func getBashCompletionCmd() { + var b bytes.Buffer + if os.Getenv("SHELL") != "/bin/bash" { + fatal("Unsupported shell for bash completion detected.. exiting") + } + b.WriteString(mcBashCompletion) + f := getMcBashCompletionFilename() + fl, err := os.OpenFile(f, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600) + defer fl.Close() + if err != nil { + if globalDebugFlag { + log.Debug.Println(iodine.New(err, nil)) + } + fatal(err) + } + _, err = fl.Write(b.Bytes()) + if err != nil { + if globalDebugFlag { + log.Debug.Println(iodine.New(err, nil)) + } + fatal(err) + } + msg := "\nConfiguration written to " + f + msg = msg + "\n\n$ source ${HOME}/.mc/mc.bash_completion\n" + msg = msg + "$ echo 'source ${HOME}/.mc/mc.bash_completion' >> ${HOME}/.bashrc" + info(msg) +} + +// saveConfigCmd writes config file to disk +func saveConfigCmd(ctx *cli.Context) { + err := saveConfig(ctx) + if os.IsExist(iodine.ToError(err)) { + if globalDebugFlag { + log.Debug.Println(iodine.New(err, nil)) + } + msg := fmt.Sprintf("mc: Configuration file [%s] already exists.", getMcConfigFilename()) + fatal(msg) + } + + if err != nil { + if globalDebugFlag { + log.Debug.Println(iodine.New(err, nil)) + } + msg := fmt.Sprintf("mc: Unable to generate config file [%s]. \nError: %v.", getMcConfigFilename(), err) + fatal(msg) + } + info("Configuration written to " + getMcConfigFilename() + ". Please update your access credentials.") +} + // doConfigCmd is the handler for "mc config" sub-command. func doConfigCmd(ctx *cli.Context) { switch true { case ctx.Bool("completion") == true: - getBashCompletion() + getBashCompletionCmd() default: - err := saveConfig(ctx) - if os.IsExist(err) { - log.Debug.Println(iodine.New(err, nil)) - msg := fmt.Sprintf("mc: Please rename your current configuration file [%s]\n", getMcConfigFilename()) - fatal(msg) - } - - if err != nil { - log.Debug.Println(iodine.New(err, nil)) - msg := fmt.Sprintf("mc: Unable to generate config file [%s]. \nError: %v\n", getMcConfigFilename(), err) - fatal(msg) - } - info("Configuration written to " + getMcConfigFilename() + "\n") + saveConfigCmd(ctx) } } diff --git a/cmd-update.go b/cmd-update.go index 1d56ed8d..0554e1b9 100644 --- a/cmd-update.go +++ b/cmd-update.go @@ -72,6 +72,7 @@ func doUpdateCmd(ctx *cli.Context) { log.Debug.Println(iodine.New(err, nil)) fatal(err) } + /* FIXME: Config file should not hold versin info (MCVersion). Use commit-id or build-date for the check */ config, err := getMcConfig() if err != nil { log.Debug.Println(iodine.New(err, nil)) diff --git a/main.go b/main.go index 9c16570d..cd89a43d 100644 --- a/main.go +++ b/main.go @@ -39,12 +39,13 @@ func checkConfig() { fatal("Unable to obtain user's home directory") } - // Ensures config file is sane and cached to _config private variable. - config, err := getMcConfig() - err = iodine.ToError(err) - if os.IsNotExist(err) { + if !isMcConfigExist() { + // Handled properly in main.app.Before return } + + // Ensures config file is sane and cached to _config private variable. + config, err := getMcConfig() if err != nil { log.Debug.Println(iodine.New(err, nil)) fatal("Unable to read config file") @@ -91,9 +92,9 @@ func main() { app.Flags = flags app.Author = "Minio.io" app.EnableBashCompletion = true - app.Before = func(c *cli.Context) error { - globalQuietFlag = c.GlobalBool("quiet") - globalDebugFlag = c.GlobalBool("debug") + app.Before = func(ctx *cli.Context) error { + globalQuietFlag = ctx.GlobalBool("quiet") + globalDebugFlag = ctx.GlobalBool("debug") if globalDebugFlag { app.ExtraInfo = getSystemData() } else { @@ -102,5 +103,13 @@ func main() { checkConfig() return nil } + app.After = func(ctx *cli.Context) error { + if !isMcConfigExist() && ctx.Command.Name != "config" { + fatal("Error: mc is not configured. Please run \"mc config\".") + } + + return nil + } + app.RunAndExitOnError() } diff --git a/url-parser.go b/url-parser.go index 9a78ed44..51587d00 100644 --- a/url-parser.go +++ b/url-parser.go @@ -154,19 +154,8 @@ func url2Bucket(urlStr string) (bucketName string, err error) { // parseURL extracts URL string from a single cmd-line argument func parseURL(arg string) (urlStr string, err error) { - urlStr = arg - // Use default host if no argument is passed - if urlStr == "" { - // Load config file - config, err := getMcConfig() - if err != nil { - return "", iodine.New(err, nil) - } - urlStr = config.DefaultHost - } - // Check and expand Alias - urlStr, err = aliasExpand(urlStr) + urlStr, err = aliasExpand(arg) if err != nil { return "", iodine.New(err, nil) }