From fa0c9e38bbe4063d34aa01da107603bebbeec07d Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Wed, 19 Aug 2015 17:11:31 -0700 Subject: [PATCH] Add error messages to fatalIf and errorIf --- access-main.go | 8 ++++---- cat-main.go | 6 +++--- cat_test.go | 4 ++++ cmd_test.go | 15 --------------- config-main.go | 32 +++++++++++--------------------- config.go | 22 +++++++++++++++------- config_test.go | 44 ++++++++++++++++++++++++++++++++++++++++++++ cp-main.go | 6 ++---- cp-url.go | 30 +++++++++++++++++------------- cp_mirror_test.go | 1 + diff-main.go | 13 +++++++------ diff_test.go | 7 +++++++ error.go | 17 +++++++++++------ ls-main.go | 7 ++++--- ls.go | 4 ++-- ls_test.go | 4 ++++ main.go | 9 +++------ mb-main.go | 8 ++++---- mb_access_test.go | 16 ++++++++++++++++ mirror-main.go | 6 ++---- mirror-url.go | 14 +++++++------- session-main.go | 14 ++++++++------ session_test.go | 17 +++++++++++++++++ share-main.go | 9 ++++----- update-main.go | 42 ++++++++++++++++++++---------------------- url.go | 12 ++++++------ verify-runtime.go | 9 +++++---- 27 files changed, 228 insertions(+), 148 deletions(-) create mode 100644 config_test.go diff --git a/access-main.go b/access-main.go index b61c53eb..a74c4abf 100644 --- a/access-main.go +++ b/access-main.go @@ -64,13 +64,13 @@ func mainAccess(ctx *cli.Context) { config := mustGetMcConfig() acl := bucketACL(ctx.Args().First()) if !acl.isValidBucketACL() { - fatalIf(probe.NewError(errInvalidACL{acl: acl.String()})) + fatalIf(probe.NewError(errInvalidACL{acl: acl.String()}), "ACL validation failed.") } for _, arg := range ctx.Args().Tail() { - targetURL, err := getExpandedURL(arg, config.Aliases) - fatalIf(err) + targetURL, err := getCanonicalizedURL(arg, config.Aliases) + fatalIf(err, "Unable to validate given URL.") - fatalIf(doUpdateAccessCmd(targetURL, acl)) + fatalIf(doUpdateAccessCmd(targetURL, acl), "Unable to set access permissions.") } } diff --git a/cat-main.go b/cat-main.go index 34bdd76a..8848c055 100644 --- a/cat-main.go +++ b/cat-main.go @@ -70,11 +70,11 @@ func mainCat(ctx *cli.Context) { } if stdinMode { - fatalIf(catOut(os.Stdin)) + fatalIf(catOut(os.Stdin), "Unable to read from stdin") } else { // Convert arguments to URLs: expand alias, fix format... for _, arg := range ctx.Args() { - fatalIf(catURL(arg)) + fatalIf(catURL(arg), "Unable to read from URL") } } @@ -83,7 +83,7 @@ func mainCat(ctx *cli.Context) { func catURL(sourceURL string) *probe.Error { config := mustGetMcConfig() - URL, err := getExpandedURL(sourceURL, config.Aliases) + URL, err := getCanonicalizedURL(sourceURL, config.Aliases) if err != nil { return err.Trace(sourceURL) } diff --git a/cat_test.go b/cat_test.go index 0911c2eb..7a8b5603 100644 --- a/cat_test.go +++ b/cat_test.go @@ -60,9 +60,13 @@ func (s *CmdTestSuite) TestCatContext(c *C) { c.Assert(err, IsNil) c.Assert(console.IsExited, Equals, false) + // reset back + console.IsExited = false + err = app.Run([]string{os.Args[0], "cat", server.URL + "/invalid"}) c.Assert(err, IsNil) c.Assert(console.IsExited, Equals, true) + // reset back console.IsExited = false } diff --git a/cmd_test.go b/cmd_test.go index 49373172..dd988ab8 100644 --- a/cmd_test.go +++ b/cmd_test.go @@ -74,21 +74,6 @@ func (s *CmdTestSuite) SetUpSuite(c *C) { perr = createSessionDir() c.Assert(perr, IsNil) - perr = doConfig("invalid", nil) - c.Assert(perr, Not(IsNil)) - - perr = doConfig("alias", []string{"test", "https://test.io"}) - c.Assert(perr, IsNil) - - perr = doConfig("alias", []string{"test", "https://test.io"}) - c.Assert(perr, Not(IsNil)) - - perr = doConfig("alias", []string{"test", "htt://test.io"}) - c.Assert(perr, Not(IsNil)) - - perr = doConfig("alias", []string{"readonly", "https://new.test.io"}) - c.Assert(perr, Not(IsNil)) - app = registerApp() } diff --git a/config-main.go b/config-main.go index ad0ffe1e..08cc97d8 100644 --- a/config-main.go +++ b/config-main.go @@ -52,7 +52,7 @@ EXAMPLES: `, } -// mainConfig is the handle for "mc config" sub-command +// mainConfig is the handle for "mc config" sub-command. writes configuration data in json format to config file. func mainConfig(ctx *cli.Context) { // show help if nothing is set if !ctx.Args().Present() || ctx.Args().First() == "help" { @@ -67,30 +67,20 @@ func mainConfig(ctx *cli.Context) { console.Fatalf("Incorrect number of arguments, please read \"mc config help\". %s", errInvalidArgument{}) } configPath, err := getMcConfigPath() - fatalIf(err) + fatalIf(err, "Unable to get mc config path") - fatalIf(doConfig(arg, tailArgs)) - // upon success - console.Infoln("Alias written successfully to [" + configPath + "].") -} - -// saveConfig writes configuration data in json format to config file. -func saveConfig(arg string, aliases []string) *probe.Error { switch arg { case "alias": - config, err := addAlias(aliases) - if err != nil { - return err.Trace() - } - return writeConfig(config).Trace() - default: - return probe.NewError(errInvalidArgument{}) - } -} + config, err := addAlias(tailArgs) + fatalIf(err, "Unable to set alias name") -// doConfig is the handler for "mc config" sub-command. -func doConfig(arg string, aliases []string) *probe.Error { - return saveConfig(arg, aliases).Trace() + err = writeConfig(config) + fatalIf(err, "Unable to write alias name") + default: + fatalIf(probe.NewError(errInvalidArgument{}), "Unable to configure") + } + // upon success + console.Infoln("Alias written successfully to [" + configPath + "].") } // addAlias - add new aliases diff --git a/config.go b/config.go index 94716820..f5c998c6 100644 --- a/config.go +++ b/config.go @@ -66,7 +66,8 @@ func getMcConfigDir() (string, *probe.Error) { // mustGetMcConfigDir - construct minio client config folder or fail func mustGetMcConfigDir() (configDir string) { configDir, err := getMcConfigDir() - fatalIf(err) + fatalIf(err, "Unable to get mcConfigDir") + return configDir } @@ -94,7 +95,8 @@ func getMcConfigPath() (string, *probe.Error) { // mustGetMcConfigPath - similar to getMcConfigPath, ignores errors func mustGetMcConfigPath() string { path, err := getMcConfigPath() - fatalIf(err) + fatalIf(err, "Unable to get mcConfigPath") + return path } @@ -132,7 +134,8 @@ func getMcConfig() (*configV1, *probe.Error) { // mustGetMcConfig - reads configuration file and returns configs, exits on error func mustGetMcConfig() *configV1 { config, err := getMcConfig() - fatalIf(err) + fatalIf(err, "Unable to get mcConfig") + return config } @@ -150,6 +153,9 @@ func isMcConfigExists() bool { // writeConfig - write configuration file func writeConfig(config quick.Config) *probe.Error { + if config == nil { + return probe.NewError(errInvalidArgument{}) + } err := createMcConfigDir() if err != nil { return err.Trace() @@ -175,9 +181,10 @@ func migrateConfigV1ToV101() { } conf := newConfigV1() config, err := quick.New(conf) - fatalIf(err) + fatalIf(err, "Unable to initialize quick config") + err = config.Load(mustGetMcConfigPath()) - fatalIf(err) + fatalIf(err, "Unable to load config") conf = config.Data().(*configV1) // version is the same return @@ -205,9 +212,10 @@ func migrateConfigV1ToV101() { } newConfig, perr := quick.New(conf) - fatalIf(perr) + fatalIf(perr, "Unable to initialize quick config") + perr = newConfig.Save(mustGetMcConfigPath()) - fatalIf(perr) + fatalIf(perr, "Unable to save config") console.Infof("Successfully migrated %s from version: %s to version: %s\n", mustGetMcConfigPath(), globalMCPreviousConfigVersion, globalMCCurrentConfigVersion) } diff --git a/config_test.go b/config_test.go new file mode 100644 index 00000000..5687237f --- /dev/null +++ b/config_test.go @@ -0,0 +1,44 @@ +/* + * Minio Client (C) 2014, 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 ( + "os" + + . "github.com/minio/mc/internal/gopkg.in/check.v1" + "github.com/minio/mc/pkg/console" +) + +func (s *CmdTestSuite) TestConfigContext(c *C) { + err := app.Run([]string{os.Args[0], "config", "invalid"}) + c.Assert(err, IsNil) + c.Assert(console.IsExited, Equals, true) + + // reset back + console.IsExited = false + + err = app.Run([]string{os.Args[0], "config", "alias", "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", "alias", "test", "http://test.io"}) + c.Assert(err, IsNil) + c.Assert(console.IsExited, Equals, false) +} diff --git a/cp-main.go b/cp-main.go index 76a36508..4dcff63e 100644 --- a/cp-main.go +++ b/cp-main.go @@ -159,8 +159,7 @@ func doPrepareCopyURLs(session *sessionV2, trapCh <-chan bool) { break } if cpURLs.Error != nil { - console.PrintC("Failed to prepare copy URLs, ") - errorIf(cpURLs.Error) + errorIf(cpURLs.Error, "Failed to prepare copy URLs.") break } @@ -231,8 +230,7 @@ func doCopyCmdSession(session *sessionV2) { session.Header.LastCopied = cpURLs.SourceContent.Name } else { console.Println() - console.PrintC(fmt.Sprintf("Failed to copy ‘%s’, ", cpURLs.SourceContent.Name)) - errorIf(cpURLs.Error) + errorIf(cpURLs.Error, fmt.Sprintf("Failed to copy ‘%s’. ", cpURLs.SourceContent.Name)) } case <-trapCh: // Receive interrupt notification. session.Close() diff --git a/cp-url.go b/cp-url.go index c0333ec8..efbb68e4 100644 --- a/cp-url.go +++ b/cp-url.go @@ -108,12 +108,13 @@ func checkCopySyntaxTypeA(srcURLs []string, tgtURL string) { } srcURL := srcURLs[0] _, srcContent, err := url2Stat(srcURL) - fatalIf(err) + fatalIf(err, "Unable to stat source URL") + if srcContent.Type.IsDir() { console.Fatalf("Source ‘%s’ is a folder. Use ‘%s...’ argument to copy this folder and its contents recursively. %s\n", srcURL, srcURL, errInvalidArgument{}) } if !srcContent.Type.IsRegular() { - fatalIf(probe.NewError(errSourceIsNotFile{URL: srcURL})) + fatalIf(probe.NewError(errSourceIsNotFile{URL: srcURL}), "Invalid source type") } } @@ -124,19 +125,20 @@ func checkCopySyntaxTypeB(srcURLs []string, tgtURL string) { } srcURL := srcURLs[0] _, srcContent, err := url2Stat(srcURL) - fatalIf(err) + fatalIf(err, "Unable to stat source URL") + if srcContent.Type.IsDir() { console.Fatalf("Source ‘%s’ is a folder. Use ‘%s...’ argument to copy this folder and its contents recursively. %s\n", srcURL, srcURL, errInvalidArgument{}) } if !srcContent.Type.IsRegular() { - fatalIf(probe.NewError(errSourceIsNotFile{URL: srcURL})) + fatalIf(probe.NewError(errSourceIsNotFile{URL: srcURL}), "Invalid source type") } _, tgtContent, err := url2Stat(tgtURL) // Target exist?. if err == nil { if !tgtContent.Type.IsDir() { - fatalIf(probe.NewError(errTargetIsNotDir{URL: tgtURL})) + fatalIf(probe.NewError(errTargetIsNotDir{URL: tgtURL}), "Invalid target type") } } } @@ -149,16 +151,16 @@ func checkCopySyntaxTypeC(srcURLs []string, tgtURL string) { srcURL := srcURLs[0] srcURL = stripRecursiveURL(srcURL) _, srcContent, err := url2Stat(srcURL) - fatalIf(err) + fatalIf(err, "Unable to stat source URL") if srcContent.Type.IsRegular() { // Ellipses is supported only for folders. - fatalIf(probe.NewError(errSourceIsNotDir{URL: srcURL})) + fatalIf(probe.NewError(errSourceIsNotDir{URL: srcURL}), "Invalid source type") } _, tgtContent, err := url2Stat(tgtURL) // Target exist?. if err == nil { if !tgtContent.Type.IsDir() { - fatalIf(probe.NewError(errTargetIsNotDir{URL: tgtURL})) + fatalIf(probe.NewError(errTargetIsNotDir{URL: tgtURL}), "Invalid target type") } } } @@ -169,18 +171,20 @@ func checkCopySyntaxTypeD(srcURLs []string, tgtURL string) { if isURLRecursive(srcURL) { srcURL = stripRecursiveURL(srcURL) _, srcContent, err := url2Stat(srcURL) - fatalIf(err) + fatalIf(err, "Unable to stat source URL") + if !srcContent.Type.IsDir() { // Ellipses is supported only for folders. - fatalIf(probe.NewError(errSourceIsNotDir{URL: srcURL})) + fatalIf(probe.NewError(errSourceIsNotDir{URL: srcURL}), "Invalid source type") } } else { // Regular URL. _, srcContent, err := url2Stat(srcURL) - fatalIf(err) + fatalIf(err, "Unable to stat source URL") + if srcContent.Type.IsDir() { console.Fatalf("Source ‘%s’ is a folder. Use ‘%s...’ argument to copy this folder and its contents recursively. %s\n", srcURL, srcURL, errInvalidArgument{}) } if !srcContent.Type.IsRegular() { - fatalIf(probe.NewError(errSourceIsNotFile{URL: srcURL})) + fatalIf(probe.NewError(errSourceIsNotFile{URL: srcURL}), "Unable to read from source url") } } } @@ -188,7 +192,7 @@ func checkCopySyntaxTypeD(srcURLs []string, tgtURL string) { // Target exist?. if err == nil { if !tgtContent.Type.IsDir() { - fatalIf(probe.NewError(errTargetIsNotDir{URL: tgtURL})) + fatalIf(probe.NewError(errTargetIsNotDir{URL: tgtURL}), "Unable to write to target url") } } } diff --git a/cp_mirror_test.go b/cp_mirror_test.go index a0c29ca7..169e4f55 100644 --- a/cp_mirror_test.go +++ b/cp_mirror_test.go @@ -58,6 +58,7 @@ func (s *CmdTestSuite) TestCopyContext(c *C) { err := app.Run([]string{os.Args[0], "cp", server.URL + "/invalid...", server.URL + "/bucket"}) c.Assert(err, IsNil) c.Assert(console.IsError, Equals, true) + // reset back console.IsError = false } diff --git a/diff-main.go b/diff-main.go index 9715ead0..013935a5 100644 --- a/diff-main.go +++ b/diff-main.go @@ -62,17 +62,18 @@ func mainDiff(ctx *cli.Context) { secondURL := ctx.Args()[1] var err *probe.Error - firstURL, err = getExpandedURL(firstURL, config.Aliases) - fatalIf(err) - secondURL, err = getExpandedURL(secondURL, config.Aliases) - fatalIf(err) + firstURL, err = getCanonicalizedURL(firstURL, config.Aliases) + fatalIf(err, "Unable to canonicalize first URL.") + + secondURL, err = getCanonicalizedURL(secondURL, config.Aliases) + fatalIf(err, "Unable to canonicalize second URL.") + if isURLRecursive(secondURL) { console.Fatalf("Second URL cannot be recursive. %s\n", errInvalidArgument{}) } newFirstURL := stripRecursiveURL(firstURL) for diff := range doDiffCmd(newFirstURL, secondURL, isURLRecursive(firstURL)) { - fatalIf(diff.err) - console.PrintC(diff.message) + fatalIf(diff.err, diff.message) } console.Println() } diff --git a/diff_test.go b/diff_test.go index 200f4f99..234adddd 100644 --- a/diff_test.go +++ b/diff_test.go @@ -94,13 +94,20 @@ func (s *CmdTestSuite) TestDiffContext(c *C) { c.Assert(err, IsNil) c.Assert(console.IsExited, Equals, false) + // reset back + console.IsExited = false + err = app.Run([]string{os.Args[0], "diff", server.URL + "/bucket...", server.URL + "/bucket"}) c.Assert(err, IsNil) c.Assert(console.IsExited, Equals, false) + // reset back + console.IsExited = false + err = app.Run([]string{os.Args[0], "diff", server.URL + "/invalid", server.URL + "/invalid..."}) c.Assert(err, IsNil) c.Assert(console.IsExited, Equals, true) + // reset back console.IsExited = false } diff --git a/error.go b/error.go index eeb7b02a..c0f220f0 100644 --- a/error.go +++ b/error.go @@ -18,6 +18,7 @@ package main import ( "encoding/json" + "fmt" "os" "github.com/minio/mc/internal/github.com/minio/minio/pkg/probe" @@ -25,17 +26,19 @@ import ( ) // fatalIf wrapper function which takes error and selectively prints stack frames if available on debug -func fatalIf(err *probe.Error) { +func fatalIf(err *probe.Error, msg string) { if err == nil { return } if globalJSONFlag { errorMessage := struct { + Message string `json:"message"` Cause error `json:"cause"` Type string `json:"type"` CallTrace []probe.TracePoint `json:"trace,omitempty"` SysInfo map[string]string `json:"sysinfo"` }{ + Message: msg, Type: "Fatal", Cause: err.ToGoError(), SysInfo: err.SysInfo, @@ -51,23 +54,25 @@ func fatalIf(err *probe.Error) { os.Exit(1) } if !globalDebugFlag { - console.Fatalln(err.ToGoError()) + console.Fatalln(fmt.Sprintf("%s %s", msg, err.ToGoError())) } - console.Fatalln(err) + console.Fatalln(fmt.Sprintf("%s %s", msg, err)) } // errorIf synonymous with fatalIf but doesn't exit on error != nil -func errorIf(err *probe.Error) { +func errorIf(err *probe.Error, msg string) { if err == nil { return } if globalJSONFlag { errorMessage := struct { + Message string `json:"message"` Cause error `json:"cause"` Type string `json:"type"` CallTrace []probe.TracePoint `json:"trace,omitempty"` SysInfo map[string]string `json:"sysinfo"` }{ + Message: msg, Type: "Error", Cause: err.ToGoError(), SysInfo: err.SysInfo, @@ -83,8 +88,8 @@ func errorIf(err *probe.Error) { return } if !globalDebugFlag { - console.Errorln(err.ToGoError()) + console.Errorln(fmt.Sprintf("%s %s", msg, err.ToGoError())) return } - console.Errorln(err) + console.Errorln(fmt.Sprintf("%s %s", msg, err)) } diff --git a/ls-main.go b/ls-main.go index a6163562..83c39d88 100644 --- a/ls-main.go +++ b/ls-main.go @@ -82,11 +82,12 @@ func mainList(ctx *cli.Context) { config := mustGetMcConfig() for _, arg := range args { - targetURL, err := getExpandedURL(arg, config.Aliases) - fatalIf(err) + targetURL, err := getCanonicalizedURL(arg, config.Aliases) + fatalIf(err, "Unable to canonicalize URL") + // if recursive strip off the "..." newTargetURL := stripRecursiveURL(targetURL) - fatalIf(doListCmd(newTargetURL, isURLRecursive(targetURL))) + fatalIf(doListCmd(newTargetURL, isURLRecursive(targetURL)), "Unable to list target URL") } } diff --git a/ls.go b/ls.go index 36e65501..34d6d618 100644 --- a/ls.go +++ b/ls.go @@ -79,11 +79,11 @@ func doList(clnt client.Client, recursive bool) *probe.Error { switch contentCh.Err.ToGoError().(type) { // handle this specifically for filesystem case client.ISBrokenSymlink: - errorIf(contentCh.Err.Trace()) + errorIf(contentCh.Err.Trace(), "Unable to list broken link.") continue } if os.IsNotExist(contentCh.Err.ToGoError()) || os.IsPermission(contentCh.Err.ToGoError()) { - errorIf(contentCh.Err.Trace()) + errorIf(contentCh.Err.Trace(), "Unable to list.") continue } err = contentCh.Err.Trace() diff --git a/ls_test.go b/ls_test.go index 272090f4..3fb855ac 100644 --- a/ls_test.go +++ b/ls_test.go @@ -69,9 +69,13 @@ func (s *CmdTestSuite) TestLSContext(c *C) { c.Assert(err, IsNil) c.Assert(console.IsError, Equals, false) + // reset back + console.IsExited = false + err = app.Run([]string{os.Args[0], "ls", server.URL + "/invalid"}) c.Assert(err, IsNil) c.Assert(console.IsExited, Equals, true) + // reset back console.IsExited = false } diff --git a/main.go b/main.go index 4f0d6544..c49535c3 100644 --- a/main.go +++ b/main.go @@ -38,10 +38,8 @@ func checkConfig() { } // Ensures config file is sane - { - _, err := getMcConfig() - fatalIf(err) - } + _, perr := getMcConfig() + fatalIf(perr, "Unable to read config") } func migrate() { @@ -90,8 +88,7 @@ func registerBefore(ctx *cli.Context) error { } if err := console.SetTheme(themeName); err != nil { - console.Errorf("Failed to set theme ‘%s’.", themeName) - errorIf(err) + errorIf(err, "Unable to set theme") } verifyMCRuntime() diff --git a/mb-main.go b/mb-main.go index bde3e8cd..7b5c79d6 100644 --- a/mb-main.go +++ b/mb-main.go @@ -59,11 +59,11 @@ func mainMakeBucket(ctx *cli.Context) { } config := mustGetMcConfig() for _, arg := range ctx.Args() { - targetURL, err := getExpandedURL(arg, config.Aliases) - fatalIf(err) + targetURL, err := getCanonicalizedURL(arg, config.Aliases) + fatalIf(err, "Unable to canonicalize URL") + + fatalIf(doMakeBucketCmd(targetURL), "Unable to make bucket") - err = doMakeBucketCmd(targetURL) - fatalIf(err) console.Infoln("Bucket created successfully : " + targetURL) } } diff --git a/mb_access_test.go b/mb_access_test.go index 732b8eeb..f9752c14 100644 --- a/mb_access_test.go +++ b/mb_access_test.go @@ -57,9 +57,13 @@ func (s *CmdTestSuite) TestMBContext(c *C) { c.Assert(err, IsNil) c.Assert(console.IsExited, Equals, false) + // reset back + console.IsExited = false + err = app.Run([]string{os.Args[0], "mb", server.URL + "/$.bucket"}) c.Assert(err, IsNil) c.Assert(console.IsExited, Equals, true) + // reset back console.IsExited = false } @@ -69,18 +73,30 @@ func (s *CmdTestSuite) TestAccessContext(c *C) { c.Assert(err, IsNil) c.Assert(console.IsExited, Equals, false) + // reset back + console.IsExited = false + err = app.Run([]string{os.Args[0], "access", "public", server.URL + "/bucket"}) c.Assert(err, IsNil) c.Assert(console.IsExited, Equals, false) + // reset back + console.IsExited = false + err = app.Run([]string{os.Args[0], "access", "readonly", server.URL + "/bucket"}) c.Assert(err, IsNil) c.Assert(console.IsExited, Equals, false) + // reset back + console.IsExited = false + err = app.Run([]string{os.Args[0], "access", "authenticated", server.URL + "/bucket"}) c.Assert(err, IsNil) c.Assert(console.IsExited, Equals, false) + // reset back + console.IsExited = false + err = app.Run([]string{os.Args[0], "access", "invalid", server.URL + "/bucket"}) c.Assert(err, IsNil) c.Assert(console.IsExited, Equals, true) diff --git a/mirror-main.go b/mirror-main.go index d694797d..efcbfd39 100644 --- a/mirror-main.go +++ b/mirror-main.go @@ -156,8 +156,7 @@ func doPrepareMirrorURLs(session *sessionV2, trapCh <-chan bool) { break } if sURLs.Error != nil { - console.PrintC("Failed to prepare mirror URLs, ") - errorIf(sURLs.Error) + errorIf(sURLs.Error, "Unable to prepare mirror URLs.") break } if sURLs.isEmpty() { @@ -230,8 +229,7 @@ func doMirrorCmdSession(session *sessionV2) { session.Header.LastCopied = sURLs.SourceContent.Name } else { console.Println() - console.PrintC(fmt.Sprintf("Failed to mirror ‘%s’, ", sURLs.SourceContent.Name)) - errorIf(sURLs.Error) + errorIf(sURLs.Error, fmt.Sprintf("Failed to mirror ‘%s’.", sURLs.SourceContent.Name)) } case <-trapCh: // Receive interrupt notification. session.Close() diff --git a/mirror-url.go b/mirror-url.go index d5628e34..a0d5f0f2 100644 --- a/mirror-url.go +++ b/mirror-url.go @@ -57,7 +57,7 @@ func checkMirrorSyntax(ctx *cli.Context) { // extract URLs. URLs, err := args2URLs(ctx.Args()) - fatalIf(err) + fatalIf(err, "Unable to convert args to URLs") srcURL := URLs[0] tgtURLs := URLs[1:] @@ -66,7 +66,7 @@ func checkMirrorSyntax(ctx *cli.Context) { // Source cannot be a folder (except when recursive) if !isURLRecursive(srcURL) { _, srcContent, err := url2Stat(srcURL) - fatalIf(err) + fatalIf(err, "Unable to stat URL") if !srcContent.Type.IsRegular() { if srcContent.Type.IsDir() { @@ -91,18 +91,18 @@ func checkMirrorSyntax(ctx *cli.Context) { if len(tgtURLs) == 0 && tgtURLs == nil { console.Fatalf("Invalid number of target arguments to mirror command. %s\n", errInvalidArgument{}) } - srcURL = stripRecursiveURL(srcURL) - _, srcContent, err := url2Stat(srcURL) - fatalIf(err) + + _, srcContent, err := url2Stat(stripRecursiveURL(srcURL)) + fatalIf(err, "Unable to stat source URL.") if srcContent.Type.IsRegular() { // Ellipses is supported only for folders. - fatalIf(probe.NewError(errSourceIsNotDir{URL: srcURL})) + fatalIf(probe.NewError(errSourceIsNotDir{URL: srcURL}), "Invalid source type.") } for _, tgtURL := range tgtURLs { _, content, err := url2Stat(tgtURL) if err == nil { if !content.Type.IsDir() { - fatalIf(probe.NewError(errTargetIsNotDir{URL: tgtURL})) + fatalIf(probe.NewError(errTargetIsNotDir{URL: tgtURL}), "Invalid target type.") } } } diff --git a/session-main.go b/session-main.go index 47362bf0..a6675814 100644 --- a/session-main.go +++ b/session-main.go @@ -85,7 +85,8 @@ func clearSession(sid string) { if sid == "all" { for _, sid := range getSessionIDs() { session, err := loadSessionV2(sid) - fatalIf(err) + fatalIf(err, "Unable to load session") + session.Delete() } return @@ -96,7 +97,8 @@ func clearSession(sid string) { } session, err := loadSessionV2(sid) - fatalIf(err) + fatalIf(err, "Unable to load session") + if session != nil { session.Delete() } @@ -119,13 +121,12 @@ func mainSession(ctx *cli.Context) { cli.ShowCommandHelpAndExit(ctx, "session", 1) // last argument is exit code } if !isSessionDirExists() { - fatalIf(createSessionDir()) + fatalIf(createSessionDir(), "Unable to create session") } switch strings.TrimSpace(ctx.Args().First()) { // list resumable sessions case "list": - err := listSessions() - fatalIf(err) + fatalIf(listSessions(), "Unable to list sessions") case "resume": if len(ctx.Args().Tail()) != 1 { cli.ShowCommandHelpAndExit(ctx, "session", 1) // last argument is exit code @@ -140,7 +141,8 @@ func mainSession(ctx *cli.Context) { console.Fatalln(errInvalidSessionID{id: sid}) } s, err := loadSessionV2(sid) - fatalIf(err) + fatalIf(err, "Unable to load session") + // extra check for testing purposes if s == nil { return diff --git a/session_test.go b/session_test.go index cc82aa33..ca3431d6 100644 --- a/session_test.go +++ b/session_test.go @@ -59,23 +59,37 @@ func (s *CmdTestSuite) TestSessionContext(c *C) { c.Assert(err, IsNil) c.Assert(console.IsExited, Equals, false) + // reset back + console.IsExited = false + err = app.Run([]string{os.Args[0], "session", "clear", "all"}) c.Assert(err, IsNil) c.Assert(console.IsExited, Equals, false) + // reset back + console.IsExited = false + err = app.Run([]string{os.Args[0], "session", "resume", "invalid"}) c.Assert(err, IsNil) c.Assert(console.IsExited, Equals, true) + // reset back + console.IsExited = false + err = app.Run([]string{os.Args[0], "session", "clear", "invalid"}) c.Assert(err, IsNil) c.Assert(console.IsExited, Equals, true) + + // reset back console.IsExited = false perr := createSessionDir() c.Assert(perr, IsNil) c.Assert(isSessionDirExists(), Equals, true) + // reset back + console.IsExited = false + session := newSessionV2() c.Assert(session.Header.CommandArgs, IsNil) c.Assert(len(session.SessionID), Equals, 8) @@ -86,4 +100,7 @@ func (s *CmdTestSuite) TestSessionContext(c *C) { err = app.Run([]string{os.Args[0], "session", "clear", session.SessionID}) c.Assert(err, IsNil) c.Assert(console.IsExited, Equals, false) + + // reset back + console.IsExited = false } diff --git a/share-main.go b/share-main.go index 245a4bf3..677f7b78 100644 --- a/share-main.go +++ b/share-main.go @@ -73,13 +73,12 @@ func mainShare(ctx *cli.Context) { if len(args) == 2 { var err error expires, err = time.ParseDuration(args.Last()) - fatalIf(probe.NewError(err)) + fatalIf(probe.NewError(err), "Unable to parse time argument") } - targetURL, err := getExpandedURL(url, config.Aliases) - fatalIf(err) + targetURL, err := getCanonicalizedURL(url, config.Aliases) + fatalIf(err, "Unable to canonicalize URL") // if recursive strip off the "..." - newTargetURL := stripRecursiveURL(targetURL) - fatalIf(doShareCmd(newTargetURL, isURLRecursive(targetURL), expires)) + fatalIf(doShareCmd(stripRecursiveURL(targetURL), isURLRecursive(targetURL), expires), "Unable generate URL for sharing") } func getNewTargetURL(targetParser *client.URL, name string) string { diff --git a/update-main.go b/update-main.go index c7ce9a33..cd1a97ab 100644 --- a/update-main.go +++ b/update-main.go @@ -60,34 +60,33 @@ func mainUpdate(ctx *cli.Context) { if ctx.Args().First() == "help" { cli.ShowCommandHelpAndExit(ctx, "update", 1) // last argument is exit code } - fatalIf(doUpdateCheck()) -} - -func doUpdateCheck() *probe.Error { clnt, err := url2Client(mcUpdateURL) - if err != nil { - return err.Trace(mcUpdateURL) - } + fatalIf(err, "Unable to initalize update URL") + data, _, err := clnt.GetObject(0, 0) - if err != nil { - return err.Trace(mcUpdateURL) - } - current, _ := time.Parse(time.RFC3339Nano, Version) + fatalIf(err, "Unable to read update URL") + + current, goerr := time.Parse(time.RFC3339Nano, Version) + fatalIf(probe.NewError(goerr), "Unable to parse Version as time") + if current.IsZero() { console.Infoln(`Version is empty, must be a custom build cannot update. Please download releases from https://dl.minio.io:9000 for continuous updates`) - return nil + return } var updates Updates decoder := json.NewDecoder(data) - if err := decoder.Decode(&updates); err != nil { - return probe.NewError(err) - } - latest, _ := time.Parse(http.TimeFormat, updates.BuildDate) + goerr = decoder.Decode(&updates) + fatalIf(probe.NewError(goerr), "Unable to decode update URL data") + + latest, goerr := time.Parse(http.TimeFormat, updates.BuildDate) + fatalIf(probe.NewError(goerr), "Unable to parse BuildDate") + if latest.IsZero() { console.Infoln("No update available at this time") - return nil + return } + mcUpdateURLParse := clnt.URL() if latest.After(current) { var updateString string @@ -97,12 +96,11 @@ https://dl.minio.io:9000 for continuous updates`) updateString = "mc cp " + mcUpdateURLParse.Scheme + "://" + mcUpdateURLParse.Host + string(mcUpdateURLParse.Separator) + updates.Platforms[runtime.GOOS] + " ./mc" } msg, err := printUpdateNotify(updateString, "new", "old") - if err != nil { - return probe.NewError(err) - } + fatalIf(probe.NewError(err), "Unable to print update notification") + console.Println(msg) - return nil + return } console.Infoln("You are already running the most recent version of ‘mc’") - return nil + return } diff --git a/url.go b/url.go index 8f05f6e4..ed3dc847 100644 --- a/url.go +++ b/url.go @@ -45,8 +45,8 @@ func stripRecursiveURL(urlStr string) string { return urlStr } -// getExpandedURL - extracts URL string from a single cmd-line argument -func getExpandedURL(arg string, aliases map[string]string) (urlStr string, err *probe.Error) { +// getCanonicalizedURL - extracts URL string from a single cmd-line argument +func getCanonicalizedURL(arg string, aliases map[string]string) (urlStr string, err *probe.Error) { // Check and expand Alias urlStr, err = aliasExpand(arg, aliases) if err != nil { @@ -59,10 +59,10 @@ func getExpandedURL(arg string, aliases map[string]string) (urlStr string, err * return urlStr, nil } -// getExpandedURLs - extracts multiple URL strings from a single cmd-line argument -func getExpandedURLs(args []string, aliases map[string]string) (urls []string, err *probe.Error) { +// getCanonicalizedURLs - extracts multiple URL strings from a single cmd-line argument +func getCanonicalizedURLs(args []string, aliases map[string]string) (urls []string, err *probe.Error) { for _, arg := range args { - u, err := getExpandedURL(arg, aliases) + u, err := getCanonicalizedURL(arg, aliases) if err != nil { return nil, err.Trace() } @@ -79,7 +79,7 @@ func args2URLs(args []string) ([]string, *probe.Error) { } // Convert arguments to URLs: expand alias, fix format... - URLs, err := getExpandedURLs(args, config.Aliases) + URLs, err := getCanonicalizedURLs(args, config.Aliases) if err != nil { return nil, err.Trace() } diff --git a/verify-runtime.go b/verify-runtime.go index bf6d418e..b0b1c261 100644 --- a/verify-runtime.go +++ b/verify-runtime.go @@ -111,14 +111,15 @@ func verifyMCRuntime() { console.Fatalf("Unable to create ‘mc’ config folder. %s\n", err) } config, err := newConfig() - fatalIf(err) + fatalIf(err, "Unable to initialize newConfig") + err = writeConfig(config) - fatalIf(err) + fatalIf(err, "Unable to write newConfig") + console.Infoln("Configuration written to [" + mustGetMcConfigPath() + "]. Please update your access credentials.") } if !isSessionDirExists() { - err := createSessionDir() - fatalIf(err) + fatalIf(createSessionDir(), "Unable to create session dir") } checkGolangRuntimeVersion() }