main: look up executables locally before PATH [GH-157]

This commit is contained in:
Mitchell Hashimoto 2014-08-27 20:46:50 -07:00
parent d8cc32b49f
commit bec5a2a9c5
2 changed files with 25 additions and 13 deletions

View File

@ -43,6 +43,8 @@ BUG FIXES:
Terraform structures. [GH-177] Terraform structures. [GH-177]
* core: Resources with only `file()` calls will interpolate. [GH-159] * core: Resources with only `file()` calls will interpolate. [GH-159]
* core: Variables work in block names. [GH-234] * 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] * 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: Fix issues around failing to read EIPs. [GH-122]
* providers/aws: Autoscaling groups now register and export load * providers/aws: Autoscaling groups now register and export load

View File

@ -3,8 +3,10 @@ package main
import ( import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"strings"
"github.com/hashicorp/hcl" "github.com/hashicorp/hcl"
"github.com/hashicorp/terraform/plugin" "github.com/hashicorp/terraform/plugin"
@ -180,27 +182,35 @@ func (c *Config) provisionerFactory(path string) terraform.ResourceProvisionerFa
} }
func pluginCmd(path string) *exec.Cmd { func pluginCmd(path string) *exec.Cmd {
originalPath := path cmdPath := ""
// First look for the provider on the PATH. // If the path doesn't contain a separator, look in the same
path, err := exec.LookPath(path) // directory as the Terraform executable first.
if err != nil { if !strings.ContainsRune(path, os.PathSeparator) {
// If that doesn't work, look for it in the same directory
// as the executable that is running.
exePath, err := osext.Executable() exePath, err := osext.Executable()
if err == nil { if err == nil {
path = filepath.Join( temp := filepath.Join(
filepath.Dir(exePath), filepath.Dir(exePath),
filepath.Base(originalPath)) filepath.Base(path))
if _, err := os.Stat(temp); err == nil {
cmdPath = temp
} }
} }
// If we still don't have a path set, then set it to the // If we still haven't found the executable, look for it
// original path and let any errors that happen bubble out. // in the PATH.
if path == "" { if v, err := exec.LookPath(path); err == nil {
path = originalPath cmdPath = v
}
}
// 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 // Build the command to execute the plugin
return exec.Command(path) return exec.Command(cmdPath)
} }