Hilbish/golibs/fs/fs.go

61 lines
953 B
Go
Raw Normal View History

package fs
import (
"os"
2021-03-31 17:46:49 +00:00
"strings"
"github.com/yuin/gopher-lua"
"layeh.com/gopher-luar"
)
func Loader(L *lua.LState) int {
2021-04-28 11:26:23 +00:00
mod := L.SetFuncs(L.NewTable(), exports)
2021-04-28 11:26:23 +00:00
L.Push(mod)
return 1
}
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
L.Error(lua.LNumber(code), 2)
2021-03-31 03:56:37 +00:00
}
var exports = map[string]lua.LGFunction{
"cd": cd,
2021-04-28 22:09:10 +00:00
"mkdir": mkdir,
"stat": stat,
}
func cd(L *lua.LState) int {
path := L.CheckString(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":
LuaErr(L, 1)
2021-03-31 03:56:37 +00:00
}
}
return 0
}
func mkdir(L *lua.LState) int {
dirname := L.CheckString(1)
// TODO: handle error here
os.Mkdir(strings.TrimSpace(dirname), 0744)
return 0
}
func stat(L *lua.LState) int {
path := L.CheckString(1)
// TODO: handle error here
pathinfo, _ := os.Stat(path)
L.Push(luar.New(L, pathinfo))
return 1
}