aboutsummaryrefslogtreecommitdiff
path: root/pkg/cli/command.go
diff options
context:
space:
mode:
authorStefan Majewsky <majewsky@gmx.net>2017-05-09 21:32:16 +0200
committerStefan Majewsky <majewsky@gmx.net>2017-05-09 21:38:24 +0200
commita92faf7689fa01595fd613ddd3880e9c301ed558 (patch)
treef6607fa23ac41385f9f18d8ed6ad0f970c4ba6df /pkg/cli/command.go
parenta4dfbab3b06360245f6b2fce4b56d6caf2729f8f (diff)
downloadgofu-a92faf7689fa01595fd613ddd3880e9c301ed558.tar.gz
refactor cli.Interface yet again
- Only one global instance now, so we don't need to pass it around all the time. - Allow to swap out the Command.Run implementation for unit tests. - Can now use cli.Interface during func init() of packages importing pkg/cli.
Diffstat (limited to 'pkg/cli/command.go')
-rw-r--r--pkg/cli/command.go10
1 files changed, 8 insertions, 2 deletions
diff --git a/pkg/cli/command.go b/pkg/cli/command.go
index 5b3ded5..d07a120 100644
--- a/pkg/cli/command.go
+++ b/pkg/cli/command.go
@@ -26,7 +26,7 @@ import (
)
//Command describes a command that can be run using the methods in the
-//Interface interface.
+//Implementation interface.
type Command struct {
Program []string
WorkDir string
@@ -49,8 +49,14 @@ func (e commandError) Error() string {
)
}
-func (c Command) run(stdout, stderr io.Writer) error {
+//CommandRunner is a function that can execute commands given to it.
+//This interface is only useful for unit tests; the default CommandRunner
+//suffices for all regular operation.
+type CommandRunner func(c Command, stdin io.Reader, stdout, stderr io.Writer) error
+
+func DefaultCommandRunner(c Command, stdin io.Reader, stdout, stderr io.Writer) error {
cmd := exec.Command(c.Program[0], c.Program[1:]...)
+ cmd.Stdin = stdin
cmd.Stdout = stdout
cmd.Stderr = stderr
cmd.Dir = c.WorkDir