Skip to content

Commit

Permalink
feat: add automatic distro detection
Browse files Browse the repository at this point in the history
  • Loading branch information
M0Rf30 committed Nov 1, 2024
1 parent b712b87 commit 3a084bd
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 26 deletions.
27 changes: 19 additions & 8 deletions cmd/yap/command/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,28 @@ import (
var (
// buildCmd represents the command to build the entire project.
buildCmd = &cobra.Command{
Use: "build [distro] [path]",
Use: "build [distro] path",
Short: "Build multiple PKGBUILD definitions within a yap.json project",
Args: cobra.MinimumNArgs(2),
Args: cobra.RangeArgs(1, 2), // Allow 1 or 2 arguments
Run: func(_ *cobra.Command, args []string) {
fullJSONPath, _ := filepath.Abs(args[1])
split := strings.Split(args[0], "-")
distro := split[0]
release := ""
fullJSONPath, _ := filepath.Abs(args[len(args)-1]) // Always take the last argument as path
var distro, release string

if len(split) > 1 {
release = split[1]
if len(args) == 2 {
split := strings.Split(args[0], "-")
distro = split[0]

if len(split) > 1 {
release = split[1]
}
}

// Use the default distro if none is provided
if distro == "" {
osRelease, _ := utils.ParseOSRelease()
distro = osRelease.ID
utils.Logger.Warn("distro not specified, using detected",
utils.Logger.Args("distro", distro))
}

mpc := project.MultipleProject{}
Expand Down
35 changes: 22 additions & 13 deletions pkg/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,40 @@ const (
)

var (
// These values are not invented,
// but refer to /etc/os-release ID field values.
Releases = [...]string{
"alma",
"almalinux",
"alpine",
"amazon",
"amzn",
"arch",
"centos",
"debian",
"fedora",
"linuxmint",
"opensuse-leap",
"ol",
"pop",
"rhel",
"rocky",
"ubuntu",
}

DistroToPackageManager = map[string]string{
"alma": "rpm",
"alpine": "apk",
"amazon": "rpm",
"arch": "pacman",
"centos": "rpm",
"debian": "dpkg",
"fedora": "rpm",
"oracle": "rpm",
"rhel": "rpm",
"rocky": "rpm",
"ubuntu": "dpkg",
"almalinux": "rpm",
"alpine": "apk",
"amzn": "rpm",
"arch": "pacman",
"centos": "rpm",
"debian": "dpkg",
"fedora": "rpm",
"linuxmint": "dpkg",
"ol": "rpm",
"opensuse-leap": "rpm",
"pop": "dpkg",
"rhel": "rpm",
"rocky": "rpm",
"ubuntu": "dpkg",
}

ReleasesSet = set.NewSet()
Expand Down
11 changes: 6 additions & 5 deletions pkg/rpm/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,11 @@ var (
}

RPMDistros = map[string]string{
"alma": ".el",
"amazon": ".amzn",
"fedora": ".fc",
"rhel": ".el",
"rocky": ".el",
"almalinux": ".el",
"amzn": ".amzn",
"fedora": ".fc",
"ol": ".ol",
"rhel": ".el",
"rocky": ".el",
}
)
50 changes: 50 additions & 0 deletions pkg/utils/os.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,59 @@
package utils

import (
"bufio"
"os"
"runtime"
"strings"
)

// OSRelease represents the contents of the /etc/os-release file.
type OSRelease struct {
ID string
}

// ParseOSRelease reads the /etc/os-release file and populates the OSRelease struct.
func ParseOSRelease() (OSRelease, error) {
file, err := os.Open("/etc/os-release")
if err != nil {
return OSRelease{}, err
}
defer file.Close()

var osRelease OSRelease

scanner := bufio.NewScanner(file)

// Map to associate keys with struct fields
fieldMap := map[string]*string{
"ID": &osRelease.ID,
}

for scanner.Scan() {
line := scanner.Text()
if line == "" || strings.HasPrefix(line, "#") {
continue
}

parts := strings.SplitN(line, "=", 2)

if len(parts) == 2 {
key := strings.TrimSpace(parts[0])
value := strings.Trim(parts[1], "\"")

if fieldPtr, ok := fieldMap[key]; ok {
*fieldPtr = value
}
}
}

if err := scanner.Err(); err != nil {
return OSRelease{}, err
}

return osRelease, nil
}

// GetArchitecture returns the corresponding pacman architecture for the current GOARCH.
func GetArchitecture() string {
// Create a map of GOARCH values to pacman arch ones
Expand Down

0 comments on commit 3a084bd

Please sign in to comment.