From 68d603e6ed416f0c5b11c731c438afb54667432e Mon Sep 17 00:00:00 2001 From: Adrian Moreno Date: Fri, 22 Apr 2022 18:52:22 +0200 Subject: [PATCH] write_stdout: add tabulated data format Add a new write_stdout format called "fields". If specified, write a tabulated list of fields. It's easier to read than raw json or text outputs. Signed-off-by: Adrian Moreno --- docs/api.md | 2 +- pkg/api/write_stdout.go | 2 +- pkg/pipeline/write/write_stdout.go | 11 +++++++++++ 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/docs/api.md b/docs/api.md index b78b22765..cb3b40c60 100644 --- a/docs/api.md +++ b/docs/api.md @@ -145,7 +145,7 @@ Following is the supported API format for writing to standard output:
  stdout:
-         format: the format of each line: printf (default) or json
+         format: the format of each line: printf (default), fields, or json
 
## Aggregate metrics API Following is the supported API format for specifying metrics aggregations: diff --git a/pkg/api/write_stdout.go b/pkg/api/write_stdout.go index 17191ee9d..a972f7cef 100644 --- a/pkg/api/write_stdout.go +++ b/pkg/api/write_stdout.go @@ -1,5 +1,5 @@ package api type WriteStdout struct { - Format string `yaml:"format" json:"format" doc:"the format of each line: printf (default) or json"` + Format string `yaml:"format" json:"format" doc:"the format of each line: printf (default), fields, or json"` } diff --git a/pkg/pipeline/write/write_stdout.go b/pkg/pipeline/write/write_stdout.go index 525212e28..f78232189 100644 --- a/pkg/pipeline/write/write_stdout.go +++ b/pkg/pipeline/write/write_stdout.go @@ -20,6 +20,8 @@ package write import ( "encoding/json" "fmt" + "os" + "text/tabwriter" "time" "github.com/netobserv/flowlogs-pipeline/pkg/config" @@ -39,6 +41,15 @@ func (t *writeStdout) Write(in []config.GenericMap) { txt, _ := json.Marshal(v) fmt.Println(string(txt)) } + } else if t.format == "fields" { + for _, v := range in { + fmt.Printf("Flow record at %s:\n", time.Now().Format(time.StampMilli)) + w := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', 0) + for key, val := range v { + fmt.Fprintf(w, "%v\t=\t%v\n", key, val) + } + w.Flush() + } } else { for _, v := range in { fmt.Printf("%s: %v\n", time.Now().Format(time.StampMilli), v)