Convert distro.ParseOSRelease() into a singleton

This commit is contained in:
Elara 2022-12-28 18:40:20 -08:00
parent 0b4cfd8827
commit 1ae30bdf78

View File

@ -46,8 +46,16 @@ type OSRelease struct {
Logo string
}
// OSReleaseName returns the NAME field of the
var parsed *OSRelease
// OSReleaseName returns a struct parsed from the system's os-release
// file. It checks /etc/os-release as well as /usr/lib/os-release.
// The returned OSRelease struct is a singleton.
func ParseOSRelease(ctx context.Context) (*OSRelease, error) {
if parsed != nil {
return parsed, nil
}
fl, err := os.Open("/usr/lib/os-release")
if err != nil {
fl, err = os.Open("/etc/os-release")
@ -99,5 +107,6 @@ func ParseOSRelease(ctx context.Context) (*OSRelease, error) {
out.Like = strings.Split(runner.Vars["ID_LIKE"].Str, " ")
}
parsed = out
return out, nil
}