Skip to content

Commit 65b11c5

Browse files
authored
Merge pull request #231 from amorenoz/tabulated-write
Add tabulated output to write_stdout
2 parents a16d990 + 1f348fd commit 65b11c5

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

docs/api.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,8 @@ Following is the supported API format for writing to standard output:
144144

145145
<pre>
146146
stdout:
147-
format: the format of each line: printf (default) or json
147+
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
148+
148149
</pre>
149150
## Aggregate metrics API
150151
Following is the supported API format for specifying metrics aggregations:

pkg/api/write_stdout.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
package api
22

33
type WriteStdout struct {
4-
Format string `yaml:"format,omitempty" json:"format,omitempty" doc:"the format of each line: printf (default) or json"`
4+
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"`
55
}

pkg/pipeline/write/write_stdout.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ package write
2020
import (
2121
"encoding/json"
2222
"fmt"
23+
"os"
24+
"sort"
25+
"text/tabwriter"
2326
"time"
2427

2528
"github.com/netobserv/flowlogs-pipeline/pkg/config"
@@ -39,6 +42,20 @@ func (t *writeStdout) Write(in []config.GenericMap) {
3942
txt, _ := json.Marshal(v)
4043
fmt.Println(string(txt))
4144
}
45+
} else if t.format == "fields" {
46+
for _, v := range in {
47+
var order sort.StringSlice
48+
for fieldName := range v {
49+
order = append(order, fieldName)
50+
}
51+
order.Sort()
52+
w := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', 0)
53+
fmt.Fprintf(w, "\n\nFlow record at %s:\n", time.Now().Format(time.StampMilli))
54+
for _, field := range order {
55+
fmt.Fprintf(w, "%v\t=\t%v\n", field, v[field])
56+
}
57+
w.Flush()
58+
}
4259
} else {
4360
for _, v := range in {
4461
fmt.Printf("%s: %v\n", time.Now().Format(time.StampMilli), v)

0 commit comments

Comments
 (0)