2021-03-20 22:49:15 +00:00
|
|
|
package fs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
2021-03-31 17:46:49 +00:00
|
|
|
"strings"
|
2021-03-20 22:49:15 +00:00
|
|
|
|
2021-04-28 11:26:23 +00:00
|
|
|
lua "github.com/yuin/gopher-lua"
|
|
|
|
luar "layeh.com/gopher-luar"
|
2021-03-20 22:49:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func Loader(L *lua.LState) int {
|
2021-04-28 11:26:23 +00:00
|
|
|
mod := L.SetFuncs(L.NewTable(), exports)
|
2021-03-20 22:49:15 +00:00
|
|
|
|
2021-04-28 11:26:23 +00:00
|
|
|
L.Push(mod)
|
|
|
|
return 1
|
2021-03-20 22:49:15 +00:00
|
|
|
}
|
|
|
|
|
2021-03-31 03:56:37 +00:00
|
|
|
func LuaErr(L *lua.LState, code int) {
|
2021-03-31 04:02:39 +00:00
|
|
|
// TODO: Error with a table, with path and error code
|
2021-03-31 11:32:47 +00:00
|
|
|
L.Error(lua.LNumber(code), 2)
|
2021-03-31 03:56:37 +00:00
|
|
|
}
|
|
|
|
|
2021-03-20 22:49:15 +00:00
|
|
|
var exports = map[string]lua.LGFunction{
|
2021-04-28 22:54:16 +00:00
|
|
|
"cd": cd,
|
2021-04-28 22:09:10 +00:00
|
|
|
"mkdir": mkdir,
|
2021-04-28 22:54:16 +00:00
|
|
|
"stat": stat,
|
2021-03-20 22:49:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func cd(L *lua.LState) int {
|
|
|
|
path := L.ToString(1)
|
|
|
|
|
2021-03-31 17:46:49 +00:00
|
|
|
err := os.Chdir(strings.TrimSpace(path))
|
2021-03-31 03:56:37 +00:00
|
|
|
if err != nil {
|
|
|
|
switch err.(*os.PathError).Err.Error() {
|
|
|
|
case "no such file or directory":
|
2021-03-31 11:32:47 +00:00
|
|
|
LuaErr(L, 1)
|
2021-03-31 03:56:37 +00:00
|
|
|
}
|
|
|
|
}
|
2021-03-20 22:49:15 +00:00
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2021-04-09 22:10:18 +00:00
|
|
|
func mkdir(L *lua.LState) int {
|
|
|
|
dirname := L.ToString(1)
|
|
|
|
|
|
|
|
// TODO: handle error here
|
|
|
|
os.Mkdir(strings.TrimSpace(dirname), 0744)
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func stat(L *lua.LState) int {
|
|
|
|
path := L.ToString(1)
|
|
|
|
|
|
|
|
// TODO: handle error here
|
|
|
|
pathinfo, _ := os.Stat(path)
|
|
|
|
L.Push(luar.New(L, pathinfo))
|
|
|
|
|
|
|
|
return 1
|
|
|
|
}
|