-
Notifications
You must be signed in to change notification settings - Fork 7
/
cli.go
275 lines (232 loc) · 6.76 KB
/
cli.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
package main
import (
"encoding/json"
"errors"
"fmt"
"io"
"strings"
"text/template"
"github.com/BurntSushi/toml"
"github.com/Masterminds/sprig/v3"
"github.com/bluebrown/treasure-map/textfunc"
"github.com/spf13/pflag"
"gopkg.in/yaml.v3"
)
var version = "dev"
// the state of the program
type state struct {
// options
defaultTemplateName string
files []string
globs []string
templateName string
options []string
decoder decoder
noNewline bool
showVersion bool
// internal state
flagSet *pflag.FlagSet
template *template.Template
}
// create a new cli instance and bind flags to it
// flag.Parse is called on run
func new(fs *pflag.FlagSet) *state {
if fs == nil {
fs = pflag.CommandLine
}
cli := &state{
flagSet: fs,
decoder: decodeJson,
defaultTemplateName: "_gotpl_default",
}
fs.StringArrayVarP(&cli.files, "file", "f", cli.files, "template file path. Can be specified multiple times")
fs.StringArrayVarP(&cli.globs, "glob", "g", cli.globs, "template file glob. Can be specified multiple times")
fs.StringVarP(&cli.templateName, "name", "n", cli.templateName, "if specified, execute the template with the given name")
fs.VarP(&cli.decoder, "decoder", "d", "decoder to use for input data. Supported values: json, yaml, toml (default \"json\")")
fs.StringArrayVar(&cli.options, "option", cli.options, "option to pass to the template engine. Can be specified multiple times")
fs.BoolVar(&cli.noNewline, "no-newline", cli.noNewline, "do not print newline at the end of the output")
fs.BoolVar(&cli.showVersion, "version", cli.showVersion, "show version information and exit")
return cli
}
// parse the options and input, decode the input and render the result
func (cli *state) run(args []string, r io.Reader, w io.Writer) (err error) {
if err := cli.parse(args); err != nil {
return fmt.Errorf("parse: %w", err)
}
if cli.showVersion {
fmt.Fprintln(w, version)
return nil
}
data, err := cli.decode(r)
if err != nil {
return fmt.Errorf("decode: %w", err)
}
if err := cli.render(w, data); err != nil {
return fmt.Errorf("render: %w", err)
}
return nil
}
// parse options and args / files into templates. this method builds up the
// state of the program. It revolves primarily around the parsed templates. The
// args are parsed in a specific order:
// 1. parse the options into the flagset
// 2. parse positional arguments into the default template
// 3. parse files and globs in the order specified
func (cli *state) parse(rawArgs []string) error {
if err := cli.parseFlagset(rawArgs); err != nil {
return fmt.Errorf("parse raw args: %s", err)
}
if err := cli.parsePositional(); err != nil {
return fmt.Errorf("parse pos args: %w", err)
}
if _, err := cli.parseFilesAndGlobs(rawArgs); err != nil {
return fmt.Errorf("parse opt args: %w", err)
}
return nil
}
func (cli *state) parseFlagset(rawArgs []string) error {
cli.flagSet.SortFlags = false
if err := cli.flagSet.Parse(rawArgs); err != nil {
return err
}
cli.template = baseTemplate(cli.defaultTemplateName, cli.options...)
return nil
}
// parse all positional arguments into the "default" template. should be called
// after parseFlagset
func (cli *state) parsePositional() (err error) {
for _, arg := range cli.flagSet.Args() {
cli.template, err = cli.template.Parse(arg)
if err != nil {
return fmt.Errorf("parse template: %v", err)
}
}
return nil
}
// parse files and globs in the order they were specified, to align with go's
// template engine. should be called after parseFlagset
func (cli *state) parseFilesAndGlobs(rawArgs []string) (*template.Template, error) {
var (
err error
fileIndex uint8
globIndex uint8
)
for _, arg := range rawArgs {
if strings.HasPrefix(arg, "-f") || strings.HasPrefix(arg, "--file") {
file := cli.files[fileIndex]
cli.template, err = cli.template.ParseFiles(file)
if err != nil {
err = fmt.Errorf("error parsing file %s: %v", file, err)
break
}
fileIndex++
} else if strings.HasPrefix(arg, "-g") || strings.HasPrefix(arg, "--glob") {
glob := cli.globs[globIndex]
cli.template, err = cli.template.ParseGlob(glob)
if err != nil {
err = fmt.Errorf("error parsing glob %s: %v", glob, err)
break
}
globIndex++
}
}
return cli.template, err
}
// decode the input stream into context data
func (cli *state) decode(r io.Reader) (any, error) {
if r == nil || cli.decoder == nil {
return nil, nil
}
var data any
err := cli.decoder(r, &data)
return data, err
}
type decoder func(in io.Reader, out any) error
func (dec decoder) String() string { return "" }
func (dec *decoder) Type() string { return "func" }
func (dec *decoder) Set(kind string) error {
switch kind {
case "json":
*dec = decodeJson
case "yaml":
*dec = decodeYaml
case "toml":
*dec = decodeToml
default:
return fmt.Errorf("unsupported decoder %q", kind)
}
return nil
}
func decodeYaml(in io.Reader, out any) error {
dec := yaml.NewDecoder(in)
for {
err := dec.Decode(out)
if err != nil {
if err == io.EOF {
break
}
return err
}
}
return nil
}
func decodeToml(in io.Reader, out any) error {
dec := toml.NewDecoder(in)
_, err := dec.Decode(out)
return err
}
func decodeJson(in io.Reader, out any) error {
dec := json.NewDecoder(in)
for {
err := dec.Decode(out)
if err != nil {
if err == io.EOF {
break
}
return err
}
}
return nil
}
// render a template
func (cli *state) render(w io.Writer, data any) error {
templateName, err := cli.selectTemplate()
if err != nil {
return fmt.Errorf("select template: %w", err)
}
if err := cli.template.ExecuteTemplate(w, templateName, data); err != nil {
return fmt.Errorf("execute template: %v", err)
}
if !cli.noNewline {
fmt.Fprintln(w)
}
return nil
}
// determine the template to execute. In the order of precedence:
// 1. current name, if set
// 2. default name, if at least 1 positional arg
// 3. templates name, if exactly 1 template
// 4. --name flag required, otherwise
func (cli *state) selectTemplate() (string, error) {
templates := cli.template.Templates()
if len(templates) == 0 {
return "", errors.New("no templates found")
}
if cli.templateName != "" {
return cli.templateName, nil
}
if len(cli.flagSet.Args()) > 0 {
return cli.defaultTemplateName, nil
}
if len(templates) == 1 {
return templates[0].Name(), nil
}
return "", fmt.Errorf("the --name flag is required when multiple templates are defined and no default template exists")
}
// construct a base templates with custom functions attached
func baseTemplate(defaultName string, options ...string) *template.Template {
tpl := template.New(defaultName)
tpl = tpl.Option(options...)
tpl = tpl.Funcs(textfunc.MapClosure(sprig.TxtFuncMap(), tpl))
return tpl
}