Skip to content

Commit

Permalink
Initial
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Ellis <alexellis2@gmail.com>
  • Loading branch information
alexellis committed Oct 9, 2019
0 parents commit 08963d2
Show file tree
Hide file tree
Showing 3 changed files with 178 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
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
93 changes: 93 additions & 0 deletions exec.go
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
}
82 changes: 82 additions & 0 deletions exec_test.go
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()
}

}

0 comments on commit 08963d2

Please sign in to comment.