mirror of
https://github.com/shirou/gopsutil.git
synced 2025-05-01 13:48:52 +08:00
Merge pull request #1584 from DataDog/bryce.kahle/host-platform
ensure host platform are files and have contents
This commit is contained in:
commit
9b6f82813a
@ -229,47 +229,47 @@ func PlatformInformationWithContext(ctx context.Context) (platform string, famil
|
|||||||
version = contents[0]
|
version = contents[0]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if common.PathExists(common.HostEtcWithContext(ctx, "neokylin-release")) {
|
} else if common.PathExistsWithContents(common.HostEtcWithContext(ctx, "neokylin-release")) {
|
||||||
contents, err := common.ReadLines(common.HostEtcWithContext(ctx, "neokylin-release"))
|
contents, err := common.ReadLines(common.HostEtcWithContext(ctx, "neokylin-release"))
|
||||||
if err == nil {
|
if err == nil {
|
||||||
version = getRedhatishVersion(contents)
|
version = getRedhatishVersion(contents)
|
||||||
platform = getRedhatishPlatform(contents)
|
platform = getRedhatishPlatform(contents)
|
||||||
}
|
}
|
||||||
} else if common.PathExists(common.HostEtcWithContext(ctx, "redhat-release")) {
|
} else if common.PathExistsWithContents(common.HostEtcWithContext(ctx, "redhat-release")) {
|
||||||
contents, err := common.ReadLines(common.HostEtcWithContext(ctx, "redhat-release"))
|
contents, err := common.ReadLines(common.HostEtcWithContext(ctx, "redhat-release"))
|
||||||
if err == nil {
|
if err == nil {
|
||||||
version = getRedhatishVersion(contents)
|
version = getRedhatishVersion(contents)
|
||||||
platform = getRedhatishPlatform(contents)
|
platform = getRedhatishPlatform(contents)
|
||||||
}
|
}
|
||||||
} else if common.PathExists(common.HostEtcWithContext(ctx, "system-release")) {
|
} else if common.PathExistsWithContents(common.HostEtcWithContext(ctx, "system-release")) {
|
||||||
contents, err := common.ReadLines(common.HostEtcWithContext(ctx, "system-release"))
|
contents, err := common.ReadLines(common.HostEtcWithContext(ctx, "system-release"))
|
||||||
if err == nil {
|
if err == nil {
|
||||||
version = getRedhatishVersion(contents)
|
version = getRedhatishVersion(contents)
|
||||||
platform = getRedhatishPlatform(contents)
|
platform = getRedhatishPlatform(contents)
|
||||||
}
|
}
|
||||||
} else if common.PathExists(common.HostEtcWithContext(ctx, "gentoo-release")) {
|
} else if common.PathExistsWithContents(common.HostEtcWithContext(ctx, "gentoo-release")) {
|
||||||
platform = "gentoo"
|
platform = "gentoo"
|
||||||
contents, err := common.ReadLines(common.HostEtcWithContext(ctx, "gentoo-release"))
|
contents, err := common.ReadLines(common.HostEtcWithContext(ctx, "gentoo-release"))
|
||||||
if err == nil {
|
if err == nil {
|
||||||
version = getRedhatishVersion(contents)
|
version = getRedhatishVersion(contents)
|
||||||
}
|
}
|
||||||
} else if common.PathExists(common.HostEtcWithContext(ctx, "SuSE-release")) {
|
} else if common.PathExistsWithContents(common.HostEtcWithContext(ctx, "SuSE-release")) {
|
||||||
contents, err := common.ReadLines(common.HostEtcWithContext(ctx, "SuSE-release"))
|
contents, err := common.ReadLines(common.HostEtcWithContext(ctx, "SuSE-release"))
|
||||||
if err == nil {
|
if err == nil {
|
||||||
version = getSuseVersion(contents)
|
version = getSuseVersion(contents)
|
||||||
platform = getSusePlatform(contents)
|
platform = getSusePlatform(contents)
|
||||||
}
|
}
|
||||||
// TODO: slackware detecion
|
// TODO: slackware detecion
|
||||||
} else if common.PathExists(common.HostEtcWithContext(ctx, "arch-release")) {
|
} else if common.PathExistsWithContents(common.HostEtcWithContext(ctx, "arch-release")) {
|
||||||
platform = "arch"
|
platform = "arch"
|
||||||
version = lsb.Release
|
version = lsb.Release
|
||||||
} else if common.PathExists(common.HostEtcWithContext(ctx, "alpine-release")) {
|
} else if common.PathExistsWithContents(common.HostEtcWithContext(ctx, "alpine-release")) {
|
||||||
platform = "alpine"
|
platform = "alpine"
|
||||||
contents, err := common.ReadLines(common.HostEtcWithContext(ctx, "alpine-release"))
|
contents, err := common.ReadLines(common.HostEtcWithContext(ctx, "alpine-release"))
|
||||||
if err == nil && len(contents) > 0 && contents[0] != "" {
|
if err == nil && len(contents) > 0 && contents[0] != "" {
|
||||||
version = contents[0]
|
version = contents[0]
|
||||||
}
|
}
|
||||||
} else if common.PathExists(common.HostEtcWithContext(ctx, "os-release")) {
|
} else if common.PathExistsWithContents(common.HostEtcWithContext(ctx, "os-release")) {
|
||||||
p, v, err := common.GetOSReleaseWithContext(ctx)
|
p, v, err := common.GetOSReleaseWithContext(ctx)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
platform = p
|
platform = p
|
||||||
|
@ -343,7 +343,7 @@ func PathExistsWithContents(filename string) bool {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
return info.Size() > 4 // at least 4 bytes
|
return info.Size() > 4 && !info.IsDir() // at least 4 bytes
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetEnvWithContext retrieves the environment variable key. If it does not exist it returns the default.
|
// GetEnvWithContext retrieves the environment variable key. If it does not exist it returns the default.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user