summaryrefslogtreecommitdiff
path: root/internal
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2021-08-12 10:56:36 +0300
committerPaul Buetow <paul@buetow.org>2021-08-12 10:56:36 +0300
commit3cc8887885f24a3f0d607af24197bc364ab16b8d (patch)
tree48e85a54d3e4f6139001a58a3e57dd03b27c780a /internal
parent1e00c256842e125a865a6cc3f9aa70a1a498f6dc (diff)
add missing brush and also add color client configs plus jsonschema
Diffstat (limited to 'internal')
-rw-r--r--internal/color/brush/brush.go104
-rw-r--r--internal/color/color.go2
-rw-r--r--internal/color/color_test.go1
-rw-r--r--internal/config/client.go6
-rw-r--r--internal/io/logger/logger.go4
-rw-r--r--internal/version/version.go9
6 files changed, 118 insertions, 8 deletions
diff --git a/internal/color/brush/brush.go b/internal/color/brush/brush.go
new file mode 100644
index 0000000..c5efff6
--- /dev/null
+++ b/internal/color/brush/brush.go
@@ -0,0 +1,104 @@
+package brush
+
+import (
+ "fmt"
+ "strings"
+
+ "github.com/mimecast/dtail/internal/color"
+ "github.com/mimecast/dtail/internal/config"
+)
+
+// 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] = color.Paint(splitted[2],
+ config.Client.TermColors.RemoteStatsOkFg,
+ config.Client.TermColors.RemoteStatsOkBg)
+ } else {
+ splitted[2] = color.Paint(splitted[2],
+ config.Client.TermColors.RemoteStatsWarnFg,
+ config.Client.TermColors.RemoteStatsWarnBg)
+ }
+
+ info := strings.Join(splitted[0:5], "|")
+ log := strings.Join(splitted[5:], "|")
+
+ switch {
+ case strings.HasPrefix(log, "WARN"):
+ log = color.PaintWithAttr(log,
+ config.Client.TermColors.RemoteWarnFg,
+ config.Client.TermColors.RemoteWarnBg,
+ config.Client.TermColors.RemoteWarnAttr)
+
+ case strings.HasPrefix(log, "ERROR"):
+ log = color.PaintWithAttr(log,
+ config.Client.TermColors.RemoteErrorFg,
+ config.Client.TermColors.RemoteErrorBg,
+ config.Client.TermColors.RemoteErrorAttr)
+
+ case strings.HasPrefix(log, "FATAL"):
+ log = color.PaintWithAttr(log,
+ config.Client.TermColors.RemoteFatalFg,
+ config.Client.TermColors.RemoteFatalBg,
+ config.Client.TermColors.RemoteFatalAttr)
+
+ case strings.HasPrefix(log, "DEBUG"):
+ log = color.PaintWithAttr(log,
+ config.Client.TermColors.RemoteDebugFg,
+ config.Client.TermColors.RemoteDebugBg,
+ config.Client.TermColors.RemoteDebugAttr)
+
+ case strings.HasPrefix(log, "TRACE"):
+ log = color.PaintWithAttr(log,
+ config.Client.TermColors.RemoteTraceFg,
+ config.Client.TermColors.RemoteTraceBg,
+ config.Client.TermColors.RemoteTraceAttr)
+
+ default:
+ log = color.PaintWithAttr(log,
+ config.Client.TermColors.RemoteTextFg,
+ config.Client.TermColors.RemoteTextBg,
+ config.Client.TermColors.RemoteTextAttr)
+ }
+
+ 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 := color.PaintWithAttr(splitted[4],
+ config.Client.TermColors.ClientStatsFg,
+ config.Client.TermColors.ClientStatsBg,
+ config.Client.TermColors.ClientStatsAttr)
+ 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 color.PaintWithAttr(line,
+ config.Client.TermColors.ClientErrorFg,
+ config.Client.TermColors.ClientErrorBg,
+ config.Client.TermColors.ClientErrorAttr)
+
+ case strings.Contains(line, "WARN"):
+ return color.PaintWithAttr(line,
+ config.Client.TermColors.ClientWarnFg,
+ config.Client.TermColors.ClientWarnBg,
+ config.Client.TermColors.ClientWarnAttr)
+ }
+
+ return line
+}
diff --git a/internal/color/color.go b/internal/color/color.go
index 692de51..5c25855 100644
--- a/internal/color/color.go
+++ b/internal/color/color.go
@@ -138,6 +138,6 @@ func ToAttribute(s string) (Attribute, error) {
case "":
return AttrNone, nil
default:
- return AttrReset, fmt.Errorf("unknown text attribute '" + s + "'")
+ return AttrNone, fmt.Errorf("unknown text attribute '" + s + "'")
}
}
diff --git a/internal/color/color_test.go b/internal/color/color_test.go
index 16b2f90..bfc7c54 100644
--- a/internal/color/color_test.go
+++ b/internal/color/color_test.go
@@ -36,6 +36,7 @@ func TestColors(t *testing.T) {
t.Log(builder.String())
}
+
func TestAttributes(t *testing.T) {
text := " Mimecast "
builder := strings.Builder{}
diff --git a/internal/config/client.go b/internal/config/client.go
index 5386576..c1b488c 100644
--- a/internal/config/client.go
+++ b/internal/config/client.go
@@ -52,14 +52,14 @@ type termColors struct {
// 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 {
- TermColorsEnabled bool
- TermColors termColors `json:",omitempty"`
+ TermColorsEnable bool
+ TermColors termColors `json:",omitempty"`
}
// Create a new default client configuration.
func newDefaultClientConfig() *ClientConfig {
return &ClientConfig{
- TermColorsEnabled: true,
+ TermColorsEnable: true,
TermColors: termColors{
ClientErrorAttr: color.AttrBold,
ClientErrorBg: color.BgBlack,
diff --git a/internal/io/logger/logger.go b/internal/io/logger/logger.go
index bef5293..bb9dc02 100644
--- a/internal/io/logger/logger.go
+++ b/internal/io/logger/logger.go
@@ -207,7 +207,7 @@ func write(what, severity, message string) {
if Mode.logToStdout {
line := fmt.Sprintf("%s|%s|%s|%s\n", what, hostname, severity, message)
- if config.Client.TermColorsEnabled {
+ if config.Client.TermColorsEnable {
line = brush.Colorfy(line)
}
@@ -262,7 +262,7 @@ func Raw(message string) {
}
if Mode.logToStdout {
- if config.Client.TermColorsEnabled {
+ if config.Client.TermColorsEnable {
message = brush.Colorfy(message)
}
stdoutBufCh <- message
diff --git a/internal/version/version.go b/internal/version/version.go
index 7cfe12c..3e683bd 100644
--- a/internal/version/version.go
+++ b/internal/version/version.go
@@ -26,7 +26,7 @@ func String() string {
// PaintedString is a prettier string representation of the DTail version.
func PaintedString() string {
- if !config.Client.TermColorsEnabled {
+ if !config.Client.TermColorsEnable {
return String()
}
@@ -45,8 +45,13 @@ func PaintedString() string {
return fmt.Sprintf("%s%v%s%s", name, version, protocol, additional)
}
+// Print the version.
+func Print() {
+ fmt.Println(PaintedString())
+}
+
// PrintAndExit prints the program version and exists.
func PrintAndExit() {
- fmt.Println(PaintedString())
+ Print()
os.Exit(0)
}