diff options
author | Chris Taylor <ctaylorr@gmail.com> | 2020-02-01 17:09:46 -0500 |
---|---|---|
committer | Jesse Duffield <jessedduffield@gmail.com> | 2020-02-02 11:29:22 +1100 |
commit | 75ba2196ba365e25bb8888a218536cc8ec713a78 (patch) | |
tree | 63785f7fe91119ff5fec97179c0419ac19c41330 | |
parent | 4cb50b15e43606a1fe4d36fc36e0b1ca447949ab (diff) |
perpetuate this style of dependency injection
-rw-r--r-- | pkg/commands/os.go | 8 |
1 files changed, 8 insertions, 0 deletions
diff --git a/pkg/commands/os.go b/pkg/commands/os.go index 375a8e77e..e85b3e19c 100644 --- a/pkg/commands/os.go +++ b/pkg/commands/os.go @@ -36,6 +36,7 @@ type OSCommand struct { Platform *Platform Config config.AppConfigurer command func(string, ...string) *exec.Cmd + beforeExecuteCmd func(*exec.Cmd) getGlobalGitConfig func(string) (string, error) getenv func(string) string } @@ -47,6 +48,7 @@ func NewOSCommand(log *logrus.Entry, config config.AppConfigurer) *OSCommand { Platform: getPlatform(), Config: config, command: exec.Command, + beforeExecuteCmd: func(*exec.Cmd) {}, getGlobalGitConfig: gitconfig.Global, getenv: os.Getenv, } @@ -58,6 +60,10 @@ func (c *OSCommand) SetCommand(cmd func(string, ...string) *exec.Cmd) { c.command = cmd } +func (c *OSCommand) SetBeforeExecuteCmd(cmd func(*exec.Cmd)) { + c.beforeExecuteCmd = cmd +} + // RunCommandWithOutput wrapper around commands returning their output and error // NOTE: If you don't pass any formatArgs we'll just use the command directly, // however there's a bizarre compiler error/warning when you pass in a formatString @@ -76,6 +82,7 @@ func (c *OSCommand) RunCommandWithOutput(formatString string, formatArgs ...inte // RunExecutableWithOutput runs an executable file and returns its output func (c *OSCommand) RunExecutableWithOutput(cmd *exec.Cmd) (string, error) { + c.beforeExecuteCmd(cmd) return sanitisedCommandOutput(cmd.CombinedOutput()) } @@ -308,6 +315,7 @@ func (c *OSCommand) FileExists(path string) (bool, error) { // this is useful if you need to give your command some environment variables // before running it func (c *OSCommand) RunPreparedCommand(cmd *exec.Cmd) error { + c.beforeExecuteCmd(cmd) out, err := cmd.CombinedOutput() outString := string(out) c.Log.Info(outString) |