DRY up the config.go

This commit is contained in:
Mitchell Hashimoto 2014-07-24 07:29:53 -07:00
parent b51d18f1de
commit f82b0552e0
1 changed files with 28 additions and 44 deletions

View File

@ -108,31 +108,10 @@ func (c *Config) ProviderFactories() map[string]terraform.ResourceProviderFactor
} }
func (c *Config) providerFactory(path string) terraform.ResourceProviderFactory { func (c *Config) providerFactory(path string) terraform.ResourceProviderFactory {
originalPath := path
return func() (terraform.ResourceProvider, error) { return func() (terraform.ResourceProvider, error) {
// 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.
exePath, err := osext.Executable()
if err == nil {
path = filepath.Join(
filepath.Dir(exePath),
filepath.Base(originalPath))
}
}
// 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
}
// Build the plugin client configuration and init the plugin // Build the plugin client configuration and init the plugin
var config plugin.ClientConfig var config plugin.ClientConfig
config.Cmd = exec.Command(path) config.Cmd = pluginCmd(path)
config.Managed = true config.Managed = true
client := plugin.NewClient(&config) client := plugin.NewClient(&config)
@ -168,31 +147,10 @@ func (c *Config) ProvisionerFactories() map[string]terraform.ResourceProvisioner
} }
func (c *Config) provisionerFactory(path string) terraform.ResourceProvisionerFactory { func (c *Config) provisionerFactory(path string) terraform.ResourceProvisionerFactory {
originalPath := path
return func() (terraform.ResourceProvisioner, error) { return func() (terraform.ResourceProvisioner, error) {
// 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.
exePath, err := osext.Executable()
if err == nil {
path = filepath.Join(
filepath.Dir(exePath),
filepath.Base(originalPath))
}
}
// 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
}
// Build the plugin client configuration and init the plugin // Build the plugin client configuration and init the plugin
var config plugin.ClientConfig var config plugin.ClientConfig
config.Cmd = exec.Command(path) config.Cmd = pluginCmd(path)
config.Managed = true config.Managed = true
client := plugin.NewClient(&config) client := plugin.NewClient(&config)
@ -214,3 +172,29 @@ func (c *Config) provisionerFactory(path string) terraform.ResourceProvisionerFa
}, nil }, nil
} }
} }
func pluginCmd(path string) *exec.Cmd {
originalPath := path
// 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.
exePath, err := osext.Executable()
if err == nil {
path = filepath.Join(
filepath.Dir(exePath),
filepath.Base(originalPath))
}
}
// 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
}
// Build the command to execute the plugin
return exec.Command(path)
}