feat: add fs.readdir function

it takes 1 argument: the directory to read.
pull/78/head
sammyette 2021-10-16 13:47:39 -04:00
parent 452335d84a
commit 539a39f83a
No known key found for this signature in database
GPG Key ID: 50EE40A2809851F5
1 changed files with 26 additions and 1 deletions

View File

@ -30,6 +30,7 @@ var exports = map[string]lua.LGFunction{
"cd": fcd,
"mkdir": fmkdir,
"stat": fstat,
"readdir": freaddir,
}
// cd(dir)
@ -77,8 +78,32 @@ func fstat(L *lua.LState) int {
path := L.CheckString(1)
// TODO: handle error here
pathinfo, _ := os.Stat(path)
pathinfo, err := os.Stat(path)
if err != nil {
luaErr(L, err.Error())
return 0
}
L.Push(luar.New(L, pathinfo))
return 1
}
// 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
}