From f82b0552e06ff8c29b1f1b9b91ff9a903d64750a Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 24 Jul 2014 07:29:53 -0700 Subject: [PATCH] DRY up the config.go --- config.go | 72 ++++++++++++++++++++++--------------------------------- 1 file changed, 28 insertions(+), 44 deletions(-) diff --git a/config.go b/config.go index 29a30e66c..b12c7ce64 100644 --- a/config.go +++ b/config.go @@ -108,31 +108,10 @@ func (c *Config) ProviderFactories() map[string]terraform.ResourceProviderFactor } func (c *Config) providerFactory(path string) terraform.ResourceProviderFactory { - originalPath := path - 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 var config plugin.ClientConfig - config.Cmd = exec.Command(path) + config.Cmd = pluginCmd(path) config.Managed = true client := plugin.NewClient(&config) @@ -168,31 +147,10 @@ func (c *Config) ProvisionerFactories() map[string]terraform.ResourceProvisioner } func (c *Config) provisionerFactory(path string) terraform.ResourceProvisionerFactory { - originalPath := path - 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 var config plugin.ClientConfig - config.Cmd = exec.Command(path) + config.Cmd = pluginCmd(path) config.Managed = true client := plugin.NewClient(&config) @@ -214,3 +172,29 @@ func (c *Config) provisionerFactory(path string) terraform.ResourceProvisionerFa }, 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) +}