Move GetRedHatRelease() and GetHostDistroName() to common package
The `distro` package is now used for distro definitions supported by osbuild-composer, not for introspecting the Host system. Move `GetRedHatRelease()` and `GetHostDistroName()` functions to the `common` package.
This commit is contained in:
parent
804d4210df
commit
c7e5e3c9c2
7 changed files with 85 additions and 80 deletions
79
internal/common/distro.go
Normal file
79
internal/common/distro.go
Normal file
|
|
@ -0,0 +1,79 @@
|
|||
package common
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func GetHostDistroName() (string, bool, bool, error) {
|
||||
f, err := os.Open("/etc/os-release")
|
||||
if err != nil {
|
||||
return "", false, false, err
|
||||
}
|
||||
defer f.Close()
|
||||
osrelease, err := readOSRelease(f)
|
||||
if err != nil {
|
||||
return "", false, false, err
|
||||
}
|
||||
|
||||
isStream := osrelease["NAME"] == "CentOS Stream"
|
||||
|
||||
// NOTE: We only consider major releases up until rhel 8.4
|
||||
version := strings.Split(osrelease["VERSION_ID"], ".")
|
||||
name := osrelease["ID"] + "-" + version[0]
|
||||
if osrelease["ID"] == "rhel" && ((version[0] == "8" && version[1] >= "4") || version[0] == "9") {
|
||||
name = name + version[1]
|
||||
}
|
||||
|
||||
// TODO: We should probably index these things by the full CPE
|
||||
beta := strings.Contains(osrelease["CPE_NAME"], "beta")
|
||||
return name, beta, isStream, nil
|
||||
}
|
||||
|
||||
// GetRedHatRelease returns the content of /etc/redhat-release
|
||||
// without the trailing new-line.
|
||||
func GetRedHatRelease() (string, error) {
|
||||
raw, err := ioutil.ReadFile("/etc/redhat-release")
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("cannot read /etc/redhat-release: %v", err)
|
||||
}
|
||||
|
||||
//Remove the trailing new-line.
|
||||
redHatRelease := strings.TrimSpace(string(raw))
|
||||
|
||||
return redHatRelease, nil
|
||||
}
|
||||
|
||||
func readOSRelease(r io.Reader) (map[string]string, error) {
|
||||
osrelease := make(map[string]string)
|
||||
scanner := bufio.NewScanner(r)
|
||||
for scanner.Scan() {
|
||||
line := strings.TrimSpace(scanner.Text())
|
||||
if len(line) == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
parts := strings.SplitN(line, "=", 2)
|
||||
if len(parts) != 2 {
|
||||
return nil, errors.New("readOSRelease: invalid input")
|
||||
}
|
||||
|
||||
key := strings.TrimSpace(parts[0])
|
||||
value := strings.TrimSpace(parts[1])
|
||||
if value[0] == '"' {
|
||||
if len(value) < 2 || value[len(value)-1] != '"' {
|
||||
return nil, errors.New("readOSRelease: invalid input")
|
||||
}
|
||||
value = value[1 : len(value)-1]
|
||||
}
|
||||
|
||||
osrelease[key] = value
|
||||
}
|
||||
|
||||
return osrelease, nil
|
||||
}
|
||||
54
internal/common/distro_test.go
Normal file
54
internal/common/distro_test.go
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
package common
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestOSRelease(t *testing.T) {
|
||||
var cases = []struct {
|
||||
Input string
|
||||
OSRelease map[string]string
|
||||
}{
|
||||
{
|
||||
``,
|
||||
map[string]string{},
|
||||
},
|
||||
{
|
||||
`NAME=Fedora
|
||||
VERSION="30 (Workstation Edition)"
|
||||
ID=fedora
|
||||
VERSION_ID=30
|
||||
VERSION_CODENAME=""
|
||||
PLATFORM_ID="platform:f30"
|
||||
PRETTY_NAME="Fedora 30 (Workstation Edition)"
|
||||
VARIANT="Workstation Edition"
|
||||
VARIANT_ID=workstation`,
|
||||
map[string]string{
|
||||
"NAME": "Fedora",
|
||||
"VERSION": "30 (Workstation Edition)",
|
||||
"ID": "fedora",
|
||||
"VERSION_ID": "30",
|
||||
"VERSION_CODENAME": "",
|
||||
"PLATFORM_ID": "platform:f30",
|
||||
"PRETTY_NAME": "Fedora 30 (Workstation Edition)",
|
||||
"VARIANT": "Workstation Edition",
|
||||
"VARIANT_ID": "workstation",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for i, c := range cases {
|
||||
r := strings.NewReader(c.Input)
|
||||
|
||||
osrelease, err := readOSRelease(r)
|
||||
if err != nil {
|
||||
t.Fatalf("%d: readOSRelease: %v", i, err)
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(osrelease, c.OSRelease) {
|
||||
t.Fatalf("%d: readOSRelease returned unexpected result: %#v", i, osrelease)
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue