added lua functions from old version of packets

This commit is contained in:
2025-10-31 21:31:12 -03:00
parent b15d847fd2
commit acf00bc5f8
2 changed files with 215 additions and 0 deletions

View File

@@ -0,0 +1,120 @@
package lua
import (
"log"
"os"
"path/filepath"
lua "github.com/yuin/gopher-lua"
)
func LRemove(L *lua.LState) int {
filename := L.CheckString(1)
err := os.RemoveAll(filename)
if err != nil {
L.Push(lua.LFalse)
L.Push(lua.LString(err.Error()))
return 2
}
L.Push(lua.LTrue)
L.Push(lua.LNil)
return 2
}
func LRename(L *lua.LState) int {
oldname := L.CheckString(1)
newname := L.CheckString(2)
if err := os.Rename(oldname, newname); err != nil {
L.Push(lua.LFalse)
L.Push(lua.LString(err.Error()))
return 2
}
L.Push(lua.LTrue)
return 1
}
func LCopy(L *lua.LState) int {
oldname := L.CheckString(1)
newname := L.CheckString(2)
if err := copyDir(oldname, newname); err != nil {
L.Push(lua.LFalse)
L.Push(lua.LString(err.Error()))
return 2
}
L.Push(lua.LTrue)
L.Push(lua.LNil)
return 2
}
func LSymlink(L *lua.LState) int {
fileName := L.CheckString(1)
destination := L.CheckString(2)
_ = os.RemoveAll(destination)
if err := os.Symlink(fileName, destination); err != nil {
L.Push(lua.LFalse)
L.Push(lua.LString(err.Error()))
return 2
}
L.Push(lua.LTrue)
L.Push(lua.LNil)
return 2
}
func Ljoin(L *lua.LState) int {
n := L.GetTop()
parts := make([]string, 0, n)
for i := 1; i <= n; i++ {
val := L.Get(i)
parts = append(parts, val.String())
}
result := filepath.Join(parts...)
L.Push(lua.LString(result))
return 1
}
func LMkdir(L *lua.LState) int {
path := L.CheckString(1)
perm := L.CheckInt(2)
if err := os.MkdirAll(path, os.FileMode(perm)); err != nil {
L.Push(lua.LFalse)
L.Push(lua.LString(err.Error()))
return 2
}
L.Push(lua.LTrue)
L.Push(lua.LNil)
return 2
}
func LError(L *lua.LState) int {
n := L.GetTop()
parts := make([]any, 0, n)
for i := 1; i <= n; i++ {
val := L.Get(i)
parts = append(parts, val.String())
}
llogger().Panic(parts...)
return 0
}
func LSetEnv(L *lua.LState) int {
env := L.CheckString(1)
value := L.CheckString(2)
os.Setenv(env, value)
return 0
}
func llogger() *log.Logger { return log.New(os.Stderr, "script error: ", 0) }