From b6a2e5c4af05aa0e8be2225deca5769bd7cf365e Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Tue, 4 Aug 2015 21:41:26 -0700 Subject: [PATCH 1/2] 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"` From 26eb25bb92e82347ee6931ac0b783d93c2474388 Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Tue, 4 Aug 2015 23:21:11 -0700 Subject: [PATCH 2/2] Fix console issues related to os.Stderr and terminal handling --- pkg/console/console.go | 121 +++++++++++++++++++---------------------- pkg/console/themes.go | 28 +++++----- 2 files changed, 69 insertions(+), 80 deletions(-) diff --git a/pkg/console/console.go b/pkg/console/console.go index 0023ebcc..2b2cc3a8 100644 --- a/pkg/console/console.go +++ b/pkg/console/console.go @@ -59,7 +59,6 @@ type Theme struct { var ( mutex = &sync.RWMutex{} - stdoutColoredOutput = ansicolor.NewAnsiColorWriter(os.Stdout) stderrColoredOutput = ansicolor.NewAnsiColorWriter(os.Stderr) // themesDB contains supported list of Themes @@ -70,14 +69,7 @@ var ( } // currTheme is current theme - currThemeName = func() string { - theme := GetDefaultThemeName() - // if not a TTY disable color - if !isatty.IsTerminal(os.Stdout.Fd()) || !isatty.IsTerminal(os.Stderr.Fd()) { - theme = "nocolor" - } - return theme - }() + currThemeName = GetDefaultThemeName() // Bar print progress bar Bar = func(data ...interface{}) { @@ -236,118 +228,115 @@ var ( // wrap around standard fmt functions // print prints a message prefixed with message type and program name print = func(c *color.Color, a ...interface{}) { + mutex.Lock() + defer mutex.Unlock() + switch c { case themesDB[currThemeName].Debug: - mutex.Lock() output := color.Output color.Output = stderrColoredOutput - c.Print(ProgramName() + ": ") - c.Print(a...) + if isatty.IsTerminal(os.Stderr.Fd()) { + c.Print(ProgramName() + ": ") + c.Print(a...) + } else { + fmt.Fprint(color.Output, ProgramName()+": ") + fmt.Fprint(color.Output, a...) + } color.Output = output - mutex.Unlock() case themesDB[currThemeName].Fatal: - mutex.Lock() - output := color.Output - color.Output = stderrColoredOutput - c.Print(ProgramName() + ": ") - c.Print(a...) - color.Output = output - mutex.Unlock() + fallthrough case themesDB[currThemeName].Error: - mutex.Lock() output := color.Output color.Output = stderrColoredOutput - c.Print(ProgramName() + ": ") - c.Print(a...) + if isatty.IsTerminal(os.Stderr.Fd()) { + c.Print(ProgramName() + ": ") + c.Print(a...) + } else { + fmt.Fprint(color.Output, ProgramName()+": ") + fmt.Fprint(color.Output, a...) + } color.Output = output - mutex.Unlock() case themesDB[currThemeName].Info: - mutex.Lock() c.Print(ProgramName() + ": ") c.Print(a...) - mutex.Unlock() default: - mutex.Lock() c.Print(a...) - mutex.Unlock() } } // printf - same as print with a new line printf = func(c *color.Color, f string, a ...interface{}) { + mutex.Lock() + defer mutex.Unlock() + switch c { case themesDB[currThemeName].Debug: - mutex.Lock() output := color.Output color.Output = stderrColoredOutput - c.Print(ProgramName() + ": ") - c.Printf(f, a...) + if isatty.IsTerminal(os.Stderr.Fd()) { + c.Print(ProgramName() + ": ") + c.Printf(f, a...) + } else { + fmt.Fprint(color.Output, ProgramName()+": ") + fmt.Fprintf(color.Output, f, a...) + } color.Output = output - mutex.Unlock() case themesDB[currThemeName].Fatal: - mutex.Lock() - output := color.Output - color.Output = stderrColoredOutput - c.Print(ProgramName() + ": ") - c.Printf(f, a...) - color.Output = output - mutex.Unlock() + fallthrough case themesDB[currThemeName].Error: - mutex.Lock() output := color.Output color.Output = stderrColoredOutput - c.Print(ProgramName() + ": ") - c.Printf(f, a...) + if isatty.IsTerminal(os.Stderr.Fd()) { + c.Print(ProgramName() + ": ") + c.Printf(f, a...) + } else { + fmt.Fprint(color.Output, ProgramName()+": ") + fmt.Fprintf(color.Output, f, a...) + } color.Output = output - mutex.Unlock() case themesDB[currThemeName].Info: - mutex.Lock() c.Print(ProgramName() + ": ") c.Printf(f, a...) - mutex.Unlock() default: - mutex.Lock() c.Printf(f, a...) - mutex.Unlock() } } // println - same as print with a new line println = func(c *color.Color, a ...interface{}) { + mutex.Lock() + defer mutex.Unlock() + switch c { case themesDB[currThemeName].Debug: - mutex.Lock() output := color.Output color.Output = stderrColoredOutput - c.Print(ProgramName() + ": ") - c.Println(a...) + if isatty.IsTerminal(os.Stderr.Fd()) { + c.Print(ProgramName() + ": ") + c.Println(a...) + } else { + fmt.Fprint(color.Output, ProgramName()+": ") + fmt.Fprintln(color.Output, a...) + } color.Output = output - mutex.Unlock() case themesDB[currThemeName].Fatal: - mutex.Lock() - output := color.Output - color.Output = stderrColoredOutput - c.Print(ProgramName() + ": ") - c.Println(a...) - color.Output = output - mutex.Unlock() + fallthrough case themesDB[currThemeName].Error: - mutex.Lock() output := color.Output color.Output = stderrColoredOutput - c.Print(ProgramName() + ": ") - c.Println(a...) + if isatty.IsTerminal(os.Stderr.Fd()) { + c.Print(ProgramName() + ": ") + c.Println(a...) + } else { + fmt.Fprint(color.Output, ProgramName()+": ") + fmt.Fprintln(color.Output, a...) + } color.Output = output - mutex.Unlock() case themesDB[currThemeName].Info: - mutex.Lock() c.Print(ProgramName() + ": ") c.Println(a...) - mutex.Unlock() default: - mutex.Lock() c.Println(a...) - mutex.Unlock() } } ) diff --git a/pkg/console/themes.go b/pkg/console/themes.go index 7b22b7e7..f2ff515f 100644 --- a/pkg/console/themes.go +++ b/pkg/console/themes.go @@ -56,18 +56,18 @@ var WhiteTheme = Theme{ // NoColorTheme - Disables color theme var NoColorTheme = Theme{ - Debug: (color.New()), - Fatal: (color.New()), - Error: (color.New()), - Info: (color.New()), - File: (color.New()), - Dir: (color.New()), - Command: (color.New()), - SessionID: (color.New()), - Size: (color.New()), - Time: (color.New()), - JSON: (color.New()), - Bar: (color.New()), - PrintC: (color.New()), - Print: (color.New()), + Debug: new(color.Color), + Fatal: new(color.Color), + Error: new(color.Color), + Info: new(color.Color), + File: new(color.Color), + Dir: new(color.Color), + Command: new(color.Color), + SessionID: new(color.Color), + Size: new(color.Color), + Time: new(color.Color), + JSON: new(color.Color), + Bar: new(color.Color), + PrintC: new(color.Color), + Print: new(color.Color), }