initializer.go 1.29 KB
Newer Older
1 2 3 4 5
package loader

import (
	"github.com/ipfs/go-ipfs/core/coredag"
	"github.com/ipfs/go-ipfs/plugin"
Jeromy's avatar
Jeromy committed
6
	"github.com/ipfs/go-ipfs/repo/fsrepo"
7

Jeromy's avatar
Jeromy committed
8
	"gx/ipfs/QmWLWmRVSiagqP15jczsGME1qpob6HDbtbHAY2he9W5iUo/opentracing-go"
9
	ipld "gx/ipfs/QmdDXJs4axxefSPgK6Y1QhpJWKuDPnGJiqgq4uncb4rFHL/go-ipld-format"
10 11
)

Jakub Sztandera's avatar
Jakub Sztandera committed
12
func initialize(plugins []plugin.Plugin) error {
13 14 15 16 17 18 19 20 21 22 23 24
	for _, p := range plugins {
		err := p.Init()
		if err != nil {
			return err
		}
	}

	return nil
}

func run(plugins []plugin.Plugin) error {
	for _, pl := range plugins {
ForrestWeston's avatar
ForrestWeston committed
25 26 27 28 29 30 31 32 33 34 35
		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
			}
Jeromy's avatar
Jeromy committed
36 37 38 39 40
		case plugin.PluginDatastore:
			err := fsrepo.AddDatastoreConfigHandler(pl.DatastoreTypeName(), pl.DatastoreConfigParser())
			if err != nil {
				return err
			}
ForrestWeston's avatar
ForrestWeston committed
41 42
		default:
			panic(pl)
43 44 45 46 47
		}
	}
	return nil
}

ForrestWeston's avatar
ForrestWeston committed
48 49 50 51
func runIPLDPlugin(pl plugin.PluginIPLD) error {
	err := pl.RegisterBlockDecoders(ipld.DefaultBlockDecoder)
	if err != nil {
		return err
52
	}
ForrestWeston's avatar
ForrestWeston committed
53 54
	return pl.RegisterInputEncParsers(coredag.DefaultInputEncParsers)
}
55

ForrestWeston's avatar
ForrestWeston committed
56 57
func runTracerPlugin(pl plugin.PluginTracer) error {
	tracer, err := pl.InitTracer()
58 59 60
	if err != nil {
		return err
	}
ForrestWeston's avatar
ForrestWeston committed
61 62
	opentracing.SetGlobalTracer(tracer)
	return nil
63
}