-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_lua.go
66 lines (54 loc) · 1.09 KB
/
file_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
package file
import (
"github.com/vela-ssoc/vela-kit/auxlib"
"github.com/vela-ssoc/vela-kit/lua"
"os"
"time"
)
func (xf *xFile) pushL(L *lua.LState) int {
n := L.GetTop()
if n == 0 {
L.Push(lua.LNil)
return 0
}
for i := 1; i <= n; i++ {
xf.Push(auxlib.Format(L, 0))
}
return 0
}
func (xf *xFile) backupL(L *lua.LState) int {
filename := xf.filename(time.Now())
fd, err := os.OpenFile(filename, os.O_CREATE|os.O_RDWR|os.O_APPEND, os.ModeAppend|os.ModePerm)
if err != nil {
xEnv.Errorf("file backup fail %v", err)
return 0
}
old := xf.fd
defer old.Close()
xf.fd = fd
return 0
}
func (xf *xFile) dayL(L *lua.LState) int {
format := L.IsString(1)
if format == "" {
format = "2006-01-02"
}
return 0
}
func (xf *xFile) startL(L *lua.LState) int {
xEnv.Start(L, xf).From(L.CodeVM()).Do()
return 0
}
func (xf *xFile) Index(L *lua.LState, key string) lua.LValue {
switch key {
case "push":
return lua.NewFunction(xf.pushL)
case "backup":
return lua.NewFunction(xf.backupL)
case "day":
return lua.NewFunction(xf.dayL)
default:
return lua.LNil
}
return lua.LNil
}