diff --git a/cmd.go b/cmd.go index 40a11df..fa65b34 100644 --- a/cmd.go +++ b/cmd.go @@ -61,6 +61,7 @@ type Cmd struct { Name string Args []string Env []string + Dir string Stdout chan string // streaming STDOUT if enabled, else nil (see Options) Stderr chan string // streaming STDERR if enabled, else nil (see Options) *sync.Mutex @@ -152,6 +153,22 @@ func NewCmdOptions(options Options, name string, args ...string) *Cmd { return out } +// SetDir sets the environment variables for the launched command. +// This can only have effect before starting the command. +func (c *Cmd) SetDir(dir string) { + c.Lock() + defer c.Unlock() + c.Dir = dir +} + +// SetEnv sets the working directory of the command. +// This can only have effect before starting the command. +func (c *Cmd) SetEnv(env []string) { + c.Lock() + defer c.Unlock() + c.Env = env +} + // Start starts the command and immediately returns a channel that the caller // can use to receive the final Status of the command when it ends. The caller // can start the command and wait like, @@ -301,6 +318,10 @@ func (c *Cmd) run() { // Set the runtime environment for the command as per os/exec.Cmd. If Env // is nil, use the current process' environment. cmd.Env = c.Env + // Dir specifies the working directory of the command. + // If Dir is the empty string, this runs the command in the + // calling process's current directory. + cmd.Dir = c.Dir // ////////////////////////////////////////////////////////////////////// // Start command diff --git a/cmd_test.go b/cmd_test.go index 9f1023a..60184d4 100644 --- a/cmd_test.go +++ b/cmd_test.go @@ -9,7 +9,7 @@ import ( "testing" "time" - "github.com/go-cmd/cmd" + "github.com/ShinyTrinkets/go-cmd" "github.com/go-test/deep" )