-
Notifications
You must be signed in to change notification settings - Fork 20
/
line_processor.go
83 lines (71 loc) · 2.15 KB
/
line_processor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package funnel
import (
"bytes"
"fmt"
"io"
"text/template"
"time"
)
// LineProcessor interface is passed down to the consumer
// which just calls the write function
type LineProcessor interface {
Write(io.Writer, string) error
}
// GetLineProcessor function returns the particular processor depending
// on the config.
func GetLineProcessor(cfg *Config) LineProcessor {
// If no prepend value is needed, return no processor
if cfg.PrependValue == "" {
return &NoProcessor{}
}
t := template.Must(template.New("line").Parse(cfg.PrependValue))
// Check if there is a template action in the string
// If yes, return the template processor
if len(t.Tree.Root.Nodes) > 1 {
return &TemplateLineProcessor{template: t}
}
return &SimpleLineProcessor{prependStr: cfg.PrependValue}
}
// NoProcessor is used when there is no prepend value.
// It just prints the line without any other action
type NoProcessor struct {
}
func (*NoProcessor) Write(w io.Writer, line string) error {
_, err := fmt.Fprint(w, line)
return err
}
// SimpleLineProcessor is used when the prependValue is only a simple string
// It just concatenates the string with the line and prints it
type SimpleLineProcessor struct {
prependStr string
}
func (lp *SimpleLineProcessor) Write(w io.Writer, line string) error {
_, err := fmt.Fprint(w, lp.prependStr+line)
return err
}
// TemplateLineProcessor is used when there is a template action in the prependValue
// It parses the prependValue and store the template. Then for every write call,
// it executes the template and writes it
type TemplateLineProcessor struct {
template *template.Template
}
type templateData struct {
RFC822Timestamp string
ISO8601Timestamp string
UnixTimestamp int64
}
func (lp *TemplateLineProcessor) Write(w io.Writer, line string) error {
// Populating the template data struct
t := time.Now()
data := templateData{t.Format(time.RFC822), t.Format("2006-01-02T15:04:05Z0700"), t.UnixNano()}
var b bytes.Buffer
if err := lp.template.Execute(&b, data); err != nil {
return err
}
if _, err := fmt.Fprint(&b, line); err != nil {
return err
}
// Writing the buffer to io.Writer
_, err := b.WriteTo(w)
return err
}