diff --git a/go.mod b/go.mod index 2fc494c..1d494d5 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/M0Rf30/yap go 1.23 require ( + github.com/blakesmith/ar v0.0.0-20190502131153-809d4375e1fb github.com/cavaliergopher/grab/v3 v3.0.1 github.com/github/go-spdx/v2 v2.3.2 github.com/go-git/go-git/v5 v5.12.0 diff --git a/go.sum b/go.sum index 724bc0e..cc433c7 100644 --- a/go.sum +++ b/go.sum @@ -50,6 +50,8 @@ github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be/go.mod h1:ySMOLuW github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPdPJAN/hZIm0C4OItdklCFmMRWYpio= github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs= github.com/atomicgo/cursor v0.0.1/go.mod h1:cBON2QmmrysudxNBFthvMtN32r3jxVRIvzkUiF/RuIk= +github.com/blakesmith/ar v0.0.0-20190502131153-809d4375e1fb h1:m935MPodAbYS46DG4pJSv7WO+VECIWUQ7OJYSoTrMh4= +github.com/blakesmith/ar v0.0.0-20190502131153-809d4375e1fb/go.mod h1:PkYb9DJNAwrSvRx5DYA+gUcOIgTGVMNkfSCbZM8cWpI= github.com/bodgit/plumbing v1.3.0 h1:pf9Itz1JOQgn7vEOE7v7nlEfBykYqvUYioC61TwWCFU= github.com/bodgit/plumbing v1.3.0/go.mod h1:JOTb4XiRu5xfnmdnDJo6GmSbSbtSyufrsyZFByMtKEs= github.com/bodgit/sevenzip v1.5.1 h1:rVj0baZsooZFy64DJN0zQogPzhPrT8BQ8TTRd1H4WHw= diff --git a/pkg/apk/apk.go b/pkg/apk/apk.go index 2680bd1..08dc8fb 100644 --- a/pkg/apk/apk.go +++ b/pkg/apk/apk.go @@ -30,6 +30,7 @@ func (a *Apk) BuildPackage(artifactsPath string) error { // It initializes the apkDir, cleans up any existing directory, creates the necessary packer directory, // and generates the APKBUILD and post-installation script files. The method returns an error if any step fails. func (a *Apk) PrepareFakeroot(_ string) error { + a.PKGBUILD.ArchComputed = APKArchs[a.PKGBUILD.ArchComputed] a.apkDir = filepath.Join(a.PKGBUILD.StartDir, "apk") if err := utils.RemoveAll(a.apkDir); err != nil { @@ -62,24 +63,22 @@ func (a *Apk) PrepareFakeroot(_ string) error { // It takes a string parameter `artifactsPath` which specifies the path where the artifacts are located. // It returns an error if there was an error during the installation process. func (a *Apk) Install(artifactsPath string) error { - for _, arch := range a.PKGBUILD.Arch { - pkgName := a.PKGBUILD.PkgName + "-" + - a.PKGBUILD.PkgVer + - "-" + - "r" + a.PKGBUILD.PkgRel + - "-" + - arch + - ".apk" - - pkgFilePath := filepath.Join(artifactsPath, a.PKGBUILD.PkgName, arch, pkgName) - - if err := utils.Exec(true, - "apk", - "add", - "--allow-untrusted", - pkgFilePath); err != nil { - return err - } + pkgName := a.PKGBUILD.PkgName + "-" + + a.PKGBUILD.PkgVer + + "-" + + "r" + a.PKGBUILD.PkgRel + + "-" + + a.PKGBUILD.ArchComputed + + ".apk" + + pkgFilePath := filepath.Join(artifactsPath, a.PKGBUILD.PkgName, a.PKGBUILD.ArchComputed, pkgName) + + if err := utils.Exec(true, + "apk", + "add", + "--allow-untrusted", + pkgFilePath); err != nil { + return err } return nil diff --git a/pkg/apk/constants.go b/pkg/apk/constants.go index f510c76..61d4f86 100644 --- a/pkg/apk/constants.go +++ b/pkg/apk/constants.go @@ -1,8 +1,19 @@ package apk -var buildEnvironmentDeps = []string{ - "alpine-sdk", -} +var ( + APKArchs = map[string]string{ + "x86_64": "x86_64", + "i686": "x86", + "aarch64": "aarch64", + "armv7h": "armv7h", + "armv6h": "armv6h", + "any": "all", + } + + buildEnvironmentDeps = []string{ + "alpine-sdk", + } +) const postInstall = ` {{- if .PreInst}} @@ -51,7 +62,9 @@ epoch={{.Epoch}} pkgver={{.PkgVer}} pkgrel={{.PkgRel}} pkgdesc="{{.PkgDesc}}" -arch="all" +{{- if .ArchComputed}} +arch="{{.ArchComputed}}" +{{- end}} {{- if .Depends}} depends=" {{ range .Depends }}{{ . }} diff --git a/pkg/constants/constants.go b/pkg/constants/constants.go index ee17ca5..c9d8541 100644 --- a/pkg/constants/constants.go +++ b/pkg/constants/constants.go @@ -15,24 +15,28 @@ const ( var ( Releases = [...]string{ + "alma", "alpine", "amazon", "arch", "centos", "debian", "fedora", + "rhel", "rocky", "ubuntu", } DistroToPackageManager = map[string]string{ + "alma": "redhat", "alpine": "alpine", - "arch": "pacman", "amazon": "redhat", - "fedora": "redhat", + "arch": "pacman", "centos": "redhat", "debian": "debian", + "fedora": "redhat", "oracle": "redhat", + "rhel": "redhat", "rocky": "redhat", "ubuntu": "debian", } diff --git a/pkg/dpkg/constants.go b/pkg/dpkg/constants.go index ea806d6..b803716 100644 --- a/pkg/dpkg/constants.go +++ b/pkg/dpkg/constants.go @@ -28,8 +28,8 @@ Version: {{ if .Epoch}}{{ .Epoch }}:{{ end }}{{.PkgVer}} {{- if .PkgRel}}-{{ .PkgRel }}{{- end }} Section: {{.Section}} Priority: {{.Priority}} -{{- with .Arch}} -Architecture: {{join .}} +{{- if .ArchComputed}} +Architecture: {{.ArchComputed}} {{- end }} {{- /* Optional fields */ -}} {{- if .Maintainer}} @@ -77,3 +77,9 @@ Copyright: {{ range .Copyright}}{{ . }} License: {{ . }}{{- end }} {{- end }} ` +const ( + binaryContent = "2.0\n" + binaryFilename = "debian-binary" + controlFilename = "control.tar.zst" + dataFilename = "data.tar.zst" +) diff --git a/pkg/dpkg/dpkg.go b/pkg/dpkg/dpkg.go index ddacbaf..8eabdf3 100644 --- a/pkg/dpkg/dpkg.go +++ b/pkg/dpkg/dpkg.go @@ -1,7 +1,9 @@ package dpkg import ( + "context" "fmt" + "os" "path/filepath" "regexp" "strings" @@ -9,7 +11,10 @@ import ( "github.com/M0Rf30/yap/pkg/options" "github.com/M0Rf30/yap/pkg/pkgbuild" "github.com/M0Rf30/yap/pkg/utils" + "github.com/blakesmith/ar" + "github.com/mholt/archiver/v4" "github.com/otiai10/copy" + "github.com/pkg/errors" ) // Deb represents a Deb package. @@ -21,23 +26,40 @@ type Deb struct { PKGBUILD *pkgbuild.PKGBUILD } -// getArch updates the architecture field in the Deb struct. -// -// It iterates over the architecture values in the PKGBUILD field of the Deb struct -// and replaces them with the corresponding values from the DebArchs map. -func (d *Deb) getArch() { - for index, arch := range d.PKGBUILD.Arch { - d.PKGBUILD.Arch[index] = DebArchs[arch] +// createTarZst creates a compressed tar.zst archive from the specified source directory. +// It takes the source directory and the output file path as arguments and returns an error if any occurs. +func createTarZst(sourceDir, outputFile string) error { + // Retrieve the list of files from the source directory on disk. + // The map specifies that the files should be read from the sourceDir + // and the output path in the archive should be empty. + files, err := archiver.FilesFromDisk(nil, map[string]string{ + sourceDir + string(os.PathSeparator): "", + }) + + if err != nil { + return err + } + + cleanFilePath := filepath.Clean(outputFile) + + out, err := os.Create(cleanFilePath) + if err != nil { + return err + } + defer out.Close() + + format := archiver.CompressedArchive{ + Compression: archiver.Zstd{}, + Archival: archiver.Tar{}, } + + return format.Archive(context.Background(), out, files) } -// createConfFiles creates the conffiles file in the Deb package. -// -// It iterates over the Backup field of the PKGBUILD struct and adds each name -// to the data string. The data string is then written to the conffiles file -// located at the debDir path. -// -// Returns an error if there was a problem creating or writing to the file. +// createConfFiles creates the configuration files for the Debian package. +// It generates a file located at the debDir path containing the backup +// files specified in the PKGBUILD. Returns an error if there was a +// problem creating or writing to the file. func (d *Deb) createConfFiles() error { if len(d.PKGBUILD.Backup) == 0 { return nil @@ -58,6 +80,10 @@ func (d *Deb) createConfFiles() error { return utils.CreateWrite(path, data) } +// createCopyrightFile generates a copyright file for the Debian package. +// It checks if there is a license specified in the PKGBUILD and creates +// the copyright file accordingly. Returns an error if there was an +// issue creating the file. func (d *Deb) createCopyrightFile() error { if len(d.PKGBUILD.License) == 0 { return nil @@ -69,13 +95,10 @@ func (d *Deb) createCopyrightFile() error { return d.PKGBUILD.CreateSpec(copyrightFilePath, tmpl) } -// createDebconfFile creates a debconf file with the given variable and name. -// -// Parameters: -// - variable: the variable used to create the debconf asset. -// - name: the name of the debconf asset. -// -// Return type: error. +// createDebconfFile creates a debconf file with the given variable and +// name. It takes parameters for the variable used to create the debconf +// asset and the name of the debconf asset. Returns an error if there +// was an issue during the creation. func (d *Deb) createDebconfFile(name, variable string) error { if variable == "" { return nil @@ -88,9 +111,8 @@ func (d *Deb) createDebconfFile(name, variable string) error { } // createScripts generates and writes the scripts for the Deb package. -// -// It takes no parameters. -// It returns an error if there was an issue generating or writing the scripts. +// It takes no parameters and returns an error if there was an issue +// generating or writing the scripts. func (d *Deb) createScripts() error { scripts := map[string]string{ "preinst": d.PKGBUILD.PreInst, @@ -122,27 +144,55 @@ func (d *Deb) createScripts() error { return nil } -// dpkgDeb generates Deb package files from the given artifact path. -// -// It takes a string parameter `artifactPath` which represents the path where the -// Deb package files will be generated. -// -// The function returns an error if there was an issue generating the Deb package -// files. -func (d *Deb) dpkgDeb(artifactPath string) error { - for _, arch := range d.PKGBUILD.Arch { - artifactFilePath := filepath.Join(artifactPath, - fmt.Sprintf("%s_%s-%s_%s.deb", - d.PKGBUILD.PkgName, d.PKGBUILD.PkgVer, d.PKGBUILD.PkgRel, - arch)) - - if err := utils.Exec(true, "", - "dpkg-deb", - "-b", - "-Zzstd", - d.PKGBUILD.PackageDir, artifactFilePath); err != nil { - return err - } +// createDeb generates Deb package files from the given artifact path. +// It takes a string parameter `artifactPath` which represents the path +// where the Deb package files will be generated. The function returns +// an error if there was an issue generating the Deb package files. +func (d *Deb) createDeb(artifactPath, control, data string) error { + // Create the .deb package + artifactFilePath := filepath.Join(artifactPath, + fmt.Sprintf("%s_%s-%s_%s.deb", + d.PKGBUILD.PkgName, d.PKGBUILD.PkgVer, d.PKGBUILD.PkgRel, + d.PKGBUILD.ArchComputed)) + + cleanFilePath := filepath.Clean(artifactFilePath) + debianBinary := []byte(binaryContent) + + debPackage, err := os.Create(cleanFilePath) + if err != nil { + return err + } + defer debPackage.Close() + + cleanFilePath = filepath.Clean(control) + + controlArchive, err := os.ReadFile(cleanFilePath) + if err != nil { + return err + } + + cleanFilePath = filepath.Clean(data) + + dataArchive, err := os.ReadFile(cleanFilePath) + if err != nil { + return err + } + + writer := ar.NewWriter(debPackage) + if err := writer.WriteGlobalHeader(); err != nil { + return err + } + + if err := addArFile(writer, binaryFilename, debianBinary); err != nil { + return err + } + + if err := addArFile(writer, controlFilename, controlArchive); err != nil { + return err + } + + if err := addArFile(writer, dataFilename, dataArchive); err != nil { + return err } return nil @@ -247,7 +297,30 @@ func (d *Deb) createDebResources() error { // The method calls dpkgDeb to create the package and removes the // package directory, returning an error if any step fails. func (d *Deb) BuildPackage(artifactsPath string) error { - if err := d.dpkgDeb(artifactsPath); err != nil { + debTemp, err := os.MkdirTemp(d.PKGBUILD.StartDir, "temp") + if err != nil { + return err + } + defer os.RemoveAll(debTemp) + + controlArchive := filepath.Join(debTemp, controlFilename) + dataArchive := filepath.Join(debTemp, dataFilename) + + // Create control archive + if err := createTarZst(d.debDir, controlArchive); err != nil { + return err + } + + if err := utils.RemoveAll(d.debDir); err != nil { + return err + } + + // Create data archive + if err := createTarZst(d.PKGBUILD.PackageDir, dataArchive); err != nil { + return err + } + + if err := d.createDeb(artifactsPath, controlArchive, dataArchive); err != nil { return err } @@ -262,8 +335,8 @@ func (d *Deb) BuildPackage(artifactsPath string) error { // It retrieves architecture and release information, cleans up the debDir, creates necessary // resources, and strips binaries. The method returns an error if any step fails. func (d *Deb) PrepareFakeroot(_ string) error { - d.getArch() d.getRelease() + d.PKGBUILD.ArchComputed = DebArchs[d.PKGBUILD.ArchComputed] if err := utils.RemoveAll(d.debDir); err != nil { return err @@ -281,15 +354,13 @@ func (d *Deb) PrepareFakeroot(_ string) error { } func (d *Deb) Install(artifactsPath string) error { - for _, arch := range d.PKGBUILD.Arch { - artifactFilePath := filepath.Join(artifactsPath, - fmt.Sprintf("%s_%s-%s_%s.deb", - d.PKGBUILD.PkgName, d.PKGBUILD.PkgVer, d.PKGBUILD.PkgRel, - arch)) + artifactFilePath := filepath.Join(artifactsPath, + fmt.Sprintf("%s_%s-%s_%s.deb", + d.PKGBUILD.PkgName, d.PKGBUILD.PkgVer, d.PKGBUILD.PkgRel, + d.PKGBUILD.ArchComputed)) - if err := utils.Exec(false, "", "apt-get", "install", "-y", artifactFilePath); err != nil { - return err - } + if err := utils.Exec(false, "", "apt-get", "install", "-y", artifactFilePath); err != nil { + return err } return nil @@ -319,6 +390,22 @@ func (d *Deb) PrepareEnvironment(golang bool) error { return nil } +func addArFile(writer *ar.Writer, name string, body []byte) error { + header := ar.Header{ + Name: name, + Size: int64(len(body)), + Mode: 0o644, + } + + if err := writer.WriteHeader(&header); err != nil { + return errors.Errorf("cannot write file header") + } + + _, err := writer.Write(body) + + return err +} + func (d *Deb) getRelease() { if d.PKGBUILD.Codename != "" { d.PKGBUILD.PkgRel += d.PKGBUILD.Codename diff --git a/pkg/options/strip.go b/pkg/options/strip.go index 45cd624..1619125 100644 --- a/pkg/options/strip.go +++ b/pkg/options/strip.go @@ -1,7 +1,7 @@ package options // StripScript is a scriptlet taken from makepkg resources. It's executed by -// mvdan/sh interpreter and provides strip instructions to dpkg-buildpackage. +// mvdan/sh interpreter and provides strip instructions to debian packaging. // Although it's a very dirty solution, for now it's the faster way to have this // essential feature. const StripScript = ` diff --git a/pkg/pacman/constants.go b/pkg/pacman/constants.go index 8bba42b..e4fff60 100644 --- a/pkg/pacman/constants.go +++ b/pkg/pacman/constants.go @@ -56,11 +56,8 @@ epoch={{.Epoch}} pkgver={{.PkgVer}} pkgrel={{.PkgRel}} pkgdesc="{{.PkgDesc}}" -{{- if .Arch}} -arch=( - {{ range .Arch }}"{{ . }}" - {{ end }} -) +{{- if .ArchComputed}} +arch=({{.ArchComputed}}) {{- end }} {{- if .Depends}} depends=( diff --git a/pkg/pacman/pacman.go b/pkg/pacman/pacman.go index 1cfeb58..fe08227 100644 --- a/pkg/pacman/pacman.go +++ b/pkg/pacman/pacman.go @@ -63,24 +63,22 @@ func (p *Pacman) PrepareFakeroot(artifactsPath string) error { // artifactsPath: the path where the package artifacts are located. // error: an error if the installation fails. func (p *Pacman) Install(artifactsPath string) error { - for _, arch := range p.PKGBUILD.Arch { - pkgName := p.PKGBUILD.PkgName + "-" + - p.PKGBUILD.PkgVer + - "-" + - p.PKGBUILD.PkgRel + - "-" + - arch + - ".pkg.tar.zst" - - pkgFilePath := filepath.Join(artifactsPath, pkgName) - - if err := utils.Exec(false, "", - "pacman", - "-U", - "--noconfirm", - pkgFilePath); err != nil { - return err - } + pkgName := p.PKGBUILD.PkgName + "-" + + p.PKGBUILD.PkgVer + + "-" + + p.PKGBUILD.PkgRel + + "-" + + p.PKGBUILD.ArchComputed + + ".pkg.tar.zst" + + pkgFilePath := filepath.Join(artifactsPath, pkgName) + + if err := utils.Exec(false, "", + "pacman", + "-U", + "--noconfirm", + pkgFilePath); err != nil { + return err } return nil diff --git a/pkg/pkgbuild/pkgbuild.go b/pkg/pkgbuild/pkgbuild.go index 0856ff9..9c0bb71 100644 --- a/pkg/pkgbuild/pkgbuild.go +++ b/pkg/pkgbuild/pkgbuild.go @@ -20,6 +20,7 @@ var Verbose bool // templating and other rpm/deb descriptors. type PKGBUILD struct { Arch []string + ArchComputed string Backup []string Build string Codename string @@ -340,6 +341,31 @@ func (pkgBuild *PKGBUILD) parseDirective(input string) (string, int, error) { return key, priority, nil } +// ValidateArchitecture checks if the architecture specified in the PKGBUILD +// is supported. If the architecture is "any", it sets the computed architecture +// to "any". If the architecture is not "any", it checks if the current architecture +// is in the list of supported architectures. If the current architecture is not +// supported, it logs a fatal error with the package name. Finally, it sets the +// computed architecture to the current architecture if it is supported. +func (pkgBuild *PKGBUILD) ValidateArchitecture() { + isSupported := utils.Contains(pkgBuild.Arch, "any") + if isSupported { + pkgBuild.ArchComputed = "any" + + return + } + + currentArch := utils.GetArchitecture() + + isSupported = utils.Contains(pkgBuild.Arch, currentArch) + if !isSupported { + utils.Logger.Fatal("unsupported architecture", + utils.Logger.Args("pkgname", pkgBuild.PkgName)) + } + + pkgBuild.ArchComputed = currentArch +} + // setMainFolders sets the main folders for the PKGBUILD. // // It takes no parameters. diff --git a/pkg/project/project.go b/pkg/project/project.go index d715ed4..0964f48 100644 --- a/pkg/project/project.go +++ b/pkg/project/project.go @@ -126,7 +126,7 @@ func (mpc *MultipleProject) BuildAll() error { } if !NoBuild { - if err := mpc.createPackages(proj); err != nil { + if err := mpc.createPackage(proj); err != nil { return err } } @@ -249,11 +249,11 @@ func (mpc *MultipleProject) checkPkgsRange(fromPkgName, toPkgName string) { } } -// createPackages creates packages for the MultipleProject. +// createPackage creates packages for the MultipleProject. // // It takes a pointer to a MultipleProject as a receiver and a pointer to a Project as a parameter. // It returns an error. -func (mpc *MultipleProject) createPackages(proj *Project) error { +func (mpc *MultipleProject) createPackage(proj *Project) error { if mpc.Output != "" { absOutput, err := filepath.Abs(mpc.Output) if err != nil { @@ -337,6 +337,8 @@ func (mpc *MultipleProject) populateProjects(distro, release, path string) error return err } + pkgbuildFile.ValidateArchitecture() + packageManager = packer.GetPackageManager(pkgbuildFile, distro) proj := &Project{ diff --git a/pkg/project/project_test.go b/pkg/project/project_test.go index 96c2cf1..4d46a05 100644 --- a/pkg/project/project_test.go +++ b/pkg/project/project_test.go @@ -19,7 +19,7 @@ pkgdesc__fedora="Http file server written with Go for Fedora" pkgdesc__rocky="Http file server written with Go for Rocky" pkgdesc__ubuntu="Http file server written with Go for Ubuntu" maintainer="Example " -arch=("all") +arch=("x86_64") license=("GPL-3.0-only") section="utils" priority="optional" diff --git a/pkg/rpm/constants.go b/pkg/rpm/constants.go index 44846b9..be19987 100644 --- a/pkg/rpm/constants.go +++ b/pkg/rpm/constants.go @@ -71,8 +71,10 @@ var ( } RPMDistros = map[string]string{ + "alma": ".el", "amazon": ".amzn", "fedora": ".fc", + "rhel": ".el", "rocky": ".el", } ) @@ -86,8 +88,8 @@ Epoch: {{.Epoch}} {{- end }} Version: {{.PkgVer}} Release: {{.PkgRel}} -{{- with .Arch}} -BuildArch: {{join .}} +{{- if .ArchComputed}} +BuildArch: {{.ArchComputed}} {{- end }} {{- if .Section}} Group: {{.Section}} diff --git a/pkg/rpm/rpm.go b/pkg/rpm/rpm.go index 0bc41a2..c16aec8 100644 --- a/pkg/rpm/rpm.go +++ b/pkg/rpm/rpm.go @@ -40,9 +40,9 @@ func (r *RPM) BuildPackage(_ string) error { // It also processes package dependencies and creates the RPM spec file, returning // an error if any step fails. func (r *RPM) PrepareFakeroot(artifactsPath string) error { - r.getArch() r.getGroup() r.getRelease() + r.PKGBUILD.ArchComputed = RPMArchs[r.PKGBUILD.ArchComputed] r.PKGBUILD.PkgDest, _ = filepath.Abs(artifactsPath) @@ -63,7 +63,7 @@ func (r *RPM) PrepareFakeroot(artifactsPath string) error { r.PKGBUILD.PkgName, r.PKGBUILD.PkgVer, r.PKGBUILD.PkgRel, - r.PKGBUILD.Arch[0]) + r.PKGBUILD.ArchComputed) if err := copy.Copy(r.PKGBUILD.PackageDir, buildRootPackageDir); err != nil { return err @@ -89,25 +89,23 @@ func (r *RPM) PrepareFakeroot(artifactsPath string) error { // // It returns an error if there was an issue during the installation process. func (r *RPM) Install(artifactsPath string) error { - for _, arch := range r.PKGBUILD.Arch { - pkgName := r.PKGBUILD.PkgName + - "-" + - r.PKGBUILD.PkgVer + - "-" + - r.PKGBUILD.PkgRel + - "." + - RPMArchs[arch] + - ".rpm" - - pkgFilePath := filepath.Join(artifactsPath, RPMArchs[arch], pkgName) - - if err := utils.Exec(false, "", - "dnf", - "install", - "-y", - pkgFilePath); err != nil { - return err - } + pkgName := r.PKGBUILD.PkgName + + "-" + + r.PKGBUILD.PkgVer + + "-" + + r.PKGBUILD.PkgRel + + "." + + r.PKGBUILD.ArchComputed + + ".rpm" + + pkgFilePath := filepath.Join(artifactsPath, r.PKGBUILD.ArchComputed, pkgName) + + if err := utils.Exec(false, "", + "dnf", + "install", + "-y", + pkgFilePath); err != nil { + return err } return nil @@ -239,16 +237,6 @@ func (r *RPM) getFiles() error { return nil } -// getArch updates the architecture values in the RPM struct. -// -// It does not take any parameters. -// It does not return anything. -func (r *RPM) getArch() { - for index, arch := range r.PKGBUILD.Arch { - r.PKGBUILD.Arch[index] = RPMArchs[arch] - } -} - // getGroup updates the section of the RPM struct with the corresponding // value from the RPMGroups map. // @@ -346,6 +334,8 @@ func (r *RPM) rpmBuild() error { "-bb", r.PKGBUILD.PkgName + ".spec", + "--target", + r.PKGBUILD.ArchComputed, } if pkgbuild.Verbose { diff --git a/pkg/utils/os.go b/pkg/utils/os.go new file mode 100644 index 0000000..0d44a0d --- /dev/null +++ b/pkg/utils/os.go @@ -0,0 +1,31 @@ +package utils + +import ( + "runtime" +) + +// GetArchitecture returns the corresponding uname -m output for the current GOARCH. +func GetArchitecture() string { + // Create a map of GOARCH values to uname -m outputs + architectureMap := map[string]string{ + "amd64": "x86_64", + "386": "i386", + "arm": "armv7l", // Common for ARM 32-bit + "arm64": "aarch64", // Common for ARM 64-bit + "ppc64": "ppc64", + "ppc64le": "ppc64le", + "s390x": "s390x", + "mips": "mips", + "mipsle": "mipsle", + "riscv64": "riscv64", + // Add more mappings as needed + } + + // Get the current architecture using runtime.GOARCH + currentArch := runtime.GOARCH + + // Get the corresponding uname -m output from the map + unameOutput := architectureMap[currentArch] + + return unameOutput +} diff --git a/pkg/utils/strings.go b/pkg/utils/strings.go index 4d730cd..a589bc6 100644 --- a/pkg/utils/strings.go +++ b/pkg/utils/strings.go @@ -62,3 +62,14 @@ func StringifyFuncDecl(node *syntax.FuncDecl) string { return funcDecl } + +// Contains checks if a string is present in an array of strings. +func Contains(array []string, str string) bool { + for _, item := range array { + if item == str { + return true + } + } + + return false +}