diff --git a/src/go.mod b/src/go.mod index f460328..ca0a8f9 100644 --- a/src/go.mod +++ b/src/go.mod @@ -4,6 +4,7 @@ go 1.13 require ( github.com/HarryMichal/go-version v1.0.0 + github.com/acobaugh/osrelease v0.0.0-20181218015638-a93a0a55a249 github.com/godbus/dbus/v5 v5.0.3 github.com/sirupsen/logrus v1.5.0 github.com/spf13/cobra v0.0.6 diff --git a/src/go.sum b/src/go.sum index 9ec138d..9698a11 100644 --- a/src/go.sum +++ b/src/go.sum @@ -3,6 +3,8 @@ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03 github.com/HarryMichal/go-version v1.0.0 h1:fXYa5vT46C3pULfSIgnfeNfSxJ9bCGZ2ERn/wKPlD6c= github.com/HarryMichal/go-version v1.0.0/go.mod h1:w3uLQ2NlFmZ01qBywppIbDplbPEjeBW8xywlluMcMsc= github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU= +github.com/acobaugh/osrelease v0.0.0-20181218015638-a93a0a55a249 h1:fMi9ZZ/it4orHj3xWrM6cLkVFcCbkXQALFUiNtHtCPs= +github.com/acobaugh/osrelease v0.0.0-20181218015638-a93a0a55a249/go.mod h1:iU1PxQMQwoHZZWmMKrMkrNlY+3+p9vxIjpZOVyxWa0g= github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8= diff --git a/src/pkg/utils/utils.go b/src/pkg/utils/utils.go index 57ab574..4883147 100644 --- a/src/pkg/utils/utils.go +++ b/src/pkg/utils/utils.go @@ -27,6 +27,7 @@ import ( "strings" "syscall" + "github.com/acobaugh/osrelease" "github.com/containers/toolbox/pkg/shell" "github.com/godbus/dbus/v5" "github.com/sirupsen/logrus" @@ -184,6 +185,45 @@ func GetGroupForSudo() (string, error) { return "", errors.New("group for sudo not found") } +// GetHostID returns the ID from the os-release files +// +// Examples: +// - host is Fedora, returned string is 'fedora' +func GetHostID() (string, error) { + osRelease, err := osrelease.Read() + if err != nil { + return "", err + } + + return osRelease["ID"], nil +} + +// GetHostVariantID returns the VARIANT_ID from the os-release files +// +// Examples: +// - host is Fedora Workstation, returned string is 'workstation' +func GetHostVariantID() (string, error) { + osRelease, err := osrelease.Read() + if err != nil { + return "", err + } + + return osRelease["VARIANT_ID"], nil +} + +// GetHostVersionID returns the VERSION_ID from the os-release files +// +// Examples: +// - host is Fedora 32, returned string is '32' +func GetHostVersionID() (string, error) { + osRelease, err := osrelease.Read() + if err != nil { + return "", err + } + + return osRelease["VERSION_ID"], nil +} + // GetMountPoint returns the mount point of a target. func GetMountPoint(target string) (string, error) { var stdout strings.Builder