-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Alex Ellis <alexellis2@gmail.com>
- Loading branch information
0 parents
commit 08963d2
Showing
3 changed files
with
178 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
See Godoc [github.com/alexellis/go-execute](https://godoc.org/github.com/alexellis/go-execute) | ||
|
||
License: MIT |
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,93 @@ | ||
package execute | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
"os/exec" | ||
"strings" | ||
) | ||
|
||
type ExecTask struct { | ||
Command string | ||
Args []string | ||
Shell bool | ||
Env []string | ||
Cwd string | ||
} | ||
|
||
type ExecResult struct { | ||
Stdout string | ||
Stderr string | ||
ExitCode int | ||
} | ||
|
||
func (et ExecTask) Execute() (ExecResult, error) { | ||
fmt.Println("exec: ", et.Command) | ||
|
||
var cmd *exec.Cmd | ||
|
||
if et.Shell { | ||
startArgs := strings.Split(et.Command, " ") | ||
args := []string{"-c"} | ||
for _, part := range startArgs { | ||
args = append(args, part) | ||
} | ||
args = append(args) | ||
|
||
cmd = exec.Command("/bin/bash", args...) | ||
} else { | ||
if strings.Index(et.Command, " ") > 0 { | ||
parts := strings.Split(et.Command, " ") | ||
command := parts[0] | ||
args := parts[1:] | ||
cmd = exec.Command(command, args...) | ||
|
||
} else { | ||
cmd = exec.Command(et.Command, et.Args...) | ||
} | ||
} | ||
|
||
cmd.Dir = et.Cwd | ||
|
||
if len(et.Env) > 0 { | ||
cmd.Env = os.Environ() | ||
for _, env := range et.Env { | ||
cmd.Env = append(cmd.Env, env) | ||
} | ||
} | ||
|
||
stdoutPipe, stdoutPipeErr := cmd.StdoutPipe() | ||
if stdoutPipeErr != nil { | ||
return ExecResult{}, stdoutPipeErr | ||
} | ||
|
||
stderrPipe, stderrPipeErr := cmd.StderrPipe() | ||
if stderrPipeErr != nil { | ||
return ExecResult{}, stderrPipeErr | ||
} | ||
|
||
startErr := cmd.Start() | ||
|
||
if startErr != nil { | ||
return ExecResult{}, startErr | ||
} | ||
|
||
stdoutBytes, err := ioutil.ReadAll(stdoutPipe) | ||
if err != nil { | ||
return ExecResult{}, err | ||
} | ||
|
||
stderrBytes, err := ioutil.ReadAll(stderrPipe) | ||
|
||
if err != nil { | ||
return ExecResult{}, err | ||
} | ||
|
||
fmt.Println("res: " + string(stdoutBytes)) | ||
|
||
return ExecResult{ | ||
Stdout: string(stdoutBytes), | ||
Stderr: string(stderrBytes), | ||
}, nil | ||
} |
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,82 @@ | ||
package execute | ||
|
||
import ( | ||
"os" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestExec_WithShell(t *testing.T) { | ||
task := ExecTask{Command: "/bin/ls /", Shell: true} | ||
res, err := task.Execute() | ||
if err != nil { | ||
t.Errorf(err.Error()) | ||
t.Fail() | ||
} | ||
|
||
if len(res.Stdout) == 0 { | ||
t.Errorf("want data, but got empty") | ||
t.Fail() | ||
} | ||
|
||
if len(res.Stderr) != 0 { | ||
t.Errorf("want empty, but got: %s", res.Stderr) | ||
t.Fail() | ||
} | ||
} | ||
|
||
func TestExec_WithEnvVars(t *testing.T) { | ||
task := ExecTask{Command: "env", Shell: false, Env: []string{"GOTEST=1", "GOTEST2=2"}} | ||
res, err := task.Execute() | ||
if err != nil { | ||
t.Errorf(err.Error()) | ||
t.Fail() | ||
} | ||
|
||
if !strings.Contains(res.Stdout, "GOTEST") { | ||
t.Errorf("want env to show GOTEST=1 since we passed that variable") | ||
t.Fail() | ||
} | ||
|
||
if !strings.Contains(res.Stdout, "GOTEST2") { | ||
t.Errorf("want env to show GOTEST2=2 since we passed that variable") | ||
t.Fail() | ||
} | ||
|
||
} | ||
|
||
func TestExec_WithEnvVarsInheritedFromParent(t *testing.T) { | ||
os.Setenv("TEST", "value") | ||
task := ExecTask{Command: "env", Shell: false, Env: []string{"GOTEST=1"}} | ||
res, err := task.Execute() | ||
if err != nil { | ||
t.Errorf(err.Error()) | ||
t.Fail() | ||
} | ||
|
||
if !strings.Contains(res.Stdout, "TEST") { | ||
t.Errorf("want env to show TEST=value since we passed that variable") | ||
t.Fail() | ||
} | ||
|
||
if !strings.Contains(res.Stdout, "GOTEST") { | ||
t.Errorf("want env to show GOTEST=1 since we passed that variable") | ||
t.Fail() | ||
} | ||
|
||
} | ||
|
||
func TestExec_WithEnvVarsAndShell(t *testing.T) { | ||
task := ExecTask{Command: "env", Shell: true, Env: []string{"GOTEST=1"}} | ||
res, err := task.Execute() | ||
if err != nil { | ||
t.Errorf(err.Error()) | ||
t.Fail() | ||
} | ||
|
||
if !strings.Contains(res.Stdout, "GOTEST") { | ||
t.Errorf("want env to show GOTEST=1 since we passed that variable") | ||
t.Fail() | ||
} | ||
|
||
} |