added lua functions from old version of packets
This commit is contained in:
		
							
								
								
									
										120
									
								
								internal/lua/luafunctions.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										120
									
								
								internal/lua/luafunctions.go
									
									
									
									
									
										Normal 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) }
 | 
			
		||||
							
								
								
									
										95
									
								
								internal/lua/utils.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										95
									
								
								internal/lua/utils.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,95 @@
 | 
			
		||||
package lua
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"io"
 | 
			
		||||
	"os"
 | 
			
		||||
	"path/filepath"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func copyDir(src string, dest string) error {
 | 
			
		||||
	if stats, err := os.Stat(src); err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	} else {
 | 
			
		||||
		if stats.IsDir() {
 | 
			
		||||
			files, err := os.ReadDir(src)
 | 
			
		||||
			if err != nil {
 | 
			
		||||
				return err
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			if err := os.MkdirAll(dest, 0o755); err != nil {
 | 
			
		||||
				return err
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			for _, file := range files {
 | 
			
		||||
				if file.IsDir() {
 | 
			
		||||
					copyDir(filepath.Join(src, file.Name()), filepath.Join(dest, file.Name()))
 | 
			
		||||
					continue
 | 
			
		||||
				}
 | 
			
		||||
				srcFile := filepath.Join(src, file.Name())
 | 
			
		||||
 | 
			
		||||
				f, err := os.Create(filepath.Join(dest, file.Name()))
 | 
			
		||||
				if err != nil {
 | 
			
		||||
					return err
 | 
			
		||||
				}
 | 
			
		||||
				defer f.Close()
 | 
			
		||||
 | 
			
		||||
				opennedSrcFile, err := os.Open(srcFile)
 | 
			
		||||
				if err != nil {
 | 
			
		||||
					return err
 | 
			
		||||
				}
 | 
			
		||||
				defer opennedSrcFile.Close()
 | 
			
		||||
				if _, err := io.Copy(f, opennedSrcFile); err != nil {
 | 
			
		||||
					return err
 | 
			
		||||
				}
 | 
			
		||||
 | 
			
		||||
			}
 | 
			
		||||
		} else {
 | 
			
		||||
			if err := copyFile(src, dest); err != nil {
 | 
			
		||||
				return err
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func copyFile(source string, destination string) error {
 | 
			
		||||
	src, err := os.Open(source)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
	defer src.Close()
 | 
			
		||||
 | 
			
		||||
	status, err := src.Stat()
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	err = os.MkdirAll(filepath.Dir(destination), 0o755)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	dst, err := os.Create(destination)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		if !os.IsExist(err) {
 | 
			
		||||
			dst, err = os.Open(destination)
 | 
			
		||||
			if err != nil {
 | 
			
		||||
				return err
 | 
			
		||||
			}
 | 
			
		||||
		} else {
 | 
			
		||||
			return err
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	defer dst.Close()
 | 
			
		||||
	if err := dst.Chmod(status.Mode()); err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	_, err = io.Copy(dst, src)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user