diff --git a/cp-json.go b/cp-json.go index d114f13d..aade1071 100644 --- a/cp-json.go +++ b/cp-json.go @@ -19,6 +19,8 @@ package main import ( "encoding/json" "fmt" + + "github.com/minio/mc/internal/github.com/minio/minio/pkg/probe" ) // CopyMessage container for file copy messages @@ -34,8 +36,7 @@ func (c CopyMessage) String() string { return fmt.Sprintf("‘%s’ -> ‘%s’\n", c.Source, c.Target) } copyMessageBytes, err := json.Marshal(c) - if err != nil { - panic(err) - } + fatalIf(probe.NewError(err), "Failed to marshal copy message.") + return string(copyMessageBytes) + "\n" } diff --git a/cp-main.go b/cp-main.go index 4dcff63e..c5017346 100644 --- a/cp-main.go +++ b/cp-main.go @@ -159,14 +159,14 @@ func doPrepareCopyURLs(session *sessionV2, trapCh <-chan bool) { break } if cpURLs.Error != nil { - errorIf(cpURLs.Error, "Failed to prepare copy URLs.") + errorIf(cpURLs.Error.Trace(), "Unable to prepare URLs for copying.") break } jsonData, err := json.Marshal(cpURLs) if err != nil { session.Close() - console.Fatalf("Unable to marshal URLs to JSON. %s\n", err) + fatalIf(probe.NewError(err), "Unable to prepare URLs for copying. Error in JSON marshaling.") } fmt.Fprintln(dataFP, string(jsonData)) if !globalQuietFlag && !globalJSONFlag { @@ -230,7 +230,7 @@ func doCopyCmdSession(session *sessionV2) { session.Header.LastCopied = cpURLs.SourceContent.Name } else { console.Println() - errorIf(cpURLs.Error, fmt.Sprintf("Failed to copy ‘%s’. ", cpURLs.SourceContent.Name)) + errorIf(cpURLs.Error.Trace(), fmt.Sprintf("Failed to copy ‘%s’.", cpURLs.SourceContent.Name)) } case <-trapCh: // Receive interrupt notification. session.Close() @@ -274,22 +274,22 @@ func mainCopy(ctx *cli.Context) { session := newSessionV2() - var err error + var e error session.Header.CommandType = "cp" - session.Header.RootPath, err = os.Getwd() - if err != nil { + session.Header.RootPath, e = os.Getwd() + if e != nil { session.Close() session.Delete() - console.Fatalf("Unable to get current working folder. %s\n", err) + fatalIf(probe.NewError(e), "Unable to get current working folder.") } // extract URLs. - var perr *probe.Error - session.Header.CommandArgs, perr = args2URLs(ctx.Args()) - if perr != nil { + var err *probe.Error + session.Header.CommandArgs, err = args2URLs(ctx.Args()) + if err != nil { session.Close() session.Delete() - console.Fatalf("One or more unknown URL types found %s. %s\n", ctx.Args(), perr) + fatalIf(err.Trace(), "One or more unknown URL types passed.") } doCopyCmdSession(session) diff --git a/cp-url.go b/cp-url.go index feadddd3..6632c577 100644 --- a/cp-url.go +++ b/cp-url.go @@ -17,13 +17,14 @@ package main import ( + "errors" + "fmt" "path/filepath" "strings" "github.com/minio/mc/internal/github.com/minio/cli" "github.com/minio/mc/internal/github.com/minio/minio/pkg/probe" "github.com/minio/mc/pkg/client" - "github.com/minio/mc/pkg/console" ) type copyURLs struct { @@ -63,9 +64,7 @@ func checkCopySyntax(ctx *cli.Context) { } // extract URLs. URLs, err := args2URLs(ctx.Args()) - if err != nil { - console.Fatalf("One or more unknown URL types found %s. %s\n", ctx.Args(), err.Trace()) - } + fatalIf(err.Trace(ctx.Args()...), fmt.Sprintf("One or more unknown URL types passed.")) srcURLs := URLs[:len(URLs)-1] tgtURL := URLs[len(URLs)-1] @@ -73,17 +72,17 @@ func checkCopySyntax(ctx *cli.Context) { /****** Generic rules *******/ // Recursive URLs are not allowed in target. if isURLRecursive(tgtURL) { - console.Fatalf("Recursive option is not supported for target ‘%s’ argument. %s\n", tgtURL, probe.NewError(errInvalidArgument)) + fatalIf(probe.NewError(errors.New("")), fmt.Sprintf("Recursive option is not supported for target ‘%s’ argument.", tgtURL)) } // scope locally { url, err := client.Parse(tgtURL) if err != nil { - console.Fatalf("Unable to parse target ‘%s’ argument. %s\n", tgtURL, probe.NewError(err)) + fatalIf(probe.NewError(err), fmt.Sprintf("Unable to parse target ‘%s’ argument.", tgtURL)) } if url.Host != "" { if url.Path == string(url.Separator) { - console.Fatalf("Bucket creation detected for %s, cloud storage URL's should use ‘mc mb’ to create buckets\n", tgtURL) + fatalIf(probe.NewError(errors.New("")), fmt.Sprintf("Bucket creation detected for %s, cloud storage URL's should use ‘mc mb’ to create buckets.", tgtURL)) } } } @@ -97,48 +96,49 @@ func checkCopySyntax(ctx *cli.Context) { case copyURLsTypeD: // File | Folder... -> Folder. checkCopySyntaxTypeD(srcURLs, tgtURL) default: - console.Fatalln("Invalid arguments. Unable to determine how to copy. Please report this issue at https://github.com/minio/mc/issues") + fatalIf(probe.NewError(errInvalidArgument), "Invalid arguments to copy command.") } } // checkCopySyntaxTypeA verifies if the source and target are valid file arguments. func checkCopySyntaxTypeA(srcURLs []string, tgtURL string) { if len(srcURLs) != 1 { - console.Fatalf("Invalid number of source arguments to copy command. %s\n", probe.NewError(errInvalidArgument)) + fatalIf(probe.NewError(errInvalidArgument), "Invalid number of source arguments to copy command.") } srcURL := srcURLs[0] _, srcContent, err := url2Stat(srcURL) - fatalIf(err.Trace(), "Unable to stat source URL") + + fatalIf(err.Trace(srcURL), "Unable to stat source ‘"+srcURL+"’.") 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) + fatalIf(probe.NewError(errInvalidArgument), fmt.Sprintf("Source ‘%s’ is a folder. Use ‘%s...’ argument to copy this folder and its contents recursively.", srcURL, srcURL)) } if !srcContent.Type.IsRegular() { - fatalIf(probe.NewError(errSourceIsNotFile{URL: srcURL}), "Invalid source type") + fatalIf(probe.NewError(errInvalidArgument), "Source ‘"+srcURL+"’ is not a file.") } } // checkCopySyntaxTypeB verifies if the source is a valid file and target is a valid dir. func checkCopySyntaxTypeB(srcURLs []string, tgtURL string) { if len(srcURLs) != 1 { - console.Fatalf("Invalid number of source arguments to copy command. %s\n", errInvalidArgument) + fatalIf(probe.NewError(errInvalidArgument), "Invalid number of source arguments to copy command.") } srcURL := srcURLs[0] _, srcContent, err := url2Stat(srcURL) - fatalIf(err.Trace(), "Unable to stat source URL") + fatalIf(err.Trace(srcURL), "Unable to stat source ‘"+srcURL+"’.") 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) + fatalIf(probe.NewError(errInvalidArgument), fmt.Sprintf("Source ‘%s’ is a folder. Use ‘%s...’ argument to copy this folder and its contents recursively.", srcURL, srcURL)) } if !srcContent.Type.IsRegular() { - fatalIf(probe.NewError(errSourceIsNotFile{URL: srcURL}), "Invalid source type") + fatalIf(probe.NewError(errInvalidArgument), "Source ‘"+srcURL+"’ is not a file.") } _, tgtContent, err := url2Stat(tgtURL) // Target exist?. if err == nil { if !tgtContent.Type.IsDir() { - fatalIf(probe.NewError(errTargetIsNotDir{URL: tgtURL}), "Invalid target type") + fatalIf(probe.NewError(errInvalidArgument), "Target ‘"+tgtURL+"’ is not a folder.") } } } @@ -146,21 +146,21 @@ func checkCopySyntaxTypeB(srcURLs []string, tgtURL string) { // checkCopySyntaxTypeC verifies if the source is a valid recursive dir and target is a valid dir. func checkCopySyntaxTypeC(srcURLs []string, tgtURL string) { if len(srcURLs) != 1 { - console.Fatalf("Invalid number of source arguments to copy command. %s\n", errInvalidArgument) + fatalIf(probe.NewError(errInvalidArgument), "Invalid number of source arguments to copy command.") } srcURL := srcURLs[0] srcURL = stripRecursiveURL(srcURL) _, srcContent, err := url2Stat(srcURL) - fatalIf(err.Trace(), "Unable to stat source URL") + fatalIf(err.Trace(srcURL), "Unable to stat source ‘"+srcURL+"’.") if srcContent.Type.IsRegular() { // Ellipses is supported only for folders. - fatalIf(probe.NewError(errSourceIsNotDir{URL: srcURL}), "Invalid source type") + fatalIf(probe.NewError(errInvalidArgument), "Source ‘"+srcURL+"’ is not a folder.") } _, tgtContent, err := url2Stat(tgtURL) // Target exist?. if err == nil { if !tgtContent.Type.IsDir() { - fatalIf(probe.NewError(errTargetIsNotDir{URL: tgtURL}), "Invalid target type") + fatalIf(probe.NewError(errInvalidArgument), "Target ‘"+tgtURL+"’ is not a folder.") } } } @@ -171,20 +171,20 @@ func checkCopySyntaxTypeD(srcURLs []string, tgtURL string) { if isURLRecursive(srcURL) { srcURL = stripRecursiveURL(srcURL) _, srcContent, err := url2Stat(srcURL) - fatalIf(err.Trace(), "Unable to stat source URL") + fatalIf(err.Trace(srcURL), "Unable to stat source ‘"+srcURL+"’.") if !srcContent.Type.IsDir() { // Ellipses is supported only for folders. - fatalIf(probe.NewError(errSourceIsNotDir{URL: srcURL}), "Invalid source type") + fatalIf(probe.NewError(errInvalidArgument), "Source ‘"+srcURL+"’ is not a folder.") } } else { // Regular URL. _, srcContent, err := url2Stat(srcURL) - fatalIf(err.Trace(), "Unable to stat source URL") + fatalIf(err.Trace(srcURL), "Unable to stat source ‘"+srcURL+"’.") 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) + fatalIf(probe.NewError(errInvalidArgument), fmt.Sprintf("Source ‘%s’ is a folder. Use ‘%s...’ argument to copy this folder and its contents recursively.", srcURL, srcURL)) } if !srcContent.Type.IsRegular() { - fatalIf(probe.NewError(errSourceIsNotFile{URL: srcURL}), "Unable to read from source url") + fatalIf(probe.NewError(errInvalidArgument), "Source ‘"+srcURL+"’ is not a file.") } } } @@ -192,7 +192,7 @@ func checkCopySyntaxTypeD(srcURLs []string, tgtURL string) { // Target exist?. if err == nil { if !tgtContent.Type.IsDir() { - fatalIf(probe.NewError(errTargetIsNotDir{URL: tgtURL}), "Unable to write to target url") + fatalIf(probe.NewError(errInvalidArgument), "Target ‘"+tgtURL+"’ is not a folder.") } } } @@ -233,7 +233,7 @@ func prepareCopyURLsTypeA(sourceURL string, targetURL string) copyURLs { _, sourceContent, err := url2Stat(sourceURL) if err != nil { // Source does not exist or insufficient privileges. - return copyURLs{Error: err.Trace()} + return copyURLs{Error: err.Trace(sourceURL)} } if !sourceContent.Type.IsRegular() { // Source is not a regular file @@ -250,7 +250,7 @@ func prepareCopyURLsTypeB(sourceURL string, targetURL string) copyURLs { _, sourceContent, err := url2Stat(sourceURL) if err != nil { // Source does not exist or insufficient privileges. - return copyURLs{Error: err.Trace()} + return copyURLs{Error: err.Trace(sourceURL)} } if !sourceContent.Type.IsRegular() { // Source is not a regular file. @@ -290,7 +290,7 @@ func prepareCopyURLsTypeC(sourceURL, targetURL string) <-chan copyURLs { sourceClient, sourceContent, err := url2Stat(sourceURL) if err != nil { // Source does not exist or insufficient privileges. - copyURLsCh <- copyURLs{Error: err.Trace()} + copyURLsCh <- copyURLs{Error: err.Trace(sourceURL)} return } diff --git a/typed-errors.go b/typed-errors.go index 3b253ef6..7a4eb491 100644 --- a/typed-errors.go +++ b/typed-errors.go @@ -91,10 +91,4 @@ func (e errSourceIsNotDir) Error() string { return "Source ‘" + e.URL + "’ is not a folder." } -type errSourceIsNotFile errTargetIsNotDir - -func (e errSourceIsNotFile) Error() string { - return "Source ‘" + e.URL + "’ is not a file." -} - var errSourceListEmpty = errors.New("Source list is empty.")