Skip to content

Commit

Permalink
Load static plugins in init
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Łukasz Magiera <magik6k@gmail.com>
  • Loading branch information
magik6k committed Nov 4, 2018
1 parent e1be2f7 commit 47dbe90
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 23 deletions.
9 changes: 5 additions & 4 deletions cmd/ipfs/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,11 @@ func makeExecutor(req *cmds.Request, env interface{}) (cmds.Executor, error) {
if err != nil {
return nil, err
}
if ok {
if _, err := loader.LoadPlugins(pluginpath); err != nil {
log.Error("error loading plugins: ", err)
}
if !ok {
pluginpath = ""
}
if _, err := loader.LoadPlugins(pluginpath); err != nil {
log.Error("error loading plugins: ", err)
}

exctr = cmds.NewExecutor(req.Root)
Expand Down
28 changes: 15 additions & 13 deletions plugin/loader/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,30 @@ func LoadPlugins(pluginDir string) ([]plugin.Plugin, error) {
plMap[v.Name()] = v
}

newPls, err := loadDynamicPlugins(pluginDir)
if err != nil {
return nil, err
}
if pluginDir != "" {
newPls, err := loadDynamicPlugins(pluginDir)
if err != nil {
return nil, err
}

for _, pl := range newPls {
if ppl, ok := plMap[pl.Name()]; ok {
// plugin is already preloaded
return nil, fmt.Errorf(
"plugin: %s, is duplicated in version: %s, "+
"while trying to load dynamically: %s",
ppl.Name(), ppl.Version(), pl.Version())
for _, pl := range newPls {
if ppl, ok := plMap[pl.Name()]; ok {
// plugin is already preloaded
return nil, fmt.Errorf(
"plugin: %s, is duplicated in version: %s, "+
"while trying to load dynamically: %s",
ppl.Name(), ppl.Version(), pl.Version())
}
plMap[pl.Name()] = pl
}
plMap[pl.Name()] = pl
}

pls := make([]plugin.Plugin, 0, len(plMap))
for _, v := range plMap {
pls = append(pls, v)
}

err = initialize(pls)
err := initialize(pls)
if err != nil {
return nil, err
}
Expand Down
17 changes: 11 additions & 6 deletions repo/fsrepo/config_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package fsrepo
package fsrepo_test

import (
"encoding/json"
Expand All @@ -7,7 +7,10 @@ import (
"reflect"
"testing"

config "gx/ipfs/QmPEpj17FDRpc7K1aArKZp3RsHtzRMKykeK9GVgn4WQGPR/go-ipfs-config"
"github.com/ipfs/go-ipfs/plugin/loader"
"github.com/ipfs/go-ipfs/repo/fsrepo"

"gx/ipfs/QmPEpj17FDRpc7K1aArKZp3RsHtzRMKykeK9GVgn4WQGPR/go-ipfs-config"
)

// note: to test sorting of the mountpoints in the disk spec they are
Expand Down Expand Up @@ -72,6 +75,8 @@ var measureConfig = []byte(`{
}`)

func TestDefaultDatastoreConfig(t *testing.T) {
loader.LoadPlugins("")

dir, err := ioutil.TempDir("", "ipfs-datastore-config-test")
if err != nil {
t.Fatal(err)
Expand All @@ -84,7 +89,7 @@ func TestDefaultDatastoreConfig(t *testing.T) {
t.Fatal(err)
}

dsc, err := AnyDatastoreConfig(config.Spec)
dsc, err := fsrepo.AnyDatastoreConfig(config.Spec)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -122,7 +127,7 @@ func TestLevelDbConfig(t *testing.T) {
t.Fatal(err)
}

dsc, err := AnyDatastoreConfig(spec)
dsc, err := fsrepo.AnyDatastoreConfig(spec)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -160,7 +165,7 @@ func TestFlatfsConfig(t *testing.T) {
t.Fatal(err)
}

dsc, err := AnyDatastoreConfig(spec)
dsc, err := fsrepo.AnyDatastoreConfig(spec)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -198,7 +203,7 @@ func TestMeasureConfig(t *testing.T) {
t.Fatal(err)
}

dsc, err := AnyDatastoreConfig(spec)
dsc, err := fsrepo.AnyDatastoreConfig(spec)
if err != nil {
t.Fatal(err)
}
Expand Down

0 comments on commit 47dbe90

Please sign in to comment.