diff --git a/conv.go b/conv.go index 188ef266..d4769056 100644 --- a/conv.go +++ b/conv.go @@ -7,6 +7,7 @@ import ( "io" "os" "strconv" + "strings" "time" api "github.com/macrat/ayd/lib-ayd" @@ -34,6 +35,7 @@ Options: -c, --csv Convert to CSV. (default format) -j, --json Convert to JSON. + -l, --ltsv Convert to LTSV. -h, --help Show this help message and exit. ` @@ -45,6 +47,7 @@ func (c ConvCommand) Run(args []string) int { toCsv := flags.BoolP("csv", "c", false, "Convert to CSV") toJson := flags.BoolP("json", "j", false, "Convert to JSON") + toLtsv := flags.BoolP("ltsv", "l", false, "Convert to LTSV") help := flags.BoolP("help", "h", false, "Show this message and exit") @@ -66,6 +69,9 @@ func (c ConvCommand) Run(args []string) int { if *toJson { count++ } + if *toLtsv { + count++ + } if count > 1 { fmt.Fprintln(c.ErrStream, "error: flags for output format can not use multiple in the same time.") return 2 @@ -105,6 +111,8 @@ func (c ConvCommand) Run(args []string) int { switch { case *toJson: err = c.toJson(scanners, output) + case *toLtsv: + err = c.toLTSV(scanners, output) default: err = c.toCSV(scanners, output) } @@ -168,3 +176,43 @@ func (c ConvCommand) toCSV(scanners []api.LogScanner, output io.Writer) error { return nil } + +func (c ConvCommand) toLTSV(scanners []api.LogScanner, output io.Writer) error { + for _, s := range scanners { + for s.Scan() { + r := s.Record() + fmt.Fprintf( + output, + "time:%s\tstatus:%s\tlatency:%.3f\ttarget:%s", + r.CheckedAt.Format(time.RFC3339), + r.Status, + float64(r.Latency.Microseconds())/1000, + r.Target, + ) + + if r.Message != "" { + fmt.Fprintf(output, "\tmessage:%s", r.Message) + } + + extra := r.ReadableExtra() + + for _, e := range extra { + s := e.Value + if _, ok := r.Extra[e.Key].(string); ok { + // You should escape if the value is string. Otherwise, it's already escaped as a JSON value. + s = strings.ReplaceAll(strings.ReplaceAll(strings.ReplaceAll(strings.ReplaceAll(e.Value, `\`, `\\`), "\t", `\t`), "\n", `\n`), "\r", `\r`) + } + fmt.Fprintf( + output, + "\t%s:%s", + e.Key, + s, + ) + } + + fmt.Fprintln(output) + } + } + + return nil +} diff --git a/conv_test.go b/conv_test.go index 28b63406..1421fcbe 100644 --- a/conv_test.go +++ b/conv_test.go @@ -22,6 +22,9 @@ var testLogCSV string //go:embed testdata/log.json var testLogJson string +//go:embed testdata/log.ltsv +var testLogLtsv string + func TestConvCommand_Run(t *testing.T) { tests := []struct { args []string @@ -72,6 +75,13 @@ func TestConvCommand_Run(t *testing.T) { "", 0, }, + { + []string{"-l"}, + testLog, + testLogLtsv, + "", + 0, + }, { []string{"-j", "-c"}, testLog,