Skip to content

Commit

Permalink
feat(cli/conv): support LTSV format as output of conv subcommand
Browse files Browse the repository at this point in the history
  • Loading branch information
macrat committed May 29, 2022
1 parent af514c9 commit 9d95fdb
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
48 changes: 48 additions & 0 deletions conv.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io"
"os"
"strconv"
"strings"
"time"

api "github.com/macrat/ayd/lib-ayd"
Expand Down Expand Up @@ -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.
`
Expand All @@ -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")

Expand All @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
}
10 changes: 10 additions & 0 deletions conv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -72,6 +75,13 @@ func TestConvCommand_Run(t *testing.T) {
"",
0,
},
{
[]string{"-l"},
testLog,
testLogLtsv,
"",
0,
},
{
[]string{"-j", "-c"},
testLog,
Expand Down

0 comments on commit 9d95fdb

Please sign in to comment.