From ae0f3b14745bb59418cdf8fde6249829d477f123 Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Sat, 22 Aug 2015 02:52:03 -0700 Subject: [PATCH] Use DiffMessage rather than diffV1 --- diff-json.go | 24 +++++++-------- diff.go | 78 +++++++++++++++++++++++-------------------------- typed-errors.go | 2 +- 3 files changed, 47 insertions(+), 57 deletions(-) diff --git a/diff-json.go b/diff-json.go index 7689f41e..eaa51054 100644 --- a/diff-json.go +++ b/diff-json.go @@ -18,32 +18,28 @@ package main import "encoding/json" -// DiffJSONMessage json container for diff messages -type DiffJSONMessage struct { +// DiffMessage json container for diff messages +type DiffMessage struct { FirstURL string `json:"first"` SecondURL string `json:"second"` Diff string `json:"diff"` } -func (s diffV1) String() string { +func (d DiffMessage) String() string { if !globalJSONFlag { var message string - if s.diffType == "Only-in" { - message = "‘" + s.firstURL + "’ Only in ‘" + s.secondURL + "’\n" + if d.Diff == "Only-in" { + message = "‘" + d.FirstURL + "’ Only in ‘" + d.SecondURL + "’\n" } - if s.diffType == "Type" { - message = s.firstURL + " and " + s.secondURL + " differs in type.\n" + if d.Diff == "Type" { + message = d.FirstURL + " and " + d.SecondURL + " differs in type.\n" } - if s.diffType == "Size" { - message = s.firstURL + " and " + s.secondURL + " differs in size.\n" + if d.Diff == "Size" { + message = d.FirstURL + " and " + d.SecondURL + " differs in size.\n" } return message } - diffMessage := DiffJSONMessage{} - diffMessage.FirstURL = s.firstURL - diffMessage.SecondURL = s.secondURL - diffMessage.Diff = s.diffType - diffJSONBytes, err := json.Marshal(diffMessage) + diffJSONBytes, err := json.Marshal(d) if err != nil { panic(err) } diff --git a/diff.go b/diff.go index afc65b4d..c3dac124 100644 --- a/diff.go +++ b/diff.go @@ -43,12 +43,6 @@ import ( // 2. diff(d1..., d2...) -> INVALID // -type diffV1 struct { - firstURL string - secondURL string - diffType string -} - type diff struct { message string err *probe.Error @@ -100,10 +94,10 @@ func doDiffInRoutine(firstURL, secondURL string, recursive bool, ch chan diff) { doDiffObjects(firstURL, newSecondURL, ch) case !secondContent.Type.IsRegular(): ch <- diff{ - message: diffV1{ - firstURL: firstURL, - secondURL: secondURL, - diffType: "Type", + message: DiffMessage{ + FirstURL: firstURL, + SecondURL: secondURL, + Diff: "Type", }.String(), err: nil, } @@ -116,10 +110,10 @@ func doDiffInRoutine(firstURL, secondURL string, recursive bool, ch chan diff) { switch { case !secondContent.Type.IsDir(): ch <- diff{ - message: diffV1{ - firstURL: firstURL, - secondURL: secondURL, - diffType: "Type", + message: DiffMessage{ + FirstURL: firstURL, + SecondURL: secondURL, + Diff: "Type", }.String(), err: nil, } @@ -154,10 +148,10 @@ func doDiffObjects(firstURL, secondURL string, ch chan diff) { case firstContent.Type.IsRegular(): if !secondContent.Type.IsRegular() { ch <- diff{ - message: diffV1{ - firstURL: firstURL, - secondURL: secondURL, - diffType: "Type", + message: DiffMessage{ + FirstURL: firstURL, + SecondURL: secondURL, + Diff: "Type", }.String(), err: nil, } @@ -172,10 +166,10 @@ func doDiffObjects(firstURL, secondURL string, ch chan diff) { if firstContent.Size != secondContent.Size { ch <- diff{ - message: diffV1{ - firstURL: firstURL, - secondURL: secondURL, - diffType: "Size", + message: DiffMessage{ + FirstURL: firstURL, + SecondURL: secondURL, + Diff: "Size", }.String(), err: nil, } @@ -212,20 +206,20 @@ func dodiff(firstClnt, secondClnt client.Client, ch chan diff) { switch { case errFirst != nil && errSecond == nil: ch <- diff{ - message: diffV1{ - firstURL: newSecondURL, - secondURL: secondClnt.URL().String(), - diffType: "Only-in", + message: DiffMessage{ + FirstURL: newSecondURL, + SecondURL: secondClnt.URL().String(), + Diff: "Only-in", }.String(), err: nil, } continue case errFirst == nil && errSecond != nil: ch <- diff{ - message: diffV1{ - firstURL: newFirstURL, - secondURL: firstClnt.URL().String(), - diffType: "Only-in", + message: DiffMessage{ + FirstURL: newFirstURL, + SecondURL: firstClnt.URL().String(), + Diff: "Only-in", }.String(), err: nil, } @@ -235,10 +229,10 @@ func dodiff(firstClnt, secondClnt client.Client, ch chan diff) { case newFirstContent.Type.IsDir(): if !newSecondContent.Type.IsDir() { ch <- diff{ - message: diffV1{ - firstURL: newFirstURL, - secondURL: newSecondURL, - diffType: "Type", + message: DiffMessage{ + FirstURL: newFirstURL, + SecondURL: newSecondURL, + Diff: "Type", }.String(), err: nil, } @@ -247,10 +241,10 @@ func dodiff(firstClnt, secondClnt client.Client, ch chan diff) { case newFirstContent.Type.IsRegular(): if !newSecondContent.Type.IsRegular() { ch <- diff{ - message: diffV1{ - firstURL: newFirstURL, - secondURL: newSecondURL, - diffType: "Type", + message: DiffMessage{ + FirstURL: newFirstURL, + SecondURL: newSecondURL, + Diff: "Type", }.String(), err: nil, } @@ -327,10 +321,10 @@ 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: diffV1{ - firstURL: firstURL, - secondURL: firstClnt.URL().String(), - diffType: "Only-in", + message: DiffMessage{ + FirstURL: firstURL, + SecondURL: firstClnt.URL().String(), + Diff: "Only-in", }.String(), err: nil, } diff --git a/typed-errors.go b/typed-errors.go index 8c7dcf49..2b7abeb5 100644 --- a/typed-errors.go +++ b/typed-errors.go @@ -85,7 +85,7 @@ func (e errSourceNotRecursive) Error() string { return "Source ‘" + e.URL + "’ is not recursive." } -type errSourceIsNotDir errTargetIsNotDir +type errSourceIsNotDir errInvalidURL func (e errSourceIsNotDir) Error() string { return "Source ‘" + e.URL + "’ is not a folder."