From 61ec25d2bfcf8713957c62e048c80220944d2ce0 Mon Sep 17 00:00:00 2001 From: Krishna Srinivas Date: Tue, 17 Nov 2015 21:32:35 -0800 Subject: [PATCH] diff - continue on non-fatal error. refactor to remove unneeded goroutine --- diff-main.go | 108 ++++++++++++++++++++++----------------------------- diff_test.go | 10 +++-- 2 files changed, 54 insertions(+), 64 deletions(-) diff --git a/diff-main.go b/diff-main.go index d795c991..68a01597 100644 --- a/diff-main.go +++ b/diff-main.go @@ -113,56 +113,56 @@ func (d diffMessage) JSON() string { } // doDiffMain runs the diff. -func doDiffMain(firstURL, secondURL string) <-chan diffMessage { - ch := make(chan diffMessage, 10000) - go func() { - defer close(ch) - // source and targets are always directories - sourceSeparator := string(client.NewURL(firstURL).Separator) - if !strings.HasSuffix(firstURL, sourceSeparator) { - firstURL = firstURL + sourceSeparator - } - targetSeparator := string(client.NewURL(secondURL).Separator) - if !strings.HasSuffix(secondURL, targetSeparator) { - secondURL = secondURL + targetSeparator - } +func doDiffMain(firstURL, secondURL string) { + // source and targets are always directories + sourceSeparator := string(client.NewURL(firstURL).Separator) + if !strings.HasSuffix(firstURL, sourceSeparator) { + firstURL = firstURL + sourceSeparator + } + targetSeparator := string(client.NewURL(secondURL).Separator) + if !strings.HasSuffix(secondURL, targetSeparator) { + secondURL = secondURL + targetSeparator + } - firstClient, err := url2Client(firstURL) + firstClient, err := url2Client(firstURL) + if err != nil { + fatalIf(err.Trace(firstURL, secondURL), fmt.Sprintf("Failed to diff '%s' and '%s'", firstURL, secondURL)) + } + difference, err := objectDifferenceFactory(secondURL) + if err != nil { + fatalIf(err.Trace(secondURL), fmt.Sprintf("Failed to diff '%s' and '%s'", firstURL, secondURL)) + } + isRecursive := true + isIncomplete := false + for sourceContent := range firstClient.List(isRecursive, isIncomplete) { + if sourceContent.Err != nil { + switch sourceContent.Err.ToGoError().(type) { + // handle this specifically for filesystem related errors. + case client.BrokenSymlink, client.TooManyLevelsSymlink, client.PathNotFound, client.PathInsufficientPermission: + errorIf(sourceContent.Err.Trace(firstURL, secondURL), fmt.Sprintf("Failed on '%s'", firstURL)) + default: + fatalIf(sourceContent.Err.Trace(firstURL, secondURL), fmt.Sprintf("Failed on '%s'", firstURL)) + } + continue + } + if sourceContent.Type.IsDir() { + continue + } + suffix := strings.TrimPrefix(sourceContent.URL.String(), firstURL) + differ, err := difference(suffix, sourceContent.Type, sourceContent.Size) if err != nil { - ch <- diffMessage{Error: err.Trace(firstURL)} - return + errorIf(sourceContent.Err.Trace(), fmt.Sprintf("Failed on '%s'", urlJoinPath(secondURL, suffix))) + continue } - difference, err := objectDifferenceFactory(secondURL) - if err != nil { - ch <- diffMessage{Error: err.Trace(secondURL)} - return + if differ == differNone { + continue } - isRecursive := true - for sourceContent := range firstClient.List(isRecursive, false) { - if sourceContent.Err != nil { - ch <- diffMessage{Error: sourceContent.Err.Trace()} - continue - } - if sourceContent.Type.IsDir() { - continue - } - suffix := strings.TrimPrefix(sourceContent.URL.String(), firstURL) - differ, err := difference(suffix, sourceContent.Type, sourceContent.Size) - if err != nil { - ch <- diffMessage{Error: err.Trace()} - continue - } - if differ == differNone { - continue - } - ch <- diffMessage{ - FirstURL: sourceContent.URL.String(), - SecondURL: urlJoinPath(secondURL, suffix), - Diff: differ, - } - } - }() - return ch + printMsg(diffMessage{ + FirstURL: sourceContent.URL.String(), + SecondURL: urlJoinPath(secondURL, suffix), + Diff: differ, + }) + } } // mainDiff main for 'diff'. @@ -196,19 +196,5 @@ func mainDiff(ctx *cli.Context) { if !secondContent.Type.IsDir() { fatalIf(errInvalidArgument().Trace(), fmt.Sprintf("‘%s’ is not a folder.", secondURL)) } - - for diff := range doDiffMain(firstURL, secondURL) { - if diff.Error != nil { - // Print in new line and adjust to top so that we don't print over the ongoing scan bar - if !globalQuietFlag && !globalJSONFlag { - console.Eraseline() - } - } - fatalIf(diff.Error.Trace(firstURL, secondURL), "Failed to diff ‘"+firstURL+"’ and ‘"+secondURL+"’.") - printMsg(diff) - } - // Print in new line and adjust to top so that we don't print over the ongoing scan bar - if !globalQuietFlag && !globalJSONFlag { - console.Eraseline() - } + doDiffMain(firstURL, secondURL) } diff --git a/diff_test.go b/diff_test.go index 0f13f56e..55a9a7ff 100644 --- a/diff_test.go +++ b/diff_test.go @@ -67,9 +67,13 @@ func (s *TestSuite) TestDiffDirs(c *C) { c.Assert(err, IsNil) } - for diff := range doDiffMain(root1, root2) { - c.Assert(diff.Error, IsNil) - } + e = app.Run([]string{os.Args[0], "diff", root1, root2}) + c.Assert(e, IsNil) + c.Assert(console.IsError, Equals, false) + c.Assert(console.IsExited, Equals, false) + + // reset back + console.IsExited = false } func (s *TestSuite) TestDiffContext(c *C) {