From e45d9be065138b8ea239b581ad62804513f58335 Mon Sep 17 00:00:00 2001 From: James Shubin Date: Tue, 15 Oct 2024 20:33:10 -0400 Subject: [PATCH] util: distro: Parse the os-release file --- go.mod | 1 + go.sum | 2 ++ util/distro/distro.go | 53 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+) diff --git a/go.mod b/go.mod index e520d2ab..2a14d590 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index fcaab50c..22acfd99 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/util/distro/distro.go b/util/distro/distro.go index cbb4d578..3031d95f 100644 --- a/util/distro/distro.go +++ b/util/distro/distro.go @@ -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 +}