-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
134 lines (114 loc) · 2.84 KB
/
main.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package main
import (
"bytes"
"errors"
"io"
"os"
"reflect"
"text/template"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
func main() {
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr, TimeFormat: "2006-01-02 15:04:05"})
olist, err := importObjects()
if err != nil {
log.Error().Msgf("error reading input: %v", err)
os.Exit(1)
}
var templates []entry
for _, fn := range args.Templates {
list, err := load(fn, true)
if err != nil {
log.Error().Msgf("error loading template '%s': %v", fn, err)
os.Exit(1)
}
templates = append(templates, list...)
}
var output bytes.Buffer
if args.Verbose {
log.Info().Msgf("generating %d objects using %d templates", len(olist), len(templates))
}
t, err := prepare(templates)
if err != nil {
log.Error().Msgf("prepare-template-error: %v", err)
os.Exit(1)
}
for _, o := range olist {
err = process(t, o, &output)
if err != nil {
var execErr template.ExecError
if errors.As(err, &execErr) {
log.Error().Str("name", execErr.Name).Msgf("template exec-error: %v", execErr)
} else {
log.Error().Msgf("template-error: %v", err)
}
os.Exit(1)
}
}
if args.Output == "" {
_, _ = os.Stdout.Write(output.Bytes())
} else {
file, err := os.Create(args.Output)
if err != nil {
log.Error().Msgf("unable to save output to '%s': %v", args.Output, err)
os.Exit(1)
}
_, _ = output.WriteTo(file)
_ = file.Close()
}
}
func prepare(list []entry) ([]*template.Template, error) {
if args.VeryVerbose {
log.Trace().Msgf("preparing %d template", len(list))
}
var tmpls []*template.Template
tmpl := template.New("base").Funcs(tmpl8funcs())
var err error
for _, e := range list {
if args.VeryVerbose {
log.Trace().Msgf("parsing template '%s'...", e.name)
}
tmpl, err = tmpl.New(e.name).Parse(e.data)
if err != nil {
return nil, err
}
tmpls = append(tmpls, tmpl)
}
return tmpls, nil
}
func process(tmpls []*template.Template, o interface{}, w io.Writer) error {
for _, tmpl := range tmpls {
if args.VeryVerbose {
log.Trace().Msgf("executing template '%s'...", tmpl.Name())
}
var buf bytes.Buffer
err := tmpl.Execute(&buf, o)
if err != nil {
return err
}
if !args.Raw && buf.Len() > 0 {
bytes := buf.Bytes()
if bytes[len(bytes)-1] != '\n' {
buf.WriteByte('\n')
}
}
_, _ = buf.WriteTo(w)
}
return nil
}
func interfaceSlice(slice interface{}) ([]interface{}, bool) {
s := reflect.ValueOf(slice)
if s.Kind() != reflect.Slice {
return nil, false
}
// Keep the distinction between nil and empty slice input
if s.IsNil() {
return nil, false
}
ret := make([]interface{}, s.Len())
for i := 0; i < s.Len(); i++ {
ret[i] = s.Index(i).Interface()
}
return ret, true
}