Skip to content

Commit

Permalink
Merge pull request #506 from shirou/add_command_context
Browse files Browse the repository at this point in the history
change to use CommandContext.
  • Loading branch information
shirou authored Mar 31, 2018
2 parents 837fc76 + 145dca9 commit 27cc4e1
Show file tree
Hide file tree
Showing 25 changed files with 87 additions and 67 deletions.
13 changes: 10 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,21 @@ check: ## Check
BUILD_FAIL_PATTERN=grep -v "exec format error" | grep "build failed" && exit 1 || exit 0
build_test: ## test only buildable
# Supported operating systems
GOOS=linux go test ./... | $(BUILD_FAIL_PATTERN)
GOOS=linux GOARCH=amd64 go test ./... | $(BUILD_FAIL_PATTERN)
GOOS=linux GOARCH=386 go test ./... | $(BUILD_FAIL_PATTERN)
GOOS=linux GOARCH=arm go test ./... | $(BUILD_FAIL_PATTERN)
GOOS=linux GOARCH=arm64 go test ./... | $(BUILD_FAIL_PATTERN)
GOOS=freebsd go test ./... | $(BUILD_FAIL_PATTERN)
# GOOS=openbsd go test ./... | $(BUILD_FAIL_PATTERN)
CGO_ENABLED=0 GOOS=darwin go test ./... | $(BUILD_FAIL_PATTERN)
GOOS=windows go test ./... | $(BUILD_FAIL_PATTERN)
# Operating systems supported for building only (not implemented error if used)
GOOS=solaris go test ./... | $(BUILD_FAIL_PATTERN)
# GOOS=dragonfly go test ./... | $(BUILD_FAIL_PATTERN)
GOOS=dragonfly go test ./... | $(BUILD_FAIL_PATTERN)
GOOS=netbsd go test ./... | $(BUILD_FAIL_PATTERN)
# cross build to OpenBSD not worked since process has "C"
# GOOS=openbsd go test ./... | $(BUILD_FAIL_PATTERN)

