/
command.go
318 lines (290 loc) · 8.29 KB
/
command.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
// Copyright 2024 cloudeng llc. All rights reserved.
// Use of this source code is governed by the Apache-2.0
// license that can be found in the LICENSE file.
package runnercmd
import (
"context"
"fmt"
"io"
"os"
"text/template"
"cloudeng.io/cmdutil/cmdexec"
"cloudeng.io/cmdutil/subcmd"
"cloudeng.io/errors"
"cloudeng.io/sync/errgroup"
)
type T struct {
Datasources []string
CrawlCommands map[string][]string
ProcessCommands map[string][]string
IndexCommands map[string][]string
IndexStatsCommands map[string][]string
TestCacheCommands map[string][]string
AuthFiles map[string]string
GlobalExecOpts []cmdexec.Option
DatasourceConfigFile string
}
const cmdSpec = `name: runner
commands:
- name: crawl
summary: crawl a datasource, optionally processing the results
arguments:
- name - datasource
- name: crawl-all
summary: crawl all configured datasources concurrently
- name: index
summary: index a datasource
arguments:
- name - datasource
- name: index-all
summary: index all configured datasources concurrently
- name: crawl-index
summary: crawl and index a datasource
arguments:
- name - datasource
- name: crawl-index-all
summary: crawl and index all datasources
- name: show-all
summary: show all commands
- name: test-cache
summary: test the cache configuration for a datasource and display the first n items therein
arguments:
- name - datasource
- name: test-cache-all
summary: test the cache configuration for all datasources
- name: index-stats
summary: stats for indexed data for a given datasource, or for all datasources if none are specified on the command line
arguments:
- datasource... - datasource to display stats for
`
type CrawlFlags struct {
ProcessDownloads bool `subcmd:"process-downloads,true,'process downloaded files'"`
}
type IndexFlags struct {
IndexingDryRun bool `subcmd:"indexing-dry-run,false,'dry run only'"`
}
type CrawlIndexFlags struct {
CrawlFlags
IndexFlags
}
// CommandSpec represents a simple top-level command that can
// be added to a subcmd.CommandSetYAML.
type CommandSpec struct {
Name string
FlagValues any
Runner subcmd.Runner
}
func (c *T) Spec() (string, []CommandSpec) {
return cmdSpec, []CommandSpec{
{
Name: "crawl",
FlagValues: &CrawlFlags{},
Runner: c.Crawl,
},
{
Name: "crawl-all",
FlagValues: &CrawlFlags{},
Runner: c.CrawlAll,
},
{
Name: "index",
FlagValues: &IndexFlags{},
Runner: c.Index,
},
{
Name: "index-all",
FlagValues: &IndexFlags{},
Runner: c.IndexAll,
},
{
Name: "crawl-index",
FlagValues: &CrawlIndexFlags{},
Runner: c.CrawlIndex,
},
{
Name: "crawl-index-all",
FlagValues: &CrawlIndexFlags{},
Runner: c.CrawlIndexAll,
},
{
Name: "show-all",
FlagValues: &struct{}{},
Runner: c.ShowAll,
},
{
Name: "test-cache",
FlagValues: &struct{}{},
Runner: c.TestCache,
},
{
Name: "test-cache-all",
FlagValues: &struct{}{},
Runner: c.TestCacheAll,
},
{
Name: "index-stats",
FlagValues: &struct{}{},
Runner: c.IndexingStats,
},
}
}
func (c *T) NewRunnerOpts(datasource string, flags any) []cmdexec.Option {
opts := append([]cmdexec.Option{}, c.GlobalExecOpts...)
vars := TemplateVars{
DatasourceName: datasource,
DatasourceConfigFile: c.DatasourceConfigFile,
Flags: flags,
}
opts = append(opts,
cmdexec.WithTemplateFuncs(TemplateFuncs(c.AuthFiles)),
cmdexec.WithTemplateVars(vars))
return opts
}
func (c *T) NewRunner(datasource string, flags any) *cmdexec.Runner {
return cmdexec.New(datasource, c.NewRunnerOpts(datasource, flags)...)
}
// RunCommands runs the supplied commands for the specified datasource.
// Flags represents the flags made available as template variables.
func (c *T) RunCommands(ctx context.Context, datasource string, flags any, cmdsets ...map[string][]string) error {
for _, cmdmap := range cmdsets {
cmds, ok := cmdmap[datasource]
if !ok {
return fmt.Errorf("no commands for datasource: %v", datasource)
}
if err := c.NewRunner(datasource, flags).Run(ctx, cmds...); err != nil {
return err
}
}
return nil
}
func (c *T) Crawl(ctx context.Context, values interface{}, args []string) error {
fv := values.(*CrawlFlags)
return c.crawlAndProcess(ctx, fv, args[0])
}
func (c *T) crawlAndProcess(ctx context.Context, fv *CrawlFlags, datasource string) error {
if err := c.RunCommands(ctx, datasource, fv, c.CrawlCommands); err != nil {
return err
}
if !fv.ProcessDownloads || len(c.ProcessCommands[datasource]) == 0 {
return nil
}
return c.RunCommands(ctx, datasource, fv, c.ProcessCommands)
}
func (c *T) CrawlAll(ctx context.Context, values interface{}, _ []string) error {
fv := values.(*CrawlFlags)
var g errgroup.T
for _, ds := range c.Datasources {
ds := ds
g.Go(func() error {
return c.crawlAndProcess(ctx, fv, ds)
})
}
return g.Wait()
}
func (c *T) Index(ctx context.Context, values interface{}, args []string) error {
return c.RunCommands(ctx, args[0], values, c.IndexCommands)
}
func (c *T) IndexAll(ctx context.Context, values interface{}, _ []string) error {
var g errgroup.T
for _, ds := range c.Datasources {
ds := ds
g.Go(func() error {
return c.RunCommands(ctx, ds, values, c.IndexCommands)
})
}
return g.Wait()
}
func (c *T) CrawlIndex(ctx context.Context, values interface{}, args []string) error {
return c.RunCommands(ctx, args[0], values, c.CrawlCommands, c.IndexCommands)
}
func (c *T) CrawlIndexAll(ctx context.Context, values interface{}, _ []string) error {
fv := values.(*CrawlIndexFlags)
var g errgroup.T
for _, ds := range c.Datasources {
ds := ds
g.Go(func() error {
if err := c.crawlAndProcess(ctx, &fv.CrawlFlags, ds); err != nil {
return err
}
return c.RunCommands(ctx, ds, &fv.IndexFlags, c.IndexCommands)
})
}
return g.Wait()
}
func (c *T) TestCache(ctx context.Context, values interface{}, args []string) error {
return c.RunCommands(ctx, args[0], values, c.TestCacheCommands)
}
func (c *T) TestCacheAll(ctx context.Context, values interface{}, _ []string) error {
for _, ds := range c.Datasources {
if err := c.RunCommands(ctx, ds, values, c.TestCacheCommands); err != nil {
return err
}
}
return nil
}
func (c *T) formatCommands(out io.Writer, cmds map[string][]string) error {
for arg, cmd := range cmds {
if len(cmd) == 0 {
continue
}
cl, err := c.NewRunner("", struct{}{}).ExpandCommandLine(cmd...)
if err != nil {
return err
}
fmt.Fprintf(out, " %v: %v\n", arg, cl)
}
return nil
}
func (c *T) IndexingStats(ctx context.Context, values interface{}, args []string) error {
datasources := args
if len(datasources) == 0 {
datasources = c.Datasources
}
for _, ds := range datasources {
if err := c.RunCommands(ctx, ds, values, c.IndexStatsCommands); err != nil {
return err
}
}
return nil
}
func (c *T) ShowAll(_ context.Context, _ interface{}, _ []string) error {
fmt.Printf("Crawl commands\n")
var errs errors.M
err := c.formatCommands(os.Stdout, c.CrawlCommands)
errs.Append(err)
fmt.Printf("Crawl Process commands (second step in Crawl command)\n")
err = c.formatCommands(os.Stdout, c.ProcessCommands)
errs.Append(err)
fmt.Printf("Crawl Index commands\n")
err = c.formatCommands(os.Stdout, c.IndexCommands)
errs.Append(err)
return errs.Err()
}
func NewCmdSet(spec string, cmds []CommandSpec, globalFlagValues any) *subcmd.CommandSetYAML {
cmdSet := subcmd.MustFromYAMLTemplate(spec)
for _, c := range cmds {
cmdSet.Set(c.Name).MustRunner(c.Runner, c.FlagValues)
}
if globalFlagValues != nil {
globals := subcmd.NewFlagSet()
globals.MustRegisterFlagStruct(globalFlagValues, nil, nil)
cmdSet.WithGlobalFlags(globals)
}
return cmdSet
}
// TemplateFuncs returns a template.FuncMap that contains an AuthFile
// function that provides access to the supplied authFiles map.
func TemplateFuncs(authFiles map[string]string) template.FuncMap {
return template.FuncMap{
"AuthFile": func(a string) string {
return authFiles[a]
},
}
}
// TemplateVars represents the variables that can be accessed
// from templates.
type TemplateVars struct {
DatasourceName string
DatasourceConfigFile string
Flags any
}