aboutsummaryrefslogtreecommitdiffhomepage
path: root/vendor/github.com/hashicorp/terraform/plugin/discovery/get_cache.go
blob: 1a100426482714f42b3cb2cdf2e64039ced15ccc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package discovery

// PluginCache is an interface implemented by objects that are able to maintain
// a cache of plugins.
type PluginCache interface {
	// CachedPluginPath returns a path where the requested plugin is already
	// cached, or an empty string if the requested plugin is not yet cached.
	CachedPluginPath(kind string, name string, version Version) string

	// InstallDir returns the directory that new plugins should be installed into
	// in order to populate the cache. This directory should be used as the
	// first argument to getter.Get when downloading plugins with go-getter.
	//
	// After installing into this directory, use CachedPluginPath to obtain the
	// path where the plugin was installed.
	InstallDir() string
}

// NewLocalPluginCache returns a PluginCache that caches plugins in a
// given local directory.
func NewLocalPluginCache(dir string) PluginCache {
	return &pluginCache{
		Dir: dir,
	}
}

type pluginCache struct {
	Dir string
}

func (c *pluginCache) CachedPluginPath(kind string, name string, version Version) string {
	allPlugins := FindPlugins(kind, []string{c.Dir})
	plugins := allPlugins.WithName(name).WithVersion(version)

	if plugins.Count() == 0 {
		// nothing cached
		return ""
	}

	// There should generally be only one plugin here; if there's more than
	// one match for some reason then we'll just choose one arbitrarily.
	plugin := plugins.Newest()
	return plugin.Path
}

func (c *pluginCache) InstallDir() string {
	return c.Dir
}