@cryptotaxi247 / kubo / commits / 01514d517

plugin: allow plugins to implement multiple (or no) plugin interfaces

There's no reason for these to be exclusive. License: MIT Signed-off-by: Steven Allen <steven@stebalien.com>

Steven Allen committed Jan 29, 2019 at 12:41 UTC 01514d5179c31909f2bd0d315d3d088b832cc318
3 files changed +19 -16
cmd/ipfs/main.go
+1 -1
@@ -69,7 +69,7 @@ func loadPlugins(repoPath string) (*loader.PluginLoader, error) {
69 log.Error("error initializing plugins: ", err)
70 }
71
72 - if err := plugins.Run(); err != nil {
72 + if err := plugins.Inject(); err != nil {
73 log.Error("error running plugins: ", err)
74 }
75 return plugins, nil
plugin/loader/loader.go
+17 -14
@@ -69,7 +69,7 @@ func loadDynamicPlugins(pluginDir string) ([]plugin.Plugin, error) {
69 return loadPluginsFunc(pluginDir)
70 }
71
72 -//Initialize all loaded plugins
72 +// Initialize initializes all loaded plugins
73 func (loader *PluginLoader) Initialize() error {
74 for _, p := range loader.plugins {
75 err := p.Init()
@@ -81,33 +81,36 @@ func (loader *PluginLoader) Initialize() error {
81 return nil
82 }
83
84 -//Run the plugins
85 -func (loader *PluginLoader) Run() error {
84 +// Inject hooks all the plugins into the appropriate subsystems.
85 +func (loader *PluginLoader) Inject() error {
86 for _, pl := range loader.plugins {
87 - switch pl := pl.(type) {
88 - case plugin.PluginIPLD:
89 - err := runIPLDPlugin(pl)
87 + if pl, ok := pl.(plugin.PluginIPLD); ok {
88 + err := injectIPLDPlugin(pl)
89 if err != nil {
90 return err
91 }
93 - case plugin.PluginTracer:
94 - err := runTracerPlugin(pl)
92 + }
93 + if pl, ok := pl.(plugin.PluginTracer); ok {
94 + err := injectTracerPlugin(pl)
95 if err != nil {
96 return err
97 }
98 - case plugin.PluginDatastore:
99 - err := fsrepo.AddDatastoreConfigHandler(pl.DatastoreTypeName(), pl.DatastoreConfigParser())
98 + }
99 + if pl, ok := pl.(plugin.PluginDatastore); ok {
100 + err := injectDatastorePlugin(pl)
101 if err != nil {
102 return err
103 }
103 - default:
104 - panic(pl)
104 }
105 }
106 return nil
107 }
108
110 -func runIPLDPlugin(pl plugin.PluginIPLD) error {
109 +func injectDatastorePlugin(pl plugin.PluginDatastore) error {
110 + return fsrepo.AddDatastoreConfigHandler(pl.DatastoreTypeName(), pl.DatastoreConfigParser())
111 +}
112 +
113 +func injectIPLDPlugin(pl plugin.PluginIPLD) error {
114 err := pl.RegisterBlockDecoders(ipld.DefaultBlockDecoder)
115 if err != nil {
116 return err
@@ -115,7 +118,7 @@ func runIPLDPlugin(pl plugin.PluginIPLD) error {
118 return pl.RegisterInputEncParsers(coredag.DefaultInputEncParsers)
119 }
120
118 -func runTracerPlugin(pl plugin.PluginTracer) error {
121 +func injectTracerPlugin(pl plugin.PluginTracer) error {
122 tracer, err := pl.InitTracer()
123 if err != nil {
124 return err
repo/fsrepo/config_test.go
+1 -1
@@ -84,7 +84,7 @@ func TestDefaultDatastoreConfig(t *testing.T) {
84 t.Fatal(err)
85 }
86
87 - err = loader.Run()
87 + err = loader.Inject()
88 if err != nil {
89 t.Fatal(err)
90 }