Hilbish/golibs/fs/fs.go

39 lines
513 B
Go
Raw Normal View History

package fs
import (
"os"
"github.com/yuin/gopher-lua"
)
func Loader(L *lua.LState) int {
mod := L.SetFuncs(L.NewTable(), exports)
L.Push(mod)
return 1
}
2021-03-31 03:56:37 +00:00
func LuaErr(L *lua.LState, code int) {
L.Error(lua.LNumber(code), 1)
}
var exports = map[string]lua.LGFunction{
"cd": cd,
}
func cd(L *lua.LState) int {
path := L.ToString(1)
2021-03-31 03:56:37 +00:00
err := os.Chdir(path)
if err != nil {
switch err.(*os.PathError).Err.Error() {
case "no such file or directory":
2021-03-31 03:57:13 +00:00
LuaErr(L, 2)
2021-03-31 03:56:37 +00:00
}
}
return 0
}