Merge pull request #970 from harshavardhana/pr_out_fix_console_issues_related_to_os_stderr_and_terminal_handling

Fix console issues related to os.Stderr and terminal handling
This commit is contained in:
Harshavardhana
2015-08-04 23:22:32 -07:00
5 changed files with 167 additions and 106 deletions
+2 -4
View File
@@ -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()
}
+62 -22
View File
@@ -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,
}
}
}
+55 -66
View File
@@ -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() + ": <DEBUG> ")
c.Print(a...)
if isatty.IsTerminal(os.Stderr.Fd()) {
c.Print(ProgramName() + ": <DEBUG> ")
c.Print(a...)
} else {
fmt.Fprint(color.Output, ProgramName()+": <DEBUG> ")
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() + ": <ERROR> ")
c.Print(a...)
color.Output = output
mutex.Unlock()
fallthrough
case themesDB[currThemeName].Error:
mutex.Lock()
output := color.Output
color.Output = stderrColoredOutput
c.Print(ProgramName() + ": <ERROR> ")
c.Print(a...)
if isatty.IsTerminal(os.Stderr.Fd()) {
c.Print(ProgramName() + ": <ERROR> ")
c.Print(a...)
} else {
fmt.Fprint(color.Output, ProgramName()+": <ERROR> ")
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() + ": <DEBUG> ")
c.Printf(f, a...)
if isatty.IsTerminal(os.Stderr.Fd()) {
c.Print(ProgramName() + ": <DEBUG> ")
c.Printf(f, a...)
} else {
fmt.Fprint(color.Output, ProgramName()+": <DEBUG> ")
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() + ": <ERROR> ")
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() + ": <ERROR> ")
c.Printf(f, a...)
if isatty.IsTerminal(os.Stderr.Fd()) {
c.Print(ProgramName() + ": <ERROR> ")
c.Printf(f, a...)
} else {
fmt.Fprint(color.Output, ProgramName()+": <ERROR> ")
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() + ": <DEBUG> ")
c.Println(a...)
if isatty.IsTerminal(os.Stderr.Fd()) {
c.Print(ProgramName() + ": <DEBUG> ")
c.Println(a...)
} else {
fmt.Fprint(color.Output, ProgramName()+": <DEBUG> ")
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() + ": <ERROR> ")
c.Println(a...)
color.Output = output
mutex.Unlock()
fallthrough
case themesDB[currThemeName].Error:
mutex.Lock()
output := color.Output
color.Output = stderrColoredOutput
c.Print(ProgramName() + ": <ERROR> ")
c.Println(a...)
if isatty.IsTerminal(os.Stderr.Fd()) {
c.Print(ProgramName() + ": <ERROR> ")
c.Println(a...)
} else {
fmt.Fprint(color.Output, ProgramName()+": <ERROR> ")
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()
}
}
)
+14 -14
View File
@@ -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),
}
+34
View File
@@ -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"`