-
Notifications
You must be signed in to change notification settings - Fork 0
/
glob_lua.go
87 lines (70 loc) · 1.66 KB
/
glob_lua.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
package file
import (
"fmt"
"github.com/vela-ssoc/vela-kit/pipe"
"github.com/vela-ssoc/vela-kit/lua"
)
func newFileGlob(L *lua.LState) *glob {
gl := &glob{
co: xEnv.Clone(L),
pipe: pipe.New(pipe.Env(xEnv)),
}
n := L.GetTop()
if n == 0 {
return gl
}
for i := 1; i <= n; i++ {
gl.append(L.CheckString(i))
}
return gl
}
func (gl *glob) String() string { return fmt.Sprintf("%p", gl) }
func (gl *glob) Type() lua.LValueType { return lua.LTObject }
func (gl *glob) AssertFloat64() (float64, bool) { return 0, false }
func (gl *glob) AssertString() (string, bool) { return "", false }
func (gl *glob) AssertFunction() (*lua.LFunction, bool) { return nil, false }
func (gl *glob) Peek() lua.LValue { return gl }
func (gl *glob) append(pattern string) {
gl.patterns = append(gl.patterns, pattern)
}
func (gl *glob) pipeL(L *lua.LState) int {
gl.pipe.CheckMany(L, pipe.Seek(0))
return 0
}
func (gl *glob) runL(L *lua.LState) int {
gl.run()
return 0
}
func (gl *glob) wrapL(L *lua.LState) int {
err := gl.err.Wrap()
if err != nil {
L.Push(lua.S2L(err.Error()))
} else {
L.Push(lua.LNil)
}
return 1
}
func (gl *glob) r() lua.LValue {
rn := len(gl.result)
if rn == 0 {
return lua.Slice{}
}
rv := lua.Slice{}
for i := 0; i < rn; i++ {
rv = append(rv, lua.S2L(gl.result[i]))
}
return rv
}
func (gl *glob) Index(L *lua.LState, key string) lua.LValue {
switch key {
case "pipe":
return lua.NewFunction(gl.pipeL)
case "run":
return lua.NewFunction(gl.runL)
case "wrap":
return lua.NewFunction(gl.wrapL)
case "result":
return gl.r()
}
return lua.LNil
}