Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"testing"
"time"

"github.com/go-cmd/cmd"
"github.com/ShinyTrinkets/go-cmd"
"github.com/go-test/deep"
)

Expand Down