From 4d136c66054830e7a8c0a0ff09c2d51bed853d17 Mon Sep 17 00:00:00 2001 From: "Anand Babu (AB) Periasamy" Date: Sat, 22 Aug 2015 16:48:55 -0700 Subject: [PATCH] diff error handling update --- diff-json.go | 47 ----------------------------------------------- diff-main.go | 13 +++++++------ diff.go | 30 ++++++++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 53 deletions(-) delete mode 100644 diff-json.go diff --git a/diff-json.go b/diff-json.go deleted file mode 100644 index eaa51054..00000000 --- a/diff-json.go +++ /dev/null @@ -1,47 +0,0 @@ -/* - * Minio Client (C) 2015 Minio, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package main - -import "encoding/json" - -// DiffMessage json container for diff messages -type DiffMessage struct { - FirstURL string `json:"first"` - SecondURL string `json:"second"` - Diff string `json:"diff"` -} - -func (d DiffMessage) String() string { - if !globalJSONFlag { - var message string - if d.Diff == "Only-in" { - message = "‘" + d.FirstURL + "’ Only in ‘" + d.SecondURL + "’\n" - } - if d.Diff == "Type" { - message = d.FirstURL + " and " + d.SecondURL + " differs in type.\n" - } - if d.Diff == "Size" { - message = d.FirstURL + " and " + d.SecondURL + " differs in size.\n" - } - return message - } - diffJSONBytes, err := json.Marshal(d) - if err != nil { - panic(err) - } - return string(diffJSONBytes) + "\n" -} diff --git a/diff-main.go b/diff-main.go index d9b89725..9fe3f376 100644 --- a/diff-main.go +++ b/diff-main.go @@ -17,9 +17,10 @@ package main import ( + "fmt" + "github.com/minio/mc/internal/github.com/minio/cli" "github.com/minio/mc/internal/github.com/minio/minio/pkg/probe" - "github.com/minio/mc/pkg/console" ) // Help message. @@ -63,19 +64,19 @@ func mainDiff(ctx *cli.Context) { var err *probe.Error firstURL, err = getCanonicalizedURL(firstURL, config.Aliases) - fatalIf(err.Trace(), "Unable to canonicalize first URL.") + fatalIf(err.Trace(firstURL), "Unable to canonicalize first URL.") secondURL, err = getCanonicalizedURL(secondURL, config.Aliases) - fatalIf(err.Trace(), "Unable to canonicalize second URL.") + fatalIf(err.Trace(secondURL), "Unable to canonicalize second URL.") if isURLRecursive(secondURL) { - console.Fatalf("Second URL cannot be recursive. %s\n", errInvalidArgument) + fatalIf(probe.NewError(errInvalidArgument), "Second argument ‘"+secondURL+"’ cannot be recursive.") } newFirstURL := stripRecursiveURL(firstURL) for diff := range doDiffCmd(newFirstURL, secondURL, isURLRecursive(firstURL)) { - fatalIf(diff.err.Trace(), diff.message) + fatalIf(diff.err.Trace(newFirstURL, secondURL), "Failed to diff ‘"+firstURL+"’ and ‘"+secondURL+"’.") + fmt.Println(diff.message) } - console.Println() } // doDiffCmd - Execute the diff command diff --git a/diff.go b/diff.go index c3dac124..a3b1327f 100644 --- a/diff.go +++ b/diff.go @@ -17,6 +17,8 @@ package main import ( + "encoding/json" + "errors" "path/filepath" "strings" "sync" @@ -43,6 +45,34 @@ import ( // 2. diff(d1..., d2...) -> INVALID // +// DiffMessage json container for diff messages +type DiffMessage struct { + FirstURL string `json:"first"` + SecondURL string `json:"second"` + Diff string `json:"diff"` +} + +func (d DiffMessage) String() string { + if !globalJSONFlag { + msg := "" + switch d.Diff { + case "Only-in": + msg = "‘" + d.FirstURL + "’ only in ‘" + d.SecondURL + "’." + case "Type": + msg = d.FirstURL + " and " + d.SecondURL + " differs in type." + case "Size": + msg = d.FirstURL + " and " + d.SecondURL + " differs in size." + default: + fatalIf(probe.NewError(errors.New("")), "Unhandled difference between ‘"+d.FirstURL+"’ and ‘"+d.SecondURL+"’.") + } + return msg + } + diffJSONBytes, err := json.Marshal(d) + fatalIf(probe.NewError(err), "Unable to marshal diff message ‘"+d.FirstURL+"’, ‘"+d.SecondURL+"’ and ‘"+d.Diff+"’.") + + return string(diffJSONBytes) +} + type diff struct { message string err *probe.Error