-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
278 lines (246 loc) · 6.91 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
//go:build go1.15
// +build go1.15
package main
// Copyright Programmfabrik GmbH
// All Rights Reserved
// The gotmplx command wires up variables into a go template and renders it as output
import (
"encoding/json"
"fmt"
"html/template"
"io"
"io/ioutil"
"log"
"os"
"strings"
ttemplate "text/template"
"github.com/Masterminds/sprig/v3"
"github.com/programmfabrik/go-csvx"
"github.com/spf13/cobra"
"github.com/yudai/pp"
"gopkg.in/yaml.v3"
)
var (
version = "dev"
commit = "none-commit"
date = "2006-01-02 15:04:05Z07:00"
builtBy = "unknown"
rootCmd = &cobra.Command{
Use: "gotmplx TEMPLATE [PARTIAL_TEMPLATE]*",
Short: "gotmplx: Command line tool to render a go template",
Version: fmt.Sprintf("%s %s %v %s", version, commit, date, builtBy),
Example: "gotmplx --var some=something --csv one=example/sample1.csv example/sample1.txt example/partial_tpl_1.txt",
Run: render,
PreRun: parseVariables,
}
vars, csvs, ymls, jsons []string
eval, output string
templateEnvVariables, templateVariables, templateYML, templateJSON map[string]any
templateCSVVariables map[string][]map[string]any
dump, html bool
templateDelimLeft, templateDelimRight string
stdinBytes []byte
)
func init() {
rootCmd.Flags().StringArrayVarP(&vars, "var", "", []string{}, "Parse and use variable in template (--var myvar=value)")
rootCmd.Flags().StringArrayVarP(&csvs, "csv", "", []string{}, "Parse and use CSV file rows in template (--csv key=file)")
rootCmd.Flags().StringArrayVarP(&ymls, "yml", "", []string{}, "Parse and use YML file in template (--yml key=file)")
rootCmd.Flags().StringArrayVarP(&jsons, "json", "", []string{}, "Parse and use JSON file in template (--json key=file)")
rootCmd.Flags().BoolVarP(&dump, "dump", "d", false, "Pretty print data passed to template to stdout")
rootCmd.Flags().BoolVarP(&html, "html", "", false, "Render template as HTML (default is TEXT)")
rootCmd.Flags().StringVarP(&eval, "eval", "e", "", "Parse this text instead of file argument (--eval \"{{ .Var.myvar }}\"")
rootCmd.Flags().StringVarP(&output, "output", "o", "-", `Send output to file. Use for "-" (default)`)
rootCmd.Flags().StringVarP(&templateDelimLeft, "template-delim-left", "l", "", "Use this as left delimiter in go templates")
rootCmd.Flags().StringVarP(&templateDelimRight, "template-delim-right", "r", "", "Use this as right delimiter in go templates")
}
func main() {
err := rootCmd.Execute()
if err != nil {
fmt.Fprintln(rootCmd.OutOrStderr(), err)
os.Exit(1)
}
}
// stdin reads all bytes from stdin. if called more than once, the stdin bytes
// from the first call are returns
func stdin() []byte {
if stdinBytes == nil {
var err error
stdinBytes, err = ioutil.ReadAll(os.Stdin)
if err != nil {
log.Fatalf("Could not read stdin: %s", err.Error())
}
}
return stdinBytes
}
func parseVariables(cmd *cobra.Command, args []string) {
envStr := os.Environ()
templateEnvVariables = map[string]any{}
for _, v := range envStr {
key, value, ok := strings.Cut(v, "=")
if ok {
templateEnvVariables[key] = value
}
}
templateYML = map[string]any{}
for _, y := range ymls {
var (
ymlBytes []byte
err error
)
key, ymlFileName, ok := strings.Cut(y, "=")
if !ok {
log.Fatalf("Unable to split yml file %q", y)
}
if ymlFileName == "-" {
ymlBytes = stdin()
} else {
ymlBytes, err = ioutil.ReadFile(ymlFileName)
if err != nil {
log.Fatalf("Could not read yml file %q: %s", key, err.Error())
}
}
var d any
err = yaml.Unmarshal(ymlBytes, &d)
if err != nil {
log.Fatalf("Could not parse yml file %q: %s", ymlFileName, err.Error())
}
templateYML[key] = d
}
templateJSON = map[string]any{}
for _, j := range jsons {
var (
jsonBytes []byte
err error
)
key, jsonFileName, ok := strings.Cut(j, "=")
if !ok {
log.Fatalf("Unable to split json file %q", j)
}
if jsonFileName == "-" {
jsonBytes = stdin()
} else {
jsonBytes, err = ioutil.ReadFile(jsonFileName)
if err != nil {
log.Fatalf("Could not read json file %q: %s", key, err.Error())
}
}
var d any
err = json.Unmarshal(jsonBytes, &d)
if err != nil {
log.Fatalf("Could not parse json file %q: %s", jsonFileName, err.Error())
}
templateJSON[key] = d
}
templateCSVVariables = map[string][]map[string]any{}
for _, v := range csvs {
var (
csvBytes []byte
err error
)
key, csvFileName, ok := strings.Cut(v, "=")
if !ok {
log.Fatalf("Unable to split csv file %q", v)
}
if csvFileName == "-" {
csvBytes = stdin()
} else {
csvBytes, err = ioutil.ReadFile(csvFileName)
if err != nil {
log.Fatalf("Could not read csv file %q: %s", key, err.Error())
}
}
csvp := csvx.CSVParser{
Comma: ',',
Comment: '#',
TrimLeadingSpace: true,
SkipEmptyColumns: true,
}
templateCSVVariables[key], err = csvp.Typed(csvBytes)
if err != nil {
println(len(csvBytes))
log.Fatalf("Could not parse csv file %q: %s", csvFileName, err.Error())
}
}
templateVariables = map[string]any{}
for _, v := range vars {
key, value, ok := strings.Cut(v, "=")
if !ok {
log.Fatalf("Unable to split --var %q", v)
}
templateVariables[key] = value
}
}
func render(cmd *cobra.Command, args []string) {
if len(args) == 0 && eval == "" {
cmd.Usage()
os.Exit(1)
}
tplBytes := []byte{}
if eval != "" {
tplBytes = append(tplBytes, eval...)
}
for _, arg := range args {
if arg == "-" {
tplBytes = append(tplBytes, stdin()...)
} else {
fBytes, err := os.ReadFile(arg)
if err != nil {
log.Fatalf("Unable to read %q: %s", arg, err.Error())
}
tplBytes = append(tplBytes, fBytes...)
}
}
data := map[string]any{
"env": templateEnvVariables,
"var": templateVariables,
"csv": templateCSVVariables,
"yml": templateYML,
"json": templateJSON,
}
if dump {
pp.Println(data)
os.Exit(0)
}
var err error
// HTML rendering
if html {
tpl := template.
New("tmpl").
Funcs(sprig.FuncMap()).
Delims(templateDelimLeft, templateDelimRight)
if len(tplBytes) > 0 {
tpl, err = tpl.Parse(string(tplBytes))
if err != nil {
log.Fatalf("Could not parse template: %s", err.Error())
}
}
err = tpl.Execute(os.Stdout, data)
if err != nil {
log.Fatal(err.Error())
}
} else {
tpl := ttemplate.
New("tmpl").
Funcs(sprig.FuncMap()).
Delims(templateDelimLeft, templateDelimRight)
if len(tplBytes) > 0 {
tpl, err = tpl.Parse(string(tplBytes))
if err != nil {
log.Fatalf("Could not parse template: %s", err.Error())
}
}
var out io.Writer
if output != "-" {
out, err = os.Create(output)
if err != nil {
log.Fatalf("Unable to open %q for output: %s", output, err.Error())
}
} else {
out = os.Stdout
}
err = tpl.Execute(out, data)
if err != nil {
log.Fatal(err.Error())
}
}
}