Skip to content

Commit

Permalink
fix: use golang.org/x/sys/execabs (#224)
Browse files Browse the repository at this point in the history
  • Loading branch information
bassosimone authored Feb 10, 2021
1 parent f53b3be commit 18ca6d5
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 20 deletions.
8 changes: 4 additions & 4 deletions cmd/ooniprobe/internal/autorun/autorun_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"strconv"
"strings"
"text/template"

"github.com/apex/log"
"github.com/ooni/probe-cli/v3/cmd/ooniprobe/internal/utils"
"github.com/ooni/probe-cli/v3/internal/engine/shellx"
"golang.org/x/sys/execabs"
"golang.org/x/sys/unix"
)

Expand Down Expand Up @@ -55,7 +55,7 @@ func runQuiteQuietly(name string, arg ...string) error {
}

func darwinVersionMajor() (int, error) {
out, err := exec.Command("uname", "-r").Output()
out, err := execabs.Command("uname", "-r").Output()
if err != nil {
return 0, err
}
Expand Down Expand Up @@ -129,7 +129,7 @@ func (m managerDarwin) Start() error {
}

func (managerDarwin) stop() error {
var failure *exec.ExitError
var failure *execabs.ExitError
err := runQuiteQuietly("launchctl", "bootout", serviceTarget)
if errors.As(err, &failure) && failure.ExitCode() == int(unix.ESRCH) {
err = nil
Expand Down Expand Up @@ -158,7 +158,7 @@ func (m managerDarwin) Stop() error {

func (m managerDarwin) Status() (string, error) {
err := runQuiteQuietly("launchctl", "kill", "SIGINFO", serviceTarget)
var failure *exec.ExitError
var failure *execabs.ExitError
if errors.As(err, &failure) {
switch failure.ExitCode() {
case int(unix.ESRCH):
Expand Down
15 changes: 8 additions & 7 deletions cmd/ooniprobe/internal/utils/homedir/homedir.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ import (
"bytes"
"errors"
"os"
"os/exec"
"path/filepath"
"runtime"
"strconv"
"strings"
"sync"

"golang.org/x/sys/execabs"
)

// DisableCache will disable caching of the home directory. Caching is enabled
Expand Down Expand Up @@ -103,7 +104,7 @@ func dirDarwin() (string, error) {
var stdout bytes.Buffer

// If that fails, try OS specific commands
cmd := exec.Command("sh", "-c", `dscl -q . -read /Users/"$(whoami)" NFSHomeDirectory | sed 's/^[^ ]*: //'`)
cmd := execabs.Command("sh", "-c", `dscl -q . -read /Users/"$(whoami)" NFSHomeDirectory | sed 's/^[^ ]*: //'`)
cmd.Stdout = &stdout
if err := cmd.Run(); err == nil {
result := strings.TrimSpace(stdout.String())
Expand All @@ -114,7 +115,7 @@ func dirDarwin() (string, error) {

// try the shell
stdout.Reset()
cmd = exec.Command("sh", "-c", "cd && pwd")
cmd = execabs.Command("sh", "-c", "cd && pwd")
cmd.Stdout = &stdout
if err := cmd.Run(); err == nil {
result := strings.TrimSpace(stdout.String())
Expand All @@ -125,7 +126,7 @@ func dirDarwin() (string, error) {

// try to figure out the user and check the default location
stdout.Reset()
cmd = exec.Command("whoami")
cmd = execabs.Command("whoami")
cmd.Stdout = &stdout
if err := cmd.Run(); err == nil {
user := strings.TrimSpace(stdout.String())
Expand All @@ -150,7 +151,7 @@ func dirUnix() (string, error) {
var stdout bytes.Buffer

// If that fails, try OS specific commands
cmd := exec.Command("getent", "passwd", strconv.Itoa(os.Getuid()))
cmd := execabs.Command("getent", "passwd", strconv.Itoa(os.Getuid()))
cmd.Stdout = &stdout
if err := cmd.Run(); err == nil {
if passwd := strings.TrimSpace(stdout.String()); passwd != "" {
Expand All @@ -164,7 +165,7 @@ func dirUnix() (string, error) {

// If all else fails, try the shell
stdout.Reset()
cmd = exec.Command("sh", "-c", "cd && pwd")
cmd = execabs.Command("sh", "-c", "cd && pwd")
cmd.Stdout = &stdout
if err := cmd.Run(); err == nil {
result := strings.TrimSpace(stdout.String())
Expand All @@ -175,7 +176,7 @@ func dirUnix() (string, error) {

// try to figure out the user and check the default location
stdout.Reset()
cmd = exec.Command("whoami")
cmd = execabs.Command("whoami")
cmd.Stdout = &stdout
if err := cmd.Run(); err == nil {
user := strings.TrimSpace(stdout.String())
Expand Down
5 changes: 3 additions & 2 deletions internal/cmd/e2epostprocess/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ import (
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"strings"

"golang.org/x/sys/execabs"
)

func fatalOnError(err error) {
Expand Down Expand Up @@ -70,7 +71,7 @@ func main() {
options = append(options, *entry.Input)
}
log.Printf("run: go %s", strings.Join(options, " "))
cmd := exec.Command("go", options...)
cmd := execabs.Command("go", options...)
cmd.Stdout, cmd.Stderr = os.Stdout, os.Stderr
err = cmd.Run()
fatalOnError(err)
Expand Down
7 changes: 4 additions & 3 deletions internal/cmd/jafar/iptables/iptables_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ import (
"net/http"
"net/http/httptest"
"net/url"
"os/exec"
"runtime"
"strings"
"testing"
"time"

"golang.org/x/sys/execabs"

"github.com/apex/log"
"github.com/ooni/probe-cli/v3/internal/cmd/jafar/resolver"
"github.com/ooni/probe-cli/v3/internal/cmd/jafar/uncensored"
Expand Down Expand Up @@ -295,7 +296,7 @@ func TestHijackHTTP(t *testing.T) {
if err == nil {
t.Fatal("expected an error here")
}
var exitErr *exec.ExitError
var exitErr *execabs.ExitError
if !errors.As(err, &exitErr) {
t.Fatal("not the error type we expected")
}
Expand Down Expand Up @@ -335,7 +336,7 @@ func TestHijackHTTPS(t *testing.T) {
t.Fatal("expected an error here")
}
t.Log(err)
var exitErr *exec.ExitError
var exitErr *execabs.ExitError
if !errors.As(err, &exitErr) {
t.Fatal("not the error type we expected")
}
Expand Down
5 changes: 3 additions & 2 deletions internal/cmd/jafar/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ import (
"net"
"net/http"
"os"
"os/exec"
"os/signal"
"strings"
"syscall"

"golang.org/x/sys/execabs"

"github.com/apex/log"
"github.com/apex/log/handlers/cli"
"github.com/miekg/dns"
Expand Down Expand Up @@ -243,7 +244,7 @@ func mustx(err error, message string, osExit func(int)) {
if err != nil {
var (
exitcode = 1
exiterr *exec.ExitError
exiterr *execabs.ExitError
)
if errors.As(err, &exiterr) {
exitcode = exiterr.ExitCode()
Expand Down
5 changes: 3 additions & 2 deletions internal/engine/shellx/shellx.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ package shellx
import (
"errors"
"os"
"os/exec"
"strings"

"golang.org/x/sys/execabs"

"github.com/apex/log"
"github.com/google/shlex"
"github.com/ooni/probe-cli/v3/internal/engine/model"
Expand All @@ -22,7 +23,7 @@ type runconfig struct {

func run(config runconfig) error {
config.loginfof("exec: %s %s", config.name, strings.Join(config.args, " "))
cmd := exec.Command(config.name, config.args...)
cmd := execabs.Command(config.name, config.args...)
cmd.Stdout = config.stdout
cmd.Stderr = config.stderr
err := cmd.Run()
Expand Down

0 comments on commit 18ca6d5

Please sign in to comment.