pkg/utils: Add functions for reading various os-release fields

https://github.com/containers/toolbox/pull/318
This commit is contained in:
Debarshi Ray 2020-04-29 15:00:06 +02:00
parent 2edc30836b
commit 9e2825524a
3 changed files with 43 additions and 0 deletions

View file

@ -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

View file

@ -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=

View file

@ -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