/
custom.go
43 lines (34 loc) · 914 Bytes
/
custom.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
package formatters
import (
"io"
"os"
"text/template"
"github.com/mrtazz/checkmake/logger"
"github.com/mrtazz/checkmake/rules"
)
// CustomFormatter is a formatter that is configurable via a template string
type CustomFormatter struct {
out io.Writer
template *template.Template
}
// NewCustomFormatter returns a CustomFormatter struct
func NewCustomFormatter(templateString string) (ret *CustomFormatter, err error) {
var tmpl *template.Template
ret = &CustomFormatter{}
tmpl, err = template.New("CustomFormatter").Parse(templateString)
if err != nil {
return ret, err
}
ret.template = tmpl
ret.out = os.Stdout
return
}
// Format is the function to call to get the formatted output
func (f *CustomFormatter) Format(violations rules.RuleViolationList) {
for _, val := range violations {
err := f.template.Execute(f.out, val)
if err != nil {
logger.Error(err.Error())
}
}
}