refractor(build): move os logic to its own module.
This commit is contained in:
parent
662dd1c6dc
commit
e1d1d0be3d
9 changed files with 158 additions and 86 deletions
90
pkg/os/os.go
Normal file
90
pkg/os/os.go
Normal file
|
|
@ -0,0 +1,90 @@
|
|||
// apparmor.d - Full set of apparmor profiles
|
||||
// Copyright (C) 2023-2024 Alexandre Pujol <alexandre@pujol.io>
|
||||
// SPDX-License-Identifier: GPL-2.0-only
|
||||
|
||||
package util
|
||||
|
||||
import (
|
||||
"os"
|
||||
"slices"
|
||||
"strings"
|
||||
|
||||
"github.com/arduino/go-paths-helper"
|
||||
)
|
||||
|
||||
var (
|
||||
Distribution = getDistribution()
|
||||
Release = getOSRelease()
|
||||
Family = getFamily()
|
||||
)
|
||||
|
||||
var (
|
||||
osReleaseFile = "/etc/os-release"
|
||||
supportedDists = map[string][]string{
|
||||
"arch": {},
|
||||
"debian": {},
|
||||
"ubuntu": {},
|
||||
"opensuse": {"suse", "opensuse-tumbleweed"},
|
||||
"whonix": {},
|
||||
}
|
||||
famillyDists = map[string][]string{
|
||||
"apt": {"debian", "ubuntu", "whonix"},
|
||||
"pacman": {"arch"},
|
||||
"zypper": {"opensuse"},
|
||||
}
|
||||
)
|
||||
|
||||
func getOSRelease() map[string]string {
|
||||
var lines []string
|
||||
var err error
|
||||
for _, name := range []string{osReleaseFile, "/usr/lib/os-release"} {
|
||||
path := paths.New(name)
|
||||
if path.Exist() {
|
||||
lines, err = path.ReadFileAsLines()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
os := map[string]string{}
|
||||
for _, line := range lines {
|
||||
item := strings.Split(line, "=")
|
||||
if len(item) == 2 {
|
||||
os[item[0]] = strings.Trim(item[1], "\"")
|
||||
}
|
||||
}
|
||||
return os
|
||||
}
|
||||
|
||||
func getDistribution() string {
|
||||
dist, present := os.LookupEnv("DISTRIBUTION")
|
||||
if present {
|
||||
return dist
|
||||
}
|
||||
|
||||
id := Release["ID"]
|
||||
if id == "ubuntu" {
|
||||
return id
|
||||
}
|
||||
id_like := Release["ID_LIKE"]
|
||||
for main, based := range supportedDists {
|
||||
if main == id || main == id_like {
|
||||
return main
|
||||
} else if slices.Contains(based, id) {
|
||||
return main
|
||||
} else if slices.Contains(based, id_like) {
|
||||
return main
|
||||
}
|
||||
}
|
||||
return id
|
||||
}
|
||||
|
||||
func getFamily() string {
|
||||
for familly, dist := range famillyDists {
|
||||
if slices.Contains(dist, Distribution) {
|
||||
return familly
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue