summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--cmd/dcat/main.go5
-rw-r--r--cmd/dgrep/main.go5
-rw-r--r--cmd/dmap/main.go5
-rw-r--r--cmd/dserver/main.go8
-rw-r--r--cmd/dtail/main.go5
-rw-r--r--internal/color/color.go69
-rw-r--r--internal/color/colorfy.go56
-rw-r--r--internal/config/client.go51
-rw-r--r--internal/io/logger/logger.go10
-rw-r--r--internal/version/version.go3
10 files changed, 57 insertions, 160 deletions
diff --git a/cmd/dcat/main.go b/cmd/dcat/main.go
index 238f97a..1aa7798 100644
--- a/cmd/dcat/main.go
+++ b/cmd/dcat/main.go
@@ -6,7 +6,6 @@ import (
"os"
"github.com/mimecast/dtail/internal/clients"
- "github.com/mimecast/dtail/internal/color"
"github.com/mimecast/dtail/internal/config"
"github.com/mimecast/dtail/internal/io/logger"
"github.com/mimecast/dtail/internal/io/signal"
@@ -42,7 +41,9 @@ func main() {
flag.Parse()
config.Read(cfgFile, sshPort)
- color.Colored = !noColor
+ if noColor {
+ config.Client.TermColorsEnabled = false
+ }
if displayVersion {
version.PrintAndExit()
diff --git a/cmd/dgrep/main.go b/cmd/dgrep/main.go
index 4da1bb3..422bdd4 100644
--- a/cmd/dgrep/main.go
+++ b/cmd/dgrep/main.go
@@ -6,7 +6,6 @@ import (
"os"
"github.com/mimecast/dtail/internal/clients"
- "github.com/mimecast/dtail/internal/color"
"github.com/mimecast/dtail/internal/config"
"github.com/mimecast/dtail/internal/io/logger"
"github.com/mimecast/dtail/internal/io/signal"
@@ -46,7 +45,9 @@ func main() {
flag.Parse()
config.Read(cfgFile, sshPort)
- color.Colored = !noColor
+ if noColor {
+ config.Client.TermColorsEnabled = false
+ }
if displayVersion {
version.PrintAndExit()
diff --git a/cmd/dmap/main.go b/cmd/dmap/main.go
index 1a05549..8a38069 100644
--- a/cmd/dmap/main.go
+++ b/cmd/dmap/main.go
@@ -6,7 +6,6 @@ import (
"os"
"github.com/mimecast/dtail/internal/clients"
- "github.com/mimecast/dtail/internal/color"
"github.com/mimecast/dtail/internal/config"
"github.com/mimecast/dtail/internal/io/logger"
"github.com/mimecast/dtail/internal/io/signal"
@@ -49,7 +48,9 @@ func main() {
flag.Parse()
config.Read(cfgFile, sshPort)
- color.Colored = !noColor
+ if noColor {
+ config.Client.TermColorsEnabled = false
+ }
if displayVersion {
version.PrintAndExit()
diff --git a/cmd/dserver/main.go b/cmd/dserver/main.go
index 07f5270..5993481 100644
--- a/cmd/dserver/main.go
+++ b/cmd/dserver/main.go
@@ -12,7 +12,6 @@ import (
"syscall"
"time"
- "github.com/mimecast/dtail/internal/color"
"github.com/mimecast/dtail/internal/config"
"github.com/mimecast/dtail/internal/io/logger"
"github.com/mimecast/dtail/internal/server"
@@ -25,7 +24,7 @@ func main() {
var cfgFile string
var debugEnable bool
var displayVersion bool
- var noColor bool
+ var color bool
var pprof int
var shutdownAfter int
var sshPort int
@@ -35,16 +34,15 @@ func main() {
flag.BoolVar(&debugEnable, "debug", false, "Activate debug messages")
flag.BoolVar(&displayVersion, "version", false, "Display version")
flag.BoolVar(&config.ServerRelaxedAuthEnable, "relaxedAuth", false, "Enable relaxced SSH auth mode (don't use in production!)")
- flag.BoolVar(&noColor, "noColor", false, "Disable ANSII terminal colors")
+ flag.BoolVar(&color, "color", false, "Enable ANSII terminal colors")
flag.IntVar(&pprof, "pprof", -1, "Start PProf server this port")
flag.IntVar(&shutdownAfter, "shutdownAfter", 0, "Automatically shutdown after so many seconds")
flag.IntVar(&sshPort, "port", 2222, "SSH server port")
flag.StringVar(&cfgFile, "cfg", "", "Config file path")
flag.Parse()
-
config.Read(cfgFile, sshPort)
- color.Colored = !noColor
+ config.Client.TermColorsEnabled = color
if displayVersion {
version.PrintAndExit()
diff --git a/cmd/dtail/main.go b/cmd/dtail/main.go
index f2a039f..178ea52 100644
--- a/cmd/dtail/main.go
+++ b/cmd/dtail/main.go
@@ -11,7 +11,6 @@ import (
"time"
"github.com/mimecast/dtail/internal/clients"
- "github.com/mimecast/dtail/internal/color"
"github.com/mimecast/dtail/internal/config"
"github.com/mimecast/dtail/internal/io/logger"
"github.com/mimecast/dtail/internal/io/signal"
@@ -65,7 +64,9 @@ func main() {
}
config.Read(cfgFile, sshPort)
- color.Colored = !noColor
+ if noColor {
+ config.Client.TermColorsEnabled = false
+ }
if displayVersion {
version.PrintAndExit()
diff --git a/internal/color/color.go b/internal/color/color.go
deleted file mode 100644
index 7309544..0000000
--- a/internal/color/color.go
+++ /dev/null
@@ -1,69 +0,0 @@
-// Package color is used to prettify console output via ANSII terminal colors.
-package color
-
-import (
- "fmt"
-)
-
-// Color name.
-type Color string
-
-// Attribute of a color.
-type Attribute string
-
-// The possible color variations.
-const (
- escape = "\x1b"
- reset = escape + "[0m"
- seq string = "%s%s%s"
-
- Gray Color = escape + "[30m"
- Red Color = escape + "[31m"
- Green Color = escape + "[32m"
- Orange Color = escape + "[33m"
- Blue Color = escape + "[34m"
- Magenta Color = escape + "[35m"
- Yellow Color = escape + "[36m"
- LightGray Color = escape + "[37m"
-
- BgGray Color = escape + "[40m" BgRed Color = escape + "[41m"
- BgGreen Color = escape + "[42m"
- BgOrange Color = escape + "[43m"
- BgBlue Color = escape + "[44m"
- BgMagenta Color = escape + "[45m"
- BgYellow Color = escape + "[46m"
- BgLightGray Color = escape + "[47m"
-
- Bold Attribute = escape + "[1m"
- Italic Attribute = escape + "[3m"
- Underline Attribute = escape + "[4m"
- ReverseColor Attribute = escape + "[7m"
-
- resetBold = escape + "[22m"
- resetItalic = escape + "[23m"
- resetUnderline = escape + "[24m"
-
- Test Color = BgYellow
- TestAttr Attribute = Bold
-)
-
-// Colored DTail client output enabled.
-var Colored bool
-
-// Paint a given string in a given color.
-func Paint(c Color, s string) string {
- return fmt.Sprintf(seq, c, s, reset)
-}
-
-// Attr adds a given attribute to a given string, such as "bold" or "italic".
-func Attr(c Attribute, s string) string {
- switch c {
- case Bold:
- return fmt.Sprintf(seq, Bold, s, resetBold)
- case Italic:
- return fmt.Sprintf(seq, Italic, s, resetItalic)
- case Underline:
- return fmt.Sprintf(seq, Underline, s, resetUnderline)
- }
- panic("Unknown attribute")
-}
diff --git a/internal/color/colorfy.go b/internal/color/colorfy.go
deleted file mode 100644
index a2beb7a..0000000
--- a/internal/color/colorfy.go
+++ /dev/null
@@ -1,56 +0,0 @@
-package color
-
-import (
- "fmt"
- "strings"
-)
-
-// Add some color to log lines received from remote servers.
-func paintRemote(line string) string {
- splitted := strings.Split(line, "|")
- if splitted[2] == "100" {
- splitted[2] = Paint(BgGreen, splitted[2])
- } else {
- splitted[2] = Paint(BgRed, splitted[2])
- }
- info := strings.Join(splitted[0:5], "|")
- log := strings.Join(splitted[5:], "|")
-
- if strings.HasPrefix(log, "WARN") {
- log = Paint(BgYellow, log)
- } else if strings.HasPrefix(log, "ERROR") {
- log = Paint(BgRed, log)
- } else if strings.HasPrefix(log, "FATAL") {
- log = Attr(Bold, Paint(BgRed, log))
- } else {
- log = Paint(Blue, log)
- }
-
- return fmt.Sprintf("%s|%s", info, log)
-}
-
-// Add some color to stats generated by the client.
-func paintClientStats(line string) string {
- splitted := strings.Split(line, "|")
- first := strings.Join(splitted[0:4], "|")
- connected := Paint(BgBlue, splitted[4])
- last := strings.Join(splitted[5:], "|")
-
- return fmt.Sprintf("%s|%s|%s", first, connected, last)
-}
-
-// Colorfy a given line based on the line's content.
-func Colorfy(line string) string {
- switch {
- case strings.HasPrefix(line, "REMOTE"):
- return paintRemote(line)
- case strings.HasPrefix(line, "CLIENT") && strings.Contains(line, "|stats|"):
- return paintClientStats(line)
- case strings.Contains(line, "ERROR"):
- return Paint(Magenta, line)
- case strings.Contains(line, "WARN"):
- return Paint(Magenta, line)
- }
-
- return line
-}
diff --git a/internal/config/client.go b/internal/config/client.go
index 3d8c45a..d6d106f 100644
--- a/internal/config/client.go
+++ b/internal/config/client.go
@@ -1,28 +1,47 @@
package config
-// ClientColorConfig allows to override the default terminal color codes.
-type ClientColorConfig struct {
- OkBgColor Color
-}
+import "github.com/mimecast/dtail/internal/color"
-/*
- splitted := strings.Split(line, "|")
- if splitted[2] == "100" {
- splitted[2] = Paint(BgGreen, splitted[2])
- } else {
- splitted[2] = Paint(BgRed, splitted[2])
- }
- info := strings.Join(splitted[0:5], "|")
- log := strings.Join(splitted[5:], "|")
-*/
+// ClientColorConfig allows to override the default terminal color color.
+type termColors struct {
+ RemoteTextFg color.Color
+ RemoteStatsOkBg color.Color
+ RemoteStatsWarnBg color.Color
+ RemoteTraceBg color.Color
+ RemoteDebugBg color.Color
+ RemoteWarnBg color.Color
+ RemoteErrorBg color.Color
+ RemoteFatalBg color.Color
+ RemoteFatalAttr color.Attribute
+ ClientStatsBg color.Color
+ ClientWarnFg color.Color
+ ClientErrorFg color.Color
+}
// ClientConfig represents a DTail client configuration (empty as of now as there
// are no available config options yet, but that may changes in the future).
type ClientConfig struct {
- TerminalColors ClientColorConfig `json:",omitempty"`
+ TermColorsEnabled bool
+ TermColors termColors `json:",omitempty"`
}
// Create a new default client configuration.
func newDefaultClientConfig() *ClientConfig {
- return &ClientConfig{}
+ return &ClientConfig{
+ TermColorsEnabled: true,
+ TermColors: termColors{
+ RemoteTextFg: color.LightGray,
+ RemoteStatsOkBg: color.BgGreen,
+ RemoteStatsWarnBg: color.BgRed,
+ RemoteTraceBg: color.BgYellow,
+ RemoteDebugBg: color.BgYellow,
+ RemoteWarnBg: color.BgYellow,
+ RemoteErrorBg: color.BgRed,
+ RemoteFatalBg: color.BgRed,
+ RemoteFatalAttr: color.Bold,
+ ClientStatsBg: color.BgBlue,
+ ClientWarnFg: color.Purple,
+ ClientErrorFg: color.BgRed,
+ },
+ }
}
diff --git a/internal/io/logger/logger.go b/internal/io/logger/logger.go
index 4254eef..bef5293 100644
--- a/internal/io/logger/logger.go
+++ b/internal/io/logger/logger.go
@@ -12,7 +12,7 @@ import (
"syscall"
"time"
- "github.com/mimecast/dtail/internal/color"
+ "github.com/mimecast/dtail/internal/color/brush"
"github.com/mimecast/dtail/internal/config"
)
@@ -207,8 +207,8 @@ func write(what, severity, message string) {
if Mode.logToStdout {
line := fmt.Sprintf("%s|%s|%s|%s\n", what, hostname, severity, message)
- if color.Colored {
- line = color.Colorfy(line)
+ if config.Client.TermColorsEnabled {
+ line = brush.Colorfy(line)
}
stdoutBufCh <- line
@@ -262,8 +262,8 @@ func Raw(message string) {
}
if Mode.logToStdout {
- if color.Colored {
- message = color.Colorfy(message)
+ if config.Client.TermColorsEnabled {
+ message = brush.Colorfy(message)
}
stdoutBufCh <- message
}
diff --git a/internal/version/version.go b/internal/version/version.go
index c0f349c..7c206b4 100644
--- a/internal/version/version.go
+++ b/internal/version/version.go
@@ -5,6 +5,7 @@ import (
"os"
"github.com/mimecast/dtail/internal/color"
+ "github.com/mimecast/dtail/internal/config"
)
const (
@@ -25,7 +26,7 @@ func String() string {
// PaintedString is a prettier string representation of the DTail version.
func PaintedString() string {
- if !color.Colored {
+ if !config.Client.TermColorsEnabled {
return String()
}
name := color.Paint(color.Yellow, Name)