mostly remove OS_ARCH knowledge from discovery

Since the command package also needs to know about the specific OS_ARCH
directories, remove the logic fom the discovery package.

This doesn't completely remove the knowledge of the path from discovery,
in order to maintain the current behavior of skipping legacy plugin
names within a new-style path.
This commit is contained in:
James Bardin 2017-06-16 13:58:40 -04:00
parent 5d19148f47
commit ba5b0dc609
2 changed files with 38 additions and 45 deletions

View File

@ -4,11 +4,14 @@ import (
"io/ioutil" "io/ioutil"
"log" "log"
"path/filepath" "path/filepath"
"regexp"
"runtime" "runtime"
"strings" "strings"
) )
const machineName = runtime.GOOS + "_" + runtime.GOARCH // Store the machine name for excluding legacy plugins in new-style directories.
// This is a var to override in testing
var machineName = runtime.GOOS + "_" + runtime.GOARCH
// FindPlugins looks in the given directories for files whose filenames // FindPlugins looks in the given directories for files whose filenames
// suggest that they are plugins of the given kind (e.g. "provider") and // suggest that they are plugins of the given kind (e.g. "provider") and
@ -41,66 +44,53 @@ func FindPluginPaths(kind string, dirs []string) []string {
// This is just a thin wrapper around findPluginPaths so that we can // This is just a thin wrapper around findPluginPaths so that we can
// use the latter in tests with a fake machineName so we can use our // use the latter in tests with a fake machineName so we can use our
// test fixtures. // test fixtures.
return findPluginPaths(kind, machineName, dirs) return findPluginPaths(kind, dirs)
} }
func findPluginPaths(kind string, machineName string, dirs []string) []string { func findPluginPaths(kind string, dirs []string) []string {
hasMachineSuffix := regexp.MustCompile(machineName + "/?").MatchString
prefix := "terraform-" + kind + "-" prefix := "terraform-" + kind + "-"
ret := make([]string, 0, len(dirs)) ret := make([]string, 0, len(dirs))
for _, baseDir := range dirs { for _, dir := range dirs {
baseItems, err := ioutil.ReadDir(baseDir) items, err := ioutil.ReadDir(dir)
if err != nil { if err != nil {
// Ignore missing dirs, non-dirs, etc // Ignore missing dirs, non-dirs, etc
continue continue
} }
log.Printf("[DEBUG] checking for plugins in %q", baseDir) log.Printf("[DEBUG] checking for plugins in %q", dir)
for _, item := range baseItems { isMachineDir := hasMachineSuffix(dir)
for _, item := range items {
fullName := item.Name() fullName := item.Name()
if fullName == machineName && item.Mode().IsDir() { if !strings.HasPrefix(fullName, prefix) {
// Current-style $GOOS-$GOARCH directory prefix log.Printf("[DEBUG] skipping %q, not a plugin", fullName)
machineDir := filepath.Join(baseDir, machineName)
machineItems, err := ioutil.ReadDir(machineDir)
if err != nil {
continue
}
log.Printf("[DEBUG] checking for plugins in %q", machineDir)
for _, item := range machineItems {
fullName := item.Name()
if !strings.HasPrefix(fullName, prefix) {
continue
}
// New-style paths must have a version segment in filename
if !strings.Contains(strings.ToLower(fullName), "_v") {
continue
}
absPath, err := filepath.Abs(filepath.Join(machineDir, fullName))
if err != nil {
continue
}
log.Printf("[DEBUG] found plugin %q", fullName)
ret = append(ret, filepath.Clean(absPath))
}
continue continue
} }
// FIXME: we pass in GOOS_GOARCH paths directly, so these may not be "legacy" // New-style paths must have a version segment in filename
if strings.HasPrefix(fullName, prefix) { if strings.Contains(strings.ToLower(fullName), "_v") {
// Legacy style with files directly in the base directory absPath, err := filepath.Abs(filepath.Join(dir, fullName))
absPath, err := filepath.Abs(filepath.Join(baseDir, fullName))
if err != nil { if err != nil {
log.Printf("[ERROR] plugin filepath error: %s", err)
continue
}
log.Printf("[DEBUG] found plugin %q", fullName)
ret = append(ret, filepath.Clean(absPath))
continue
}
if !isMachineDir {
// Legacy style with files directly in the base directory
absPath, err := filepath.Abs(filepath.Join(dir, fullName))
if err != nil {
log.Printf("[ERROR] plugin filepath error: %s", err)
continue continue
} }

View File

@ -7,12 +7,15 @@ import (
"testing" "testing"
) )
func init() {
machineName = "mockos_mockarch"
}
func TestFindPluginPaths(t *testing.T) { func TestFindPluginPaths(t *testing.T) {
got := findPluginPaths( got := findPluginPaths(
"foo", "foo",
"mockos_mockarch",
[]string{ []string{
"test-fixtures/current-style-plugins", "test-fixtures/current-style-plugins/mockos_mockarch",
"test-fixtures/legacy-style-plugins", "test-fixtures/legacy-style-plugins",
"test-fixtures/non-existent", "test-fixtures/non-existent",
"test-fixtures/not-a-dir", "test-fixtures/not-a-dir",