-
Notifications
You must be signed in to change notification settings - Fork 0
/
render.go
262 lines (207 loc) · 5.57 KB
/
render.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
package main
import (
"errors"
"fmt"
"io"
"os"
"unicode"
"github.com/veigaribo/qveen/params"
"github.com/veigaribo/qveen/prompts"
"github.com/veigaribo/qveen/templates"
"github.com/veigaribo/qveen/utils"
)
type RenderOptions struct {
ParamsPath string
ParamsFormat string
TemplatePath string
OutputPath string
MetaKey string
PromptValues map[string]string
Overwrite bool
TemplateLeftDelim string
TemplateRightDelim string
TemplateCase string
}
func Render(opts RenderOptions) {
paramsReader, err := utils.OpenFileOrUrl(opts.ParamsPath)
if err != nil {
panic(fmt.Errorf("Failed to open parameter file: %w", err))
}
var paramsFormat params.ParamsFormat
switch opts.ParamsFormat {
case "toml":
paramsFormat = params.ParamsTomlFormat
case "yaml":
case "json":
paramsFormat = params.ParamsYamlFormat
case "":
maybeParamsFormat := params.GuessFormat(opts.ParamsPath)
if maybeParamsFormat == nil {
panic(fmt.Errorf("Could not guess params format from file name: %s", opts.ParamsPath))
}
paramsFormat = *maybeParamsFormat
default:
panic(fmt.Errorf("Unrecognized format '%s'", opts.ParamsFormat))
}
p, err := params.ParseParams(
paramsReader,
paramsFormat,
params.ParseParamsOptions{
MetaKey: opts.MetaKey,
},
)
if err != nil {
panic(fmt.Errorf("Failed to parse parameter file: %w", err))
}
if len(p.Pairs) == 0 {
// Nothing to do.
fmt.Fprintf(os.Stderr, "Nothing to do.\n")
return
}
templateLeftDelim := utils.FirstOf(
opts.TemplateLeftDelim,
p.TemplateLeftDelim,
)
templates.LeftDelim = templateLeftDelim
templateRightDelim := utils.FirstOf(
opts.TemplateRightDelim,
p.TemplateRightDelim,
)
templates.RightDelim = templateRightDelim
templateCase := utils.FirstOf(opts.TemplateCase, p.TemplateCase)
switch templateCase {
case "turkish":
templates.Case = unicode.TurkishCase
case "azeri":
templates.Case = unicode.AzeriCase
case "":
break
default:
panic(fmt.Errorf("Invalid value for `case`: '%s'. Expected 'turkish' or 'azeri' (or empty).", templateCase))
}
templates.Init()
p.ExpandPromptParams(opts.MetaKey)
for i := range p.Prompt {
prompt := &p.Prompt[i]
prefill, ok := opts.PromptValues[prompt.Name]
if ok {
err := prompt.TryPrefill(prefill)
if err != nil {
panic(fmt.Errorf("Failed to prefill prompt '%s' with '%s': %w", prompt.Name, prefill, err))
}
}
}
err = doPrompt(p.Prompt, p.Data)
if err != nil {
panic(fmt.Errorf("Failed to run prompts: %w", err))
}
err = p.ExpandParams(opts.MetaKey)
isSinglePair := len(p.Pairs) == 1
var templatePathFlag, outputPathFlag string
if isSinglePair {
if opts.TemplatePath != "" {
templatePathFlag, err = templates.ExpandString(
"--template",
opts.TemplatePath,
p.Data,
)
if err != nil {
panic(fmt.Errorf("Failed to expand template path: %w", err))
}
}
if opts.OutputPath != "" {
outputPathFlag, err = templates.ExpandString(
"--output",
opts.OutputPath,
p.Data,
)
if err != nil {
panic(fmt.Errorf("Failed to expand output path: %w", err))
}
}
} else {
if opts.TemplatePath != "" {
fmt.Fprintf(os.Stderr, "Ignoring template flag for multiple pairs.")
}
if opts.OutputPath != "" {
if utils.IsExplicitDir(opts.OutputPath) {
outputPathFlag = opts.OutputPath
} else {
fmt.Fprintf(os.Stderr, "Ignoring non-prefix output flag for multiple pairs.")
}
}
}
for i, pair := range p.Pairs {
templatePathParams := pair.Template
templatePath := utils.FirstOf(
templatePathFlag,
templatePathParams.Resolve(opts.ParamsPath),
)
if templatePath == "" {
if isSinglePair {
panic(errors.New("Missing template file path."))
} else {
panic(fmt.Errorf("Missing template file path for pair #%d.", i))
}
}
templateReader, err := utils.OpenFileOrUrl(templatePath)
if err != nil {
if isSinglePair {
panic(fmt.Errorf("Failed to open template file: %w", err))
} else {
panic(fmt.Errorf("Failed to open template file for pair #%d: %w", i, err))
}
}
templateData, err := io.ReadAll(templateReader)
if err != nil {
if isSinglePair {
panic(fmt.Errorf("Failed to read template file: %w", err))
} else {
panic(fmt.Errorf("Failed to read template file for pair #%d: %w", i, err))
}
}
t, err := templates.GetTemplate().Parse(string(templateData))
if err != nil {
panic(fmt.Errorf("Failed to parse template: %w", err))
}
var outputPathParams = pair.Output
var outputLoc OutputLocation
outputLoc.Add(outputPathParams.Resolve(opts.ParamsPath))
outputLoc.Add(outputPathFlag)
outputPath, err := outputLoc.Path()
if err != nil {
if isSinglePair {
panic(fmt.Errorf("Failed to generate output path: %w", err))
} else {
panic(fmt.Errorf("Failed to generate output path for pair #%d: %w", i, err))
}
}
output, err := utils.FileWriter(outputPath, opts.Overwrite)
if err != nil {
if isSinglePair {
panic(fmt.Errorf("Failed to create output file: %w", err))
} else {
panic(fmt.Errorf("Failed to create output file for pair #%d: %w", i, err))
}
}
err = t.Execute(output, templates.PrepareData(p.Data))
if err != nil {
if isSinglePair {
panic(fmt.Errorf("Failed to execute template: %w", err))
} else {
panic(fmt.Errorf("Failed to execute template for pair #%d: %w", i, err))
}
}
fmt.Fprintln(os.Stderr, i, templatePath, "->", outputPath)
}
}
func doPrompt(ps []prompts.Prompt, out map[string]any) error {
prompted, err := prompts.DoPrompt(ps)
if err != nil {
return err
}
for key, value := range prompted {
out[key] = value
}
return nil
}