Skip to content

Commit

Permalink
Merge pull request #231 from amorenoz/tabulated-write
Browse files Browse the repository at this point in the history
Add tabulated output to write_stdout
  • Loading branch information
jotak committed Jul 13, 2022
2 parents a16d990 + 1f348fd commit 65b11c5
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
3 changes: 2 additions & 1 deletion docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,8 @@ Following is the supported API format for writing to standard output:

<pre>
stdout:
format: the format of each line: printf (default) or json
format: the format of each line: printf (default - writes using golang's default map printing), fields (writes one key and value field per line) or json

</pre>
## Aggregate metrics API
Following is the supported API format for specifying metrics aggregations:
Expand Down
2 changes: 1 addition & 1 deletion pkg/api/write_stdout.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package api

type WriteStdout struct {
Format string `yaml:"format,omitempty" json:"format,omitempty" doc:"the format of each line: printf (default) or json"`
Format string `yaml:"format,omitempty" json:"format,omitempty" doc:"the format of each line: printf (default - writes using golang's default map printing), fields (writes one key and value field per line) or json"`
}
17 changes: 17 additions & 0 deletions pkg/pipeline/write/write_stdout.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ package write
import (
"encoding/json"
"fmt"
"os"
"sort"
"text/tabwriter"
"time"

"github.com/netobserv/flowlogs-pipeline/pkg/config"
Expand All @@ -39,6 +42,20 @@ func (t *writeStdout) Write(in []config.GenericMap) {
txt, _ := json.Marshal(v)
fmt.Println(string(txt))
}
} else if t.format == "fields" {
for _, v := range in {
var order sort.StringSlice
for fieldName := range v {
order = append(order, fieldName)
}
order.Sort()
w := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', 0)
fmt.Fprintf(w, "\n\nFlow record at %s:\n", time.Now().Format(time.StampMilli))
for _, field := range order {
fmt.Fprintf(w, "%v\t=\t%v\n", field, v[field])
}
w.Flush()
}
} else {
for _, v := range in {
fmt.Printf("%s: %v\n", time.Now().Format(time.StampMilli), v)
Expand Down

0 comments on commit 65b11c5

Please sign in to comment.