From d3c4a604c33807c3c381cadc203a74b32e27daea Mon Sep 17 00:00:00 2001 From: robogg133 Date: Fri, 31 Oct 2025 17:52:40 -0300 Subject: [PATCH] New Packet.lua format, test parsing and entire new lua ecossystem --- cmd/main.go | 40 ++++ go.mod | 7 + go.sum | 4 + main.go | 10 + pkg/packet.lua.d/auxiliar_functions.go | 289 +++++++++++++++++++++++++ pkg/packet.lua.d/config.go | 24 ++ pkg/packet.lua.d/main.go | 166 ++++++++++++++ test/Packet.lua | 59 ----- test/bat/Packet.lua | 61 ++++++ test/utctimerightnow/Packet.lua | 42 ++++ 10 files changed, 643 insertions(+), 59 deletions(-) create mode 100644 cmd/main.go create mode 100644 go.mod create mode 100644 go.sum create mode 100644 main.go create mode 100644 pkg/packet.lua.d/auxiliar_functions.go create mode 100644 pkg/packet.lua.d/config.go create mode 100644 pkg/packet.lua.d/main.go delete mode 100644 test/Packet.lua create mode 100644 test/bat/Packet.lua create mode 100644 test/utctimerightnow/Packet.lua diff --git a/cmd/main.go b/cmd/main.go new file mode 100644 index 0000000..86d4665 --- /dev/null +++ b/cmd/main.go @@ -0,0 +1,40 @@ +package main + +import ( + "fmt" + "log" + "os" + "runtime" + + "github.com/roboogg133/packets/pkg/packet.lua.d" +) + +func main() { + f, err := os.ReadFile("test/bat/Packet.lua") + if err != nil { + log.Fatal(err) + } + + pk, err := packet.ReadPacket(f, nil) + if err != nil { + log.Fatal(err) + } + + fmt.Println( + pk.Name, + pk.Description, + ) + + if pk.Plataforms == nil { + fmt.Print("NIGGER NIGGER NIGGER NIGGER NIGGER NIGGER") + return + } + plataforms := *pk.Plataforms + + for _, v := range *plataforms[packet.OperationalSystem(runtime.GOOS)].Sources { + fmt.Printf("%s %s\n", v.Method, v.Url) + if v.Method == "GET" { + fmt.Println(*v.Specs.(packet.GETSpecs).SHA256) + } + } +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..851ee59 --- /dev/null +++ b/go.mod @@ -0,0 +1,7 @@ +module github.com/roboogg133/packets + +go 1.25.3 + +require github.com/yuin/gopher-lua v1.1.1 + +require github.com/klauspost/compress v1.18.1 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..4e25781 --- /dev/null +++ b/go.sum @@ -0,0 +1,4 @@ +github.com/klauspost/compress v1.18.1 h1:bcSGx7UbpBqMChDtsF28Lw6v/G94LPrrbMbdC3JH2co= +github.com/klauspost/compress v1.18.1/go.mod h1:ZQFFVG+MdnR0P+l6wpXgIL4NTtwiKIdBnrBd8Nrxr+0= +github.com/yuin/gopher-lua v1.1.1 h1:kYKnWBjvbNP4XLT3+bPEwAXJx262OhaHDWDVOPjL46M= +github.com/yuin/gopher-lua v1.1.1/go.mod h1:GBR0iDaNXjAgGg9zfCvksxSRnQx76gclCIb7kdAd1Pw= diff --git a/main.go b/main.go new file mode 100644 index 0000000..7cca710 --- /dev/null +++ b/main.go @@ -0,0 +1,10 @@ +package main + +import ( + "fmt" + "runtime" +) + +func main() { + fmt.Println(runtime.GOARCH) +} diff --git a/pkg/packet.lua.d/auxiliar_functions.go b/pkg/packet.lua.d/auxiliar_functions.go new file mode 100644 index 0000000..7848467 --- /dev/null +++ b/pkg/packet.lua.d/auxiliar_functions.go @@ -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 | "): + 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{} +} diff --git a/pkg/packet.lua.d/config.go b/pkg/packet.lua.d/config.go new file mode 100644 index 0000000..2fab4b8 --- /dev/null +++ b/pkg/packet.lua.d/config.go @@ -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 + } + +} diff --git a/pkg/packet.lua.d/main.go b/pkg/packet.lua.d/main.go new file mode 100644 index 0000000..5a9f070 --- /dev/null +++ b/pkg/packet.lua.d/main.go @@ -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 +} diff --git a/test/Packet.lua b/test/Packet.lua deleted file mode 100644 index a71eb18..0000000 --- a/test/Packet.lua +++ /dev/null @@ -1,59 +0,0 @@ -return { - package = { - name = "bat-bin", - version = "0.26.0", - mantainer = "robogg133", - description = "fast, opensource, easy to use package manager.", - serial = 0, - - plataforms = { - windows = { - arch = {"x86_64"}, - sources = { - ["https://github.com/sharkdp/bat/releases/download/v0.26.0/bat-v0.26.0-aarch64-unknown-linux-gnu.tar.gz"] = {method = "GET"} - }, - dependencies = { - build = {}, - runtime = {}, - conflicts = {} - } - }, - linux = { - arch = {"x86_64"}, - sources = { - ["https://github.com/sharkdp/bat/releases/download/v0.26.0/bat-v0.26.0-aarch64-unknown-linux-gnu.tar.gz"] = {method = "GET"} - }, - dependencies = { - build = {}, - runtime = {}, - conflicts = {} - } - }, - all = { - sources = {} - } - }, - - - - }, - - - prepare = function(container) - git.clone("https://github.com/roboogg133/packets.git", container.dir("/data")) - os.remove(container.dir("/data/.git")) - - end, - - build = function() - os.execute("go build ./data/cmd/packets") - end, - - install = function(container) - os.copy(container.dir("./packets"), BIN_DIR) - end, - - remove = function () - os.remove(path_join(BIN_DIR, "packets")) - end -} \ No newline at end of file diff --git a/test/bat/Packet.lua b/test/bat/Packet.lua new file mode 100644 index 0000000..4c6efd0 --- /dev/null +++ b/test/bat/Packet.lua @@ -0,0 +1,61 @@ +local arch_map = { + amd64 = "x86_64", + aarch64 = "aarch64", + arm64 = "aarch64", + ['386'] = "i686" +} +local srcarch = arch_map[CURRENT_ARCH] + +return { + package = { + name = "bat-bin", -- required + version = "0.26.0", -- required + maintainer = "robogg133", -- required + description = "A cat(1) clone with syntax highlighting and Git integration.", -- required + serial = 0,-- required + + plataforms = { + windows = { + arch = {"amd64"}, + sources = { + { + url = "https://github.com/sharkdp/bat/releases/download/v0.26.0/bat-v0.26.0-" ..srcarch .."-pc-windows-msvc.zip", + method = "GET", + sha256="a8a6862f14698b45e101b0932c69bc47a007f4c0456f3a129fdcef54d443d501" + } + }, + dependencies = { + build = {}, + runtime = {}, + conflicts = {} + } + }, + linux = { + arch = {"amd64"}, + sources = { + { + url = "https://github.com/sharkdp/bat/releases/download/v0.26.0/bat-v0.26.0-".. srcarch .."-unknown-linux-gnu.tar.gz", + method = "GET", + sha256 = "7efed0c768fae36f18ddbbb4a38f5c4b64db7c55a170dfc89fd380805809a44b" + } + }, + dependencies = { + build = {}, + runtime = {}, + conflicts = {} + } + } + }, + + sources = {} + + }, + + build = function() + end, + + pkg = function() -- required + print("oi amores") + end, + +} \ No newline at end of file diff --git a/test/utctimerightnow/Packet.lua b/test/utctimerightnow/Packet.lua new file mode 100644 index 0000000..d9a32e5 --- /dev/null +++ b/test/utctimerightnow/Packet.lua @@ -0,0 +1,42 @@ +local arch_map = { + amd64 = "x86_64", + aarch64 = "aarch64", + arm64 = "aarch64", + ['386'] = "i686" +} +local srcarch = arch_map[CURRENT_ARCH] + +return { + package = { + name = "utctimerightnow", -- required + version = "0.1.0", -- required + maintainer = "robogg133", -- required + description = "shows utc time", -- required + serial = 0,-- required + + dependencies = { + build = {"go"}, + runtime = {}, + conflicts = {} + }, + + sources = { --optional + { + url = "https://git.opentty.xyz/robogg133/utctimerightnow.git", -- required + method = "git", -- required + branch = "main" -- required + -- tag = "" + } + } + + }, + + build = function() + + end, + + pkg = function() -- required + print("goku") + end, + +} \ No newline at end of file