-
Notifications
You must be signed in to change notification settings - Fork 0
/
renderer.go
150 lines (129 loc) · 3.34 KB
/
renderer.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package main
import (
"errors"
"fmt"
"log"
"os"
"path"
"path/filepath"
"sort"
"strings"
"text/template"
)
// Renderer will render a set of inputs.
type Renderer struct {
FuncMap template.FuncMap
Inputs []string
PreloadFiles []string
StopOnError bool
}
// Execute applies a dataset against all inputs and writes output.
func (r *Renderer) Execute(out string, values map[string]interface{}) error {
return r.execute(r.Inputs, out, values)
}
func (r *Renderer) execute(inputs []string, out string, values map[string]interface{}) error {
// Do not order inputs, which may have been provided in a specific order
// from the command line
for _, fn := range inputs {
f, err := os.Open(fn)
if err != nil {
return err
}
fi, err := f.Stat()
if err != nil {
return err
}
// Render files directly
if !fi.IsDir() {
withPreloads := make([]string, 0)
for _, lib := range r.PreloadFiles {
withPreloads = append(withPreloads, lib)
}
withPreloads = append(withPreloads, fn)
err := r.render(values, withPreloads, r.getOutputPath(out, path.Base(fn)))
if err != nil {
return err
}
continue
}
// Loop through each file in a directory and render it
eis, err := f.Readdirnames(-1)
if err != nil {
return err
}
// Pluck out absolute path names; unlike inputs, these are safe to sort,
// because they were generated values
names := stringSorter{}
for _, ei := range eis {
names = append(names, filepath.Join(f.Name(), ei))
}
sort.Sort(names)
outpath := out
if strings.HasSuffix(out, "/") {
outpath = outpath + path.Base(f.Name()) + "/"
}
err = r.execute(names, outpath, values)
if err != nil {
return err
}
}
return nil
}
func (r *Renderer) getOutputPath(base, fn string) string {
if base == "" || base == "-" {
return "-"
}
if strings.HasSuffix(fn, ".tpl") {
fn = strings.TrimSuffix(fn, ".tpl")
} else if strings.HasSuffix(fn, ".tmpl") {
fn = strings.TrimSuffix(fn, ".tmpl")
}
if strings.HasSuffix(base, "/") {
return filepath.Join(base, fn)
}
if f, err := os.Open(base); err == nil {
if fi, err := f.Stat(); err == nil {
if fi.IsDir() {
return filepath.Join(base, fn)
}
}
}
return base
}
func (r *Renderer) render(values map[string]interface{}, inames []string, oname string) error {
if oname == "" {
return errors.New("Output name cannot be blank")
}
var out *os.File
var err error
if oname == "-" {
out = os.Stdout
log.Printf("Rendering [%s] to STDOUT\n", strings.Join(inames, ", "))
} else {
if strings.Contains(oname, "/") {
if err := os.MkdirAll(path.Dir(oname), 0755); err != nil {
return fmt.Errorf("Error creating directory for %q: %v", oname, err)
}
}
out, err = os.OpenFile(oname, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)
if err != nil {
return fmt.Errorf("Cannot open output file %q: %v", oname, err)
}
log.Printf("Rendering [%s] into %s\n", strings.Join(inames, ", "), oname)
defer func() { out.Sync(); out.Close() }()
}
tpl := template.New(filepath.Base(inames[len(inames)-1]))
if r.FuncMap != nil {
tpl.Funcs(r.FuncMap)
}
_, err = tpl.ParseFiles(inames...)
if err != nil {
return fmt.Errorf("Cannot parse templates [%s]: %v", strings.Join(inames, ", "), err)
}
if r.StopOnError {
tpl.Option("missingkey=error")
} else {
tpl.Option("missingkey=zero")
}
return tpl.Execute(out, values)
}