diff --git a/appveyor.yml b/appveyor.yml index bd893c51..307b5c28 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -22,8 +22,8 @@ install: # to run your custom scripts instead of automatic MSBuild build_script: - - go test - - go test -race + - go test . + - go test -race . - go test github.com/minio/mc/pkg... - go test -race github.com/minio/mc/pkg... diff --git a/cp-main.go b/cp-main.go index 25304b63..85d3dbaa 100644 --- a/cp-main.go +++ b/cp-main.go @@ -25,6 +25,7 @@ import ( "net" "os" "runtime" + "strings" "sync" "github.com/fatih/color" @@ -179,7 +180,11 @@ func doPrepareCopyURLs(session *sessionV2, trapCh <-chan bool) { if !globalQuietFlag && !globalJSONFlag { console.Eraseline() } - errorIf(cpURLs.Error.Trace(), "Unable to prepare URL for copying.") + if strings.Contains(cpURLs.Error.ToGoError().Error(), " is a folder.") { + errorIf(cpURLs.Error.Trace(), "Folder cannot be copied. Please use ‘...’ suffix.") + } else { + errorIf(cpURLs.Error.Trace(), "Unable to prepare URL for copying.") + } break } diff --git a/cp-url.go b/cp-url.go index 3ef0582f..97aedcd2 100644 --- a/cp-url.go +++ b/cp-url.go @@ -113,7 +113,7 @@ func checkCopySyntaxTypeA(srcURLs []string, tgtURL string) { fatalIf(err.Trace(srcURL), "Unable to stat source ‘"+srcURL+"’.") if srcContent.Type.IsDir() { - fatalIf(errInvalidArgument().Trace(), fmt.Sprintf("Source ‘%s’ is a folder. Use ‘%s...’ argument to copy this folder and its contents recursively.", srcURL, srcURL)) + fatalIf(errSourceIsDir(srcURL).Trace(srcURL), fmt.Sprintf("Folder cannot copied. Please use ‘%s...’ to copy this folder and its contents recursively.", srcURL)) } if !srcContent.Type.IsRegular() { fatalIf(errInvalidArgument().Trace(), "Source ‘"+srcURL+"’ is not a file.") @@ -130,17 +130,17 @@ func checkCopySyntaxTypeB(srcURLs []string, tgtURL string) { fatalIf(err.Trace(srcURL), "Unable to stat source ‘"+srcURL+"’.") if srcContent.Type.IsDir() { - fatalIf(errInvalidArgument().Trace(), fmt.Sprintf("Source ‘%s’ is a folder. Use ‘%s...’ argument to copy this folder and its contents recursively.", srcURL, srcURL)) + fatalIf(errSourceIsDir(srcURL).Trace(srcURL), fmt.Sprintf("Folder cannot be copied. Please use ‘%s...’ argument to copy this folder and its contents recursively.", srcURL)) } if !srcContent.Type.IsRegular() { - fatalIf(errInvalidArgument().Trace(), "Source ‘"+srcURL+"’ is not a file.") + fatalIf(errInvalidArgument().Trace(srcURL), "Source ‘"+srcURL+"’ is not a file.") } _, tgtContent, err := url2Stat(tgtURL) // Target exist?. if err == nil { if !tgtContent.Type.IsDir() { - fatalIf(errInvalidArgument().Trace(), "Target ‘"+tgtURL+"’ is not a folder.") + fatalIf(errInvalidArgument().Trace(tgtURL), "Target ‘"+tgtURL+"’ is not a folder.") } } } @@ -162,7 +162,7 @@ func checkCopySyntaxTypeC(srcURLs []string, tgtURL string) { // Target exist?. if err == nil { if !tgtContent.Type.IsDir() { - fatalIf(errInvalidArgument().Trace(), "Target ‘"+tgtURL+"’ is not a folder.") + fatalIf(errInvalidArgument().Trace(tgtURL), "Target ‘"+tgtURL+"’ is not a folder.") } } } @@ -176,25 +176,18 @@ func checkCopySyntaxTypeD(srcURLs []string, tgtURL string) { fatalIf(err.Trace(srcURL), "Unable to stat source ‘"+srcURL+"’.") if !srcContent.Type.IsDir() { // Ellipses is supported only for folders. - fatalIf(errInvalidArgument().Trace(), "Source ‘"+srcURL+"’ is not a folder.") + fatalIf(errInvalidArgument().Trace(srcURL), "Source ‘"+srcURL+"’ is not a folder.") } } else { // Regular URL. - _, srcContent, err := url2Stat(srcURL) + _, _, err := url2Stat(srcURL) fatalIf(err.Trace(srcURL), "Unable to stat source ‘"+srcURL+"’.") - - if srcContent.Type.IsDir() { - fatalIf(errInvalidArgument().Trace(), 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(errInvalidArgument().Trace(), "Source ‘"+srcURL+"’ is not a file.") - } } } _, tgtContent, err := url2Stat(tgtURL) // Target exist?. if err == nil { if !tgtContent.Type.IsDir() { - fatalIf(errInvalidArgument().Trace(), "Target ‘"+tgtURL+"’ is not a folder.") + fatalIf(errInvalidArgument().Trace(tgtURL), "Target ‘"+tgtURL+"’ is not a folder.") } } } @@ -255,6 +248,9 @@ func prepareCopyURLsTypeB(sourceURL string, targetURL string) copyURLs { return copyURLs{Error: err.Trace(sourceURL)} } if !sourceContent.Type.IsRegular() { + if sourceContent.Type.IsDir() { + return copyURLs{Error: errSourceIsDir(sourceURL).Trace()} + } // Source is not a regular file. return copyURLs{Error: errInvalidSource(sourceURL).Trace()} } diff --git a/error.go b/error.go index f30db513..f0443145 100644 --- a/error.go +++ b/error.go @@ -19,7 +19,6 @@ package main import ( "encoding/json" "fmt" - "os" "github.com/minio/mc/pkg/console" "github.com/minio/minio-xl/pkg/probe" @@ -34,6 +33,43 @@ type ErrorMessage struct { SysInfo map[string]string `json:"sysinfo"` } +// fatalIfMultiple wrapper function which takes error message map and selectively prints stack frames if available on debug +// fatalIfMultiple is implemented to handle multiple argument validation. +/* +func fatalIfMultiple(errMsgMap map[string]*probe.Error) { + if errMsgMap == nil { + return + } + for msg, err := range errMsgMap { + if globalJSONFlag { + errorMessage := ErrorMessage{ + Message: msg, + Type: "error", + Cause: err.ToGoError(), + SysInfo: err.SysInfo, + } + if globalDebugFlag { + errorMessage.CallTrace = err.CallTrace + } + json, err := json.Marshal(struct { + Error ErrorMessage `json:"error"` + }{ + Error: errorMessage, + }) + if err != nil { + console.Fatalln(probe.NewError(err)) + } + console.Println(string(json)) + } + if !globalDebugFlag { + console.Errorln(fmt.Sprintf("%s %s", msg, err.ToGoError())) + } + console.Errorln(fmt.Sprintf("%s %s", msg, err)) + } + console.Fatalln() +} +*/ + // fatalIf wrapper function which takes error and selectively prints stack frames if available on debug func fatalIf(err *probe.Error, msg string) { if err == nil { @@ -58,7 +94,7 @@ func fatalIf(err *probe.Error, msg string) { console.Fatalln(probe.NewError(err)) } console.Println(string(json)) - os.Exit(1) + console.Fatalln() } if !globalDebugFlag { console.Fatalln(fmt.Sprintf("%s %s", msg, err.ToGoError())) diff --git a/typed-errors.go b/typed-errors.go index 583920a1..07c5c23f 100644 --- a/typed-errors.go +++ b/typed-errors.go @@ -62,6 +62,9 @@ var ( errSourceIsNotDir = func(URL string) *probe.Error { return probe.NewError(errors.New("Source ‘" + URL + "’ is not a folder.")).Untrace() } + errSourceIsDir = func(URL string) *probe.Error { + return probe.NewError(errors.New("Source ‘" + URL + "’ is a folder.")).Untrace() + } errNotAnObject = func(URL string) *probe.Error { return probe.NewError(errors.New("‘" + URL + "’ is not an object.")).Untrace() }