cleanup(shellx): do not directly depend on apex/log (#357)
This commit is contained in:
@@ -6,11 +6,15 @@ import (
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/apex/log"
|
||||
"github.com/google/shlex"
|
||||
"golang.org/x/sys/execabs"
|
||||
)
|
||||
|
||||
// Logger is the logger expected by this package.
|
||||
type Logger interface {
|
||||
Infof(format string, v ...interface{})
|
||||
}
|
||||
|
||||
// runconfig is the configuration for run.
|
||||
type runconfig struct {
|
||||
// args contains the command line arguments.
|
||||
@@ -43,12 +47,12 @@ func run(config runconfig) error {
|
||||
return err
|
||||
}
|
||||
|
||||
// Run executes the specified command with the specified args
|
||||
func Run(name string, arg ...string) error {
|
||||
// Run executes the specified command with the specified args.
|
||||
func Run(logger Logger, name string, arg ...string) error {
|
||||
return run(runconfig{
|
||||
args: arg,
|
||||
command: name,
|
||||
loginfof: log.Log.Infof,
|
||||
loginfof: logger.Infof,
|
||||
stdout: os.Stdout,
|
||||
stderr: os.Stderr,
|
||||
})
|
||||
@@ -68,15 +72,17 @@ func RunQuiet(name string, arg ...string) error {
|
||||
})
|
||||
}
|
||||
|
||||
// RunCommandline is like Run but its only argument is a command
|
||||
// line that will be splitted using the google/shlex package.
|
||||
func RunCommandline(cmdline string) error {
|
||||
// ErrNoCommandToExecute means that the command line is empty.
|
||||
var ErrNoCommandToExecute = errors.New("shellx: no command to execute")
|
||||
|
||||
// RunCommandline executes the given command line.
|
||||
func RunCommandline(logger Logger, cmdline string) error {
|
||||
args, err := shlex.Split(cmdline)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(args) < 1 {
|
||||
return errors.New("shellx: no command to execute")
|
||||
return ErrNoCommandToExecute
|
||||
}
|
||||
return Run(args[0], args[1:]...)
|
||||
return Run(logger, args[0], args[1:]...)
|
||||
}
|
||||
|
||||
@@ -1,12 +1,16 @@
|
||||
package shellx
|
||||
|
||||
import "testing"
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/apex/log"
|
||||
)
|
||||
|
||||
func TestRun(t *testing.T) {
|
||||
if err := Run("whoami"); err != nil {
|
||||
if err := Run(log.Log, "whoami"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := Run("./nonexistent/command"); err == nil {
|
||||
if err := Run(log.Log, "./nonexistent/command"); err == nil {
|
||||
t.Fatal("expected an error here")
|
||||
}
|
||||
}
|
||||
@@ -22,22 +26,22 @@ func TestRunQuiet(t *testing.T) {
|
||||
|
||||
func TestRunCommandline(t *testing.T) {
|
||||
t.Run("when the command does not parse", func(t *testing.T) {
|
||||
if err := RunCommandline(`"foobar`); err == nil {
|
||||
if err := RunCommandline(log.Log, `"foobar`); err == nil {
|
||||
t.Fatal("expected an error here")
|
||||
}
|
||||
})
|
||||
t.Run("when we have no arguments", func(t *testing.T) {
|
||||
if err := RunCommandline(""); err == nil {
|
||||
if err := RunCommandline(log.Log, ""); err == nil {
|
||||
t.Fatal("expected an error here")
|
||||
}
|
||||
})
|
||||
t.Run("when we have a single argument", func(t *testing.T) {
|
||||
if err := RunCommandline("ls"); err != nil {
|
||||
if err := RunCommandline(log.Log, "ls"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
})
|
||||
t.Run("when we have more than one argument", func(t *testing.T) {
|
||||
if err := RunCommandline("ls ."); err != nil {
|
||||
if err := RunCommandline(log.Log, "ls ."); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user