From ece01a9ad589e86176fe00695b7d4a635ecc5548 Mon Sep 17 00:00:00 2001 From: Eduardo Tongson Date: Fri, 31 May 2024 17:09:41 +0800 Subject: [PATCH 1/5] Better headers. --- main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.go b/main.go index ae19bf6..6c035a5 100644 --- a/main.go +++ b/main.go @@ -532,7 +532,7 @@ rrl = report` if mReport { w := new(tabwriter.Writer) w.Init(os.Stdout, 0, 8, 0, '\t', 0) - hdrs := "ID \tTarget\tStarted\tNS\tScript\tOp\tLen\tResult\t" + hdrs := "ID \tTarget\tInitiated\tNS\tScript\tLog\tLen\tResult\t" _, _ = fmt.Fprintln(w, hdrs) rrl, err := os.Open(cLOG) if err != nil { From 113ef1049451efa7039b346b159a0f200015c4b2 Mon Sep 17 00:00:00 2001 From: Eduardo Tongson Date: Fri, 31 May 2024 21:08:42 +0800 Subject: [PATCH 2/5] Colorized rrl output. Split rrl into its own file. --- main.go | 47 +++---------------------- rrl.go | 105 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 109 insertions(+), 43 deletions(-) create mode 100644 rrl.go diff --git a/main.go b/main.go index 6c035a5..2b2a3c5 100644 --- a/main.go +++ b/main.go @@ -3,7 +3,6 @@ package main import ( "bufio" "encoding/base64" - "encoding/json" "fmt" "hash/maphash" "io" @@ -16,7 +15,6 @@ import ( "strconv" "strings" "syscall" - "text/tabwriter" "time" isatty "github.com/mattn/go-isatty" @@ -529,50 +527,13 @@ rrl = report` os.Exit(2) } } + + // rrl mode if mReport { - w := new(tabwriter.Writer) - w.Init(os.Stdout, 0, 8, 0, '\t', 0) - hdrs := "ID \tTarget\tInitiated\tNS\tScript\tLog\tLen\tResult\t" - _, _ = fmt.Fprintln(w, hdrs) - rrl, err := os.Open(cLOG) - if err != nil { - _, _ = fmt.Fprintf(os.Stderr, "Missing `%s` in the current directory.\n", cLOG) - os.Exit(1) - } - defer rrl.Close() - var maxSz int - scanner := bufio.NewScanner(rrl) - rrlInfo, err := rrl.Stat() - if err != nil { - _, _ = fmt.Fprintf(os.Stderr, "Unable to open `%s`.\n", cLOG) - os.Exit(1) - } - maxSz = int(rrlInfo.Size()) - buf := make([]byte, 0, maxSz) - scanner.Buffer(buf, maxSz) - for scanner.Scan() { - log := make(map[string]string) - err := json.Unmarshal(scanner.Bytes(), &log) - if err != nil { - _, _ = fmt.Fprintf(os.Stderr, "Unable to decode `%s`.\n", cLOG) - os.Exit(1) - } - if log["duration"] != "" { - _, _ = fmt.Fprintln(w, log["id"]+" \t"+ - log["target"]+"\t"+ - log["start"]+"\t"+ - log["namespace"]+"\t"+ - log["script"]+"\t"+ - log["task"]+"\t"+ - log["duration"]+"\t"+ - log["msg"]+"\t") - } - } - _, _ = fmt.Fprintln(w, hdrs) - _ = w.Flush() - _ = rrl.Close() + rrlMain() os.Exit(0) } + log.SetFlags(0) var serrLog *slog.Logger if !mReport && !mDump && opt.mode != oPlain { diff --git a/rrl.go b/rrl.go new file mode 100644 index 0000000..97f30f4 --- /dev/null +++ b/rrl.go @@ -0,0 +1,105 @@ +package main + +import ( + "bufio" + "encoding/json" + "fmt" + "io" + "os" + "strings" + "text/tabwriter" +) + +const rrlReset = "\x1b[0000m" +const rrlRed = "\x1b[0031m" +const rrlGreen = "\x1b[0032m" +const rrlYellow = "\x1b[0033m" +const rrlBlue = "\x1b[0034m" +const rrlMagenta = "\x1b[0035m" +const rrlCyan = "\x1b[0036m" +const rrlDefault = "\x1b[0039m" +const rrlWhite = "\x1b[1;37m" + +type rrlColor string + +func (c *rrlColor) String() string { + type _color *rrlColor + return fmt.Sprintf("%v", _color(c)) +} + +func rrlPaint(color rrlColor, value string) string { + return fmt.Sprintf("%v%v%v", color, value, rrlReset) +} + +func rrlPaintRow(colors []rrlColor, row []string) []string { + paintedRow := make([]string, len(row)) + for i, v := range row { + paintedRow[i] = rrlPaint(colors[i], v) + } + return paintedRow +} + +func rrlPaintUniformly(color rrlColor, row []string) []string { + colors := make([]rrlColor, len(row)) + for i, _ := range row { + colors[i] = color + } + return rrlPaintRow(colors, row) +} + +func rrlPrint(writer io.Writer, line []string) { + fmt.Fprintln(writer, strings.Join(line, "\t")) +} + +func rrlMain() { + w := new(tabwriter.Writer) + w.Init(os.Stdout, 0, 8, 1, ' ', 0) + rrl, err := os.Open(cLOG) + if err != nil { + _, _ = fmt.Fprintf(os.Stderr, "Missing `%s` in the current directory.\n", cLOG) + os.Exit(1) + } + defer rrl.Close() + hdrs := []string{"ID", "Target", "Initiated", "NS", "Script", "Log", "Len", "Result"} + rrlPrint(w, rrlPaintUniformly(rrlDefault, hdrs)) + var maxSz int + scanner := bufio.NewScanner(rrl) + rrlInfo, err := rrl.Stat() + if err != nil { + _, _ = fmt.Fprintf(os.Stderr, "Unable to open `%s`.\n", cLOG) + os.Exit(1) + } + maxSz = int(rrlInfo.Size()) + buf := make([]byte, 0, maxSz) + scanner.Buffer(buf, maxSz) + for scanner.Scan() { + log := make(map[string]string) + err := json.Unmarshal(scanner.Bytes(), &log) + if err != nil { + _, _ = fmt.Fprintf(os.Stderr, "Unable to decode `%s`.\n", cLOG) + os.Exit(1) + } + colors := []rrlColor{rrlCyan, rrlGreen, rrlMagenta, rrlCyan, rrlGreen, rrlBlue, rrlMagenta, rrlWhite} + if log["msg"] == "failed" { + colors = []rrlColor{rrlRed, rrlRed, rrlRed, rrlRed, rrlRed, rrlRed, rrlRed, rrlRed} + } + if log["msg"] == "repaired" { + colors = []rrlColor{rrlCyan, rrlGreen, rrlMagenta, rrlCyan, rrlGreen, rrlBlue, rrlMagenta, rrlYellow} + } + if log["duration"] != "" { + rrlPrint(w, rrlPaintRow(colors, []string{ + log["id"], + log["target"], + log["start"], + log["namespace"], + log["script"], + log["task"], + log["duration"], + log["msg"], + })) + } + } + rrlPrint(w, rrlPaintUniformly(rrlDefault, hdrs)) + _ = w.Flush() + _ = rrl.Close() +} From cd3bca75512dfc1c0a6ee077205c442b29711de7 Mon Sep 17 00:00:00 2001 From: Eduardo Tongson Date: Fri, 31 May 2024 21:08:08 +0800 Subject: [PATCH 3/5] Smoke test for repaired status. --- make/repair/script | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 make/repair/script diff --git a/make/repair/script b/make/repair/script new file mode 100644 index 0000000..d97d142 --- /dev/null +++ b/make/repair/script @@ -0,0 +1,3 @@ +printf "Running...\\n" +>&2 printf "__REPAIRED__" + From f4052a59f3f4dd7056ab24ae7da03032e28b106b Mon Sep 17 00:00:00 2001 From: Eduardo Tongson Date: Fri, 31 May 2024 21:08:54 +0800 Subject: [PATCH 4/5] Extra line. --- make/repair/script | 1 - 1 file changed, 1 deletion(-) diff --git a/make/repair/script b/make/repair/script index d97d142..e35a1fe 100644 --- a/make/repair/script +++ b/make/repair/script @@ -1,3 +1,2 @@ printf "Running...\\n" >&2 printf "__REPAIRED__" - From 7d0c2fccec5dcde40ab2e6bbbfa8ecd09165e72e Mon Sep 17 00:00:00 2001 From: Eduardo Tongson Date: Fri, 31 May 2024 21:28:04 +0800 Subject: [PATCH 5/5] Change environment variable to LOG. --- constants.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/constants.go b/constants.go index ef85e25..86528a0 100644 --- a/constants.go +++ b/constants.go @@ -3,7 +3,7 @@ package main const cVERSION = "2.0.1" const cCODE = "\"Hypnotic Antennae\"" -const cOP = "OP" +const cOP = "LOG" const cINC = "VARS" const cHOSTS = "HOSTS" const cLOG = "LOG"