From bec5a2a9c59d8b87aaccdbaf212516a0571fd048 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 27 Aug 2014 20:46:50 -0700 Subject: [PATCH] main: look up executables locally before PATH [GH-157] --- CHANGELOG.md | 2 ++ config.go | 36 +++++++++++++++++++++++------------- 2 files changed, 25 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c3c930a73..aad50e4fd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -43,6 +43,8 @@ BUG FIXES: Terraform structures. [GH-177] * core: Resources with only `file()` calls will interpolate. [GH-159] * core: Variables work in block names. [GH-234] + * core: Plugins are searched for in the same directory as the executable + before the PATH. [GH-157] * command/apply: "tfvars" file no longer interferes with plan apply. [GH-153] * providers/aws: Fix issues around failing to read EIPs. [GH-122] * providers/aws: Autoscaling groups now register and export load diff --git a/config.go b/config.go index 87747be5c..ab676f01f 100644 --- a/config.go +++ b/config.go @@ -3,8 +3,10 @@ package main import ( "fmt" "io/ioutil" + "os" "os/exec" "path/filepath" + "strings" "github.com/hashicorp/hcl" "github.com/hashicorp/terraform/plugin" @@ -180,27 +182,35 @@ func (c *Config) provisionerFactory(path string) terraform.ResourceProvisionerFa } func pluginCmd(path string) *exec.Cmd { - originalPath := path + cmdPath := "" - // First look for the provider on the PATH. - path, err := exec.LookPath(path) - if err != nil { - // If that doesn't work, look for it in the same directory - // as the executable that is running. + // If the path doesn't contain a separator, look in the same + // directory as the Terraform executable first. + if !strings.ContainsRune(path, os.PathSeparator) { exePath, err := osext.Executable() if err == nil { - path = filepath.Join( + temp := filepath.Join( filepath.Dir(exePath), - filepath.Base(originalPath)) + filepath.Base(path)) + + if _, err := os.Stat(temp); err == nil { + cmdPath = temp + } + } + + // If we still haven't found the executable, look for it + // in the PATH. + if v, err := exec.LookPath(path); err == nil { + cmdPath = v } } - // If we still don't have a path set, then set it to the - // original path and let any errors that happen bubble out. - if path == "" { - path = originalPath + // If we still don't have a path, then just set it to the original + // given path. + if cmdPath == "" { + cmdPath = path } // Build the command to execute the plugin - return exec.Command(path) + return exec.Command(cmdPath) }