refactor: remove unnecessary exec.LookPath calls

Executing the command does the lookup if needed and returns the same
error when not found, no need to do it separately.
pull/1268/head
Ville Skyttä 3 years ago
parent 03f9f55571
commit f7e1f36418

@ -1,6 +1,7 @@
package cpu package cpu
import ( import (
"errors"
"os" "os"
"os/exec" "os/exec"
"strconv" "strconv"
@ -43,14 +44,13 @@ func TestCPUparseStatLine_424(t *testing.T) {
} }
func TestCPUCountsAgainstLscpu(t *testing.T) { func TestCPUCountsAgainstLscpu(t *testing.T) {
lscpu, err := exec.LookPath("lscpu") cmd := exec.Command("lscpu")
if err != nil {
t.Skip("no lscpu to compare with")
}
cmd := exec.Command(lscpu)
cmd.Env = []string{"LC_ALL=C"} cmd.Env = []string{"LC_ALL=C"}
out, err := cmd.Output() out, err := cmd.Output()
if err != nil { if err != nil {
if errors.Is(err, exec.ErrNotFound) {
t.Skip("no lscpu to compare with")
}
t.Errorf("error executing lscpu: %v", err) t.Errorf("error executing lscpu: %v", err)
} }
var threadsPerCore, coresPerSocket, sockets int var threadsPerCore, coresPerSocket, sockets int

@ -4,7 +4,6 @@ import (
"context" "context"
"errors" "errors"
"fmt" "fmt"
"os/exec"
"regexp" "regexp"
"runtime" "runtime"
"sort" "sort"
@ -38,9 +37,9 @@ func Times(percpu bool) ([]TimesStat, error) {
} }
func TimesWithContext(ctx context.Context, percpu bool) ([]TimesStat, error) { func TimesWithContext(ctx context.Context, percpu bool) ([]TimesStat, error) {
kstatSys, err := exec.LookPath("kstat") kstatSysOut, err := invoke.CommandWithContext(ctx, "kstat", "-p", "cpu_stat:*:*:/^idle$|^user$|^kernel$|^iowait$|^swap$/")
if err != nil { if err != nil {
return nil, fmt.Errorf("cannot find kstat: %s", err) return nil, fmt.Errorf("cannot execute kstat: %s", err)
} }
cpu := make(map[float64]float64) cpu := make(map[float64]float64)
idle := make(map[float64]float64) idle := make(map[float64]float64)
@ -48,10 +47,6 @@ func TimesWithContext(ctx context.Context, percpu bool) ([]TimesStat, error) {
kern := make(map[float64]float64) kern := make(map[float64]float64)
iowt := make(map[float64]float64) iowt := make(map[float64]float64)
// swap := make(map[float64]float64) // swap := make(map[float64]float64)
kstatSysOut, err := invoke.CommandWithContext(ctx, kstatSys, "-p", "cpu_stat:*:*:/^idle$|^user$|^kernel$|^iowait$|^swap$/")
if err != nil {
return nil, fmt.Errorf("cannot execute kstat: %s", err)
}
re := regexp.MustCompile(`[:\s]+`) re := regexp.MustCompile(`[:\s]+`)
for _, line := range strings.Split(string(kstatSysOut), "\n") { for _, line := range strings.Split(string(kstatSysOut), "\n") {
fields := re.Split(line, -1) fields := re.Split(line, -1)
@ -122,27 +117,19 @@ func Info() ([]InfoStat, error) {
} }
func InfoWithContext(ctx context.Context) ([]InfoStat, error) { func InfoWithContext(ctx context.Context) ([]InfoStat, error) {
psrInfo, err := exec.LookPath("psrinfo") psrInfoOut, err := invoke.CommandWithContext(ctx, "psrinfo", "-p", "-v")
if err != nil {
return nil, fmt.Errorf("cannot find psrinfo: %s", err)
}
psrInfoOut, err := invoke.CommandWithContext(ctx, psrInfo, "-p", "-v")
if err != nil { if err != nil {
return nil, fmt.Errorf("cannot execute psrinfo: %s", err) return nil, fmt.Errorf("cannot execute psrinfo: %s", err)
} }
isaInfo, err := exec.LookPath("isainfo") procs, err := parseProcessorInfo(string(psrInfoOut))
if err != nil {
return nil, fmt.Errorf("cannot find isainfo: %s", err)
}
isaInfoOut, err := invoke.CommandWithContext(ctx, isaInfo, "-b", "-v")
if err != nil { if err != nil {
return nil, fmt.Errorf("cannot execute isainfo: %s", err) return nil, fmt.Errorf("error parsing psrinfo output: %s", err)
} }
procs, err := parseProcessorInfo(string(psrInfoOut)) isaInfoOut, err := invoke.CommandWithContext(ctx, "isainfo", "-b", "-v")
if err != nil { if err != nil {
return nil, fmt.Errorf("error parsing psrinfo output: %s", err) return nil, fmt.Errorf("cannot execute isainfo: %s", err)
} }
flags, err := parseISAInfo(string(isaInfoOut)) flags, err := parseISAInfo(string(isaInfoOut))

@ -9,7 +9,6 @@ import (
"context" "context"
"encoding/binary" "encoding/binary"
"fmt" "fmt"
"os/exec"
"strconv" "strconv"
"strings" "strings"
@ -168,11 +167,7 @@ func getFsType(stat unix.Statfs_t) string {
} }
func SerialNumberWithContext(ctx context.Context, name string) (string, error) { func SerialNumberWithContext(ctx context.Context, name string) (string, error) {
geom, err := exec.LookPath("geom") geomOut, err := invoke.CommandWithContext(ctx, "geom", "disk", "list", name)
if err != nil {
return "", fmt.Errorf("find geom: %w", err)
}
geomOut, err := invoke.CommandWithContext(ctx, geom, "disk", "list", name)
if err != nil { if err != nil {
return "", fmt.Errorf("exec geom: %w", err) return "", fmt.Errorf("exec geom: %w", err)
} }

@ -10,7 +10,6 @@ import (
"fmt" "fmt"
"math" "math"
"os" "os"
"os/exec"
"strings" "strings"
"github.com/shirou/gopsutil/v3/internal/common" "github.com/shirou/gopsutil/v3/internal/common"
@ -116,11 +115,7 @@ func UsageWithContext(ctx context.Context, path string) (*UsageStat, error) {
} }
func SerialNumberWithContext(ctx context.Context, name string) (string, error) { func SerialNumberWithContext(ctx context.Context, name string) (string, error) {
cfgadm, err := exec.LookPath("cfgadm") out, err := invoke.CommandWithContext(ctx, "cfgadm", "-ls", "select=type(disk),cols=ap_id:info,cols2=,noheadings")
if err != nil {
return "", fmt.Errorf("find cfgadm: %w", err)
}
out, err := invoke.CommandWithContext(ctx, cfgadm, "-ls", "select=type(disk),cols=ap_id:info,cols2=,noheadings")
if err != nil { if err != nil {
return "", fmt.Errorf("exec cfgadm: %w", err) return "", fmt.Errorf("exec cfgadm: %w", err)
} }

@ -5,6 +5,7 @@ package docker
import ( import (
"context" "context"
"errors"
"fmt" "fmt"
"os" "os"
"os/exec" "os/exec"
@ -23,13 +24,11 @@ func GetDockerStat() ([]CgroupDockerStat, error) {
} }
func GetDockerStatWithContext(ctx context.Context) ([]CgroupDockerStat, error) { func GetDockerStatWithContext(ctx context.Context) ([]CgroupDockerStat, error) {
path, err := exec.LookPath("docker") out, err := invoke.CommandWithContext(ctx, "docker", "ps", "-a", "--no-trunc", "--format", "{{.ID}}|{{.Image}}|{{.Names}}|{{.Status}}")
if err != nil {
return nil, ErrDockerNotAvailable
}
out, err := invoke.CommandWithContext(ctx, path, "ps", "-a", "--no-trunc", "--format", "{{.ID}}|{{.Image}}|{{.Names}}|{{.Status}}")
if err != nil { if err != nil {
if errors.Is(err, exec.ErrNotFound) {
return nil, ErrDockerNotAvailable
}
return []CgroupDockerStat{}, err return []CgroupDockerStat{}, err
} }
lines := strings.Split(string(out), "\n") lines := strings.Split(string(out), "\n")
@ -64,13 +63,11 @@ func GetDockerIDList() ([]string, error) {
} }
func GetDockerIDListWithContext(ctx context.Context) ([]string, error) { func GetDockerIDListWithContext(ctx context.Context) ([]string, error) {
path, err := exec.LookPath("docker") out, err := invoke.CommandWithContext(ctx, "docker", "ps", "-q", "--no-trunc")
if err != nil {
return nil, ErrDockerNotAvailable
}
out, err := invoke.CommandWithContext(ctx, path, "ps", "-q", "--no-trunc")
if err != nil { if err != nil {
if errors.Is(err, exec.ErrNotFound) {
return nil, ErrDockerNotAvailable
}
return []string{}, err return []string{}, err
} }
lines := strings.Split(string(out), "\n") lines := strings.Split(string(out), "\n")

@ -10,7 +10,6 @@ import (
"errors" "errors"
"io/ioutil" "io/ioutil"
"os" "os"
"os/exec"
"strings" "strings"
"unsafe" "unsafe"
@ -23,12 +22,7 @@ import (
const user_PROCESS = 7 const user_PROCESS = 7
func HostIDWithContext(ctx context.Context) (string, error) { func HostIDWithContext(ctx context.Context) (string, error) {
ioreg, err := exec.LookPath("ioreg") out, err := invoke.CommandWithContext(ctx, "ioreg", "-rd1", "-c", "IOPlatformExpertDevice")
if err != nil {
return "", err
}
out, err := invoke.CommandWithContext(ctx, ioreg, "-rd1", "-c", "IOPlatformExpertDevice")
if err != nil { if err != nil {
return "", err return "", err
} }
@ -102,17 +96,12 @@ func PlatformInformationWithContext(ctx context.Context) (string, string, string
family := "" family := ""
pver := "" pver := ""
sw_vers, err := exec.LookPath("sw_vers")
if err != nil {
return "", "", "", err
}
p, err := unix.Sysctl("kern.ostype") p, err := unix.Sysctl("kern.ostype")
if err == nil { if err == nil {
platform = strings.ToLower(p) platform = strings.ToLower(p)
} }
out, err := invoke.CommandWithContext(ctx, sw_vers, "-productVersion") out, err := invoke.CommandWithContext(ctx, "sw_vers", "-productVersion")
if err == nil { if err == nil {
pver = strings.ToLower(strings.TrimSpace(string(out))) pver = strings.ToLower(strings.TrimSpace(string(out)))
} }

@ -10,7 +10,6 @@ import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"os" "os"
"os/exec"
"path/filepath" "path/filepath"
"regexp" "regexp"
"strconv" "strconv"
@ -148,11 +147,7 @@ func getlsbStruct() (*lsbStruct, error) {
} }
} }
} else if common.PathExists("/usr/bin/lsb_release") { } else if common.PathExists("/usr/bin/lsb_release") {
lsb_release, err := exec.LookPath("lsb_release") out, err := invoke.Command("/usr/bin/lsb_release")
if err != nil {
return ret, err
}
out, err := invoke.Command(lsb_release)
if err != nil { if err != nil {
return ret, err return ret, err
} }

@ -9,7 +9,6 @@ import (
"io" "io"
"io/ioutil" "io/ioutil"
"os" "os"
"os/exec"
"regexp" "regexp"
"strconv" "strconv"
"strings" "strings"
@ -25,23 +24,20 @@ func HostIDWithContext(ctx context.Context) (string, error) {
if platform == "SmartOS" { if platform == "SmartOS" {
// If everything works, use the current zone ID as the HostID if present. // If everything works, use the current zone ID as the HostID if present.
zonename, err := exec.LookPath("zonename") out, err := invoke.CommandWithContext(ctx, "zonename")
if err == nil { if err == nil {
out, err := invoke.CommandWithContext(ctx, zonename) sc := bufio.NewScanner(bytes.NewReader(out))
if err == nil { for sc.Scan() {
sc := bufio.NewScanner(bytes.NewReader(out)) line := sc.Text()
for sc.Scan() {
line := sc.Text() // If we're in the global zone, rely on the hostname.
if line == "global" {
// If we're in the global zone, rely on the hostname. hostname, err := os.Hostname()
if line == "global" { if err == nil {
hostname, err := os.Hostname() return hostname, nil
if err == nil {
return hostname, nil
}
} else {
return strings.TrimSpace(line), nil
} }
} else {
return strings.TrimSpace(line), nil
} }
} }
} }
@ -50,15 +46,12 @@ func HostIDWithContext(ctx context.Context) (string, error) {
// If HostID is still unknown, use hostid(1), which can lie to callers but at // If HostID is still unknown, use hostid(1), which can lie to callers but at
// this point there are no hardware facilities available. This behavior // this point there are no hardware facilities available. This behavior
// matches that of other supported OSes. // matches that of other supported OSes.
hostID, err := exec.LookPath("hostid") out, err := invoke.CommandWithContext(ctx, "hostid")
if err == nil { if err == nil {
out, err := invoke.CommandWithContext(ctx, hostID) sc := bufio.NewScanner(bytes.NewReader(out))
if err == nil { for sc.Scan() {
sc := bufio.NewScanner(bytes.NewReader(out)) line := sc.Text()
for sc.Scan() { return strings.TrimSpace(line), nil
line := sc.Text()
return strings.TrimSpace(line), nil
}
} }
} }
@ -77,12 +70,7 @@ func numProcs(ctx context.Context) (uint64, error) {
var kstatMatch = regexp.MustCompile(`([^\s]+)[\s]+([^\s]*)`) var kstatMatch = regexp.MustCompile(`([^\s]+)[\s]+([^\s]*)`)
func BootTimeWithContext(ctx context.Context) (uint64, error) { func BootTimeWithContext(ctx context.Context) (uint64, error) {
kstat, err := exec.LookPath("kstat") out, err := invoke.CommandWithContext(ctx, "kstat", "-p", "unix:0:system_misc:boot_time")
if err != nil {
return 0, err
}
out, err := invoke.CommandWithContext(ctx, kstat, "-p", "unix:0:system_misc:boot_time")
if err != nil { if err != nil {
return 0, err return 0, err
} }
@ -108,13 +96,9 @@ func UsersWithContext(ctx context.Context) ([]UserStat, error) {
} }
func SensorsTemperaturesWithContext(ctx context.Context) ([]TemperatureStat, error) { func SensorsTemperaturesWithContext(ctx context.Context) ([]TemperatureStat, error) {
ipmitool, err := exec.LookPath("ipmitool")
var ret []TemperatureStat var ret []TemperatureStat
if err != nil {
return ret, err
}
out, err := invoke.CommandWithContext(ctx, ipmitool, "-c", "sdr", "list") out, err := invoke.CommandWithContext(ctx, "ipmitool", "-c", "sdr", "list")
if err != nil { if err != nil {
return ret, err return ret, err
} }
@ -185,12 +169,7 @@ func parseReleaseFile() (string, error) {
// parseUnameOutput returns platformFamily, kernelVersion and platformVersion // parseUnameOutput returns platformFamily, kernelVersion and platformVersion
func parseUnameOutput(ctx context.Context) (string, string, string, error) { func parseUnameOutput(ctx context.Context) (string, string, string, error) {
uname, err := exec.LookPath("uname") out, err := invoke.CommandWithContext(ctx, "uname", "-srv")
if err != nil {
return "", "", "", err
}
out, err := invoke.CommandWithContext(ctx, uname, "-srv")
if err != nil { if err != nil {
return "", "", "", err return "", "", "", err
} }

@ -14,11 +14,7 @@ import (
) )
func DoSysctrlWithContext(ctx context.Context, mib string) ([]string, error) { func DoSysctrlWithContext(ctx context.Context, mib string) ([]string, error) {
sysctl, err := exec.LookPath("sysctl") cmd := exec.CommandContext(ctx, "sysctl", "-n", mib)
if err != nil {
return []string{}, err
}
cmd := exec.CommandContext(ctx, sysctl, "-n", mib)
cmd.Env = getSysctrlEnv(os.Environ()) cmd.Env = getSysctrlEnv(os.Environ())
out, err := cmd.Output() out, err := cmd.Output()
if err != nil { if err != nil {

@ -29,11 +29,7 @@ func SysctlUint(mib string) (uint64, error) {
} }
func DoSysctrl(mib string) ([]string, error) { func DoSysctrl(mib string) ([]string, error) {
sysctl, err := exec.LookPath("sysctl") cmd := exec.Command("sysctl", "-n", mib)
if err != nil {
return []string{}, err
}
cmd := exec.Command(sysctl, "-n", mib)
cmd.Env = getSysctrlEnv(os.Environ()) cmd.Env = getSysctrlEnv(os.Environ())
out, err := cmd.Output() out, err := cmd.Output()
if err != nil { if err != nil {

@ -16,11 +16,7 @@ import (
) )
func DoSysctrl(mib string) ([]string, error) { func DoSysctrl(mib string) ([]string, error) {
sysctl, err := exec.LookPath("sysctl") cmd := exec.Command("sysctl", "-n", mib)
if err != nil {
return []string{}, err
}
cmd := exec.Command(sysctl, "-n", mib)
cmd.Env = getSysctrlEnv(os.Environ()) cmd.Env = getSysctrlEnv(os.Environ())
out, err := cmd.Output() out, err := cmd.Output()
if err != nil { if err != nil {

@ -13,11 +13,7 @@ import (
) )
func DoSysctrl(mib string) ([]string, error) { func DoSysctrl(mib string) ([]string, error) {
sysctl, err := exec.LookPath("sysctl") cmd := exec.Command("sysctl", "-n", mib)
if err != nil {
return []string{}, err
}
cmd := exec.Command(sysctl, "-n", mib)
cmd.Env = getSysctrlEnv(os.Environ()) cmd.Env = getSysctrlEnv(os.Environ())
out, err := cmd.Output() out, err := cmd.Output()
if err != nil { if err != nil {

@ -5,6 +5,7 @@ package common
import ( import (
"context" "context"
"errors"
"os/exec" "os/exec"
"strconv" "strconv"
"strings" "strings"
@ -18,12 +19,11 @@ func CallLsofWithContext(ctx context.Context, invoke Invoker, pid int32, args ..
cmd = []string{"-a", "-n", "-P", "-p", strconv.Itoa(int(pid))} cmd = []string{"-a", "-n", "-P", "-p", strconv.Itoa(int(pid))}
} }
cmd = append(cmd, args...) cmd = append(cmd, args...)
lsof, err := exec.LookPath("lsof") out, err := invoke.CommandWithContext(ctx, "lsof", cmd...)
if err != nil {
return []string{}, err
}
out, err := invoke.CommandWithContext(ctx, lsof, cmd...)
if err != nil { if err != nil {
if errors.Is(err, exec.ErrNotFound) {
return []string{}, err
}
// if no pid found, lsof returns code 1. // if no pid found, lsof returns code 1.
if err.Error() == "exit status 1" && len(out) == 0 { if err.Error() == "exit status 1" && len(out) == 0 {
return []string{}, nil return []string{}, nil
@ -42,12 +42,7 @@ func CallLsofWithContext(ctx context.Context, invoke Invoker, pid int32, args ..
} }
func CallPgrepWithContext(ctx context.Context, invoke Invoker, pid int32) ([]int32, error) { func CallPgrepWithContext(ctx context.Context, invoke Invoker, pid int32) ([]int32, error) {
cmd := []string{"-P", strconv.Itoa(int(pid))} out, err := invoke.CommandWithContext(ctx, "pgrep", "-P", strconv.Itoa(int(pid)))
pgrep, err := exec.LookPath("pgrep")
if err != nil {
return []int32{}, err
}
out, err := invoke.CommandWithContext(ctx, pgrep, cmd...)
if err != nil { if err != nil {
return []int32{}, err return []int32{}, err
} }

@ -5,7 +5,6 @@ package load
import ( import (
"context" "context"
"os/exec"
"strings" "strings"
"unsafe" "unsafe"
@ -52,11 +51,7 @@ func Misc() (*MiscStat, error) {
} }
func MiscWithContext(ctx context.Context) (*MiscStat, error) { func MiscWithContext(ctx context.Context) (*MiscStat, error) {
bin, err := exec.LookPath("ps") out, err := invoke.CommandWithContext(ctx, "ps", "axo", "state")
if err != nil {
return nil, err
}
out, err := invoke.CommandWithContext(ctx, bin, "axo", "state")
if err != nil { if err != nil {
return nil, err return nil, err
} }

@ -5,7 +5,6 @@ package load
import ( import (
"context" "context"
"os/exec"
"strings" "strings"
"unsafe" "unsafe"
@ -48,11 +47,7 @@ func Misc() (*MiscStat, error) {
} }
func MiscWithContext(ctx context.Context) (*MiscStat, error) { func MiscWithContext(ctx context.Context) (*MiscStat, error) {
bin, err := exec.LookPath("ps") out, err := invoke.CommandWithContext(ctx, "ps", "axo", "state")
if err != nil {
return nil, err
}
out, err := invoke.CommandWithContext(ctx, bin, "axo", "state")
if err != nil { if err != nil {
return nil, err return nil, err
} }

@ -7,7 +7,6 @@ import (
"bufio" "bufio"
"bytes" "bytes"
"context" "context"
"os/exec"
"strconv" "strconv"
"strings" "strings"
) )
@ -17,12 +16,7 @@ func Avg() (*AvgStat, error) {
} }
func AvgWithContext(ctx context.Context) (*AvgStat, error) { func AvgWithContext(ctx context.Context) (*AvgStat, error) {
kstat, err := exec.LookPath("kstat") out, err := invoke.CommandWithContext(ctx, "kstat", "-p", "unix:0:system_misc:avenrun_*")
if err != nil {
return nil, err
}
out, err := invoke.CommandWithContext(ctx, kstat, "-p", "unix:0:system_misc:avenrun_*")
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -63,11 +57,7 @@ func Misc() (*MiscStat, error) {
} }
func MiscWithContext(ctx context.Context) (*MiscStat, error) { func MiscWithContext(ctx context.Context) (*MiscStat, error) {
bin, err := exec.LookPath("ps") out, err := invoke.CommandWithContext(ctx, "ps", "-efo", "s")
if err != nil {
return nil, err
}
out, err := invoke.CommandWithContext(ctx, bin, "-efo", "s")
if err != nil { if err != nil {
return nil, err return nil, err
} }

@ -6,7 +6,6 @@ package mem
import ( import (
"context" "context"
"fmt" "fmt"
"os/exec"
"strconv" "strconv"
"strings" "strings"
) )
@ -25,11 +24,7 @@ func SwapDevices() ([]*SwapDevice, error) {
} }
func SwapDevicesWithContext(ctx context.Context) ([]*SwapDevice, error) { func SwapDevicesWithContext(ctx context.Context) ([]*SwapDevice, error) {
swapCommandPath, err := exec.LookPath(swapCommand) output, err := invoke.CommandWithContext(ctx, swapCommand, "-lk")
if err != nil {
return nil, fmt.Errorf("could not find command %q: %w", swapCommand, err)
}
output, err := invoke.CommandWithContext(ctx, swapCommandPath, "-lk")
if err != nil { if err != nil {
return nil, fmt.Errorf("could not execute %q: %w", swapCommand, err) return nil, fmt.Errorf("could not execute %q: %w", swapCommand, err)
} }

@ -5,7 +5,6 @@ package mem
import ( import (
"context" "context"
"os/exec"
"strconv" "strconv"
"strings" "strings"
@ -14,11 +13,7 @@ import (
// Runs vm_stat and returns Free and inactive pages // Runs vm_stat and returns Free and inactive pages
func getVMStat(vms *VirtualMemoryStat) error { func getVMStat(vms *VirtualMemoryStat) error {
vm_stat, err := exec.LookPath("vm_stat") out, err := invoke.Command("vm_stat")
if err != nil {
return err
}
out, err := invoke.Command(vm_stat)
if err != nil { if err != nil {
return err return err
} }

@ -9,8 +9,6 @@ import (
"encoding/binary" "encoding/binary"
"errors" "errors"
"fmt" "fmt"
"os/exec"
"github.com/shirou/gopsutil/v3/internal/common" "github.com/shirou/gopsutil/v3/internal/common"
"golang.org/x/sys/unix" "golang.org/x/sys/unix"
) )
@ -76,12 +74,7 @@ func SwapMemory() (*SwapMemoryStat, error) {
} }
func SwapMemoryWithContext(ctx context.Context) (*SwapMemoryStat, error) { func SwapMemoryWithContext(ctx context.Context) (*SwapMemoryStat, error) {
swapctl, err := exec.LookPath("swapctl") out, err := invoke.CommandWithContext(ctx, "swapctl", "-sk")
if err != nil {
return nil, err
}
out, err := invoke.CommandWithContext(ctx, swapctl, "-sk")
if err != nil { if err != nil {
return &SwapMemoryStat{}, nil return &SwapMemoryStat{}, nil
} }

@ -5,8 +5,8 @@ package mem
import ( import (
"context" "context"
"errors"
"fmt" "fmt"
"os/exec"
"regexp" "regexp"
"strconv" "strconv"
"strings" "strings"
@ -54,13 +54,8 @@ func SwapMemoryWithContext(ctx context.Context) (*SwapMemoryStat, error) {
} }
func zoneName() (string, error) { func zoneName() (string, error) {
zonename, err := exec.LookPath("zonename")
if err != nil {
return "", err
}
ctx := context.Background() ctx := context.Background()
out, err := invoke.CommandWithContext(ctx, zonename) out, err := invoke.CommandWithContext(ctx, "zonename")
if err != nil { if err != nil {
return "", err return "", err
} }
@ -71,20 +66,15 @@ func zoneName() (string, error) {
var globalZoneMemoryCapacityMatch = regexp.MustCompile(`[Mm]emory size: (\d+) Megabytes`) var globalZoneMemoryCapacityMatch = regexp.MustCompile(`[Mm]emory size: (\d+) Megabytes`)
func globalZoneMemoryCapacity() (uint64, error) { func globalZoneMemoryCapacity() (uint64, error) {
prtconf, err := exec.LookPath("prtconf")
if err != nil {
return 0, err
}
ctx := context.Background() ctx := context.Background()
out, err := invoke.CommandWithContext(ctx, prtconf) out, err := invoke.CommandWithContext(ctx, "prtconf")
if err != nil { if err != nil {
return 0, err return 0, err
} }
match := globalZoneMemoryCapacityMatch.FindAllStringSubmatch(string(out), -1) match := globalZoneMemoryCapacityMatch.FindAllStringSubmatch(string(out), -1)
if len(match) != 1 { if len(match) != 1 {
return 0, fmt.Errorf("memory size not contained in output of %q", prtconf) return 0, errors.New("memory size not contained in output of prtconf")
} }
totalMB, err := strconv.ParseUint(match[0][1], 10, 64) totalMB, err := strconv.ParseUint(match[0][1], 10, 64)
@ -98,13 +88,8 @@ func globalZoneMemoryCapacity() (uint64, error) {
var kstatMatch = regexp.MustCompile(`(\S+)\s+(\S*)`) var kstatMatch = regexp.MustCompile(`(\S+)\s+(\S*)`)
func nonGlobalZoneMemoryCapacity() (uint64, error) { func nonGlobalZoneMemoryCapacity() (uint64, error) {
kstat, err := exec.LookPath("kstat")
if err != nil {
return 0, err
}
ctx := context.Background() ctx := context.Background()
out, err := invoke.CommandWithContext(ctx, kstat, "-p", "-c", "zone_memory_cap", "memory_cap:*:*:physcap") out, err := invoke.CommandWithContext(ctx, "kstat", "-p", "-c", "zone_memory_cap", "memory_cap:*:*:physcap")
if err != nil { if err != nil {
return 0, err return 0, err
} }
@ -141,11 +126,7 @@ func SwapDevices() ([]*SwapDevice, error) {
} }
func SwapDevicesWithContext(ctx context.Context) ([]*SwapDevice, error) { func SwapDevicesWithContext(ctx context.Context) ([]*SwapDevice, error) {
swapCommandPath, err := exec.LookPath(swapCommand) output, err := invoke.CommandWithContext(ctx, swapCommand, "-l")
if err != nil {
return nil, fmt.Errorf("could not find command %q: %w", swapCommand, err)
}
output, err := invoke.CommandWithContext(ctx, swapCommandPath, "-l")
if err != nil { if err != nil {
return nil, fmt.Errorf("could not execute %q: %w", swapCommand, err) return nil, fmt.Errorf("could not execute %q: %w", swapCommand, err)
} }

@ -6,7 +6,6 @@ package net
import ( import (
"context" "context"
"fmt" "fmt"
"os/exec"
"regexp" "regexp"
"strconv" "strconv"
"strings" "strings"
@ -86,11 +85,7 @@ func IOCounters(pernic bool) ([]IOCountersStat, error) {
} }
func IOCountersWithContext(ctx context.Context, pernic bool) ([]IOCountersStat, error) { func IOCountersWithContext(ctx context.Context, pernic bool) ([]IOCountersStat, error) {
netstat, err := exec.LookPath("netstat") out, err := invoke.CommandWithContext(ctx, "netstat", "-idn")
if err != nil {
return nil, err
}
out, err := invoke.CommandWithContext(ctx, netstat, "-idn")
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -360,11 +355,7 @@ func ConnectionsWithContext(ctx context.Context, kind string) ([]ConnectionStat,
args = append(args, "-funix") args = append(args, "-funix")
} }
netstat, err := exec.LookPath("netstat") out, err := invoke.CommandWithContext(ctx, "netstat", args...)
if err != nil {
return nil, err
}
out, err := invoke.CommandWithContext(ctx, netstat, args...)
if err != nil { if err != nil {
return nil, err return nil, err
} }

@ -207,11 +207,7 @@ func IOCountersWithContext(ctx context.Context, pernic bool) ([]IOCountersStat,
} }
} else { } else {
// duplicated interface, list all interfaces // duplicated interface, list all interfaces
ifconfig, err := exec.LookPath("ifconfig") if out, err = invoke.CommandWithContext(ctx, "ifconfig", "-l"); err != nil {
if err != nil {
return nil, err
}
if out, err = invoke.CommandWithContext(ctx, ifconfig, "-l"); err != nil {
return nil, err return nil, err
} }
interfaceNames := strings.Fields(strings.TrimRight(string(out), endOfLine)) interfaceNames := strings.Fields(strings.TrimRight(string(out), endOfLine))

@ -5,7 +5,6 @@ package net
import ( import (
"context" "context"
"os/exec"
"strconv" "strconv"
"strings" "strings"
@ -17,11 +16,7 @@ func IOCounters(pernic bool) ([]IOCountersStat, error) {
} }
func IOCountersWithContext(ctx context.Context, pernic bool) ([]IOCountersStat, error) { func IOCountersWithContext(ctx context.Context, pernic bool) ([]IOCountersStat, error) {
netstat, err := exec.LookPath("netstat") out, err := invoke.CommandWithContext(ctx, "netstat", "-ibdnW")
if err != nil {
return nil, err
}
out, err := invoke.CommandWithContext(ctx, netstat, "-ibdnW")
if err != nil { if err != nil {
return nil, err return nil, err
} }

@ -6,7 +6,6 @@ package process
import ( import (
"context" "context"
"fmt" "fmt"
"os/exec"
"path/filepath" "path/filepath"
"strconv" "strconv"
"strings" "strings"
@ -112,11 +111,7 @@ func (p *Process) StatusWithContext(ctx context.Context) ([]string, error) {
func (p *Process) ForegroundWithContext(ctx context.Context) (bool, error) { func (p *Process) ForegroundWithContext(ctx context.Context) (bool, error) {
// see https://github.com/shirou/gopsutil/issues/596#issuecomment-432707831 for implementation details // see https://github.com/shirou/gopsutil/issues/596#issuecomment-432707831 for implementation details
pid := p.Pid pid := p.Pid
ps, err := exec.LookPath("ps") out, err := invoke.CommandWithContext(ctx, "ps", "-o", "stat=", "-p", strconv.Itoa(int(pid)))
if err != nil {
return false, err
}
out, err := invoke.CommandWithContext(ctx, ps, "-o", "stat=", "-p", strconv.Itoa(int(pid)))
if err != nil { if err != nil {
return false, err return false, err
} }
@ -292,11 +287,6 @@ func (p *Process) getKProc() (*unix.KinfoProc, error) {
// And splited by Space. Caller have responsibility to manage. // And splited by Space. Caller have responsibility to manage.
// If passed arg pid is 0, get information from all process. // If passed arg pid is 0, get information from all process.
func callPsWithContext(ctx context.Context, arg string, pid int32, threadOption bool, nameOption bool) ([][]string, error) { func callPsWithContext(ctx context.Context, arg string, pid int32, threadOption bool, nameOption bool) ([][]string, error) {
bin, err := exec.LookPath("ps")
if err != nil {
return [][]string{}, err
}
var cmd []string var cmd []string
if pid == 0 { // will get from all processes. if pid == 0 { // will get from all processes.
cmd = []string{"-ax", "-o", arg} cmd = []string{"-ax", "-o", arg}
@ -308,7 +298,7 @@ func callPsWithContext(ctx context.Context, arg string, pid int32, threadOption
if nameOption { if nameOption {
cmd = append(cmd, "-c") cmd = append(cmd, "-c")
} }
out, err := invoke.CommandWithContext(ctx, bin, cmd...) out, err := invoke.CommandWithContext(ctx, "ps", cmd...)
if err != nil { if err != nil {
return [][]string{}, err return [][]string{}, err
} }

@ -6,7 +6,6 @@ package process
import ( import (
"context" "context"
"fmt" "fmt"
"os/exec"
"strconv" "strconv"
"strings" "strings"
@ -19,11 +18,7 @@ func (p *Process) CwdWithContext(ctx context.Context) (string, error) {
} }
func (p *Process) ExeWithContext(ctx context.Context) (string, error) { func (p *Process) ExeWithContext(ctx context.Context) (string, error) {
lsof_bin, err := exec.LookPath("lsof") out, err := invoke.CommandWithContext(ctx, "lsof", "-p", strconv.Itoa(int(p.Pid)), "-Fpfn")
if err != nil {
return "", err
}
out, err := invoke.CommandWithContext(ctx, lsof_bin, "-p", strconv.Itoa(int(p.Pid)), "-Fpfn")
if err != nil { if err != nil {
return "", fmt.Errorf("bad call to lsof: %s", err) return "", fmt.Errorf("bad call to lsof: %s", err)
} }

@ -6,7 +6,6 @@ package process
import ( import (
"bytes" "bytes"
"context" "context"
"os/exec"
"path/filepath" "path/filepath"
"strconv" "strconv"
"strings" "strings"
@ -147,11 +146,7 @@ func (p *Process) StatusWithContext(ctx context.Context) ([]string, error) {
func (p *Process) ForegroundWithContext(ctx context.Context) (bool, error) { func (p *Process) ForegroundWithContext(ctx context.Context) (bool, error) {
// see https://github.com/shirou/gopsutil/issues/596#issuecomment-432707831 for implementation details // see https://github.com/shirou/gopsutil/issues/596#issuecomment-432707831 for implementation details
pid := p.Pid pid := p.Pid
ps, err := exec.LookPath("ps") out, err := invoke.CommandWithContext(ctx, "ps", "-o", "stat=", "-p", strconv.Itoa(int(pid)))
if err != nil {
return false, err
}
out, err := invoke.CommandWithContext(ctx, ps, "-o", "stat=", "-p", strconv.Itoa(int(pid)))
if err != nil { if err != nil {
return false, err return false, err
} }

@ -9,7 +9,6 @@ import (
"encoding/binary" "encoding/binary"
"fmt" "fmt"
"io" "io"
"os/exec"
"path/filepath" "path/filepath"
"strconv" "strconv"
"strings" "strings"
@ -167,11 +166,7 @@ func (p *Process) StatusWithContext(ctx context.Context) ([]string, error) {
func (p *Process) ForegroundWithContext(ctx context.Context) (bool, error) { func (p *Process) ForegroundWithContext(ctx context.Context) (bool, error) {
// see https://github.com/shirou/gopsutil/issues/596#issuecomment-432707831 for implementation details // see https://github.com/shirou/gopsutil/issues/596#issuecomment-432707831 for implementation details
pid := p.Pid pid := p.Pid
ps, err := exec.LookPath("ps") out, err := invoke.CommandWithContext(ctx, "ps", "-o", "stat=", "-p", strconv.Itoa(int(pid)))
if err != nil {
return false, err
}
out, err := invoke.CommandWithContext(ctx, ps, "-o", "stat=", "-p", strconv.Itoa(int(pid)))
if err != nil { if err != nil {
return false, err return false, err
} }

Loading…
Cancel
Save