2021-10-16 16:40:53 +00:00
|
|
|
// The fs module provides easy and simple access to filesystem functions and other
|
|
|
|
// things, and acts an addition to the Lua standard library's I/O and fs functions.
|
2021-03-20 22:49:15 +00:00
|
|
|
package fs
|
|
|
|
|
|
|
|
import (
|
2021-05-19 21:00:23 +00:00
|
|
|
"fmt"
|
2021-03-20 22:49:15 +00:00
|
|
|
"os"
|
2021-03-31 17:46:49 +00:00
|
|
|
"strings"
|
2021-03-20 22:49:15 +00:00
|
|
|
|
2021-10-16 16:40:53 +00:00
|
|
|
"hilbish/util"
|
2021-05-01 17:38:01 +00:00
|
|
|
"github.com/yuin/gopher-lua"
|
|
|
|
"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-10-16 16:40:53 +00:00
|
|
|
util.Document(L, mod, `The fs module provides easy and simple access to filesystem functions and other
|
|
|
|
things, and acts an addition to the Lua standard library's I/O and fs functions.`)
|
|
|
|
|
2021-04-28 11:26:23 +00:00
|
|
|
L.Push(mod)
|
|
|
|
return 1
|
2021-03-20 22:49:15 +00:00
|
|
|
}
|
|
|
|
|
2021-10-16 17:47:09 +00:00
|
|
|
func luaErr(L *lua.LState, msg string) {
|
|
|
|
L.Error(lua.LString(msg), 2)
|
2021-03-31 03:56:37 +00:00
|
|
|
}
|
|
|
|
|
2021-03-20 22:49:15 +00:00
|
|
|
var exports = map[string]lua.LGFunction{
|
2021-10-16 14:21:05 +00:00
|
|
|
"cd": fcd,
|
|
|
|
"mkdir": fmkdir,
|
|
|
|
"stat": fstat,
|
2021-10-16 17:47:39 +00:00
|
|
|
"readdir": freaddir,
|
2021-03-20 22:49:15 +00:00
|
|
|
}
|
|
|
|
|
2021-10-16 14:21:05 +00:00
|
|
|
// cd(dir)
|
|
|
|
// Changes directory to `dir`
|
|
|
|
func fcd(L *lua.LState) int {
|
2021-05-01 17:42:15 +00:00
|
|
|
path := L.CheckString(1)
|
2021-03-20 22:49:15 +00:00
|
|
|
|
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 {
|
2021-10-16 17:49:01 +00:00
|
|
|
e := err.(*os.PathError).Err.Error()
|
|
|
|
luaErr(L, e)
|
2021-03-31 03:56:37 +00:00
|
|
|
}
|
2021-03-20 22:49:15 +00:00
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2021-10-16 14:21:05 +00:00
|
|
|
// mkdir(name, recursive)
|
|
|
|
// Makes a directory called `name`. If `recursive` is true, it will create its parent directories.
|
|
|
|
func fmkdir(L *lua.LState) int {
|
2021-05-01 17:42:15 +00:00
|
|
|
dirname := L.CheckString(1)
|
2021-06-12 13:31:42 +00:00
|
|
|
recursive := L.ToBool(2)
|
|
|
|
path := strings.TrimSpace(dirname)
|
2021-10-16 17:49:01 +00:00
|
|
|
var err error
|
2021-04-09 22:10:18 +00:00
|
|
|
|
2021-06-12 13:31:42 +00:00
|
|
|
if recursive {
|
2021-10-16 17:49:01 +00:00
|
|
|
err = os.MkdirAll(path, 0744)
|
2021-06-12 13:31:42 +00:00
|
|
|
} else {
|
2021-10-16 17:49:01 +00:00
|
|
|
err = os.Mkdir(path, 0744)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
luaErr(L, err.Error())
|
2021-06-12 13:31:42 +00:00
|
|
|
}
|
2021-04-09 22:10:18 +00:00
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2021-10-16 14:21:05 +00:00
|
|
|
// stat(path)
|
|
|
|
// Returns info about `path`
|
|
|
|
func fstat(L *lua.LState) int {
|
2021-05-01 17:42:15 +00:00
|
|
|
path := L.CheckString(1)
|
2021-04-09 22:10:18 +00:00
|
|
|
|
|
|
|
// TODO: handle error here
|
2021-10-16 17:47:39 +00:00
|
|
|
pathinfo, err := os.Stat(path)
|
|
|
|
if err != nil {
|
|
|
|
luaErr(L, err.Error())
|
|
|
|
return 0
|
|
|
|
}
|
2021-04-09 22:10:18 +00:00
|
|
|
L.Push(luar.New(L, pathinfo))
|
|
|
|
|
|
|
|
return 1
|
|
|
|
}
|
2021-10-16 17:47:39 +00:00
|
|
|
|
|
|
|
// readdir(dir)
|
|
|
|
// Returns a table of files in `dir`
|
|
|
|
func freaddir(L *lua.LState) int {
|
|
|
|
dir := L.CheckString(1)
|
|
|
|
names := []string{}
|
|
|
|
|
|
|
|
dirEntries, err := os.ReadDir(dir)
|
|
|
|
if err != nil {
|
|
|
|
luaErr(L, err.Error())
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
for _, entry := range dirEntries {
|
|
|
|
names = append(names, entry.Name())
|
|
|
|
}
|
|
|
|
|
|
|
|
L.Push(luar.New(L, names))
|
|
|
|
|
|
|
|
return 1
|
|
|
|
}
|