diff --git a/commands/request.go b/commands/request.go index ed2b060a0ffb..346c74116c53 100644 --- a/commands/request.go +++ b/commands/request.go @@ -19,7 +19,8 @@ type Context struct { Online bool ConfigRoot string ReqLog *ReqLog - Plugins *loader.PluginLoader + + Plugins *loader.PluginLoader config *config.Config LoadConfig func(path string) (*config.Config, error) diff --git a/plugin/loader/initializer.go b/plugin/loader/initializer.go deleted file mode 100644 index 8c87453a887a..000000000000 --- a/plugin/loader/initializer.go +++ /dev/null @@ -1,57 +0,0 @@ -package loader - -import ( - "github.com/ipfs/go-ipfs/core/coredag" - "github.com/ipfs/go-ipfs/plugin" - "gx/ipfs/QmWLWmRVSiagqP15jczsGME1qpob6HDbtbHAY2he9W5iUo/opentracing-go" - - ipld "gx/ipfs/Qme5bWv7wtjUNGsK2BNGVUFPKiuxWrsqrtvYwCLRw8YFES/go-ipld-format" -) - -func initialize(plugins []plugin.Plugin) error { - for _, p := range plugins { - err := p.Init() - if err != nil { - return err - } - } - - return nil -} - -func run(plugins []plugin.Plugin) error { - for _, pl := range plugins { - switch pl := pl.(type) { - case plugin.PluginIPLD: - err := runIPLDPlugin(pl) - if err != nil { - return err - } - case plugin.PluginTracer: - err := runTracerPlugin(pl) - if err != nil { - return err - } - default: - panic(pl) - } - } - return nil -} - -func runIPLDPlugin(pl plugin.PluginIPLD) error { - err := pl.RegisterBlockDecoders(ipld.DefaultBlockDecoder) - if err != nil { - return err - } - return pl.RegisterInputEncParsers(coredag.DefaultInputEncParsers) -} - -func runTracerPlugin(pl plugin.PluginTracer) error { - tracer, err := pl.InitTracer() - if err != nil { - return err - } - opentracing.SetGlobalTracer(tracer) - return nil -} diff --git a/plugin/loader/loader.go b/plugin/loader/loader.go index 6ce5737a8865..6aa185545843 100644 --- a/plugin/loader/loader.go +++ b/plugin/loader/loader.go @@ -2,11 +2,13 @@ package loader import ( "fmt" - "os" - + "github.com/ipfs/go-ipfs/core/coredag" "github.com/ipfs/go-ipfs/plugin" + "os" logging "gx/ipfs/QmRb5jh8z2E8hMGN2tkvs1yHynUanqnZ3UeKwgN1i9P1F8/go-log" + opentracing "gx/ipfs/QmWLWmRVSiagqP15jczsGME1qpob6HDbtbHAY2he9W5iUo/opentracing-go" + ipld "gx/ipfs/Qme5bWv7wtjUNGsK2BNGVUFPKiuxWrsqrtvYwCLRw8YFES/go-ipld-format" ) var log = logging.Logger("plugin/loader") @@ -20,18 +22,6 @@ type PluginLoader struct { plugins []plugin.Plugin } -func loadDynamicPlugins(pluginDir string) ([]plugin.Plugin, error) { - _, err := os.Stat(pluginDir) - if os.IsNotExist(err) { - return nil, nil - } - if err != nil { - return nil, err - } - - return loadPluginsFunc(pluginDir) -} - // NewPluginLoader creates new plugin loader func NewPluginLoader(pluginDir string) (*PluginLoader, error) { plMap := make(map[string]plugin.Plugin) @@ -64,12 +54,64 @@ func NewPluginLoader(pluginDir string) (*PluginLoader, error) { return loader, nil } -// Initialize all the loaded plugins +func loadDynamicPlugins(pluginDir string) ([]plugin.Plugin, error) { + _, err := os.Stat(pluginDir) + if os.IsNotExist(err) { + return nil, nil + } + if err != nil { + return nil, err + } + + return loadPluginsFunc(pluginDir) +} + +//Initialize all loaded plugins func (loader *PluginLoader) Initialize() error { - return initialize(loader.plugins) + for _, p := range loader.plugins { + err := p.Init() + if err != nil { + return err + } + } + + return nil } -// Run all the loaded plugins +//Run the plugins func (loader *PluginLoader) Run() error { - return run(loader.plugins) + for _, pl := range loader.plugins { + switch pl := pl.(type) { + case plugin.PluginIPLD: + err := runIPLDPlugin(pl) + if err != nil { + return err + } + case plugin.PluginTracer: + err := runTracerPlugin(pl) + if err != nil { + return err + } + default: + panic(pl) + } + } + return nil +} + +func runIPLDPlugin(pl plugin.PluginIPLD) error { + err := pl.RegisterBlockDecoders(ipld.DefaultBlockDecoder) + if err != nil { + return err + } + return pl.RegisterInputEncParsers(coredag.DefaultInputEncParsers) +} + +func runTracerPlugin(pl plugin.PluginTracer) error { + tracer, err := pl.InitTracer() + if err != nil { + return err + } + opentracing.SetGlobalTracer(tracer) + return nil }