From b6a2e5c4af05aa0e8be2225deca5769bd7cf365e Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Tue, 4 Aug 2015 21:41:26 -0700 Subject: [PATCH] Add diff json --- diff-main.go | 6 ++-- diff.go | 84 +++++++++++++++++++++++++++++++++++------------- print-structs.go | 34 ++++++++++++++++++++ 3 files changed, 98 insertions(+), 26 deletions(-) diff --git a/diff-main.go b/diff-main.go index a0d95169..281a2860 100644 --- a/diff-main.go +++ b/diff-main.go @@ -71,10 +71,8 @@ func runDiffCmd(ctx *cli.Context) { } newFirstURL := stripRecursiveURL(firstURL) for diff := range doDiffCmd(newFirstURL, secondURL, isURLRecursive(firstURL)) { - if diff.err != nil { - console.Fatalln(diff.message) - } - console.Infoln(diff.message) + Fatal(diff.err) + console.PrintC(diff.message) } console.Println() } diff --git a/diff.go b/diff.go index 2e241697..d15ecebb 100644 --- a/diff.go +++ b/diff.go @@ -43,6 +43,12 @@ import ( // 2. diff(d1..., d2...) -> INVALID // +type diffV1 struct { + firstURL string + secondURL string + diffType string +} + type diff struct { message string err error @@ -99,8 +105,12 @@ func doDiffInRoutine(firstURL, secondURL string, recursive bool, ch chan diff) { doDiffObjects(firstURL, newSecondURL, ch) case !secondContent.Type.IsRegular(): ch <- diff{ - message: "‘" + firstURL + "’ and " + "‘" + secondURL + "’ differs in type.", - err: nil, + message: diffV1{ + firstURL: firstURL, + secondURL: secondURL, + diffType: "Type", + }.String(), + err: nil, } return case secondContent.Type.IsRegular(): @@ -111,8 +121,12 @@ func doDiffInRoutine(firstURL, secondURL string, recursive bool, ch chan diff) { switch { case !secondContent.Type.IsDir(): ch <- diff{ - message: "‘" + firstURL + "’ and " + "‘" + secondURL + "’ differs in type.", - err: nil, + message: diffV1{ + firstURL: firstURL, + secondURL: secondURL, + diffType: "Type", + }.String(), + err: nil, } return default: @@ -129,14 +143,12 @@ func doDiffObjects(firstURL, secondURL string, ch chan diff) { switch { case errFirst != nil && errSecond == nil: ch <- diff{ - message: "Only in ‘" + secondURL + "’", - err: nil, + err: errFirst.Trace(), } return case errFirst == nil && errSecond != nil: ch <- diff{ - message: "Only in ‘" + firstURL + "’", - err: nil, + err: errSecond.Trace(), } return } @@ -147,8 +159,12 @@ func doDiffObjects(firstURL, secondURL string, ch chan diff) { case firstContent.Type.IsRegular(): if !secondContent.Type.IsRegular() { ch <- diff{ - message: firstURL + " and " + secondURL + " differs in type.", - err: nil, + message: diffV1{ + firstURL: firstURL, + secondURL: secondURL, + diffType: "Type", + }.String(), + err: nil, } } default: @@ -161,8 +177,12 @@ func doDiffObjects(firstURL, secondURL string, ch chan diff) { if firstContent.Size != secondContent.Size { ch <- diff{ - message: firstURL + " and " + secondURL + " differs in size.", - err: nil, + message: diffV1{ + firstURL: firstURL, + secondURL: secondURL, + diffType: "Size", + }.String(), + err: nil, } } } @@ -197,14 +217,22 @@ func dodiff(firstClnt, secondClnt client.Client, ch chan diff) { switch { case errFirst != nil && errSecond == nil: ch <- diff{ - message: "‘" + newSecondURL + "’ Only in ‘" + secondClnt.URL().String() + "’", - err: nil, + message: diffV1{ + firstURL: newSecondURL, + secondURL: secondClnt.URL().String(), + diffType: "Only-in", + }.String(), + err: nil, } continue case errFirst == nil && errSecond != nil: ch <- diff{ - message: "‘" + newFirstURL + "’ Only in ‘" + firstClnt.URL().String() + "’", - err: nil, + message: diffV1{ + firstURL: newFirstURL, + secondURL: firstClnt.URL().String(), + diffType: "Only-in", + }.String(), + err: nil, } continue case errFirst == nil && errSecond == nil: @@ -212,16 +240,24 @@ func dodiff(firstClnt, secondClnt client.Client, ch chan diff) { case newFirstContent.Type.IsDir(): if !newSecondContent.Type.IsDir() { ch <- diff{ - message: newFirstURL + " and " + newSecondURL + " differs in type.", - err: nil, + message: diffV1{ + firstURL: newFirstURL, + secondURL: newSecondURL, + diffType: "Type", + }.String(), + err: nil, } } continue case newFirstContent.Type.IsRegular(): if !newSecondContent.Type.IsRegular() { ch <- diff{ - message: newFirstURL + " and " + newSecondURL + " differs in type.", - err: nil, + message: diffV1{ + firstURL: newFirstURL, + secondURL: newSecondURL, + diffType: "Type", + }.String(), + err: nil, } continue } @@ -296,8 +332,12 @@ func dodiffRecursive(firstClnt, secondClnt client.Client, ch chan diff) { firstURLDelimited := firstClnt.URL().String()[:strings.LastIndex(firstClnt.URL().String(), string(firstClnt.URL().Separator))+1] firstURL := firstURLDelimited + matchName ch <- diff{ - message: "‘" + firstURL + "’ Only in ‘" + firstClnt.URL().String() + "’", - err: nil, + message: diffV1{ + firstURL: firstURL, + secondURL: firstClnt.URL().String(), + diffType: "Only-in", + }.String(), + err: nil, } } } diff --git a/print-structs.go b/print-structs.go index d778bff5..16f3b35d 100644 --- a/print-structs.go +++ b/print-structs.go @@ -25,6 +25,40 @@ import ( "github.com/minio/mc/pkg/console" ) +// DiffJSONMessage json container for diff messages +type DiffJSONMessage struct { + Version string `json:"version"` + FirstURL string `json:"first"` + SecondURL string `json:"second"` + Diff string `json:"diff"` +} + +func (s diffV1) String() string { + if !globalJSONFlag { + var message string + if s.diffType == "Only-in" { + message = "‘" + s.firstURL + "’ Only in ‘" + s.secondURL + "’\n" + } + if s.diffType == "Type" { + message = s.firstURL + " and " + s.secondURL + " differs in type.\n" + } + if s.diffType == "Size" { + message = s.firstURL + " and " + s.secondURL + " differs in size.\n" + } + return message + } + diffMessage := DiffJSONMessage{} + diffMessage.Version = "1.0.0" + diffMessage.FirstURL = s.firstURL + diffMessage.SecondURL = s.secondURL + diffMessage.Diff = s.diffType + diffJSONBytes, err := json.MarshalIndent(diffMessage, "", "\t") + if err != nil { + panic(err) + } + return console.JSON(string(diffJSONBytes) + "\n") +} + // SessionJSONMessage json container for session messages type SessionJSONMessage struct { Version string `json:"version"`