util: distro: Parse the os-release file

This commit is contained in:
James Shubin
2024-10-15 20:33:10 -04:00
parent d24149518c
commit e45d9be065
3 changed files with 56 additions and 0 deletions

1
go.mod
View File

@@ -52,6 +52,7 @@ require (
github.com/ProtonMail/go-crypto v1.0.0 // indirect
github.com/alexflint/go-scalar v1.2.0 // indirect
github.com/armon/go-metrics v0.4.1 // indirect
github.com/ashcrow/osrelease v0.0.0-20180626175927-9b292693c55c // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/bits-and-blooms/bitset v1.13.0 // indirect
github.com/cenkalti/backoff/v4 v4.3.0 // indirect

2
go.sum
View File

@@ -31,6 +31,8 @@ github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310/go.mod h1:ufUuZ+zHj
github.com/armon/go-radix v1.0.0/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8=
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPdPJAN/hZIm0C4OItdklCFmMRWYpio=
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs=
github.com/ashcrow/osrelease v0.0.0-20180626175927-9b292693c55c h1:icme0QhxrgZOxTBnT6K8dfGLwbKWSOVwPB95XTbo8Ws=
github.com/ashcrow/osrelease v0.0.0-20180626175927-9b292693c55c/go.mod h1:BRljTyotlu+6N+Qlu5MhjxpdmccCnp9lDvZjNNV8qr4=
github.com/aws/aws-sdk-go v1.51.30 h1:RVFkjn9P0JMwnuZCVH0TlV5k9zepHzlbc4943eZMhGw=
github.com/aws/aws-sdk-go v1.51.30/go.mod h1:LF8svs817+Nz+DmiMQKTO3ubZ/6IaTpq3TjupRn3Eqk=
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=

View File

@@ -34,6 +34,8 @@ package distro
import (
"context"
"os"
"github.com/ashcrow/osrelease"
)
const (
@@ -136,3 +138,54 @@ func IsFamilyArchLinux(ctx context.Context) (bool, error) {
}
return true, nil
}
// Distro returns the distro name.
func Distro(ctx context.Context) (string, error) {
output, err := parseOSRelease(ctx)
if err != nil {
return "", err
}
return output.ID, nil
}
// IsDistroDebian detects if the os distro is debian. (Not ubuntu!)
func IsDistroDebian(ctx context.Context) (bool, error) {
output, err := parseOSRelease(ctx)
if err != nil {
return false, err
}
if output.ID == DistroDebian {
return true, nil
}
return false, nil
}
// IsDistroFedora detects if the os distro is fedora.
func IsDistroFedora(ctx context.Context) (bool, error) {
output, err := parseOSRelease(ctx)
if err != nil {
return false, err
}
if output.ID == DistroFedora {
return true, nil
}
return false, nil
}
// IsDistroArchLinux detects if the os distro is archlinux.
func IsDistroArchLinux(ctx context.Context) (bool, error) {
// TODO: Are there other distros in the archlinux family?
return IsFamilyArchLinux(ctx)
}
// parseOSRelease is a simple helper function to parse the /etc/os-release file.
// TODO: We could probably implement our own cleaner parser eventually.
// TODO: Cache the result in a global if we don't care about changes.
func parseOSRelease(ctx context.Context) (*osrelease.OSRelease, error) {
// TODO: use ctx around io operations
output, err := osrelease.New()
if err != nil {
return nil, err
}
return &output, nil
}