diff --git a/executor/executor.go b/executor/executor.go new file mode 100644 index 0000000..90c49e9 --- /dev/null +++ b/executor/executor.go @@ -0,0 +1,31 @@ +package executor + +import ( + "fmt" + "github.com/progrium/go-shell" + "os/exec" +) + +type Config struct { + execFullPath string + buildDirectory string +} + +func Executor(execFullPath, buildDirectory string) *Config { + shell.Shell = []string{"/bin/bash", "-c"} + shell.Run() + defer shell.ErrExit() + + shell.Run(fmt.Sprintf("pushd %s; go build -o %s .; popd", buildDirectory, execFullPath)) + + return &Config{ + execFullPath: execFullPath, + buildDirectory: buildDirectory, + } +} + +func (c *Config) Execute(args ...string) ([]byte, error) { + fmt.Println(c.execFullPath) + cmd := exec.Command(c.execFullPath, args...) + return cmd.CombinedOutput() +} diff --git a/executor/executor_test.go b/executor/executor_test.go new file mode 100644 index 0000000..446c49c --- /dev/null +++ b/executor/executor_test.go @@ -0,0 +1,27 @@ +package executor + +import ( + "fmt" + "github.com/stretchr/testify/assert" + "io/ioutil" + "path/filepath" + "testing" +) + +func TestCliBdd(t *testing.T) { + // given + tmpDir, err := ioutil.TempDir("", "clibdd") + assert.Nil(t, err) + efp, err := filepath.Abs(filepath.Join(tmpDir, "mite")) + assert.Nil(t, err) + bd, err := filepath.Abs("..") + assert.Nil(t, err) + config := Executor(efp, bd) + + // when + stdOutAndErr, err := config.Execute("-c", "testdata/.mite.toml", "config") + + // then + assert.Nil(t, err) + fmt.Println(string(stdOutAndErr)) +} diff --git a/go.mod b/go.mod index 2267d6d..7b9b81b 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ require ( github.com/cheynewallace/tabby v1.1.0 github.com/inconshreveable/mousetrap v1.0.0 // indirect github.com/mitchellh/go-homedir v1.1.0 + github.com/progrium/go-shell v0.0.0-20181023041501-104b11941186 github.com/spf13/cobra v0.0.3 github.com/spf13/viper v1.3.1 github.com/stretchr/testify v1.3.0 diff --git a/go.sum b/go.sum index 789ea1a..02984a0 100644 --- a/go.sum +++ b/go.sum @@ -25,6 +25,8 @@ github.com/pelletier/go-toml v1.2.0 h1:T5zMGML61Wp+FlcbWjRDT7yAxhJNAiPPLOFECq181 github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/progrium/go-shell v0.0.0-20181023041501-104b11941186 h1:MhWIYlyv3SL+7kBBp1R/sPqLNKoWa5BSuMzy5FVkLTU= +github.com/progrium/go-shell v0.0.0-20181023041501-104b11941186/go.mod h1:8ElsifciDrOoyOeBTsa7GpKzmxWo0AtZ/5mjoNxzKnQ= github.com/spf13/afero v1.1.2 h1:m8/z1t7/fwjysjQRYbP0RD+bUIF/8tJwPdEZsI83ACI= github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ= github.com/spf13/cast v1.3.0 h1:oget//CVOEoFewqQxwr0Ej5yjygnqGkvggSE/gB35Q8=