/
session.go
62 lines (53 loc) · 1.05 KB
/
session.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
package lua
import (
"fmt"
"os"
"path/filepath"
"sync"
lua "github.com/yuin/gopher-lua"
)
type session struct {
sync.Mutex
history []string
}
func newSession(state *lua.LState) *session {
s := &session{
history: []string{},
}
fns := map[string]lua.LGFunction{
"reset": s.reset,
"save": s.save,
}
state.RegisterModule("session", fns)
return s
}
func (s *session) addLine(l string) {
s.Lock()
s.history = append(s.history, l)
s.Unlock()
}
func (s *session) save(state *lua.LState) int {
s.Lock()
defer s.Unlock()
name := state.CheckString(1)
abs, err := filepath.Abs(name)
if err != nil {
state.RaiseError("absolute path %s: %s", name, err)
}
f, err := os.OpenFile(abs, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0660)
if err != nil {
state.RaiseError("open %s: %s", abs, err)
}
defer f.Close()
for _, l := range s.history[:len(s.history)-1] {
f.WriteString(l + "\n")
}
fmt.Printf("Session written to %s\n", abs)
return 0
}
func (s *session) reset(state *lua.LState) int {
s.Lock()
s.history = []string{}
s.Unlock()
return 0
}