diff --git a/diff-main.go b/diff-main.go index d679ce8d..0a768ee5 100644 --- a/diff-main.go +++ b/diff-main.go @@ -66,17 +66,6 @@ EXAMPLES: `, } -func checkDiffSyntax(ctx *cli.Context) { - if len(ctx.Args()) != 2 { - cli.ShowCommandHelpAndExit(ctx, "diff", 1) // last argument is exit code - } - for _, arg := range ctx.Args() { - if strings.TrimSpace(arg) == "" { - fatalIf(errInvalidArgument().Trace(ctx.Args()...), "Unable to validate empty argument.") - } - } -} - // diffMessage json container for diff messages type diffMessage struct { Status string `json:"status"` @@ -116,9 +105,45 @@ func (d diffMessage) JSON() string { return string(diffJSONBytes) } +func checkDiffSyntax(ctx *cli.Context) { + if len(ctx.Args()) != 2 { + cli.ShowCommandHelpAndExit(ctx, "diff", 1) // last argument is exit code + } + for _, arg := range ctx.Args() { + if strings.TrimSpace(arg) == "" { + fatalIf(errInvalidArgument().Trace(ctx.Args()...), "Unable to validate empty argument.") + } + } + URLs := ctx.Args() + firstURL := URLs[0] + secondURL := URLs[1] + + // Diff only works between two directories, verify them below. + + // Verify if firstURL is accessible. + _, firstContent, err := url2Stat(firstURL) + if err != nil { + fatalIf(err.Trace(firstURL), fmt.Sprintf("Unable to stat '%s'.", firstURL)) + } + // Verify if its a directory. + if !firstContent.Type.IsDir() { + fatalIf(errInvalidArgument().Trace(firstURL), fmt.Sprintf("‘%s’ is not a folder.", firstURL)) + } + + // Verify if secondURL is accessible. + _, secondContent, err := url2Stat(secondURL) + if err != nil { + fatalIf(err.Trace(secondURL), fmt.Sprintf("Unable to stat '%s'.", secondURL)) + } + // Verify if its a directory. + if !secondContent.Type.IsDir() { + fatalIf(errInvalidArgument().Trace(secondURL), fmt.Sprintf("‘%s’ is not a folder.", secondURL)) + } +} + // doDiffMain runs the diff. func doDiffMain(firstURL, secondURL string) { - // source and targets are always directories + // Source and targets are always directories sourceSeparator := string(client.NewURL(firstURL).Separator) if !strings.HasSuffix(firstURL, sourceSeparator) { firstURL = firstURL + sourceSeparator @@ -128,20 +153,26 @@ func doDiffMain(firstURL, secondURL string) { secondURL = secondURL + targetSeparator } - firstClient, err := newClient(firstURL) + // Expand aliased urls. + firstAlias, firstURL, _ := mustExpandAlias(firstURL) + secondAlias, secondURL, _ := mustExpandAlias(secondURL) + + firstClient, err := newClientFromAlias(firstAlias, firstURL) if err != nil { - fatalIf(err.Trace(firstURL, secondURL), fmt.Sprintf("Failed to diff '%s' and '%s'", firstURL, secondURL)) + fatalIf(err.Trace(firstAlias, firstURL, secondAlias, secondURL), + fmt.Sprintf("Failed to diff '%s' and '%s'", firstURL, secondURL)) } - difference, err := objectDifferenceFactory(secondURL) + difference, err := objectDifferenceFactory(secondAlias, secondURL) if err != nil { - fatalIf(err.Trace(firstURL, secondURL), fmt.Sprintf("Failed to diff '%s' and '%s'", firstURL, secondURL)) + fatalIf(err.Trace(firstAlias, firstURL, secondAlias, 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. + // 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: @@ -188,19 +219,5 @@ func mainDiff(ctx *cli.Context) { firstURL := URLs[0] secondURL := URLs[1] - _, firstContent, err := url2Stat(firstURL) - if err != nil { - fatalIf(err.Trace(firstURL), fmt.Sprintf("Unable to stat '%s'.", firstURL)) - } - if !firstContent.Type.IsDir() { - fatalIf(errInvalidArgument().Trace(firstURL), fmt.Sprintf("‘%s’ is not a folder.", firstURL)) - } - _, secondContent, err := url2Stat(secondURL) - if err != nil { - fatalIf(err.Trace(secondURL), fmt.Sprintf("Unable to stat '%s'.", secondURL)) - } - if !secondContent.Type.IsDir() { - fatalIf(errInvalidArgument().Trace(secondURL), fmt.Sprintf("‘%s’ is not a folder.", secondURL)) - } doDiffMain(firstURL, secondURL) } diff --git a/difference.go b/difference.go index 5159c4f7..ad3b9537 100644 --- a/difference.go +++ b/difference.go @@ -38,10 +38,10 @@ const ( // objectDifferenceFactory returns objectDifference function to check for difference // between sourceURL and targetURL // for usage reference check diff and mirror commands -func objectDifferenceFactory(targetURL string) (objectDifference, *probe.Error) { - clnt, err := newClient(targetURL) +func objectDifferenceFactory(targetAlias, targetURL string) (objectDifference, *probe.Error) { + clnt, err := newClientFromAlias(targetAlias, targetURL) if err != nil { - return nil, err.Trace(targetURL) + return nil, err.Trace(targetAlias, targetURL) } isIncomplete := false isRecursive := true diff --git a/mirror-url.go b/mirror-url.go index 35cf8b20..07e21743 100644 --- a/mirror-url.go +++ b/mirror-url.go @@ -92,13 +92,6 @@ func checkMirrorSyntax(ctx *cli.Context) { } func deltaSourceTargets(sourceURL string, targetURL string, isForce bool, mirrorURLsCh chan<- mirrorURLs) { - // Extract alias before fiddling with the URL. - sourceAlias, _, _ := mustExpandAlias(sourceURL) - // Find alias and expanded URL - targetAlias, targetURL, _ := mustExpandAlias(targetURL) - - defer close(mirrorURLsCh) - // source and targets are always directories sourceSeparator := string(client.NewURL(sourceURL).Separator) if !strings.HasSuffix(sourceURL, sourceSeparator) { @@ -109,21 +102,29 @@ func deltaSourceTargets(sourceURL string, targetURL string, isForce bool, mirror targetURL = targetURL + targetSeparator } - objectDifferenceTarget, err := objectDifferenceFactory(targetURL) + // Extract alias and expanded URL + sourceAlias, sourceURL, _ := mustExpandAlias(sourceURL) + targetAlias, targetURL, _ := mustExpandAlias(targetURL) + + defer close(mirrorURLsCh) + + objectDifferenceTarget, err := objectDifferenceFactory(targetAlias, targetURL) if err != nil { - mirrorURLsCh <- mirrorURLs{Error: err.Trace(targetURL)} + mirrorURLsCh <- mirrorURLs{Error: err.Trace(targetAlias, targetURL)} return } - sourceClient, err := newClient(sourceURL) + sourceClient, err := newClientFromAlias(sourceAlias, sourceURL) if err != nil { - mirrorURLsCh <- mirrorURLs{Error: err.Trace(sourceURL)} + mirrorURLsCh <- mirrorURLs{Error: err.Trace(sourceAlias, sourceURL)} return } for sourceContent := range sourceClient.List(true, false) { if sourceContent.Err != nil { - mirrorURLsCh <- mirrorURLs{Error: sourceContent.Err.Trace(sourceClient.GetURL().String())} + mirrorURLsCh <- mirrorURLs{ + Error: sourceContent.Err.Trace(sourceClient.GetURL().String()), + } continue } if sourceContent.Type.IsDir() {