11 // FindPlugins looks in the given directories for files whose filenames
12 // suggest that they are plugins of the given kind (e.g. "provider") and
13 // returns a PluginMetaSet representing the discovered potential-plugins.
15 // Currently this supports two different naming schemes. The current
16 // standard naming scheme is a subdirectory called $GOOS-$GOARCH containing
17 // files named terraform-$KIND-$NAME-V$VERSION. The legacy naming scheme is
18 // files directly in the given directory whose names are like
19 // terraform-$KIND-$NAME.
21 // Only one plugin will be returned for each unique plugin (name, version)
22 // pair, with preference given to files found in earlier directories.
24 // This is a convenience wrapper around FindPluginPaths and ResolvePluginsPaths.
25 func FindPlugins(kind string, dirs []string) PluginMetaSet {
26 return ResolvePluginPaths(FindPluginPaths(kind, dirs))
29 // FindPluginPaths looks in the given directories for files whose filenames
30 // suggest that they are plugins of the given kind (e.g. "provider").
32 // The return value is a list of absolute paths that appear to refer to
33 // plugins in the given directories, based only on what can be inferred
34 // from the naming scheme. The paths returned are ordered such that files
35 // in later dirs appear after files in earlier dirs in the given directory
36 // list. Within the same directory plugins are returned in a consistent but
38 func FindPluginPaths(kind string, dirs []string) []string {
39 // This is just a thin wrapper around findPluginPaths so that we can
40 // use the latter in tests with a fake machineName so we can use our
42 return findPluginPaths(kind, dirs)
45 func findPluginPaths(kind string, dirs []string) []string {
46 prefix := "terraform-" + kind + "-"
48 ret := make([]string, 0, len(dirs))
50 for _, dir := range dirs {
51 items, err := ioutil.ReadDir(dir)
53 // Ignore missing dirs, non-dirs, etc
57 log.Printf("[DEBUG] checking for %s in %q", kind, dir)
59 for _, item := range items {
60 fullName := item.Name()
62 if !strings.HasPrefix(fullName, prefix) {
66 // New-style paths must have a version segment in filename
67 if strings.Contains(strings.ToLower(fullName), "_v") {
68 absPath, err := filepath.Abs(filepath.Join(dir, fullName))
70 log.Printf("[ERROR] plugin filepath error: %s", err)
74 // Check that the file we found is usable
75 if !pathIsFile(absPath) {
76 log.Printf("[ERROR] ignoring non-file %s", absPath)
80 log.Printf("[DEBUG] found %s %q", kind, fullName)
81 ret = append(ret, filepath.Clean(absPath))
85 // Legacy style with files directly in the base directory
86 absPath, err := filepath.Abs(filepath.Join(dir, fullName))
88 log.Printf("[ERROR] plugin filepath error: %s", err)
92 // Check that the file we found is usable
93 if !pathIsFile(absPath) {
94 log.Printf("[ERROR] ignoring non-file %s", absPath)
98 log.Printf("[WARN] found legacy %s %q", kind, fullName)
100 ret = append(ret, filepath.Clean(absPath))
107 // Returns true if and only if the given path refers to a file or a symlink
109 func pathIsFile(path string) bool {
110 info, err := os.Stat(path)
118 // ResolvePluginPaths takes a list of paths to plugin executables (as returned
119 // by e.g. FindPluginPaths) and produces a PluginMetaSet describing the
120 // referenced plugins.
122 // If the same combination of plugin name and version appears multiple times,
123 // the earlier reference will be preferred. Several different versions of
124 // the same plugin name may be returned, in which case the methods of
125 // PluginMetaSet can be used to filter down.
126 func ResolvePluginPaths(paths []string) PluginMetaSet {
127 s := make(PluginMetaSet)
129 type nameVersion struct {
133 found := make(map[nameVersion]struct{})
135 for _, path := range paths {
136 baseName := strings.ToLower(filepath.Base(path))
137 if !strings.HasPrefix(baseName, "terraform-") {
138 // Should never happen with reasonable input
142 baseName = baseName[10:]
143 firstDash := strings.Index(baseName, "-")
145 // Should never happen with reasonable input
149 baseName = baseName[firstDash+1:]
151 // Should never happen with reasonable input
155 // Trim the .exe suffix used on Windows before we start wrangling
156 // the remainder of the path.
157 if strings.HasSuffix(baseName, ".exe") {
158 baseName = baseName[:len(baseName)-4]
161 parts := strings.SplitN(baseName, "_v", 2)
163 version := VersionZero
168 // Auto-installed plugins contain an extra name portion representing
169 // the expected plugin version, which we must trim off.
170 if underX := strings.Index(version, "_x"); underX != -1 {
171 version = version[:underX]
174 if _, ok := found[nameVersion{name, version}]; ok {
175 // Skip duplicate versions of the same plugin
176 // (We do this during this step because after this we will be
177 // dealing with sets and thus lose our ordering with which to
178 // decide preference.)
184 Version: VersionStr(version),
187 found[nameVersion{name, version}] = struct{}{}