New Packet.lua format, test parsing and entire new lua ecossystem
This commit is contained in:
289
pkg/packet.lua.d/auxiliar_functions.go
Normal file
289
pkg/packet.lua.d/auxiliar_functions.go
Normal file
@@ -0,0 +1,289 @@
|
||||
package packet
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
lua "github.com/yuin/gopher-lua"
|
||||
)
|
||||
|
||||
func getStringFromTable(table *lua.LTable, key string) string {
|
||||
value := table.RawGetString(key)
|
||||
if value.Type() == lua.LTString {
|
||||
return value.String()
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func getIntFromTable(table *lua.LTable, key string) int {
|
||||
value := table.RawGetString(key)
|
||||
if value.Type() == lua.LTNumber {
|
||||
if num, ok := value.(lua.LNumber); ok {
|
||||
return int(num)
|
||||
}
|
||||
}
|
||||
return -133
|
||||
}
|
||||
|
||||
func getStringArrayFromTable(table *lua.LTable, key string) []string {
|
||||
value := table.RawGetString(key)
|
||||
if value.Type() != lua.LTTable {
|
||||
return []string{}
|
||||
}
|
||||
|
||||
arrayTable := value.(*lua.LTable)
|
||||
var result []string
|
||||
|
||||
arrayTable.ForEach(func(_, value lua.LValue) {
|
||||
if value.Type() == lua.LTString {
|
||||
result = append(result, value.String())
|
||||
}
|
||||
})
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
func getFunctionFromTable(table *lua.LTable, key string) *lua.LFunction {
|
||||
value := table.RawGetString(key)
|
||||
if value.Type() == lua.LTFunction {
|
||||
return value.(*lua.LFunction)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type version struct {
|
||||
Name string
|
||||
Constraint VersionConstraint
|
||||
}
|
||||
|
||||
func getDependenciesFromTable(table *lua.LTable, key string) *PkgDependencies {
|
||||
value := table.RawGetString(key)
|
||||
if value.Type() != lua.LTTable {
|
||||
return &PkgDependencies{}
|
||||
}
|
||||
|
||||
var pkgDeps PkgDependencies
|
||||
|
||||
depnTable := value.(*lua.LTable)
|
||||
|
||||
pkgDeps.RuntimeDependencies = depsParse(depnTable, "runtime")
|
||||
pkgDeps.BuildDependencies = depsParse(depnTable, "build")
|
||||
pkgDeps.Conflicts = depsParse(depnTable, "conflicts")
|
||||
|
||||
return &pkgDeps
|
||||
}
|
||||
|
||||
func getSourcesFromTable(table *lua.LTable, key string) *[]Source {
|
||||
value := table.RawGetString(key)
|
||||
if value.Type() != lua.LTTable {
|
||||
return nil
|
||||
}
|
||||
|
||||
var srcList []Source
|
||||
|
||||
srcTable := value.(*lua.LTable)
|
||||
|
||||
srcTable.ForEach(func(_, value lua.LValue) {
|
||||
if value.Type() == lua.LTTable {
|
||||
src := value.(*lua.LTable)
|
||||
|
||||
var srcInfo Source
|
||||
|
||||
method := src.RawGetString("method")
|
||||
if method.Type() == lua.LTString {
|
||||
srcInfo.Method = method.String()
|
||||
}
|
||||
|
||||
url := src.RawGetString("url")
|
||||
|
||||
if url.Type() == lua.LTString {
|
||||
srcInfo.Url = url.String()
|
||||
}
|
||||
|
||||
switchlabel:
|
||||
switch srcInfo.Method {
|
||||
case "GET":
|
||||
var getSpecs GETSpecs
|
||||
|
||||
getSpecs.SHA256 = new(string)
|
||||
sha256sumL := src.RawGetString("sha256")
|
||||
if sha256sumL.Type() == lua.LTString {
|
||||
*getSpecs.SHA256 = sha256sumL.String()
|
||||
}
|
||||
|
||||
headersLT := src.RawGetString("headers")
|
||||
if headersLT.Type() == lua.LTTable {
|
||||
|
||||
headers := headersLT.(*lua.LTable)
|
||||
|
||||
tmpMap := make(map[string]string)
|
||||
headers.ForEach(func(headerKey, value lua.LValue) {
|
||||
if value.Type() == lua.LTString {
|
||||
tmpMap[headerKey.String()] = value.String()
|
||||
}
|
||||
})
|
||||
|
||||
getSpecs.Headers = &tmpMap
|
||||
}
|
||||
srcInfo.Specs = getSpecs
|
||||
break switchlabel
|
||||
|
||||
case "git":
|
||||
var gitSpecs GitSpecs
|
||||
|
||||
branchL := src.RawGetString("branch")
|
||||
|
||||
if branchL.Type() == lua.LTString {
|
||||
gitSpecs.Branch = branchL.String()
|
||||
}
|
||||
|
||||
tagL := src.RawGetString("tag")
|
||||
|
||||
if tagL.Type() == lua.LTString {
|
||||
*gitSpecs.Tag = tagL.String()
|
||||
}
|
||||
|
||||
srcInfo.Specs = gitSpecs
|
||||
break switchlabel
|
||||
case "POST":
|
||||
var postSpecs POSTSpecs
|
||||
|
||||
sha256sumL := src.RawGetString("sha256")
|
||||
if sha256sumL.Type() == lua.LTString {
|
||||
*postSpecs.SHA256 = sha256sumL.String()
|
||||
}
|
||||
|
||||
headersLT := src.RawGetString("headers")
|
||||
if headersLT.Type() == lua.LTTable {
|
||||
|
||||
headers := headersLT.(*lua.LTable)
|
||||
|
||||
tmpMap := make(map[string]string)
|
||||
headers.ForEach(func(headerKey, value lua.LValue) {
|
||||
if value.Type() == lua.LTString {
|
||||
tmpMap[headerKey.String()] = value.String()
|
||||
}
|
||||
})
|
||||
|
||||
postSpecs.Headers = &tmpMap
|
||||
}
|
||||
|
||||
bodyLt := src.RawGetString("body")
|
||||
|
||||
if bodyLt.Type() == lua.LTString {
|
||||
*postSpecs.Body = bodyLt.String()
|
||||
}
|
||||
|
||||
srcInfo.Specs = postSpecs
|
||||
break switchlabel
|
||||
}
|
||||
srcList = append(srcList, srcInfo)
|
||||
}
|
||||
})
|
||||
|
||||
return &srcList
|
||||
}
|
||||
|
||||
func getPlataformsFromTable(table *lua.LTable, key string) *map[OperationalSystem]Plataform {
|
||||
value := table.RawGetString(key)
|
||||
|
||||
if value.Type() != lua.LTTable {
|
||||
return nil
|
||||
}
|
||||
|
||||
tmpMap := make(map[OperationalSystem]Plataform)
|
||||
|
||||
plataform := value.(*lua.LTable)
|
||||
|
||||
plataform.ForEach(func(osString, value lua.LValue) {
|
||||
if value.Type() != lua.LTTable {
|
||||
return
|
||||
}
|
||||
|
||||
var plat Plataform
|
||||
plat.Architetures = getStringArrayFromTable(value.(*lua.LTable), "arch")
|
||||
plat.Name = osString.String()
|
||||
plat.Sources = getSourcesFromTable(value.(*lua.LTable), "sources")
|
||||
plat.Dependencies = getDependenciesFromTable(value.(*lua.LTable), "dependencies")
|
||||
|
||||
tmpMap[OperationalSystem(osString.String())] = plat
|
||||
})
|
||||
|
||||
if len(tmpMap) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
return &tmpMap
|
||||
}
|
||||
|
||||
func depsParse(depnTable *lua.LTable, key string) *map[string]*VersionConstraint {
|
||||
if runLTable := depnTable.RawGetString(key); runLTable.Type() == lua.LTTable {
|
||||
runtimeTable := runLTable.(*lua.LTable)
|
||||
|
||||
mapTemp := make(map[string]*VersionConstraint)
|
||||
|
||||
var found bool
|
||||
|
||||
runtimeTable.ForEach(func(_, value lua.LValue) {
|
||||
if value.Type() == lua.LTString {
|
||||
version := parseVersionString(value.String())
|
||||
mapTemp[version.Name] = &version.Constraint
|
||||
found = true
|
||||
}
|
||||
})
|
||||
if !found {
|
||||
return nil
|
||||
} else {
|
||||
return &mapTemp
|
||||
}
|
||||
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func parseVersionString(s string) version {
|
||||
// >=go@1.25.3 | <=go@1.25.3 | go | >go@1.25.3 | <go@1.25.3 | go@1.25.3
|
||||
if strings.ContainsAny(s, "@") {
|
||||
slice := strings.Split(s, "@")
|
||||
|
||||
switch {
|
||||
case !strings.ContainsAny(s, "<=>"):
|
||||
return version{
|
||||
Name: slice[0],
|
||||
Constraint: VersionConstraint(slice[1]),
|
||||
}
|
||||
case s[0] == '>' && s[1] == '=':
|
||||
|
||||
return version{
|
||||
Name: slice[0][2:],
|
||||
Constraint: VersionConstraint(">=" + slice[1]),
|
||||
}
|
||||
case s[0] == '<' && s[1] == '=':
|
||||
|
||||
return version{
|
||||
Name: slice[0][2:],
|
||||
Constraint: VersionConstraint("<=" + slice[1]),
|
||||
}
|
||||
|
||||
case s[0] == '>' && s[1] != '=':
|
||||
|
||||
return version{
|
||||
Name: slice[0][1:],
|
||||
Constraint: VersionConstraint(">" + slice[1]),
|
||||
}
|
||||
case s[0] == '<' && s[1] != '=':
|
||||
|
||||
return version{
|
||||
Name: slice[0][1:],
|
||||
Constraint: VersionConstraint("<" + slice[1]),
|
||||
}
|
||||
}
|
||||
|
||||
} else if !strings.ContainsAny(s, "@=<>") {
|
||||
return version{
|
||||
Name: s,
|
||||
Constraint: VersionConstraint(0x000),
|
||||
}
|
||||
}
|
||||
|
||||
return version{}
|
||||
}
|
||||
24
pkg/packet.lua.d/config.go
Normal file
24
pkg/packet.lua.d/config.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package packet
|
||||
|
||||
type Config struct {
|
||||
BinDir string
|
||||
}
|
||||
|
||||
const defaultBinDir = "/usr/bin"
|
||||
|
||||
func checkConfig(cfg *Config) *Config {
|
||||
|
||||
if cfg == nil {
|
||||
return &Config{
|
||||
BinDir: defaultBinDir,
|
||||
}
|
||||
}
|
||||
if cfg.BinDir == "" {
|
||||
return &Config{
|
||||
BinDir: defaultBinDir,
|
||||
}
|
||||
} else {
|
||||
return cfg
|
||||
}
|
||||
|
||||
}
|
||||
166
pkg/packet.lua.d/main.go
Normal file
166
pkg/packet.lua.d/main.go
Normal file
@@ -0,0 +1,166 @@
|
||||
package packet
|
||||
|
||||
import (
|
||||
"archive/tar"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
|
||||
"github.com/klauspost/compress/zstd"
|
||||
|
||||
lua "github.com/yuin/gopher-lua"
|
||||
)
|
||||
|
||||
type OperationalSystem string
|
||||
|
||||
type PacketLua struct {
|
||||
Name string
|
||||
Version string
|
||||
Maintaner string
|
||||
Description string
|
||||
Serial int
|
||||
|
||||
Plataforms *map[OperationalSystem]Plataform
|
||||
GlobalSources *[]Source
|
||||
GlobalDependencies *PkgDependencies
|
||||
|
||||
Build *lua.LFunction
|
||||
Pkg *lua.LFunction
|
||||
}
|
||||
|
||||
type Source struct {
|
||||
Method string
|
||||
Url string
|
||||
Specs interface{}
|
||||
}
|
||||
|
||||
type VersionConstraint string
|
||||
|
||||
type PkgDependencies struct {
|
||||
RuntimeDependencies *map[string]*VersionConstraint
|
||||
BuildDependencies *map[string]*VersionConstraint
|
||||
Conflicts *map[string]*VersionConstraint
|
||||
}
|
||||
|
||||
type Plataform struct {
|
||||
Name string
|
||||
Architetures []string
|
||||
Sources *[]Source
|
||||
Dependencies *PkgDependencies
|
||||
}
|
||||
|
||||
type GitSpecs struct {
|
||||
Branch string
|
||||
Tag *string
|
||||
}
|
||||
|
||||
type POSTSpecs struct {
|
||||
SHA256 *string
|
||||
Body *string
|
||||
Headers *map[string]string
|
||||
}
|
||||
|
||||
type GETSpecs struct {
|
||||
SHA256 *string
|
||||
Headers *map[string]string
|
||||
}
|
||||
|
||||
var ErrCantFindPacketDotLua = errors.New("can't find Packet.lua in .tar.zst file")
|
||||
var ErrFileDontReturnTable = errors.New("invalid Packet.lua format: the file do not return a table")
|
||||
var ErrCannotFindPackageTable = errors.New("invalid Packet.lua format: can't find package table")
|
||||
|
||||
// ReadPacket read a Packet.lua and alredy set global vars
|
||||
func ReadPacket(f []byte, cfg *Config) (PacketLua, error) {
|
||||
cfg = checkConfig(cfg)
|
||||
|
||||
L := lua.NewState()
|
||||
defer L.Close()
|
||||
|
||||
osObject := L.GetGlobal("os").(*lua.LTable)
|
||||
ioObject := L.GetGlobal("io").(*lua.LTable)
|
||||
|
||||
L.SetGlobal("os", lua.LNil)
|
||||
L.SetGlobal("io", lua.LNil)
|
||||
|
||||
L.SetGlobal("BIN_DIR", lua.LString(cfg.BinDir))
|
||||
L.SetGlobal("CURRENT_ARCH", lua.LString(runtime.GOARCH))
|
||||
L.SetGlobal("CURRENT_PLATAFORM", lua.LString(runtime.GOOS))
|
||||
|
||||
if err := L.DoString(string(f)); err != nil {
|
||||
return PacketLua{}, err
|
||||
}
|
||||
|
||||
L.SetGlobal("os", osObject)
|
||||
L.SetGlobal("io", ioObject)
|
||||
|
||||
tableLua := L.Get(-1)
|
||||
|
||||
if tableLua.Type() != lua.LTTable {
|
||||
return PacketLua{}, ErrFileDontReturnTable
|
||||
}
|
||||
|
||||
table := tableLua.(*lua.LTable)
|
||||
|
||||
pkgTableLua := table.RawGetString("package")
|
||||
if pkgTableLua.Type() != lua.LTTable {
|
||||
return PacketLua{}, ErrCannotFindPackageTable
|
||||
}
|
||||
pkgTable := pkgTableLua.(*lua.LTable)
|
||||
|
||||
packetLua := &PacketLua{
|
||||
Name: getStringFromTable(pkgTable, "name"),
|
||||
Version: getStringFromTable(pkgTable, "version"),
|
||||
Maintaner: getStringFromTable(pkgTable, "maintainer"),
|
||||
Description: getStringFromTable(pkgTable, "description"),
|
||||
Serial: getIntFromTable(pkgTable, "serial"),
|
||||
|
||||
Plataforms: getPlataformsFromTable(pkgTable, "plataforms"),
|
||||
|
||||
GlobalDependencies: getDependenciesFromTable(pkgTable, "build_dependencies"),
|
||||
GlobalSources: getSourcesFromTable(pkgTable, "sources"),
|
||||
|
||||
Build: getFunctionFromTable(table, "build"),
|
||||
Pkg: getFunctionFromTable(table, "pkg"),
|
||||
}
|
||||
|
||||
if packetLua.Pkg == nil {
|
||||
return PacketLua{}, fmt.Errorf("pkg() does not exist")
|
||||
}
|
||||
|
||||
return *packetLua, nil
|
||||
}
|
||||
|
||||
func ReadPacketFromZSTDF(file io.Reader, cfg *Config) (PacketLua, error) {
|
||||
cfg = checkConfig(cfg)
|
||||
|
||||
zstdReader, err := zstd.NewReader(file)
|
||||
if err != nil {
|
||||
return PacketLua{}, err
|
||||
}
|
||||
defer zstdReader.Close()
|
||||
|
||||
tarReader := tar.NewReader(zstdReader)
|
||||
for {
|
||||
header, err := tarReader.Next()
|
||||
if err == io.EOF {
|
||||
break
|
||||
}
|
||||
if err != nil {
|
||||
return PacketLua{}, err
|
||||
}
|
||||
|
||||
if filepath.Base(header.Name) == "Packet.lua" {
|
||||
|
||||
packageLuaBlob, err := io.ReadAll(tarReader)
|
||||
if err != nil {
|
||||
return PacketLua{}, err
|
||||
}
|
||||
|
||||
return ReadPacket(packageLuaBlob, cfg)
|
||||
}
|
||||
|
||||
}
|
||||
return PacketLua{}, ErrCantFindPacketDotLua
|
||||
}
|
||||
Reference in New Issue
Block a user