2018-09-18 17:08:46 +00:00
|
|
|
package loader
|
|
|
|
|
|
|
|
import plugin "github.com/hashicorp/go-plugin"
|
|
|
|
|
|
|
|
// PluginInstance wraps an instance of a plugin. If the plugin is external, it
|
|
|
|
// provides methods to retrieve the ReattachConfig and to kill the plugin.
|
|
|
|
type PluginInstance interface {
|
|
|
|
// Internal returns if the plugin is internal
|
|
|
|
Internal() bool
|
|
|
|
|
|
|
|
// Kill kills the plugin if it is external. It is safe to call on internal
|
|
|
|
// plugins.
|
|
|
|
Kill()
|
|
|
|
|
|
|
|
// ReattachConfig returns the ReattachConfig and whether the plugin is internal
|
2018-09-18 17:48:37 +00:00
|
|
|
// or not. If the second return value is false, no ReattachConfig is
|
|
|
|
// possible to return.
|
|
|
|
ReattachConfig() (config *plugin.ReattachConfig, canReattach bool)
|
2018-09-18 17:08:46 +00:00
|
|
|
|
|
|
|
// Plugin returns the wrapped plugin instance.
|
|
|
|
Plugin() interface{}
|
|
|
|
|
|
|
|
// Exited returns whether the plugin has exited
|
|
|
|
Exited() bool
|
2018-12-18 00:40:58 +00:00
|
|
|
|
|
|
|
// ApiVersion returns the API version to be used with the plugin
|
|
|
|
ApiVersion() string
|
2018-09-18 17:08:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// internalPluginInstance wraps an internal plugin
|
|
|
|
type internalPluginInstance struct {
|
2018-12-18 00:40:58 +00:00
|
|
|
instance interface{}
|
|
|
|
apiVersion string
|
2020-05-26 13:44:26 +00:00
|
|
|
killFn func()
|
2018-09-18 17:08:46 +00:00
|
|
|
}
|
|
|
|
|
2020-05-26 13:44:26 +00:00
|
|
|
func (p *internalPluginInstance) Internal() bool { return true }
|
|
|
|
func (p *internalPluginInstance) Kill() { p.killFn() }
|
|
|
|
|
2018-09-18 17:08:46 +00:00
|
|
|
func (p *internalPluginInstance) ReattachConfig() (*plugin.ReattachConfig, bool) { return nil, false }
|
|
|
|
func (p *internalPluginInstance) Plugin() interface{} { return p.instance }
|
|
|
|
func (p *internalPluginInstance) Exited() bool { return false }
|
2018-12-18 00:40:58 +00:00
|
|
|
func (p *internalPluginInstance) ApiVersion() string { return p.apiVersion }
|
2018-09-18 17:08:46 +00:00
|
|
|
|
|
|
|
// externalPluginInstance wraps an external plugin
|
|
|
|
type externalPluginInstance struct {
|
2018-12-18 00:40:58 +00:00
|
|
|
client *plugin.Client
|
|
|
|
instance interface{}
|
|
|
|
apiVersion string
|
2018-09-18 17:08:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *externalPluginInstance) Internal() bool { return false }
|
|
|
|
func (p *externalPluginInstance) Plugin() interface{} { return p.instance }
|
|
|
|
func (p *externalPluginInstance) Exited() bool { return p.client.Exited() }
|
2018-12-18 00:40:58 +00:00
|
|
|
func (p *externalPluginInstance) ApiVersion() string { return p.apiVersion }
|
2018-09-18 17:08:46 +00:00
|
|
|
|
|
|
|
func (p *externalPluginInstance) ReattachConfig() (*plugin.ReattachConfig, bool) {
|
|
|
|
return p.client.ReattachConfig(), true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *externalPluginInstance) Kill() {
|
|
|
|
p.client.Kill()
|
|
|
|
}
|