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-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-04-28 11:26:23 +00:00
L . Push ( mod )
return 1
2021-03-20 22:49:15 +00:00
}
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 {
2021-04-28 22:54:16 +00:00
"cd" : cd ,
2021-04-28 22:09:10 +00:00
"mkdir" : mkdir ,
2021-04-28 22:54:16 +00:00
"stat" : stat ,
2021-03-20 22:49:15 +00:00
}
func cd ( 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-05-19 21:00:23 +00:00
switch e := err . ( * os . PathError ) . Err . Error ( ) ; e {
2021-03-31 03:56:37 +00:00
case "no such file or directory" :
2021-03-31 11:32:47 +00:00
LuaErr ( L , 1 )
2021-05-19 21:00:23 +00:00
default :
fmt . Printf ( "Found unhandled error case: %s" , e )
fmt . Printf ( "Report this at https://github.com/Hilbis/Hilbish/issues with the title being: \"fs: unahndled error case %s\", and show what caused it.\n" , e )
LuaErr ( L , 213 )
2021-03-31 03:56:37 +00:00
}
}
2021-03-20 22:49:15 +00:00
return 0
}
2021-04-09 22:10:18 +00:00
func mkdir ( L * lua . LState ) int {
2021-05-01 17:42:15 +00:00
dirname := L . CheckString ( 1 )
2021-04-09 22:10:18 +00:00
// TODO: handle error here
os . Mkdir ( strings . TrimSpace ( dirname ) , 0744 )
return 0
}
func stat ( 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
pathinfo , _ := os . Stat ( path )
L . Push ( luar . New ( L , pathinfo ) )
return 1
}