The function is a leftover from the image definitions split and it is not used. Moreover, the `images` copy of it is being reimplemented by [1]. It is better to remove this copy to prevent any unintended use of it or confusion. [1] https://github.com/osbuild/images/pull/195 Signed-off-by: Tomáš Hozza <thozza@redhat.com>
53 lines
1.2 KiB
Go
53 lines
1.2 KiB
Go
package common
|
|
|
|
import (
|
|
"bufio"
|
|
"errors"
|
|
"io"
|
|
"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"
|
|
|
|
version := strings.Split(osrelease["VERSION_ID"], ".")
|
|
name := osrelease["ID"] + "-" + strings.Join(version, "")
|
|
|
|
// TODO: We should probably index these things by the full CPE
|
|
beta := strings.Contains(osrelease["CPE_NAME"], "beta")
|
|
return name, beta, isStream, 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])
|
|
// drop all surrounding whitespace and double-quotes
|
|
value := strings.Trim(strings.TrimSpace(parts[1]), "\"")
|
|
osrelease[key] = value
|
|
}
|
|
|
|
return osrelease, nil
|
|
}
|