diff options
| author | Paul Buetow <paul@buetow.org> | 2021-08-12 10:56:36 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2021-08-12 10:56:36 +0300 |
| commit | 3cc8887885f24a3f0d607af24197bc364ab16b8d (patch) | |
| tree | 48e85a54d3e4f6139001a58a3e57dd03b27c780a | |
| parent | 1e00c256842e125a865a6cc3f9aa70a1a498f6dc (diff) | |
add missing brush and also add color client configs plus jsonschema
| -rw-r--r-- | cmd/dcat/main.go | 3 | ||||
| -rw-r--r-- | cmd/dgrep/main.go | 3 | ||||
| -rw-r--r-- | cmd/dmap/main.go | 3 | ||||
| -rw-r--r-- | cmd/dserver/main.go | 3 | ||||
| -rw-r--r-- | cmd/dtail/main.go | 4 | ||||
| -rw-r--r-- | go.mod | 2 | ||||
| -rw-r--r-- | go.sum | 2 | ||||
| -rw-r--r-- | internal/color/brush/brush.go | 104 | ||||
| -rw-r--r-- | internal/color/color.go | 2 | ||||
| -rw-r--r-- | internal/color/color_test.go | 1 | ||||
| -rw-r--r-- | internal/config/client.go | 6 | ||||
| -rw-r--r-- | internal/io/logger/logger.go | 4 | ||||
| -rw-r--r-- | internal/version/version.go | 9 | ||||
| -rw-r--r-- | samples/dtail.json.sample | 56 | ||||
| -rwxr-xr-x | samples/dtail.schema.json | 111 |
15 files changed, 290 insertions, 23 deletions
diff --git a/cmd/dcat/main.go b/cmd/dcat/main.go index 1aa7798..1732c26 100644 --- a/cmd/dcat/main.go +++ b/cmd/dcat/main.go @@ -42,12 +42,13 @@ func main() { config.Read(cfgFile, sshPort) if noColor { - config.Client.TermColorsEnabled = false + config.Client.TermColorsEnable = false } if displayVersion { version.PrintAndExit() } + version.Print() ctx := context.TODO() logger.Start(ctx, logger.Modes{ diff --git a/cmd/dgrep/main.go b/cmd/dgrep/main.go index 422bdd4..f32be34 100644 --- a/cmd/dgrep/main.go +++ b/cmd/dgrep/main.go @@ -46,12 +46,13 @@ func main() { config.Read(cfgFile, sshPort) if noColor { - config.Client.TermColorsEnabled = false + config.Client.TermColorsEnable = false } if displayVersion { version.PrintAndExit() } + version.Print() ctx := context.TODO() logger.Start(ctx, logger.Modes{ diff --git a/cmd/dmap/main.go b/cmd/dmap/main.go index 8a38069..f99be52 100644 --- a/cmd/dmap/main.go +++ b/cmd/dmap/main.go @@ -49,12 +49,13 @@ func main() { config.Read(cfgFile, sshPort) if noColor { - config.Client.TermColorsEnabled = false + config.Client.TermColorsEnable = false } if displayVersion { version.PrintAndExit() } + version.Print() ctx := context.TODO() logger.Start(ctx, logger.Modes{ diff --git a/cmd/dserver/main.go b/cmd/dserver/main.go index 5993481..5f72cb8 100644 --- a/cmd/dserver/main.go +++ b/cmd/dserver/main.go @@ -42,11 +42,12 @@ func main() { flag.Parse() config.Read(cfgFile, sshPort) - config.Client.TermColorsEnabled = color + config.Client.TermColorsEnable = color if displayVersion { version.PrintAndExit() } + version.Print() ctx, cancel := context.WithCancel(context.Background()) if shutdownAfter > 0 { diff --git a/cmd/dtail/main.go b/cmd/dtail/main.go index 869a536..c78e11a 100644 --- a/cmd/dtail/main.go +++ b/cmd/dtail/main.go @@ -68,12 +68,14 @@ func main() { config.Read(cfgFile, sshPort) if noColor { - config.Client.TermColorsEnabled = false + config.Client.TermColorsEnable = false } if displayVersion { version.PrintAndExit() } + version.Print() + if displayColorTable { color.TablePrintAndExit() } @@ -7,7 +7,7 @@ require ( github.com/google/go-licenses v0.0.0-20201026145851-73411c8fa237 // indirect github.com/ribice/glice v0.0.0-20190726034412-e55bb973f127 // indirect golang.org/x/crypto v0.0.0-20201203163018-be400aefbc4c - golang.org/x/lint v0.0.0-20201208152925-83fdc39ff7b5 // indirect + golang.org/x/lint v0.0.0-20210508222113-6edffad5e616 // indirect golang.org/x/sys v0.0.0-20201207223542-d4d67f95c62d // indirect golang.org/x/term v0.0.0-20201207232118-ee85cb95a76b // indirect ) @@ -116,6 +116,8 @@ golang.org/x/lint v0.0.0-20200302205851-738671d3881b h1:Wh+f8QHJXR411sJR8/vRBTZ7 golang.org/x/lint v0.0.0-20200302205851-738671d3881b/go.mod h1:3xt1FjdF8hUf6vQPIChWIBhFzV8gjjsPE/fR3IyQdNY= golang.org/x/lint v0.0.0-20201208152925-83fdc39ff7b5 h1:2M3HP5CCK1Si9FQhwnzYhXdG6DXeebvUHFpre8QvbyI= golang.org/x/lint v0.0.0-20201208152925-83fdc39ff7b5/go.mod h1:3xt1FjdF8hUf6vQPIChWIBhFzV8gjjsPE/fR3IyQdNY= +golang.org/x/lint v0.0.0-20210508222113-6edffad5e616 h1:VLliZ0d+/avPrXXH+OakdXhpJuEoBZuwh1m2j7U6Iug= +golang.org/x/lint v0.0.0-20210508222113-6edffad5e616/go.mod h1:3xt1FjdF8hUf6vQPIChWIBhFzV8gjjsPE/fR3IyQdNY= golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg= golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= 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) } diff --git a/samples/dtail.json.sample b/samples/dtail.json.sample index 33d445f..91233f9 100644 --- a/samples/dtail.json.sample +++ b/samples/dtail.json.sample @@ -1,22 +1,62 @@ { - "Client": {}, + "Client": { + "TermColorsEnable": true, + "TermColors": { + "ClientErrorAttr": "Bold", + "ClientErrorBg": "Black", + "ClientErrorFg": "Red", + "ClientStatsAttr": "Dim", + "ClientStatsBg": "Blue", + "ClientStatsFg": "White", + "ClientWarnAttr": "None", + "ClientWarnBg": "Black", + "ClientWarnFg": "Magenta", + "RemoteDebugAttr": "None", + "RemoteDebugBg": "Green", + "RemoteDebugFg": "Black", + "RemoteErrorAttr": "Bold", + "RemoteErrorBg": "Red", + "RemoteErrorFg": "White", + "RemoteFatalAttr": "Blink", + "RemoteFatalBg": "Red", + "RemoteFatalFg": "White", + "RemoteStatsOkAttr": "None", + "RemoteStatsOkBg": "Green", + "RemoteStatsOkFg": "Black", + "RemoteStatsWarnAttr": "None", + "RemoteStatsWarnBg": "Red", + "RemoteStatsWarnFg": "White", + "RemoteTextAttr": "None", + "RemoteTextBg": "Black", + "RemoteTextFg": "White", + "RemoteTraceAttr": "Bold", + "RemoteTraceBg": "Green", + "RemoteTraceFg": "White", + "RemoteWarnAttr": "Bold", + "RemoteWarnBg": "Yellow", + "RemoteWarnFg": "White" + } + }, "Server": { "SSHBindAddress": "0.0.0.0", "MaxConcurrentCats": 2, "MaxConcurrentTails": 50, "MaxConnections": 50, - "MapreduceLogFormat" : "default", - "HostKeyFile" : "cache/ssh_host_key", - "HostKeyBits" : 2048, + "MapreduceLogFormat": "default", + "HostKeyFile": "cache/ssh_host_key", + "HostKeyBits": 2048, "Permissions": { "Default": [ "readfiles:^/.*$" ], "Users": { + "paul": [ + "readfiles:^/.*$" + ], "pbuetow": [ "readfiles:^/.*$" ], - "jblake": [ + "jamesblake": [ "readfiles:^/tmp/foo.log$", "readfiles:^/.*$", "readfiles:!^/tmp/bar.log$" @@ -25,9 +65,9 @@ } }, "Common": { - "LogDir" : "log", - "CacheDir" : "cache", - "TmpDir" : "tmp", + "LogDir": "log", + "CacheDir": "cache", + "TmpDir": "tmp", "LogStrategy": "stdout", "SSHPort": 2222, "DebugEnable": false, diff --git a/samples/dtail.schema.json b/samples/dtail.schema.json index 0497fbf..7978baf 100755 --- a/samples/dtail.schema.json +++ b/samples/dtail.schema.json @@ -4,7 +4,116 @@ "type": "object", "properties": { "Client": { - "properties": {}, + "properties": { + "TermColorsEnable": { + "type": "boolean" + }, + "TermColors": { + "type": "object", + "properties": { + "ClientErrorAttr": { + "type": "string" + }, + "ClientErrorBg": { + "type": "string" + }, + "ClientErrorFg": { + "type": "string" + }, + "ClientStatsAttr": { + "type": "string" + }, + "ClientStatsBg": { + "type": "string" + }, + "ClientStatsFg": { + "type": "string" + }, + "ClientWarnAttr": { + "type": "string" + }, + "ClientWarnBg": { + "type": "string" + }, + "ClientWarnFg": { + "type": "string" + }, + "RemoteDebugAttr": { + "type": "string" + }, + "RemoteDebugBg": { + "type": "string" + }, + "RemoteDebugFg": { + "type": "string" + }, + "RemoteErrorAttr": { + "type": "string" + }, + "RemoteErrorBg": { + "type": "string" + }, + "RemoteErrorFg": { + "type": "string" + }, + "RemoteFatalAttr": { + "type": "string" + }, + "RemoteFatalBg": { + "type": "string" + }, + "RemoteFatalFg": { + "type": "string" + }, + "RemoteStatsOkAttr": { + "type": "string" + }, + "RemoteStatsOkBg": { + "type": "string" + }, + "RemoteStatsOkFg": { + "type": "string" + }, + "RemoteStatsWarnAttr": { + "type": "string" + }, + "RemoteStatsWarnBg": { + "type": "string" + }, + "RemoteStatsWarnFg": { + "type": "string" + }, + "RemoteTextAttr": { + "type": "string" + }, + "RemoteTextBg": { + "type": "string" + }, + "RemoteTextFg": { + "type": "string" + }, + "RemoteTraceAttr": { + "type": "string" + }, + "RemoteTraceBg": { + "type": "string" + }, + "RemoteTraceFg": { + "type": "string" + }, + "RemoteWarnAttr": { + "type": "string" + }, + "RemoteWarnBg": { + "type": "string" + }, + "RemoteWarnFg": { + "type": "string" + } + }, + "additionalProperties": false + } + }, "additionalProperties": false }, "Server": { |