ifeq ($(shell uname -s), Darwin)
CGO_ENABLED=1 GOOS=darwin go test ./... | $(BUILD_FAIL_PATTERN)
endif
@echo 'Successfully built on all known operating systems'
4 changes: 2 additions & 2 deletions cpu/cpu_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func InfoWithContext(ctx context.Context) ([]InfoStat, error) {
if err != nil {
return ret, err
}
out, err := invoke.Command(sysctl, "machdep.cpu")
out, err := invoke.CommandWithContext(ctx, sysctl, "machdep.cpu")
if err != nil {
return ret, err
}
Expand Down Expand Up @@ -99,7 +99,7 @@ func InfoWithContext(ctx context.Context) ([]InfoStat, error) {

// Use the rated frequency of the CPU. This is a static value and does not
// account for low power or Turbo Boost modes.
out, err = invoke.Command(sysctl, "hw.cpufrequency")
out, err = invoke.CommandWithContext(ctx, sysctl, "hw.cpufrequency")
if err != nil {
return ret, err
}
Expand Down
2 changes: 1 addition & 1 deletion cpu/cpu_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func init() {
if err != nil {
return
}
out, err := invoke.Command(getconf, "CLK_TCK")
out, err := invoke.CommandWithContext(context.Background(), getconf, "CLK_TCK")
// ignore errors
if err == nil {
i, err := strconv.ParseFloat(strings.TrimSpace(string(out)), 64)
Expand Down
4 changes: 2 additions & 2 deletions cpu/cpu_solaris.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func InfoWithContext(ctx context.Context) ([]InfoStat, error) {
if err != nil {
return nil, fmt.Errorf("cannot find psrinfo: %s", err)
}
psrInfoOut, err := invoke.Command(psrInfo, "-p", "-v")
psrInfoOut, err := invoke.CommandWithContext(ctx, psrInfo, "-p", "-v")
if err != nil {
return nil, fmt.Errorf("cannot execute psrinfo: %s", err)
}
Expand All @@ -56,7 +56,7 @@ func InfoWithContext(ctx context.Context) ([]InfoStat, error) {
if err != nil {
return nil, fmt.Errorf("cannot find isainfo: %s", err)
}
isaInfoOut, err := invoke.Command(isaInfo, "-b", "-v")
isaInfoOut, err := invoke.CommandWithContext(ctx, isaInfo, "-b", "-v")
if err != nil {
return nil, fmt.Errorf("cannot execute isainfo: %s", err)
}
Expand Down
2 changes: 1 addition & 1 deletion disk/disk_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ func GetDiskSerialNumberWithContext(ctx context.Context, name string) string {
return ""
}

out, err := invoke.Command(udevadm, "info", "--query=property", n)
out, err := invoke.CommandWithContext(ctx, udevadm, "info", "--query=property", n)

// does not return error, just an empty string
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions docker/docker_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func GetDockerStatWithContext(ctx context.Context) ([]CgroupDockerStat, error) {
return nil, ErrDockerNotAvailable
}

out, err := invoke.Command(path, "ps", "-a", "--no-trunc", "--format", "{{.ID}}|{{.Image}}|{{.Names}}|{{.Status}}")
out, err := invoke.CommandWithContext(ctx, path, "ps", "-a", "--no-trunc", "--format", "{{.ID}}|{{.Image}}|{{.Names}}|{{.Status}}")
if err != nil {
return []CgroupDockerStat{}, err
}
Expand Down Expand Up @@ -68,7 +68,7 @@ func GetDockerIDListWithContext(ctx context.Context) ([]string, error) {
return nil, ErrDockerNotAvailable
}

out, err := invoke.Command(path, "ps", "-q", "--no-trunc")
out, err := invoke.CommandWithContext(ctx, path, "ps", "-q", "--no-trunc")
if err != nil {
return []string{}, err
}
Expand Down
10 changes: 5 additions & 5 deletions host/host_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func InfoWithContext(ctx context.Context) (*InfoStat, error) {

uname, err := exec.LookPath("uname")
if err == nil {
out, err := invoke.Command(uname, "-r")
out, err := invoke.CommandWithContext(ctx, uname, "-r")
if err == nil {
ret.KernelVersion = strings.ToLower(strings.TrimSpace(string(out)))
}
Expand Down Expand Up @@ -70,7 +70,7 @@ func InfoWithContext(ctx context.Context) (*InfoStat, error) {
ret.Procs = uint64(len(procs))
}

values, err := common.DoSysctrl("kern.uuid")
values, err := common.DoSysctrlWithContext(ctx, "kern.uuid")
if err == nil && len(values) == 1 && values[0] != "" {
ret.HostID = strings.ToLower(values[0])
}
Expand All @@ -90,7 +90,7 @@ func BootTimeWithContext(ctx context.Context) (uint64, error) {
if t != 0 {
return t, nil
}
values, err := common.DoSysctrl("kern.boottime")
values, err := common.DoSysctrlWithContext(ctx, "kern.boottime")
if err != nil {
return 0, err
}
Expand Down Expand Up @@ -188,12 +188,12 @@ func PlatformInformationWithContext(ctx context.Context) (string, string, string
return "", "", "", err
}

out, err := invoke.Command(uname, "-s")
out, err := invoke.CommandWithContext(ctx, uname, "-s")
if err == nil {
platform = strings.ToLower(strings.TrimSpace(string(out)))
}

out, err = invoke.Command(sw_vers, "-productVersion")
out, err = invoke.CommandWithContext(ctx, sw_vers, "-productVersion")
if err == nil {
pver = strings.ToLower(strings.TrimSpace(string(out)))
}
Expand Down
4 changes: 2 additions & 2 deletions host/host_openbsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,12 @@ func PlatformInformationWithContext(ctx context.Context) (string, string, string
return "", "", "", err
}

out, err := invoke.Command(uname, "-s")
out, err := invoke.CommandWithContext(ctx, uname, "-s")
if err == nil {
platform = strings.ToLower(strings.TrimSpace(string(out)))
}

out, err = invoke.Command(uname, "-r")
out, err = invoke.CommandWithContext(ctx, uname, "-r")
if err == nil {
version = strings.ToLower(strings.TrimSpace(string(out)))
}
Expand Down
10 changes: 5 additions & 5 deletions host/host_solaris.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func InfoWithContext(ctx context.Context) (*InfoStat, error) {
return nil, err
}

out, err := invoke.Command(uname, "-srv")
out, err := invoke.CommandWithContext(ctx, uname, "-srv")
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -87,7 +87,7 @@ func InfoWithContext(ctx context.Context) (*InfoStat, error) {
// If everything works, use the current zone ID as the HostID if present.
zonename, err := exec.LookPath("/usr/bin/zonename")
if err == nil {
out, err := invoke.Command(zonename)
out, err := invoke.CommandWithContext(ctx, zonename)
if err == nil {
sc := bufio.NewScanner(bytes.NewReader(out))
for sc.Scan() {
Expand All @@ -114,7 +114,7 @@ func InfoWithContext(ctx context.Context) (*InfoStat, error) {
if result.HostID == "" {
hostID, err := exec.LookPath("/usr/bin/hostid")
if err == nil {
out, err := invoke.Command(hostID)
out, err := invoke.CommandWithContext(ctx, hostID)
if err == nil {
sc := bufio.NewScanner(bytes.NewReader(out))
for sc.Scan() {
Expand Down Expand Up @@ -156,7 +156,7 @@ func BootTimeWithContext(ctx context.Context) (uint64, error) {
return 0, err
}

out, err := invoke.Command(kstat, "-p", "unix:0:system_misc:boot_time")
out, err := invoke.CommandWithContext(ctx, kstat, "-p", "unix:0:system_misc:boot_time")
if err != nil {
return 0, err
}
Expand Down Expand Up @@ -220,7 +220,7 @@ func KernelVersionWithContext(ctx context.Context) (string, error) {
return "", err
}

out, err := invoke.Command(uname, "-srv")
out, err := invoke.CommandWithContext(ctx, uname, "-srv")
if err != nil {
return "", err
}
Expand Down
12 changes: 10 additions & 2 deletions internal/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,19 @@ var (

type Invoker interface {
Command(string, ...string) ([]byte, error)
CommandWithContext(context.Context, string, ...string) ([]byte, error)
}

type Invoke struct{}

func (i Invoke) Command(name string, arg ...string) ([]byte, error) {
ctxt, cancel := context.WithTimeout(context.Background(), Timeout)
ctx, cancel := context.WithTimeout(context.Background(), Timeout)
defer cancel()
return i.CommandWithContext(ctx, name, arg...)
}

cmd := exec.CommandContext(ctxt, name, arg...)
func (i Invoke) CommandWithContext(ctx context.Context, name string, arg ...string) ([]byte, error) {
cmd := exec.CommandContext(ctx, name, arg...)

var buf bytes.Buffer
cmd.Stdout = &buf
Expand Down Expand Up @@ -84,6 +88,10 @@ func (i FakeInvoke) Command(name string, arg ...string) ([]byte, error) {
return []byte{}, fmt.Errorf("could not find testdata: %s", fpath)
}

func (i FakeInvoke) CommandWithContext(ctx context.Context, name string, arg ...string) ([]byte, error) {
return i.Command(name, arg...)
}

var ErrNotImplementedError = errors.New("not implemented yet")

// ReadLines reads contents from a file and splits them by new lines.
Expand Down
5 changes: 3 additions & 2 deletions internal/common/common_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package common

import (
"context"
"os"
"os/exec"
"strings"
Expand All @@ -11,12 +12,12 @@ import (
"golang.org/x/sys/unix"
)

func DoSysctrl(mib string) ([]string, error) {
func DoSysctrlWithContext(ctx context.Context, mib string) ([]string, error) {
sysctl, err := exec.LookPath("/usr/sbin/sysctl")
if err != nil {
return []string{}, err
}
cmd := exec.Command(sysctl, "-n", mib)
cmd := exec.CommandContext(ctx, sysctl, "-n", mib)
cmd.Env = getSysctrlEnv(os.Environ())
out, err := cmd.Output()
if err != nil {
Expand Down
9 changes: 5 additions & 4 deletions internal/common/common_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
package common

import (
"context"
"os/exec"
"strconv"
"strings"
)

func CallLsof(invoke Invoker, pid int32, args ...string) ([]string, error) {
func CallLsofWithContext(ctx context.Context, invoke Invoker, pid int32, args ...string) ([]string, error) {
var cmd []string
if pid == 0 { // will get from all processes.
cmd = []string{"-a", "-n", "-P"}
Expand All @@ -20,7 +21,7 @@ func CallLsof(invoke Invoker, pid int32, args ...string) ([]string, error) {
if err != nil {
return []string{}, err
}
out, err := invoke.Command(lsof, cmd...)
out, err := invoke.CommandWithContext(ctx, lsof, cmd...)
if err != nil {
// if no pid found, lsof returnes code 1.
if err.Error() == "exit status 1" && len(out) == 0 {
Expand All @@ -39,14 +40,14 @@ func CallLsof(invoke Invoker, pid int32, args ...string) ([]string, error) {
return ret, nil
}

func CallPgrep(invoke Invoker, pid int32) ([]int32, error) {
func CallPgrepWithContext(ctx context.Context, invoke Invoker, pid int32) ([]int32, error) {
var cmd []string
cmd = []string{"-P", strconv.Itoa(int(pid))}
pgrep, err := exec.LookPath("pgrep")
if err != nil {
return []int32{}, err
}
out, err := invoke.Command(pgrep, cmd...)
out, err := invoke.CommandWithContext(ctx, pgrep, cmd...)
if err != nil {
return []int32{}, err
}
Expand Down
2 changes: 1 addition & 1 deletion load/load_bsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func MiscWithContext(ctx context.Context) (*MiscStat, error) {
if err != nil {
return nil, err
}
out, err := invoke.Command(bin, "axo", "state")
out, err := invoke.CommandWithContext(ctx, bin, "axo", "state")
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions load/load_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func Avg() (*AvgStat, error) {
}

func AvgWithContext(ctx context.Context) (*AvgStat, error) {
values, err := common.DoSysctrl("vm.loadavg")
values, err := common.DoSysctrlWithContext(ctx, "vm.loadavg")
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -56,7 +56,7 @@ func MiscWithContext(ctx context.Context) (*MiscStat, error) {
if err != nil {
return nil, err
}
out, err := invoke.Command(bin, "axo", "state")
out, err := invoke.CommandWithContext(ctx, bin, "axo", "state")
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion mem/mem_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func SwapMemory() (*SwapMemoryStat, error) {
func SwapMemoryWithContext(ctx context.Context) (*SwapMemoryStat, error) {
var ret *SwapMemoryStat

swapUsage, err := common.DoSysctrl("vm.swapusage")
swapUsage, err := common.DoSysctrlWithContext(ctx, "vm.swapusage")
if err != nil {
return ret, err
}
Expand Down
2 changes: 1 addition & 1 deletion mem/mem_openbsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func SwapMemoryWithContext(ctx context.Context) (*SwapMemoryStat, error) {
return nil, err
}

out, err := invoke.Command(swapctl, "-sk")
out, err := invoke.CommandWithContext(ctx, swapctl, "-sk")
if err != nil {
return &SwapMemoryStat{}, nil
}
Expand Down
9 changes: 6 additions & 3 deletions mem/mem_solaris.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ func zoneName() (string, error) {
return "", err
}

out, err := invoke.Command(zonename)
ctx := context.Background()
out, err := invoke.CommandWithContext(ctx, zonename)
if err != nil {
return "", err
}
Expand All @@ -73,7 +74,8 @@ func globalZoneMemoryCapacity() (uint64, error) {
return 0, err
}

out, err := invoke.Command(prtconf)
ctx := context.Background()
out, err := invoke.CommandWithContext(ctx, prtconf)
if err != nil {
return 0, err
}
Expand All @@ -99,7 +101,8 @@ func nonGlobalZoneMemoryCapacity() (uint64, error) {
return 0, err
}

out, err := invoke.Command(kstat, "-p", "-c", "zone_memory_cap", "memory_cap:*:*:physcap")
ctx := context.Background()
out, err := invoke.CommandWithContext(ctx, kstat, "-p", "-c", "zone_memory_cap", "memory_cap:*:*:physcap")
if err != nil {
return 0, err
}
Expand Down
6 changes: 3 additions & 3 deletions net/net_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ func IOCountersWithContext(ctx context.Context, pernic bool) ([]IOCountersStat,
}

// try to get all interface metrics, and hope there won't be any truncated
out, err := invoke.Command(netstat, "-ibdnW")
out, err := invoke.CommandWithContext(ctx, netstat, "-ibdnW")
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -208,7 +208,7 @@ func IOCountersWithContext(ctx context.Context, pernic bool) ([]IOCountersStat,
if err != nil {
return nil, err
}
if out, err = invoke.Command(ifconfig, "-l"); err != nil {
if out, err = invoke.CommandWithContext(ctx, ifconfig, "-l"); err != nil {
return nil, err
}
interfaceNames := strings.Fields(strings.TrimRight(string(out), endOfLine))
Expand All @@ -227,7 +227,7 @@ func IOCountersWithContext(ctx context.Context, pernic bool) ([]IOCountersStat,
}
if truncated {
// run netstat with -I$ifacename
if out, err = invoke.Command(netstat, "-ibdnWI"+interfaceName); err != nil {
if out, err = invoke.CommandWithContext(ctx, netstat, "-ibdnWI"+interfaceName); err != nil {
return nil, err
}
parsedIfaces, err := parseNetstatOutput(string(out))
Expand Down
Loading

0 comments on commit 27cc4e1

Please sign in to comment.