doing lua functions for build files, to run only in afero.FS
This commit is contained in:
116
internal/build/lua.go
Normal file
116
internal/build/lua.go
Normal file
@@ -0,0 +1,116 @@
|
||||
package build
|
||||
|
||||
import (
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
lua "github.com/yuin/gopher-lua"
|
||||
)
|
||||
|
||||
func (container Container) lRemove(L *lua.LState) int {
|
||||
filename := L.CheckString(1)
|
||||
|
||||
err := container.FS.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 (container Container) lRename(L *lua.LState) int {
|
||||
oldname := L.CheckString(1)
|
||||
newname := L.CheckString(2)
|
||||
|
||||
if err := container.FS.Rename(oldname, newname); err != nil {
|
||||
L.Push(lua.LFalse)
|
||||
L.Push(lua.LString(err.Error()))
|
||||
return 2
|
||||
}
|
||||
|
||||
L.Push(lua.LTrue)
|
||||
return 1
|
||||
}
|
||||
func (container Container) lCopy(L *lua.LState) int {
|
||||
oldname := L.CheckString(1)
|
||||
newname := L.CheckString(2)
|
||||
|
||||
if err := container.copyContainer(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 modeFlags(mode string) int {
|
||||
switch mode {
|
||||
case "r", "rb":
|
||||
return os.O_RDONLY
|
||||
case "w", "wb":
|
||||
return os.O_CREATE | os.O_WRONLY | os.O_TRUNC
|
||||
case "a", "ab":
|
||||
return os.O_CREATE | os.O_WRONLY | os.O_APPEND
|
||||
case "r+", "r+b", "rb+", "br+":
|
||||
return os.O_RDWR
|
||||
case "w+", "w+b", "wb+", "bw+":
|
||||
return os.O_CREATE | os.O_RDWR | os.O_TRUNC
|
||||
case "a+", "a+b", "ab+", "ba+":
|
||||
return os.O_CREATE | os.O_RDWR | os.O_APPEND
|
||||
default:
|
||||
return os.O_RDONLY
|
||||
}
|
||||
}
|
||||
|
||||
func (container Container) lOpen(L *lua.LState) int {
|
||||
path := L.CheckString(1)
|
||||
mode := L.OptString(2, "r")
|
||||
|
||||
file, err := container.FS.OpenFile(path, modeFlags(mode), 0644)
|
||||
if err != nil {
|
||||
L.Push(lua.LNil)
|
||||
L.Push(lua.LString(err.Error()))
|
||||
return 2
|
||||
}
|
||||
|
||||
ud := L.NewUserData()
|
||||
ud.Value = file
|
||||
L.SetMetatable(ud, L.GetTypeMetatable("file"))
|
||||
L.Push(ud)
|
||||
L.Push(lua.LNil)
|
||||
return 2
|
||||
}
|
||||
|
||||
func (container Container) lMkdir(L *lua.LState) int {
|
||||
path := L.CheckString(1)
|
||||
perm := L.CheckInt(2)
|
||||
|
||||
if err := container.FS.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 (container Container) lexecute(L *lua.LState) {
|
||||
cmdString := L.CheckString(1)
|
||||
|
||||
cmdSlice := strings.Fields(cmdString)
|
||||
}
|
||||
|
||||
func (container Container) GetLuaState() error {
|
||||
|
||||
lua.NewState(lua.Options{})
|
||||
return nil
|
||||
}
|
||||
@@ -33,11 +33,11 @@ func NewContainer(Root string, dataDir string, manifest configs.Manifest) (Conta
|
||||
return Container{}, err
|
||||
}
|
||||
|
||||
if err := container.FS.MkdirAll("/usr/bin", 0777); err != nil {
|
||||
if err := container.FS.MkdirAll(BinDir, 0777); err != nil {
|
||||
return Container{}, err
|
||||
}
|
||||
|
||||
if err := afero.Symlinker.SymlinkIfPossible(container.FS.(afero.Symlinker), "/usr/bin", "/bin"); err != nil {
|
||||
if err := afero.Symlinker.SymlinkIfPossible(container.FS.(afero.Symlinker), BinDir, SymLinkBinDir); err != nil {
|
||||
return Container{}, err
|
||||
}
|
||||
|
||||
|
||||
6
internal/build/specs.go
Normal file
6
internal/build/specs.go
Normal file
@@ -0,0 +1,6 @@
|
||||
package build
|
||||
|
||||
const (
|
||||
BinDir = "/usr/bin"
|
||||
SymLinkBinDir = "/bin"
|
||||
)
|
||||
79
internal/build/utils.go
Normal file
79
internal/build/utils.go
Normal file
@@ -0,0 +1,79 @@
|
||||
package build
|
||||
|
||||
import (
|
||||
"io"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/spf13/afero"
|
||||
)
|
||||
|
||||
func (container Container) copyContainer(src string, dest string) error {
|
||||
stats, err := container.FS.Stat(src)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if stats.IsDir() {
|
||||
files, err := afero.ReadDir(container.FS, src)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := container.FS.MkdirAll(dest, 0755); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, file := range files {
|
||||
srcPath := filepath.Join(src, file.Name())
|
||||
destPath := filepath.Join(dest, file.Name())
|
||||
|
||||
if file.IsDir() {
|
||||
if err := container.CopyHostToContainer(srcPath, destPath); err != nil {
|
||||
return err
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
if err := container.copySingleFileLtoL(srcPath, destPath); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if err := container.copySingleFileLtoL(src, dest); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (container Container) copySingleFileLtoL(source string, destination string) error {
|
||||
src, err := container.FS.Open(source)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer src.Close()
|
||||
|
||||
stats, err := src.Stat()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := container.FS.MkdirAll(filepath.Dir(destination), 0755); err != nil {
|
||||
return err
|
||||
}
|
||||
dst, err := container.FS.Create(destination)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer dst.Close()
|
||||
|
||||
if _, err := io.Copy(dst, src); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := container.FS.Chmod(destination, stats.Mode()); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user