-
-
Notifications
You must be signed in to change notification settings - Fork 36
/
adhoc.go
183 lines (155 loc) · 3.67 KB
/
adhoc.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
package gen
import (
"fmt"
"strings"
"cuelang.org/go/cue"
"github.com/hofstadter-io/hof/lib/templates"
)
// parsed version of the --template flag
// semicolon separated: <filepath>:<?cuepath>@<schema>;[]<?outpath>
// each extra section is
type AdhocTemplateConfig struct {
// Template filepath
Filepath string
// CUE path to input value within global value
Cuepath string
// CUE path to schema value within global value
Schema string
// Filepath to write results, possibly templated
Outpath string
// Is this a repeated template
Repeated bool
}
func (R *Runtime) CreateAdhocGenerator() error {
if R.Verbosity > 1 {
fmt.Println("Creating Adhoc Generator")
}
// parse template flags
tcfgs := []AdhocTemplateConfig{}
globs := make([]string,0)
for _, tf := range R.Flagpole.Template {
cfg, err := parseTemplateFlag(tf)
if err != nil {
return err
}
if R.Verbosity > 2 {
fmt.Printf("%s -> %#v\n", tf, cfg)
}
tcfgs = append(tcfgs, cfg)
globs = append(globs, cfg.Filepath)
}
G := NewGenerator("AdhocGen", R.CueRuntime.CueValue)
G.UseDiff3 = R.Flagpole.Diff3
G.Outdir = "./"
G.Templates = []*TemplateGlobs{ &TemplateGlobs{Globs: globs} }
G.Partials = []*TemplateGlobs{ &TemplateGlobs{Globs: R.Flagpole.Partial} }
Val := R.CueRuntime.CueValue
stdout := 0
for _, cfg := range tcfgs {
val := Val
if cfg.Cuepath != "" {
val = val.LookupPath(cue.ParsePath(cfg.Cuepath))
}
if cfg.Schema != "" {
schema := Val.LookupPath(cue.ParsePath(cfg.Schema))
val = val.Unify(schema)
}
addFile := func(val cue.Value) (err error) {
f := new(File)
f.TemplatePath = cfg.Filepath
err = val.Decode(&f.In)
if err != nil {
return err
}
// Set output filepath, always render as a template
op := cfg.Outpath
if op == "" {
op = fmt.Sprintf("hof-stdout-%d", stdout)
stdout += 1
}
ft, err := templates.CreateFromString("outpath", op, nil)
if err != nil {
return err
}
bs, err := ft.Render(f.In)
if err != nil {
return err
}
f.Filepath = string(bs)
G.Out = append(G.Out, f)
return nil
}
if cfg.Repeated {
// check if val is a list
if iter, ierr := val.List(); ierr == nil {
for iter.Next() {
v := iter.Value()
err := addFile(v)
if err != nil {
return err
}
}
// check if val is a struct
} else if iter, ierr := val.Fields(); ierr == nil {
for iter.Next() {
v := iter.Value()
err := addFile(v)
if err != nil {
return err
}
}
} else {
return fmt.Errorf("repeated template value is not iterable")
}
} else {
err := addFile(val)
if err != nil {
return err
}
}
}
errs := G.Initialize()
if len(errs) > 0 {
fmt.Println(errs)
return fmt.Errorf("while initializing adhoc generator")
}
if R.Verbosity > 2 {
fmt.Printf("G: %#v\n", G)
}
R.Generators["AdhocGen"] = G
return nil
}
// deconstructs the flag into struct
// semicolon separated: <filepath>:<?cuepath>@<schema>;<?outpath>
func parseTemplateFlag(tf string) (cfg AdhocTemplateConfig, err error) {
// look for ;
parts := strings.Split(tf, ";")
if len(parts) > 1 {
tf = parts[0]
cfg.Outpath = parts[1]
}
// repeated template?
if strings.HasPrefix(cfg.Outpath, "[]") {
cfg.Outpath = strings.TrimPrefix(cfg.Outpath, "[]")
cfg.Repeated = true
}
// look for @
parts = strings.Split(tf, "@")
if len(parts) > 1 {
tf = parts[0]
cfg.Schema = parts[1]
}
// look for :
parts = strings.Split(tf, ":")
if len(parts) > 1 {
tf = parts[0]
cfg.Cuepath = parts[1]
// '.' is an alias for "" or the root value
if cfg.Cuepath == "." {
cfg.Cuepath = ""
}
}
// should only have template path left
cfg.Filepath = tf
return cfg, nil
}