Respond to review comments

* use LookPath for better error messages
* support procfs in containers
This commit is contained in:
Punya Biswal 2021-09-08 16:15:26 -04:00
parent 84a665b712
commit 7c1aa06a5e
3 changed files with 14 additions and 3 deletions

View File

@ -24,7 +24,11 @@ func SwapDevices() ([]*SwapDevice, error) {
}
func SwapDevicesWithContext(ctx context.Context) ([]*SwapDevice, error) {
output, err := exec.Command(swapCommand, "-lk").Output()
swapCommandPath, err := exec.LookPath(swapCommand)
if err != nil {
return nil, fmt.Errorf("could not find command %q: %w", swapCommand, err)
}
output, err := exec.Command(swapCommandPath, "-lk").Output()
if err != nil {
return nil, fmt.Errorf("could not execute %q: %w", swapCommand, err)
}

View File

@ -14,6 +14,7 @@ import (
"strings"
"github.com/shirou/gopsutil/internal/common"
"github.com/shirou/gopsutil/v3/internal/common"
"golang.org/x/sys/unix"
)
@ -430,7 +431,7 @@ func calcuateAvailVmem(ret *VirtualMemoryStat, retEx *VirtualMemoryExStat) uint6
return availMemory
}
const swapsFilePath = "/proc/swaps"
const swapsFilename = "swaps"
// swaps file column indexes
const (
@ -446,6 +447,7 @@ func SwapDevices() ([]*SwapDevice, error) {
}
func SwapDevicesWithContext(ctx context.Context) ([]*SwapDevice, error) {
swapsFilePath := common.HostProc(swapsFilename)
f, err := os.Open(swapsFilePath)
if err != nil {
return nil, err
@ -456,6 +458,7 @@ func SwapDevicesWithContext(ctx context.Context) ([]*SwapDevice, error) {
}
func parseSwapsFile(r io.Reader) ([]*SwapDevice, error) {
swapsFilePath := common.HostProc(swapsFilename)
scanner := bufio.NewScanner(r)
if !scanner.Scan() {
if err := scanner.Err(); err != nil {

View File

@ -141,7 +141,11 @@ func SwapDevices() ([]*SwapDevice, error) {
}
func SwapDevicesWithContext(ctx context.Context) ([]*SwapDevice, error) {
output, err := exec.Command(swapsCommand, "-l").Output()
swapsCommandPath, err := exec.LookPath(swapsCommand)
if err != nil {
return nil, fmt.Errorf("could not find command %q: %w", swapCommand, err)
}
output, err := exec.Command(swapsCommandPath, "-l").Output()
if err != nil {
return nil, fmt.Errorf("could not execute %q: %w", swapsCommand, err)
}