From 7095cc331660de8b9950ce5b92f56c634540c815 Mon Sep 17 00:00:00 2001 From: Randy Fay Date: Mon, 18 Nov 2019 14:06:46 -0800 Subject: [PATCH] Interpret exec-host in bash context, fixes #1946 (#1950) --- cmd/ddev/cmd/commands.go | 20 ++------------------ pkg/ddevapp/task.go | 22 +++++++++++++++------- pkg/util/utils.go | 16 ++++++++++++++++ 3 files changed, 33 insertions(+), 25 deletions(-) diff --git a/cmd/ddev/cmd/commands.go b/cmd/ddev/cmd/commands.go index 1f8037ff96b..7bbd535d025 100644 --- a/cmd/ddev/cmd/commands.go +++ b/cmd/ddev/cmd/commands.go @@ -12,7 +12,6 @@ import ( "github.com/spf13/cobra" "io/ioutil" "os" - osexec "os/exec" "path" "path/filepath" "runtime" @@ -42,7 +41,7 @@ func addCustomCommands(rootCmd *cobra.Command) error { return err } if runtime.GOOS == "windows" { - windowsBashPath := findWindowsBashPath() + windowsBashPath := util.FindWindowsBashPath() if windowsBashPath == "" { fmt.Println("Unable to find bash.exe in PATH, not loading custom commands") return nil @@ -95,7 +94,7 @@ func addCustomCommands(rootCmd *cobra.Command) error { func makeHostCmd(app *ddevapp.DdevApp, fullPath, name string) func(*cobra.Command, []string) { var windowsBashPath = "" if runtime.GOOS == "windows" { - windowsBashPath = findWindowsBashPath() + windowsBashPath = util.FindWindowsBashPath() } return func(cmd *cobra.Command, cobraArgs []string) { @@ -218,18 +217,3 @@ func populateExamplesAndCommands() error { } return nil } - -// On Windows we'll need the path to bash to execute anything. -// Returns empty string if not found, path if found -func findWindowsBashPath() string { - windowsBashPath, err := osexec.LookPath(`C:\Program Files\Git\bin\bash.exe`) - if err != nil { - // This one could come back with the WSL bash, in which case we may have some trouble. - windowsBashPath, err = osexec.LookPath("bash.exe") - if err != nil { - fmt.Println("Not loading custom commands; bash is not in PATH") - return "" - } - } - return windowsBashPath -} diff --git a/pkg/ddevapp/task.go b/pkg/ddevapp/task.go index a8f92083bd3..0ce91ccefdc 100644 --- a/pkg/ddevapp/task.go +++ b/pkg/ddevapp/task.go @@ -1,10 +1,13 @@ package ddevapp import ( + "bytes" "fmt" "github.com/drud/ddev/pkg/nodeps" + "github.com/drud/ddev/pkg/util" "os" "os/exec" + "runtime" "strings" ) @@ -67,16 +70,21 @@ func (c ExecHostTask) Execute() (string, string, error) { return "", "", err } - execAry := strings.Split(c.exec, " ") - - stderr := []byte{} - stdout, err := exec.Command(execAry[0], execAry[1:]...).Output() - if exitErr, ok := err.(*exec.ExitError); ok { - stderr = exitErr.Stderr + bashPath := "bash" + if runtime.GOOS == "windows" { + bashPath = util.FindWindowsBashPath() } + cmd := exec.Command(bashPath, "-c", c.exec) + var stdout bytes.Buffer + var stderr bytes.Buffer + cmd.Stdout = &stdout + cmd.Stderr = &stderr + err = cmd.Run() + _ = os.Chdir(cwd) - return string(stdout), string(stderr), err + + return stdout.String(), stderr.String(), err } // Execute (ComposerTask) runs a composer command in the web container diff --git a/pkg/util/utils.go b/pkg/util/utils.go index 5509706a5ca..57bd3e7c442 100644 --- a/pkg/util/utils.go +++ b/pkg/util/utils.go @@ -1,6 +1,7 @@ package util import ( + "fmt" "github.com/drud/ddev/pkg/nodeps" "math/rand" osexec "os/exec" @@ -146,3 +147,18 @@ func GetFirstWord(s string) string { arr := strings.Split(s, " ") return arr[0] } + +// On Windows we'll need the path to bash to execute anything. +// Returns empty string if not found, path if found +func FindWindowsBashPath() string { + windowsBashPath, err := osexec.LookPath(`C:\Program Files\Git\bin\bash.exe`) + if err != nil { + // This one could come back with the WSL bash, in which case we may have some trouble. + windowsBashPath, err = osexec.LookPath("bash.exe") + if err != nil { + fmt.Println("Not loading custom commands; bash is not in PATH") + return "" + } + } + return windowsBashPath +}