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
|
|
|
|
|
|
|
"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) {
|
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{
|
|
|
|
"cd": cd,
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|