Skip to content

Commit

Permalink
Extract the api so that plugins and app can have independent lifecycles.
Browse files Browse the repository at this point in the history
API is found at https://github.com/metacosm/odo-event-api.
This, however, doesn't work well with vendoring so right now, both the
app and plugin can only be built against an api that's in the same spot
in $GOPATH. See golang/go#20481 for more
details.
  • Loading branch information
metacosm committed Feb 4, 2019
1 parent 98ebd3b commit f730ddf
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 94 deletions.
3 changes: 2 additions & 1 deletion cmd/odo/odo.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"flag"
"fmt"
"github.com/golang/glog"
api "github.com/metacosm/odo-event-api/odo/api/events"
"github.com/posener/complete"
"github.com/redhat-developer/odo/pkg/config"
"github.com/redhat-developer/odo/pkg/log"
Expand Down Expand Up @@ -145,7 +146,7 @@ func loadPlugins() {
}

// Assert that Listener is indeed a Listener :)
listener, ok := candidate.(events.Listener)
listener, ok := candidate.(api.Listener)
if !ok {
log.Error("exported Listener variable is not implementing the Listener interface")
os.Exit(1)
Expand Down
32 changes: 0 additions & 32 deletions pkg/odo/events/abort.go

This file was deleted.

33 changes: 17 additions & 16 deletions pkg/odo/events/bus.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@ package events

import (
"fmt"
api "github.com/metacosm/odo-event-api/odo/api/events"
"github.com/spf13/cobra"
)

type typesToListeners map[EventType][]Listener
type typesToListeners map[api.EventType][]api.Listener

type EventBus struct {
listeners map[string]typesToListeners
allListeners []Listener
allListeners []api.Listener
}

var bus = &EventBus{
allListeners: make([]Listener, 0, 5),
allListeners: make([]api.Listener, 0, 5),
}

func GetEventBus() *EventBus {
Expand All @@ -27,9 +28,9 @@ func EventNameFrom(cmd *cobra.Command) string {
return cmd.Name()
}

func DispatchEvent(cmd *cobra.Command, eventType EventType, payload interface{}) error {
func DispatchEvent(cmd *cobra.Command, eventType api.EventType, payload interface{}) error {
eventBus := GetEventBus()
err := eventBus.DispatchEvent(Event{
err := eventBus.DispatchEvent(api.Event{
Name: EventNameFrom(cmd),
Type: eventType,
Payload: payload,
Expand All @@ -38,13 +39,13 @@ func DispatchEvent(cmd *cobra.Command, eventType EventType, payload interface{})
return err
}

func (bus *EventBus) RegisterToAll(listener Listener) {
func (bus *EventBus) RegisterToAll(listener api.Listener) {
bus.allListeners = append(bus.allListeners, listener)
}

type Subscription struct {
Listener Listener
SupportedEvents map[string]EventType
Listener api.Listener
SupportedEvents map[string]api.EventType
}

func (bus *EventBus) Register(subscription Subscription) {
Expand All @@ -53,7 +54,7 @@ func (bus *EventBus) Register(subscription Subscription) {
}
}

func (bus *EventBus) RegisterSingle(event string, eventType EventType, listener Listener) {
func (bus *EventBus) RegisterSingle(event string, eventType api.EventType, listener api.Listener) {
listenersForEvent, ok := bus.listeners[event]
if !ok {
listenersForEvent = make(typesToListeners, 10)
Expand All @@ -62,16 +63,16 @@ func (bus *EventBus) RegisterSingle(event string, eventType EventType, listener

listenersForType, ok := listenersForEvent[eventType]
if !ok {
listenersForType = make([]Listener, 0, 10)
listenersForType = make([]api.Listener, 0, 10)
}

listenersForEvent[eventType] = append(listenersForType, listener)
}

func (bus *EventBus) DispatchEvent(event Event) (err error) {
func (bus *EventBus) DispatchEvent(event api.Event) (err error) {
errors := make([]error, 0, 10)
listenersForEvent, ok := bus.listeners[event.Name]
processedListeners := make([]Listener, 0, 10)
processedListeners := make([]api.Listener, 0, 10)
var abort bool
if ok {
listenersForType, ok := listenersForEvent[event.Type]
Expand All @@ -80,7 +81,7 @@ func (bus *EventBus) DispatchEvent(event Event) (err error) {
listener := listenersForType[i]
err := listener.OnEvent(event)
if err != nil {
if IsEventCausedAbort(err) {
if api.IsEventCausedAbort(err) {
abort = true
return err
}
Expand All @@ -96,7 +97,7 @@ func (bus *EventBus) DispatchEvent(event Event) (err error) {
listener := bus.allListeners[i]
err := listener.OnEvent(event)
if err != nil {
if IsEventCausedAbort(err) {
if api.IsEventCausedAbort(err) {
abort = true
return err
}
Expand All @@ -118,11 +119,11 @@ func (bus *EventBus) DispatchEvent(event Event) (err error) {
return
}

func revertProcessedListenersOnAbort(abort bool, err error, listeners []Listener) {
func revertProcessedListenersOnAbort(abort bool, err error, listeners []api.Listener) {
if abort {

for i := range listeners {
listeners[i].OnAbort(err.(*EventCausedAbortError))
listeners[i].OnAbort(err.(*api.EventCausedAbortError))
}
}
}
33 changes: 0 additions & 33 deletions pkg/odo/events/event.go

This file was deleted.

7 changes: 0 additions & 7 deletions pkg/odo/events/listener.go

This file was deleted.

11 changes: 6 additions & 5 deletions pkg/odo/genericclioptions/runnable.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package genericclioptions

import (
api "github.com/metacosm/odo-event-api/odo/api/events"
"github.com/redhat-developer/odo/pkg/log"
"github.com/redhat-developer/odo/pkg/odo/events"
"github.com/redhat-developer/odo/pkg/odo/util"
Expand All @@ -15,17 +16,17 @@ type Runnable interface {
}

func GenericRun(o Runnable, cmd *cobra.Command, args []string) {
exitIfAbort(events.DispatchEvent(cmd, events.PreRun, args), cmd)
exitIfAbort(events.DispatchEvent(cmd, api.PreRun, args), cmd)
util.CheckError(o.Complete(cmd.Name(), cmd, args), "")
exitIfAbort(events.DispatchEvent(cmd, events.PostComplete, o), cmd)
exitIfAbort(events.DispatchEvent(cmd, api.PostComplete, o), cmd)
util.CheckError(o.Validate(), "")
exitIfAbort(events.DispatchEvent(cmd, events.PostValidate, o), cmd)
exitIfAbort(events.DispatchEvent(cmd, api.PostValidate, o), cmd)
util.CheckError(o.Run(), "")
exitIfAbort(events.DispatchEvent(cmd, events.PostRun, o), cmd)
exitIfAbort(events.DispatchEvent(cmd, api.PostRun, o), cmd)
}

func exitIfAbort(err error, cmd *cobra.Command) {
if events.IsEventCausedAbort(err) {
if api.IsEventCausedAbort(err) {
log.Errorf("Processing of %s command was aborted: %v", cmd.Name(), err)
os.Exit(1)
}
Expand Down

0 comments on commit f730ddf

Please sign in to comment.