Skip to content

Commit

Permalink
Merge pull request #56 from tongson/rrl_improvement
Browse files Browse the repository at this point in the history
Improve rrl mode
  • Loading branch information
tongson authored Jun 1, 2024
2 parents 60c802c + 7d0c2fc commit e466a16
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 44 deletions.
2 changes: 1 addition & 1 deletion constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
47 changes: 4 additions & 43 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"bufio"
"encoding/base64"
"encoding/json"
"fmt"
"hash/maphash"
"io"
Expand All @@ -16,7 +15,6 @@ import (
"strconv"
"strings"
"syscall"
"text/tabwriter"
"time"

isatty "github.com/mattn/go-isatty"
Expand Down Expand Up @@ -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\tStarted\tNS\tScript\tOp\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 {
Expand Down
2 changes: 2 additions & 0 deletions make/repair/script
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
printf "Running...\\n"
>&2 printf "__REPAIRED__"
105 changes: 105 additions & 0 deletions rrl.go
Original file line number Diff line number Diff line change
@@ -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()
}

0 comments on commit e466a16

Please sign in to comment.