refractor(build): move os logic to its own module.

This commit is contained in:
Alexandre Pujol 2024-03-21 18:58:32 +00:00
parent 662dd1c6dc
commit e1d1d0be3d
No known key found for this signature in database
GPG key ID: C5469996F0DF68EC
9 changed files with 158 additions and 86 deletions

View file

@ -5,71 +5,11 @@
package prebuild
import (
"os"
"strings"
"github.com/arduino/go-paths-helper"
"golang.org/x/exp/slices"
)
var (
osReleaseFile = "/etc/os-release"
supportedDists = map[string][]string{
"arch": {},
"debian": {},
"ubuntu": {},
"opensuse": {"suse", "opensuse-tumbleweed"},
"whonix": {},
}
)
func NewOSRelease() 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 getSupportedDistribution() string {
dist, present := os.LookupEnv("DISTRIBUTION")
if present {
return dist
}
os := NewOSRelease()
id := os["ID"]
if id == "ubuntu" {
return id
}
id_like := os["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 copyTo(src *paths.Path, dst *paths.Path) error {
files, err := src.ReadDirRecursiveFiltered(nil, paths.FilterOutDirectories(), paths.FilterOutNames("README.md"))
if err != nil {