-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9c93552
commit eb779c8
Showing
2 changed files
with
41 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package mgtool | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
"os" | ||
"os/exec" | ||
) | ||
|
||
// RunInDir runs the given command in the specified directory, discarding stdout. | ||
func RunInDir(cmd, dir string, args ...string) error { | ||
b := io.Discard | ||
return run(cmd, dir, b, os.Stderr, args...) | ||
} | ||
|
||
// RunInDirV runs the given command in the specified directory, outputting to stdout. | ||
func RunInDirV(cmd, dir string, args ...string) error { | ||
return run(cmd, dir, os.Stdout, os.Stderr, args...) | ||
} | ||
|
||
// OutputRunInDir run the given command in the specified directory, returning the output ad a string. | ||
func OutputRunInDir(cmd, dir string, args ...string) (string, error) { | ||
b := &bytes.Buffer{} | ||
if err := run(cmd, dir, b, os.Stderr, args...); err != nil { | ||
return "", err | ||
} | ||
return b.String(), nil | ||
} | ||
|
||
func run(cmd, dir string, stdout, stderr io.Writer, args ...string) error { | ||
c := exec.Command(cmd, args...) | ||
c.Env = os.Environ() | ||
c.Stderr = stdout | ||
c.Stdout = stderr | ||
c.Stdin = os.Stdin | ||
c.Dir = dir | ||
|
||
return c.Run() | ||
} |