Skip to content

Commit

Permalink
Merge branch 'disable-quotes' of https://github.com/thlacroix/logrus
Browse files Browse the repository at this point in the history
…into thlacroix-disable-quotes
  • Loading branch information
David Bariod committed Apr 29, 2020
2 parents 163c051 + aff00fe commit 4d96c60
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
6 changes: 6 additions & 0 deletions text_formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ type TextFormatter struct {
// Force quoting of all values
ForceQuote bool

// DisableQuote disables quoting for all values
DisableQuote bool

// Override coloring based on CLICOLOR and CLICOLOR_FORCE. - https://bixense.com/clicolors/
EnvironmentOverrideColors bool

Expand Down Expand Up @@ -292,6 +295,9 @@ func (f *TextFormatter) needsQuoting(text string) bool {
if f.QuoteEmptyFields && len(text) == 0 {
return true
}
if f.DisableQuote {
return false
}
for _, ch := range text {
if !((ch >= 'a' && ch <= 'z') ||
(ch >= 'A' && ch <= 'Z') ||
Expand Down
18 changes: 18 additions & 0 deletions text_formatter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func TestQuoting(t *testing.T) {
checkQuoting(false, "foo@bar")
checkQuoting(false, "foobar^")
checkQuoting(false, "+/-_^@f.oobar")
checkQuoting(true, "foo\n\rbar")
checkQuoting(true, "foobar$")
checkQuoting(true, "&foobar")
checkQuoting(true, "x y")
Expand All @@ -70,13 +71,30 @@ func TestQuoting(t *testing.T) {
tf.QuoteEmptyFields = true
checkQuoting(true, "")
checkQuoting(false, "abcd")
checkQuoting(true, "foo\n\rbar")
checkQuoting(true, errors.New("invalid argument"))

// Test forcing quotes.
tf.ForceQuote = true
checkQuoting(true, "")
checkQuoting(true, "abcd")
checkQuoting(true, "foo\n\rbar")
checkQuoting(true, errors.New("invalid argument"))

// Test forcing quotes when also disabling them.
tf.DisableQuote = true
checkQuoting(true, "")
checkQuoting(true, "abcd")
checkQuoting(true, "foo\n\rbar")
checkQuoting(true, errors.New("invalid argument"))

// Test disabling quotes
tf.ForceQuote = false
tf.QuoteEmptyFields = false
checkQuoting(false, "")
checkQuoting(false, "abcd")
checkQuoting(false, "foo\n\rbar")
checkQuoting(false, errors.New("invalid argument"))
}

func TestEscaping(t *testing.T) {
Expand Down

0 comments on commit 4d96c60

Please sign in to comment.