github.com/jhixson74/hashicorp-terraform@v0.11.12-beta1/plugin/discovery/get_cache.go (about)

     1  package discovery
     2  
     3  // PluginCache is an interface implemented by objects that are able to maintain
     4  // a cache of plugins.
     5  type PluginCache interface {
     6  	// CachedPluginPath returns a path where the requested plugin is already
     7  	// cached, or an empty string if the requested plugin is not yet cached.
     8  	CachedPluginPath(kind string, name string, version Version) string
     9  
    10  	// InstallDir returns the directory that new plugins should be installed into
    11  	// in order to populate the cache. This directory should be used as the
    12  	// first argument to getter.Get when downloading plugins with go-getter.
    13  	//
    14  	// After installing into this directory, use CachedPluginPath to obtain the
    15  	// path where the plugin was installed.
    16  	InstallDir() string
    17  }
    18  
    19  // NewLocalPluginCache returns a PluginCache that caches plugins in a
    20  // given local directory.
    21  func NewLocalPluginCache(dir string) PluginCache {
    22  	return &pluginCache{
    23  		Dir: dir,
    24  	}
    25  }
    26  
    27  type pluginCache struct {
    28  	Dir string
    29  }
    30  
    31  func (c *pluginCache) CachedPluginPath(kind string, name string, version Version) string {
    32  	allPlugins := FindPlugins(kind, []string{c.Dir})
    33  	plugins := allPlugins.WithName(name).WithVersion(version)
    34  
    35  	if plugins.Count() == 0 {
    36  		// nothing cached
    37  		return ""
    38  	}
    39  
    40  	// There should generally be only one plugin here; if there's more than
    41  	// one match for some reason then we'll just choose one arbitrarily.
    42  	plugin := plugins.Newest()
    43  	return plugin.Path
    44  }
    45  
    46  func (c *pluginCache) InstallDir() string {
    47  	return c.Dir
    48  }