Skip to content

Commit

Permalink
plugins: Make execution fail if loaded plugins are erroring, print de…
Browse files Browse the repository at this point in the history
…bug output on emitted responses

Now plugins failing to answer make execution fail, adapt tests
  • Loading branch information
mudler committed May 17, 2021
1 parent 1e617b0 commit fd12227
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 20 deletions.
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ To build a package, from a tree definition:

plugin := viper.GetStringSlice("plugin")

bus.Manager.Load(plugin...).Register()
bus.Manager.Initialize(plugin...)
if len(bus.Manager.Plugins) != 0 {
Info(":lollipop:Enabled plugins:")
for _, p := range bus.Manager.Plugins {
Expand Down
64 changes: 46 additions & 18 deletions pkg/bus/events.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package bus

import (
. "github.com/mudler/luet/pkg/logger"

"github.com/mudler/go-pluggable"
)

Expand Down Expand Up @@ -47,21 +49,47 @@ var (
)

// Manager is the bus instance manager, which subscribes plugins to events emitted by Luet
var Manager *pluggable.Manager = pluggable.NewManager(
[]pluggable.EventType{
EventPackageInstall,
EventPackageUnInstall,
EventPackagePreBuild,
EventPackagePreBuildArtifact,
EventPackagePostBuildArtifact,
EventPackagePostBuild,
EventRepositoryPreBuild,
EventRepositoryPostBuild,
EventImagePreBuild,
EventImagePrePull,
EventImagePrePush,
EventImagePostBuild,
EventImagePostPull,
EventImagePostPush,
},
)
var Manager *Bus = &Bus{
Manager: pluggable.NewManager(
[]pluggable.EventType{
EventPackageInstall,
EventPackageUnInstall,
EventPackagePreBuild,
EventPackagePreBuildArtifact,
EventPackagePostBuildArtifact,
EventPackagePostBuild,
EventRepositoryPreBuild,
EventRepositoryPostBuild,
EventImagePreBuild,
EventImagePrePull,
EventImagePrePush,
EventImagePostBuild,
EventImagePostPull,
EventImagePostPush,
},
),
}

type Bus struct {
*pluggable.Manager
}

func (b *Bus) Initialize(plugin ...string) {
b.Manager.Load(plugin...).Register()

for _, e := range b.Manager.Events {
b.Manager.Response(e, func(p *pluggable.Plugin, r *pluggable.EventResponse) {
if r.Errored() {
Fatal("Plugin", p.Name, "at", p.Executable, "Error", r.Error)
}
Debug(
"plugin_event",
"received from",
p.Name,
"at",
p.Executable,
r,
)
})
}
}
4 changes: 3 additions & 1 deletion tests/fixtures/plugin/test-foo
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#!/bin/bash
echo "$1" >> $EVENT_FILE
echo "$2" >> $PAYLOAD_FILE
echo "$2" >> $PAYLOAD_FILE

echo "{}"

0 comments on commit fd12227

Please sign in to comment.