From a92faf7689fa01595fd613ddd3880e9c301ed558 Mon Sep 17 00:00:00 2001 From: Stefan Majewsky Date: Tue, 9 May 2017 21:32:16 +0200 Subject: 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. --- pkg/cli/command.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'pkg/cli/command.go') 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 -- cgit v1.3.1