Skip to content

Commit

Permalink
Interpret exec-host in bash context, fixes #1946 (#1950)
Browse files Browse the repository at this point in the history
  • Loading branch information
rfay committed Nov 18, 2019
1 parent 2a603bb commit 7095cc3
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 25 deletions.
20 changes: 2 additions & 18 deletions cmd/ddev/cmd/commands.go
Expand Up @@ -12,7 +12,6 @@ import (
"github.com/spf13/cobra"
"io/ioutil"
"os"
osexec "os/exec"
"path"
"path/filepath"
"runtime"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
}
22 changes: 15 additions & 7 deletions 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"
)

Expand Down Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions pkg/util/utils.go
@@ -1,6 +1,7 @@
package util

import (
"fmt"
"github.com/drud/ddev/pkg/nodeps"
"math/rand"
osexec "os/exec"
Expand Down Expand Up @@ -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
}

0 comments on commit 7095cc3

Please sign in to comment.