Look for docker cli next to the current executable

Signed-off-by: Mathieu Champlon <mathieu.champlon@docker.com>
This commit is contained in:
Mathieu Champlon 2021-06-22 07:12:01 +02:00
parent 6bfdfa8947
commit 313de1de4a
1 changed files with 23 additions and 3 deletions

View File

@ -22,6 +22,7 @@ import (
"os"
"os/exec"
"os/signal"
"path/filepath"
"regexp"
apicontext "github.com/docker/compose-cli/api/context"
@ -89,10 +90,13 @@ func Exec(root *cobra.Command) {
func RunDocker(childExit chan bool, args ...string) error {
execBinary, err := resolvepath.LookPath(ComDockerCli)
if err != nil {
execBinary = findBinary(ComDockerCli)
if execBinary == "" {
fmt.Fprintln(os.Stderr, err)
fmt.Fprintln(os.Stderr, "Current PATH : "+os.Getenv("PATH"))
os.Exit(1)
}
}
cmd := exec.Command(execBinary, args...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
@ -123,6 +127,22 @@ func RunDocker(childExit chan bool, args ...string) error {
return cmd.Run()
}
func findBinary(filename string) string {
currentBinaryPath, err := os.Executable()
if err != nil {
return ""
}
currentBinaryPath, err = filepath.EvalSymlinks(currentBinaryPath)
if err != nil {
return ""
}
binaryPath := filepath.Join(filepath.Dir(currentBinaryPath), filename)
if _, err := os.Stat(binaryPath); err != nil {
return ""
}
return binaryPath
}
// IsDefaultContextCommand checks if the command exists in the classic cli (issues a shellout --help)
func IsDefaultContextCommand(dockerCommand string) bool {
cmd := exec.Command(ComDockerCli, dockerCommand, "--help")