forked from hofstadter-io/hof
-
Notifications
You must be signed in to change notification settings - Fork 0
/
glob.go
61 lines (46 loc) · 1.07 KB
/
glob.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
package os
import (
"cuelang.org/go/cue"
hofcontext "github.com/hofstadter-io/hof/flow/context"
"github.com/hofstadter-io/hof/lib/yagu"
)
type Glob struct{}
func NewGlob(val cue.Value) (hofcontext.Runner, error) {
return &FileLock{}, nil
}
func (T *Glob) Run(ctx *hofcontext.Context) (interface{}, error) {
val := ctx.Value
patterns, err := extractGlobConfig(ctx, val)
if err != nil {
return nil, err
}
filepaths, err := yagu.FilesFromGlobs(patterns)
if err != nil {
return nil, err
}
return map[string]interface{}{"filepaths": filepaths}, nil
}
func extractGlobConfig(ctx *hofcontext.Context, val cue.Value) (patterns []string, err error) {
ctx.CUELock.Lock()
defer ctx.CUELock.Unlock()
ps := val.LookupPath(cue.ParsePath("globs"))
if ps.Err() != nil {
return nil, ps.Err()
}
iter, err := ps.List()
if err != nil {
return nil, err
}
for iter.Next() {
gv := iter.Value()
if gv.Err() != nil {
return nil, gv.Err()
}
gs, err := gv.String()
if err != nil {
return nil, err
}
patterns = append(patterns, gs)
}
return patterns, nil
}