Hilbish/golibs/fs/fs.go

135 lines
3.0 KiB
Go
Raw Normal View History

package fs
import (
"strconv"
"os"
2021-03-31 17:46:49 +00:00
"strings"
"hilbish/util"
2022-03-29 12:52:26 +00:00
2022-03-28 23:36:06 +00:00
rt "github.com/arnodel/golua/runtime"
"github.com/arnodel/golua/lib/packagelib"
)
2022-03-28 23:36:06 +00:00
var Loader = packagelib.Loader{
Load: loaderFunc,
2022-03-28 23:36:06 +00:00
Name: "fs",
}
func loaderFunc(rtm *rt.Runtime) (rt.Value, func()) {
2022-03-28 23:36:06 +00:00
exports := map[string]util.LuaExport{
"cd": util.LuaExport{fcd, 1, false},
"mkdir": util.LuaExport{fmkdir, 2, false},
"stat": util.LuaExport{fstat, 1, false},
"readdir": util.LuaExport{freaddir, 1, false},
}
mod := rt.NewTable()
util.SetExports(rtm, mod, exports)
2022-03-31 00:11:15 +00:00
util.Document(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.`)
2022-03-28 23:36:06 +00:00
return rt.TableValue(mod), nil
}
// cd(dir)
// Changes directory to `dir`
// --- @param dir string
2022-03-28 23:36:06 +00:00
func fcd(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
if err := c.Check1Arg(); err != nil {
return nil, err
}
path, err := c.StringArg(0)
if err != nil {
return nil, err
}
2022-03-28 23:36:06 +00:00
err = os.Chdir(strings.TrimSpace(path))
2021-03-31 03:56:37 +00:00
if err != nil {
2022-03-28 23:36:06 +00:00
return nil, err
2021-03-31 03:56:37 +00:00
}
2022-03-28 23:36:06 +00:00
return c.Next(), err
}
// mkdir(name, recursive)
// Makes a directory called `name`. If `recursive` is true, it will create its parent directories.
// --- @param name string
2022-02-25 22:17:04 +00:00
// --- @param recursive boolean
2022-03-28 23:36:06 +00:00
func fmkdir(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
if err := c.CheckNArgs(2); err != nil {
return nil, err
}
dirname, err := c.StringArg(0)
if err != nil {
return nil, err
}
recursive, err := c.BoolArg(1)
if err != nil {
return nil, err
}
path := strings.TrimSpace(dirname)
if recursive {
err = os.MkdirAll(path, 0744)
} else {
err = os.Mkdir(path, 0744)
}
if err != nil {
2022-03-28 23:36:06 +00:00
return nil, err
}
2022-03-28 23:36:06 +00:00
return c.Next(), err
}
// stat(path)
// Returns info about `path`
// --- @param path string
2022-03-28 23:36:06 +00:00
func fstat(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
if err := c.Check1Arg(); err != nil {
return nil, err
}
path, err := c.StringArg(0)
if err != nil {
return nil, err
}
pathinfo, err := os.Stat(path)
if err != nil {
2022-03-28 23:36:06 +00:00
return nil, err
}
2022-03-28 23:36:06 +00:00
statTbl := rt.NewTable()
statTbl.Set(rt.StringValue("name"), rt.StringValue(pathinfo.Name()))
statTbl.Set(rt.StringValue("size"), rt.IntValue(pathinfo.Size()))
statTbl.Set(rt.StringValue("mode"), rt.StringValue("0" + strconv.FormatInt(int64(pathinfo.Mode().Perm()), 8)))
statTbl.Set(rt.StringValue("isDir"), rt.BoolValue(pathinfo.IsDir()))
return c.PushingNext1(t.Runtime, rt.TableValue(statTbl)), nil
}
// readdir(dir)
// Returns a table of files in `dir`
// --- @param dir string
// --- @return table
2022-03-28 23:36:06 +00:00
func freaddir(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
if err := c.Check1Arg(); err != nil {
return nil, err
}
dir, err := c.StringArg(0)
if err != nil {
return nil, err
}
names := rt.NewTable()
dirEntries, err := os.ReadDir(dir)
if err != nil {
2022-03-28 23:36:06 +00:00
return nil, err
}
2022-03-28 23:36:06 +00:00
for i, entry := range dirEntries {
names.Set(rt.IntValue(int64(i + 1)), rt.StringValue(entry.Name()))
}
2022-03-28 23:36:06 +00:00
return c.PushingNext1(t.Runtime, rt.TableValue(names)), nil
}