Handle errors better with new changes, changes across cp, mirror, mb

This commit is contained in:
Harshavardhana
2015-08-03 21:13:29 -07:00
parent 08252acba7
commit e003a242f6
6 changed files with 56 additions and 83 deletions
+23 -47
View File
@@ -108,49 +108,35 @@ func checkCopySyntaxTypeA(srcURLs []string, tgtURL string) {
}
srcURL := srcURLs[0]
_, srcContent, err := url2Stat(srcURL)
// Source exist?.
if err != nil {
console.Fatalf("Unable to stat source ‘%s’. %s\n", srcURL, err.Trace())
}
ifFatal(err)
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, probe.New(errInvalidArgument{}))
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() {
console.Fatalf("Source ‘%s’ is not a file. %s\n", srcURL, probe.New(errInvalidArgument{}))
}
_, tgtContent, err := url2Stat(tgtURL)
// Target exist?.
if err == nil {
if !tgtContent.Type.IsRegular() {
console.Fatalf("Target ‘%s’ is not a file. %s\n", tgtURL, probe.New(errInvalidArgument{}))
}
ifFatal(probe.New(errSourceIsNotFile{URL: srcURL}))
}
}
// 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", probe.New(errInvalidArgument{}))
console.Fatalf("Invalid number of source arguments to copy command. %s\n", errInvalidArgument{})
}
srcURL := srcURLs[0]
_, srcContent, err := url2Stat(srcURL)
// Source exist?.
if err != nil {
console.Fatalf("Unable to stat source ‘%s’. %s\n", srcURL, probe.New(err))
}
ifFatal(err)
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, probe.New(errInvalidArgument{}))
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() {
console.Fatalf("Source ‘%s’ is not a file. %s\n", srcURL, probe.New(errInvalidArgument{}))
ifFatal(probe.New(errSourceIsNotFile{URL: srcURL}))
}
_, tgtContent, err := url2Stat(tgtURL)
// Target exist?.
if err == nil {
if !tgtContent.Type.IsDir() {
console.Fatalf("Target ‘%s’ is not a folder. %s\n", tgtURL, probe.New(errInvalidArgument{}))
ifFatal(probe.New(errTargetIsNotDir{URL: tgtURL}))
}
}
}
@@ -158,24 +144,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", probe.New(errInvalidArgument{}))
console.Fatalf("Invalid number of source arguments to copy command. %s\n", errInvalidArgument{})
}
srcURL := srcURLs[0]
srcURL = stripRecursiveURL(srcURL)
_, srcContent, err := url2Stat(srcURL)
// Source exist?.
if err != nil {
console.Fatalf("Unable to stat source ‘%s’. %s\n", srcURL, err.Trace())
}
if srcContent.Type.IsRegular() { // Ellipses is supported only for folders.
console.Fatalf("Source ‘%s’ is not a folder. %s\n", stripRecursiveURL(srcURL), probe.New(errInvalidArgument{}))
}
ifFatal(err)
if srcContent.Type.IsRegular() { // Ellipses is supported only for folders.
ifFatal(probe.New(errSourceIsNotDir{URL: srcURL}))
}
_, tgtContent, err := url2Stat(tgtURL)
// Target exist?.
if err == nil {
if !tgtContent.Type.IsDir() {
console.Fatalf("Target ‘%s’ is not a folder. %s\n", tgtURL, probe.New(errInvalidArgument{}))
ifFatal(probe.New(errTargetIsNotDir{URL: tgtURL}))
}
}
}
@@ -186,34 +169,27 @@ func checkCopySyntaxTypeD(srcURLs []string, tgtURL string) {
if isURLRecursive(srcURL) {
srcURL = stripRecursiveURL(srcURL)
_, srcContent, err := url2Stat(srcURL)
// Source exist?.
if err != nil {
console.Fatalf("Unable to stat source ‘%s’. %s\n", srcURL, err.Trace())
}
ifFatal(err)
if !srcContent.Type.IsDir() { // Ellipses is supported only for folders.
console.Fatalf("Source ‘%s’ is not a folder. %s\n", stripRecursiveURL(srcURL), probe.New(errInvalidArgument{}))
ifFatal(probe.New(errSourceIsNotDir{URL: srcURL}))
}
} else { // Regular URL.
_, srcContent, err := url2Stat(srcURL)
// Source exist?.
if err != nil {
console.Fatalf("Unable to stat source ‘%s’. %s\n", srcURL, err.Trace())
}
ifFatal(err)
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, probe.New(errInvalidArgument{}))
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() {
console.Fatalf("Source ‘%s’ is not a file. %s\n", srcURL, probe.New(errInvalidArgument{}))
ifFatal(probe.New(errSourceIsNotFile{URL: srcURL}))
}
}
}
_, tgtContent, err := url2Stat(tgtURL)
// Target exist?.
if err != nil {
console.Fatalf("Unable to stat target ‘%s’. %s\n", tgtURL, err.Trace())
}
if !tgtContent.Type.IsDir() {
console.Fatalf("Target ‘%s’ is not a folder. %s\n", tgtURL, probe.New(errInvalidArgument{}))
if err == nil {
if !tgtContent.Type.IsDir() {
ifFatal(probe.New(errTargetIsNotDir{URL: tgtURL}))
}
}
}
+6
View File
@@ -161,6 +161,12 @@ 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."
}
type errSourceListEmpty errInvalidArgument
func (e errSourceListEmpty) Error() string {
+1 -2
View File
@@ -86,8 +86,7 @@ func runListCmd(ctx *cli.Context) {
ifFatal(err)
// if recursive strip off the "..."
newTargetURL := stripRecursiveURL(targetURL)
err = doListCmd(newTargetURL, isURLRecursive(targetURL))
ifFatal(err)
ifFatal(doListCmd(newTargetURL, isURLRecursive(targetURL)))
}
}
+6 -11
View File
@@ -17,8 +17,6 @@
package main
import (
"fmt"
"github.com/minio/cli"
"github.com/minio/mc/pkg/console"
"github.com/minio/minio/pkg/probe"
@@ -63,23 +61,20 @@ func runMakeBucketCmd(ctx *cli.Context) {
for _, arg := range ctx.Args() {
targetURL, err := getExpandedURL(arg, config.Aliases)
ifFatal(err)
msg, err := doMakeBucketCmd(targetURL)
ifFatal(err)
console.Infoln(msg)
ifFatal(doMakeBucketCmd(targetURL))
console.Infoln("Bucket created successfully : " + targetURL)
}
}
// doMakeBucketCmd -
func doMakeBucketCmd(targetURL string) (string, *probe.Error) {
func doMakeBucketCmd(targetURL string) *probe.Error {
clnt, err := target2Client(targetURL)
if err != nil {
msg := fmt.Sprintf("Unable to initialize client for ‘%s’", targetURL)
return msg, err.Trace()
return err.Trace()
}
err = clnt.MakeBucket()
if err != nil {
msg := fmt.Sprintf("Failed to create bucket for URL ‘%s’", clnt.URL().String())
return msg, err.Trace()
return err.Trace()
}
return "Bucket created successfully : " + clnt.URL().String(), nil
return nil
}
+14 -13
View File
@@ -30,24 +30,25 @@ func (s *CmdTestSuite) TestMbAndAccessCmd(c *C) {
root, err := ioutil.TempDir(os.TempDir(), "cmd-")
c.Assert(err, IsNil)
defer os.RemoveAll(root)
{
err := doMakeBucketCmd(filepath.Join(root, "bucket"))
c.Assert(err, IsNil)
_, err = doMakeBucketCmd(filepath.Join(root, "bucket"))
c.Assert(err, IsNil)
err = doUpdateAccessCmd(filepath.Join(root, "bucket"), "public-read-write")
c.Assert(err, IsNil)
err = doUpdateAccessCmd(filepath.Join(root, "bucket"), "public-read-write")
c.Assert(err, IsNil)
err = doUpdateAccessCmd(filepath.Join(root, "bucket"), "invalid")
c.Assert(err, Not(IsNil))
err = doUpdateAccessCmd(filepath.Join(root, "bucket"), "invalid")
c.Assert(err, Not(IsNil))
err = doMakeBucketCmd(server.URL + "/bucket")
c.Assert(err, IsNil)
_, err = doMakeBucketCmd(server.URL + "/bucket")
c.Assert(err, IsNil)
err = doUpdateAccessCmd(server.URL+"/bucket", "public-read-write")
c.Assert(err, IsNil)
err = doUpdateAccessCmd(server.URL+"/bucket", "public-read-write")
c.Assert(err, IsNil)
err = doUpdateAccessCmd(server.URL+"/bucket", "invalid")
c.Assert(err, Not(IsNil))
err = doUpdateAccessCmd(server.URL+"/bucket", "invalid")
c.Assert(err, Not(IsNil))
}
}
+6 -10
View File
@@ -57,9 +57,7 @@ func checkMirrorSyntax(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())
}
ifFatal(err)
srcURL := URLs[0]
tgtURLs := URLs[1:]
@@ -68,10 +66,8 @@ func checkMirrorSyntax(ctx *cli.Context) {
// Source cannot be a folder (except when recursive)
if !isURLRecursive(srcURL) {
_, srcContent, err := url2Stat(srcURL)
// Source exist?.
if err != nil {
console.Fatalf("Unable to stat source ‘%s’. %s\n", srcURL, err.Trace())
}
ifFatal(err)
if !srcContent.Type.IsRegular() {
if srcContent.Type.IsDir() {
console.Fatalf("Source ‘%s’ is a folder. Please use ‘%s...’ to recursively copy this folder and its contents.\n", srcURL, srcURL)
@@ -93,20 +89,20 @@ func checkMirrorSyntax(ctx *cli.Context) {
}
if len(tgtURLs) == 0 && tgtURLs == nil {
console.Fatalf("Invalid number of target arguments to mirror command. %s\n", probe.New(errInvalidArgument{}))
console.Fatalf("Invalid number of target arguments to mirror command. %s\n", errInvalidArgument{})
}
srcURL = stripRecursiveURL(srcURL)
_, srcContent, err := url2Stat(srcURL)
ifFatal(err)
if srcContent.Type.IsRegular() { // Ellipses is supported only for folders.
console.Fatalf("Source ‘%s’ is not a folder. %s\n", stripRecursiveURL(srcURL), probe.New(errSourceIsNotDir{}))
ifFatal(probe.New(errSourceIsNotDir{URL: srcURL}))
}
for _, tgtURL := range tgtURLs {
_, content, err := url2Stat(tgtURL)
if err == nil {
if !content.Type.IsDir() {
console.Fatalf("One of the target ‘%s’ is not a folder. cannot have mixtures of directories and files while copying directories recursively. %s\n", tgtURL, probe.New(errInvalidArgument{}))
ifFatal(probe.New(errTargetIsNotDir{URL: tgtURL}))
}
}
}