Skip to content

Commit

Permalink
(#25) Extract compose execution to a function
Browse files Browse the repository at this point in the history
It is shared by invoke and down
  • Loading branch information
mdelapenya committed Sep 18, 2019
1 parent 6b395a9 commit f7027b2
Showing 1 changed file with 32 additions and 50 deletions.
82 changes: 32 additions & 50 deletions compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,29 +51,7 @@ func NewLocalDockerCompose(filePath string, identifier string) *LocalDockerCompo

// Down executes docker-compose down
func (dc *LocalDockerCompose) Down() ExecError {
if which(dc.Executable) != nil {
panic("Local Docker Compose not found. Is " + dc.Executable + " on the PATH?")
}

environment := dc.getDockerComposeEnvironment()

abs, err := filepath.Abs(dc.ComposeFilePath)
pwd, name := filepath.Split(abs)

cmds := []string{
"-f", name, "down",
}

execErr := execute(pwd, environment, dc.Executable, cmds)
err = execErr.Error
if err != nil {
args := strings.Join(dc.Cmd, " ")
panic(
"Local Docker compose exited abnormally whilst running " +
dc.Executable + ": [" + args + "]. " + err.Error())
}

return execErr
return executeCompose(dc, []string{"down"})
}

func (dc *LocalDockerCompose) getDockerComposeEnvironment() map[string]string {
Expand All @@ -87,33 +65,7 @@ func (dc *LocalDockerCompose) getDockerComposeEnvironment() map[string]string {

// Invoke invokes the docker compose
func (dc *LocalDockerCompose) Invoke() ExecError {
if which(dc.Executable) != nil {
panic("Local Docker Compose not found. Is " + dc.Executable + " on the PATH?")
}

environment := dc.getDockerComposeEnvironment()
for k, v := range dc.Env {
environment[k] = v
}

abs, err := filepath.Abs(dc.ComposeFilePath)
pwd, name := filepath.Split(abs)

cmds := []string{
"-f", name,
}
cmds = append(cmds, dc.Cmd...)

execErr := execute(pwd, environment, dc.Executable, cmds)
err = execErr.Error
if err != nil {
args := strings.Join(dc.Cmd, " ")
panic(
"Local Docker compose exited abnormally whilst running " +
dc.Executable + ": [" + args + "]. " + err.Error())
}

return execErr
return executeCompose(dc, dc.Cmd)
}

// WithCommand assigns the command
Expand Down Expand Up @@ -186,6 +138,36 @@ func execute(
}
}

func executeCompose(dc *LocalDockerCompose, args []string) ExecError {
if which(dc.Executable) != nil {
panic("Local Docker Compose not found. Is " + dc.Executable + " on the PATH?")
}

environment := dc.getDockerComposeEnvironment()
for k, v := range dc.Env {
environment[k] = v
}

abs, err := filepath.Abs(dc.ComposeFilePath)
pwd, name := filepath.Split(abs)

cmds := []string{
"-f", name,
}
cmds = append(cmds, args...)

execErr := execute(pwd, environment, dc.Executable, cmds)
err = execErr.Error
if err != nil {
args := strings.Join(dc.Cmd, " ")
panic(
"Local Docker compose exited abnormally whilst running " +
dc.Executable + ": [" + args + "]. " + err.Error())
}

return execErr
}

// capturingPassThroughWriter is a writer that remembers
// data written to it and passes it to w
type capturingPassThroughWriter struct {
Expand Down

0 comments on commit f7027b2

Please sign in to comment.