Skip to content

Commit 5918d2c

Browse files
authored
Append DDEV_EXECUTABLE to the Host commands environment (ddev#4724) [skip ci]
1 parent 0b7f046 commit 5918d2c

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

pkg/exec/exec.go

+16-5
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,21 @@ import (
1010
log "github.com/sirupsen/logrus"
1111
)
1212

13+
// HostCommand wraps RunCommand() to inject environment variables.
14+
// especially DDEV_EXECUTABLE, the full path to running ddev instance.
15+
func HostCommand(name string, args ...string) *exec.Cmd {
16+
c := exec.Command(name, args...)
17+
ddevExecutable, _ := os.Executable()
18+
c.Env = append(os.Environ(),
19+
"DDEV_EXECUTABLE="+ddevExecutable,
20+
)
21+
return c
22+
}
23+
1324
// RunCommand runs a command on the host system.
1425
// returns the stdout of the command and an err
1526
func RunCommand(command string, args []string) (string, error) {
16-
out, err := exec.Command(
27+
out, err := HostCommand(
1728
command, args...,
1829
).CombinedOutput()
1930

@@ -31,15 +42,15 @@ func RunCommandPipe(command string, args []string) (string, error) {
3142
"Command": command + " " + strings.Join(args[:], " "),
3243
}).Info("Running ")
3344

34-
cmd := exec.Command(command, args...)
45+
cmd := HostCommand(command, args...)
3546
stdoutStderr, err := cmd.CombinedOutput()
3647
return string(stdoutStderr), err
3748
}
3849

3950
// RunInteractiveCommand runs a command on the host system interactively, with stdin/stdout/stderr connected
4051
// Returns error
4152
func RunInteractiveCommand(command string, args []string) error {
42-
cmd := exec.Command(command, args...)
53+
cmd := HostCommand(command, args...)
4354
cmd.Stdin = os.Stdin
4455
cmd.Stdout = os.Stdout
4556
cmd.Stderr = os.Stderr
@@ -57,7 +68,7 @@ func RunHostCommand(command string, args ...string) (string, error) {
5768
if globalconfig.DdevVerbose {
5869
output.UserOut.Printf("RunHostCommand: " + command + " " + strings.Join(args, " "))
5970
}
60-
c := exec.Command(command, args...)
71+
c := HostCommand(command, args...)
6172
c.Stdin = os.Stdin
6273
o, err := c.CombinedOutput()
6374
if globalconfig.DdevVerbose {
@@ -73,7 +84,7 @@ func RunHostCommandSeparateStreams(command string, args ...string) (string, erro
7384
if globalconfig.DdevVerbose {
7485
output.UserOut.Printf("RunHostCommandSeparateStreams: " + command + " " + strings.Join(args, " "))
7586
}
76-
c := exec.Command(command, args...)
87+
c := HostCommand(command, args...)
7788
c.Stdin = os.Stdin
7889
o, err := c.Output()
7990
if globalconfig.DdevVerbose {

0 commit comments

Comments
 (0)