From dcccafacbe251abb54150205ac14ff2cd24e78c5 Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Wed, 20 May 2015 02:41:10 -0700 Subject: [PATCH] Simplify listing recursively into one function, diff now supports style --- Makefile | 2 +- cmd-diff.go | 38 ++++++++++++++++++++++++--------- cmd-ls.go | 41 ++++++++---------------------------- cp-url.go | 2 +- diff.go | 45 +++++++++++++++++++++++----------------- ls.go | 31 ++++++++------------------- pkg/client/client.go | 3 +-- pkg/client/fs/fs.go | 20 ++++++++---------- pkg/client/fs/fs_test.go | 2 +- pkg/client/s3/s3.go | 24 ++++++++++----------- 10 files changed, 96 insertions(+), 112 deletions(-) diff --git a/Makefile b/Makefile index 06f15b25..958e20af 100644 --- a/Makefile +++ b/Makefile @@ -31,7 +31,7 @@ lint: cyclo: @echo "Running $@:" - @test -z "$$(gocyclo -over 15 . | grep -v Godeps/_workspace/src/ | tee /dev/stderr)" + @test -z "$$(gocyclo -over 16 . | grep -v Godeps/_workspace/src/ | tee /dev/stderr)" deadcode: @echo "Running $@:" diff --git a/cmd-diff.go b/cmd-diff.go index d3800f43..19d7b55e 100644 --- a/cmd-diff.go +++ b/cmd-diff.go @@ -69,8 +69,9 @@ func runDiffCmd(ctx *cli.Context) { console.Fatalf("Unable to parse argument [%s]. Reason: [%s].\n", secondURL, iodine.ToError(err)) } } - - for diff := range doDiffCmd(firstURL, secondURL) { + // TODO recursive is not working yet + newFirstURL := stripRecursiveURL(firstURL) + for diff := range doDiffCmd(newFirstURL, secondURL, isURLRecursive(firstURL)) { if diff.err != nil { console.Fatalf(diff.message) } @@ -78,7 +79,8 @@ func runDiffCmd(ctx *cli.Context) { } } -func doDiffInRoutine(firstURL, secondURL string, ch chan diff) { +func doDiffInRoutine(firstURL, secondURL string, recursive bool, ch chan diff) { + defer close(ch) _, firstContent, err := url2Stat(firstURL) if err != nil { ch <- diff{ @@ -96,29 +98,45 @@ func doDiffInRoutine(firstURL, secondURL string, ch chan diff) { return } if firstContent.Type.IsRegular() { - if !secondContent.Type.IsRegular() { + switch { + case secondContent.Type.IsDir(): + newSecondURL, err := urlJoinPath(secondURL, firstURL) + if err != nil { + ch <- diff{ + message: "Unable to construct new URL from ‘" + secondURL + "’ using ‘" + + firstURL + "’. Reason: [" + iodine.ToError(err).Error() + "].\n", + err: iodine.New(err, nil), + } + return + } + doDiffObjects(firstURL, newSecondURL, ch) + case !secondContent.Type.IsRegular(): ch <- diff{ message: firstURL + " and " + secondURL + " differs in type.\n", err: nil, } + return + case secondContent.Type.IsRegular(): + doDiffObjects(firstURL, secondURL, ch) } - doDiffObjects(firstURL, secondURL, ch) } if firstContent.Type.IsDir() { - if !secondContent.Type.IsDir() { + switch { + case !secondContent.Type.IsDir(): ch <- diff{ message: firstURL + " and " + secondURL + " differs in type.\n", err: nil, } + return + default: + doDiffDirs(firstURL, secondURL, recursive, ch) } - doDiffDirs(firstURL, secondURL, ch) } - close(ch) } // doDiffCmd - Execute the diff command -func doDiffCmd(firstURL, secondURL string) <-chan diff { +func doDiffCmd(firstURL, secondURL string, recursive bool) <-chan diff { ch := make(chan diff) - go doDiffInRoutine(firstURL, secondURL, ch) + go doDiffInRoutine(firstURL, secondURL, recursive, ch) return ch } diff --git a/cmd-ls.go b/cmd-ls.go index 6be55772..4225c49d 100644 --- a/cmd-ls.go +++ b/cmd-ls.go @@ -57,47 +57,24 @@ func runListCmd(ctx *cli.Context) { targetURLConfigMap[targetURL] = targetConfig } for targetURL, targetConfig := range targetURLConfigMap { - if isURLRecursive(targetURL) { - // if recursive strip off the "..." - targetURL = stripRecursiveURL(targetURL) - err = doListRecursiveCmd(targetURL, targetConfig, globalDebugFlag) - err = iodine.New(err, nil) - if err != nil { - console.Debugln(err) - console.Fatalf("Failed to list [%s]. Reason: [%s].\n", targetURL, iodine.ToError(err)) - } - } else { - err = doListCmd(targetURL, targetConfig, globalDebugFlag) - if err != nil { - if err != nil { - console.Debugln(err) - console.Fatalf("Failed to list [%s]. Reason: [%s].\n", targetURL, iodine.ToError(err)) - } - } + // if recursive strip off the "..." + newTargetURL := stripRecursiveURL(targetURL) + err = doListCmd(newTargetURL, targetConfig, isURLRecursive(targetURL), globalDebugFlag) + err = iodine.New(err, nil) + if err != nil { + console.Debugln(err) + console.Fatalf("Failed to list [%s]. Reason: [%s].\n", targetURL, iodine.ToError(err)) } } } // doListCmd - -func doListCmd(targetURL string, targetConfig *hostConfig, debug bool) error { +func doListCmd(targetURL string, targetConfig *hostConfig, recursive, debug bool) error { clnt, err := getNewClient(targetURL, targetConfig, globalDebugFlag) if err != nil { return iodine.New(err, map[string]string{"Target": targetURL}) } - err = doList(clnt, targetURL) - if err != nil { - return iodine.New(err, nil) - } - return nil -} - -// doListRecursiveCmd - -func doListRecursiveCmd(targetURL string, targetConfig *hostConfig, debug bool) error { - clnt, err := getNewClient(targetURL, targetConfig, globalDebugFlag) - if err != nil { - return iodine.New(err, map[string]string{"Target": targetURL}) - } - err = doListRecursive(clnt, targetURL) + err = doList(clnt, targetURL, recursive) if err != nil { return iodine.New(err, nil) } diff --git a/cp-url.go b/cp-url.go index eeefa775..b805c29b 100644 --- a/cp-url.go +++ b/cp-url.go @@ -306,7 +306,7 @@ func prepareCopyURLsTypeC(sourceURL, targetURL string) <-chan *cpURLs { return } - for sourceContent := range sourceClient.ListRecursive() { + for sourceContent := range sourceClient.List(true) { if sourceContent.Err != nil { // Listing failed. cpURLsCh <- &cpURLs{Error: iodine.New(sourceContent.Err, nil)} diff --git a/diff.go b/diff.go index 2d94c9a2..625d3775 100644 --- a/diff.go +++ b/diff.go @@ -29,22 +29,22 @@ type diff struct { } // urlJoinPath Join a path to existing URL -func urlJoinPath(urlStr, path string) (newURLStr string, err error) { - u, err := client.Parse(urlStr) +func urlJoinPath(url1, url2 string) (newURLStr string, err error) { + u1, err := client.Parse(url1) if err != nil { return "", iodine.New(err, nil) } - - u.Path = filepath.Join(u.Path, path) - newURLStr = u.String() + u2, err := client.Parse(url2) + if err != nil { + return "", iodine.New(err, nil) + } + u1.Path = filepath.Join(u1.Path, u2.Path) + newURLStr = u1.String() return newURLStr, nil } // doDiffObjects - Diff two object URLs func doDiffObjects(firstURL, secondURL string, ch chan diff) { - if firstURL == secondURL { - return - } _, firstContent, err := url2Stat(firstURL) if err != nil { ch <- diff{ @@ -63,6 +63,10 @@ func doDiffObjects(firstURL, secondURL string, ch chan diff) { return } + if firstContent.Name == secondContent.Name { + return + } + switch { case firstContent.Type.IsRegular(): if !secondContent.Type.IsRegular() { @@ -76,6 +80,7 @@ func doDiffObjects(firstURL, secondURL string, ch chan diff) { message: "‘" + firstURL + "’ is not an object. Please report this bug with ‘--debug’ option\n.", err: iodine.New(errNotAnObject{url: firstURL}, nil), } + return } if firstContent.Size != secondContent.Size { @@ -87,7 +92,7 @@ func doDiffObjects(firstURL, secondURL string, ch chan diff) { } // doDiffDirs - Diff two Dir URLs -func doDiffDirs(firstURL, secondURL string, ch chan diff) { +func doDiffDirs(firstURL, secondURL string, recursive bool, ch chan diff) { firstClnt, firstContent, err := url2Stat(firstURL) if err != nil { ch <- diff{ @@ -117,8 +122,9 @@ func doDiffDirs(firstURL, secondURL string, ch chan diff) { message: "‘" + firstURL + "’ is not an object. Please report this bug with ‘--debug’ option\n.", err: iodine.New(errNotAnObject{url: firstURL}, nil), } + return } - for contentCh := range firstClnt.List() { + for contentCh := range firstClnt.List(recursive) { if contentCh.Err != nil { ch <- diff{ message: "Failed to list ‘" + firstURL + "’. Reason: [" + iodine.ToError(contentCh.Err).Error() + "].\n", @@ -129,16 +135,18 @@ func doDiffDirs(firstURL, secondURL string, ch chan diff) { newFirstURL, err := urlJoinPath(firstURL, contentCh.Content.Name) if err != nil { ch <- diff{ - message: "Unable to construct new URL from ‘" + firstURL + "’ using ‘" + contentCh.Content.Name + "’. Reason: [" + iodine.ToError(err).Error() + "].\n", - err: iodine.New(err, nil), + message: "Unable to construct new URL from ‘" + firstURL + "’ using ‘" + + contentCh.Content.Name + "’. Reason: [" + iodine.ToError(err).Error() + "].\n", + err: iodine.New(err, nil), } return } newSecondURL, err := urlJoinPath(secondURL, contentCh.Content.Name) if err != nil { ch <- diff{ - message: "Unable to construct new URL from ‘" + secondURL + "’ using ‘" + contentCh.Content.Name + "’. Reason: [" + iodine.ToError(err).Error() + "].\n", - err: iodine.New(err, nil), + message: "Unable to construct new URL from ‘" + secondURL + "’ using ‘" + + contentCh.Content.Name + "’. Reason: [" + iodine.ToError(err).Error() + "].\n", + err: iodine.New(err, nil), } return } @@ -149,15 +157,14 @@ func doDiffDirs(firstURL, secondURL string, ch chan diff) { err: iodine.New(err, nil), } return - } _, newSecondContent, err := url2Stat(newSecondURL) if err != nil { ch <- diff{ - message: "‘" + filepath.Base(newFirstContent.Name) + "’ only in ‘" + firstURL + "’.\n", - err: nil, + message: "Failed to stat ‘" + newSecondURL + "’. Reason: [" + iodine.ToError(err).Error() + "].\n", + err: iodine.New(err, nil), } - continue + return } switch { case newFirstContent.Type.IsDir(): @@ -166,8 +173,8 @@ func doDiffDirs(firstURL, secondURL string, ch chan diff) { message: newFirstURL + " and " + newSecondURL + " differs in type.\n", err: nil, } - continue } + continue case newFirstContent.Type.IsRegular(): if !newSecondContent.Type.IsRegular() { ch <- diff{ diff --git a/ls.go b/ls.go index 6fcc32b1..c07a5a95 100644 --- a/ls.go +++ b/ls.go @@ -55,34 +55,21 @@ func printContent(date time.Time, v int64, name string, fileType os.FileMode) { } // doList - list all entities inside a folder -func doList(clnt client.Client, targetURL string) error { +func doList(clnt client.Client, targetURL string, recursive bool) error { var err error - for contentCh := range clnt.List() { + for contentCh := range clnt.List(recursive) { if contentCh.Err != nil { err = contentCh.Err break } - printContent(contentCh.Content.Time, contentCh.Content.Size, contentCh.Content.Name, contentCh.Content.Type) - } - if err != nil { - return iodine.New(err, map[string]string{"Target": targetURL}) - } - return nil -} - -// doListRecursive - list all entities inside folders and sub-folders recursively -func doListRecursive(clnt client.Client, targetURL string) error { - var err error - for contentCh := range clnt.ListRecursive() { - if contentCh.Err != nil { - err = contentCh.Err - break + contentName := contentCh.Content.Name + if recursive { + // this special handling is necessary since we are sending back absolute paths with in ListRecursive() + // a user would not wish to see a URL just for recursive and not for regular List() + // + // To be consistent we have to filter them out + contentName = strings.TrimPrefix(contentName, strings.TrimSuffix(targetURL, "/")+"/") } - // this special handling is necessary since we are sending back absolute paths with in ListRecursive() - // a user would not wish to see a URL just for recursive and not for regular List() - // - // To be consistent we have to filter them out - contentName := strings.TrimPrefix(contentCh.Content.Name, strings.TrimSuffix(targetURL, "/")+"/") printContent(contentCh.Content.Time, contentCh.Content.Size, contentName, contentCh.Content.Type) } if err != nil { diff --git a/pkg/client/client.go b/pkg/client/client.go index b8170147..4bb2d854 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -26,8 +26,7 @@ import ( type Client interface { // Common operations Stat() (content *Content, err error) - List() <-chan ContentOnChannel - ListRecursive() <-chan ContentOnChannel + List(recursive bool) <-chan ContentOnChannel // Bucket operations MakeBucket() error diff --git a/pkg/client/fs/fs.go b/pkg/client/fs/fs.go index 923543e1..ab2e8587 100644 --- a/pkg/client/fs/fs.go +++ b/pkg/client/fs/fs.go @@ -122,13 +122,18 @@ func (f *fsClient) GetObject(offset, length uint64) (io.ReadCloser, uint64, erro } // List - list files and folders -func (f *fsClient) List() <-chan client.ContentOnChannel { +func (f *fsClient) List(recursive bool) <-chan client.ContentOnChannel { contentCh := make(chan client.ContentOnChannel) - go f.list(contentCh) + switch recursive { + case true: + go f.listRecursiveInRoutine(contentCh) + default: + go f.listInRoutine(contentCh) + } return contentCh } -func (f *fsClient) list(contentCh chan client.ContentOnChannel) { +func (f *fsClient) listInRoutine(contentCh chan client.ContentOnChannel) { defer close(contentCh) fpath := f.path @@ -215,14 +220,7 @@ func (f *fsClient) list(contentCh chan client.ContentOnChannel) { } } -// ListRecursive - list files and folders recursively -func (f *fsClient) ListRecursive() <-chan client.ContentOnChannel { - contentCh := make(chan client.ContentOnChannel) - go f.listRecursive(contentCh) - return contentCh -} - -func (f *fsClient) listRecursive(contentCh chan client.ContentOnChannel) { +func (f *fsClient) listRecursiveInRoutine(contentCh chan client.ContentOnChannel) { defer close(contentCh) visitFS := func(fp string, fi os.FileInfo, err error) error { if err != nil { diff --git a/pkg/client/fs/fs_test.go b/pkg/client/fs/fs_test.go index 23b67eff..edcf7bd0 100644 --- a/pkg/client/fs/fs_test.go +++ b/pkg/client/fs/fs_test.go @@ -60,7 +60,7 @@ func (s *MySuite) TestList(c *C) { c.Assert(err, IsNil) var contents []*client.Content - for contentCh := range fsc.ListRecursive() { + for contentCh := range fsc.List(true) { contents = append(contents, contentCh.Content) } c.Assert(err, IsNil) diff --git a/pkg/client/s3/s3.go b/pkg/client/s3/s3.go index ac3d36a1..2021ebb2 100644 --- a/pkg/client/s3/s3.go +++ b/pkg/client/s3/s3.go @@ -152,7 +152,7 @@ func (c *s3Client) Stat() (*client.Content, error) { return nil, iodine.New(err, nil) } objectMetadata := new(client.Content) - objectMetadata.Name = c.hostURL.String() + objectMetadata.Name = metadata.Key objectMetadata.Time = metadata.LastModified objectMetadata.Size = metadata.Size objectMetadata.Type = os.FileMode(0664) @@ -187,21 +187,19 @@ func (c *s3Client) url2BucketAndObject() (bucketName, objectName string) { /// Bucket API operations -// List - list at delimited path not recursive -func (c *s3Client) List() <-chan client.ContentOnChannel { +// List - list at delimited path, if not recursive +func (c *s3Client) List(recursive bool) <-chan client.ContentOnChannel { contentCh := make(chan client.ContentOnChannel) - go c.listInGoRoutine(contentCh) + switch recursive { + case true: + go c.listRecursiveInRoutine(contentCh) + default: + go c.listInRoutine(contentCh) + } return contentCh } -// ListRecursive - list buckets and objects recursively -func (c *s3Client) ListRecursive() <-chan client.ContentOnChannel { - contentCh := make(chan client.ContentOnChannel) - go c.listRecursiveInGoRoutine(contentCh) - return contentCh -} - -func (c *s3Client) listInGoRoutine(contentCh chan client.ContentOnChannel) { +func (c *s3Client) listInRoutine(contentCh chan client.ContentOnChannel) { defer close(contentCh) bucket, object := c.url2BucketAndObject() switch { @@ -266,7 +264,7 @@ func (c *s3Client) listInGoRoutine(contentCh chan client.ContentOnChannel) { } } -func (c *s3Client) listRecursiveInGoRoutine(contentCh chan client.ContentOnChannel) { +func (c *s3Client) listRecursiveInRoutine(contentCh chan client.ContentOnChannel) { defer close(contentCh) bucket, object := c.url2BucketAndObject() for object := range c.api.ListObjects(bucket, object, true) {