Skip to content

Commit

Permalink
plugin: Address last review
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Kacper Łukawski <[email protected]>
  • Loading branch information
PlayerWithoutName committed Apr 21, 2018
1 parent 238ea73 commit abdfeca
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 76 deletions.
3 changes: 2 additions & 1 deletion commands/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
57 changes: 0 additions & 57 deletions plugin/loader/initializer.go

This file was deleted.

78 changes: 60 additions & 18 deletions plugin/loader/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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)
Expand Down Expand Up @@ -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
}

0 comments on commit abdfeca

Please sign in to comment.