Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check if path exists and is nonempty before reading host files #35

Merged
merged 3 commits into from
Nov 12, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 16 additions & 14 deletions host/host_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ func getOSRelease() (platform string, version string, err error) {

func getLSB() (*LSB, error) {
ret := &LSB{}
if common.PathExists(common.HostEtc("lsb-release")) {
if common.PathExistsWithContents(common.HostEtc("lsb-release")) {
contents, err := common.ReadLines(common.HostEtc("lsb-release"))
if err != nil {
return ret, err // return empty
Expand All @@ -210,7 +210,7 @@ func getLSB() (*LSB, error) {
ret.Description = field[1]
}
}
} else if common.PathExists("/usr/bin/lsb_release") {
} else if common.PathExistsWithContents("/usr/bin/lsb_release") {
lsb_release, err := exec.LookPath("/usr/bin/lsb_release")
if err != nil {
return ret, err
Expand Down Expand Up @@ -248,34 +248,34 @@ func PlatformInformation() (platform string, family string, version string, err
lsb = &LSB{}
}

if common.PathExists(common.HostEtc("oracle-release")) {
if common.PathExistsWithContents(common.HostEtc("oracle-release")) {
platform = "oracle"
contents, err := common.ReadLines(common.HostEtc("oracle-release"))
if err == nil {
version = getRedhatishVersion(contents)
}

} else if common.PathExists(common.HostEtc("enterprise-release")) {
} else if common.PathExistsWithContents(common.HostEtc("enterprise-release")) {
platform = "oracle"
contents, err := common.ReadLines(common.HostEtc("enterprise-release"))
if err == nil {
version = getRedhatishVersion(contents)
}
} else if common.PathExists(common.HostEtc("slackware-version")) {
} else if common.PathExistsWithContents(common.HostEtc("slackware-version")) {
platform = "slackware"
contents, err := common.ReadLines(common.HostEtc("slackware-version"))
if err == nil {
version = getSlackwareVersion(contents)
}
} else if common.PathExists(common.HostEtc("debian_version")) {
} else if common.PathExistsWithContents(common.HostEtc("debian_version")) {
if lsb.ID == "Ubuntu" {
platform = "ubuntu"
version = lsb.Release
} else if lsb.ID == "LinuxMint" {
platform = "linuxmint"
version = lsb.Release
} else {
if common.PathExists("/usr/bin/raspi-config") {
if common.PathExistsWithContents("/usr/bin/raspi-config") {
platform = "raspbian"
} else {
platform = "debian"
Expand All @@ -285,41 +285,41 @@ func PlatformInformation() (platform string, family string, version string, err
version = contents[0]
}
}
} else if common.PathExists(common.HostEtc("redhat-release")) {
} else if common.PathExistsWithContents(common.HostEtc("redhat-release")) {
contents, err := common.ReadLines(common.HostEtc("redhat-release"))
if err == nil {
version = getRedhatishVersion(contents)
platform = getRedhatishPlatform(contents)
}
} else if common.PathExists(common.HostEtc("system-release")) {
} else if common.PathExistsWithContents(common.HostEtc("system-release")) {
contents, err := common.ReadLines(common.HostEtc("system-release"))
if err == nil {
version = getRedhatishVersion(contents)
platform = getRedhatishPlatform(contents)
}
} else if common.PathExists(common.HostEtc("gentoo-release")) {
} else if common.PathExistsWithContents(common.HostEtc("gentoo-release")) {
platform = "gentoo"
contents, err := common.ReadLines(common.HostEtc("gentoo-release"))
if err == nil {
version = getRedhatishVersion(contents)
}
} else if common.PathExists(common.HostEtc("SuSE-release")) {
} else if common.PathExistsWithContents(common.HostEtc("SuSE-release")) {
contents, err := common.ReadLines(common.HostEtc("SuSE-release"))
if err == nil {
version = getSuseVersion(contents)
platform = getSusePlatform(contents)
}
// TODO: slackware detecion
} else if common.PathExists(common.HostEtc("arch-release")) {
} else if common.PathExistsWithContents(common.HostEtc("arch-release")) {
platform = "arch"
version = lsb.Release
} else if common.PathExists(common.HostEtc("alpine-release")) {
} else if common.PathExistsWithContents(common.HostEtc("alpine-release")) {
platform = "alpine"
contents, err := common.ReadLines(common.HostEtc("alpine-release"))
if err == nil && len(contents) > 0 {
version = contents[0]
}
} else if common.PathExists(common.HostEtc("os-release")) {
} else if common.PathExistsWithContents(common.HostEtc("os-release")) {
p, v, err := getOSRelease()
if err == nil {
platform = p
Expand All @@ -342,6 +342,8 @@ func PlatformInformation() (platform string, family string, version string, err
version = lsb.Release
}

platform = strings.Trim(platform, "\"")

switch platform {
case "debian", "ubuntu", "linuxmint", "raspbian":
family = "debian"
Expand Down
19 changes: 19 additions & 0 deletions internal/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,25 @@ func PathExists(filename string) bool {
return false
}

func PathExistsWithContents(filename string) bool {
if _, err := os.Stat(filename); err != nil {
return false
}

f, err := os.Open(filename)
if err != nil {
return false
}
defer f.Close()

r := bufio.NewReader(f)
_, err = r.ReadString('\n')
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please use r.ReadSlice('\n') here, this would not create an unused string.Builder

if err != nil {
return false
}
return true
}

//GetEnv retrieves the environment variable key. If it does not exist it returns the default.
func GetEnv(key string, dfault string, combineWith ...string) string {
value := os.Getenv(key)
Expand Down