Skip to content

Commit 75aa2f6

Browse files
committed
Extract the api so that plugins and app can have independent lifecycles.
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.
1 parent eaca9c2 commit 75aa2f6

File tree

6 files changed

+25
-94
lines changed

6 files changed

+25
-94
lines changed

cmd/odo/odo.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"flag"
55
"fmt"
66
"github.com/golang/glog"
7+
api "github.com/metacosm/odo-event-api/odo/api/events"
78
"github.com/posener/complete"
89
"github.com/redhat-developer/odo/pkg/config"
910
"github.com/redhat-developer/odo/pkg/log"
@@ -145,7 +146,7 @@ func loadPlugins() {
145146
}
146147

147148
// Assert that Listener is indeed a Listener :)
148-
listener, ok := candidate.(events.Listener)
149+
listener, ok := candidate.(api.Listener)
149150
if !ok {
150151
log.Error("exported Listener variable is not implementing the Listener interface")
151152
os.Exit(1)

pkg/odo/events/abort.go

Lines changed: 0 additions & 32 deletions
This file was deleted.

pkg/odo/events/bus.go

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,19 @@ package events
22

33
import (
44
"fmt"
5+
api "github.com/metacosm/odo-event-api/odo/api/events"
56
"github.com/spf13/cobra"
67
)
78

8-
type typesToListeners map[EventType][]Listener
9+
type typesToListeners map[api.EventType][]api.Listener
910

1011
type EventBus struct {
1112
listeners map[string]typesToListeners
12-
allListeners []Listener
13+
allListeners []api.Listener
1314
}
1415

1516
var bus = &EventBus{
16-
allListeners: make([]Listener, 0, 5),
17+
allListeners: make([]api.Listener, 0, 5),
1718
}
1819

1920
func GetEventBus() *EventBus {
@@ -27,9 +28,9 @@ func EventNameFrom(cmd *cobra.Command) string {
2728
return cmd.Name()
2829
}
2930

30-
func DispatchEvent(cmd *cobra.Command, eventType EventType, payload interface{}) error {
31+
func DispatchEvent(cmd *cobra.Command, eventType api.EventType, payload interface{}) error {
3132
eventBus := GetEventBus()
32-
err := eventBus.DispatchEvent(Event{
33+
err := eventBus.DispatchEvent(api.Event{
3334
Name: EventNameFrom(cmd),
3435
Type: eventType,
3536
Payload: payload,
@@ -38,13 +39,13 @@ func DispatchEvent(cmd *cobra.Command, eventType EventType, payload interface{})
3839
return err
3940
}
4041

41-
func (bus *EventBus) RegisterToAll(listener Listener) {
42+
func (bus *EventBus) RegisterToAll(listener api.Listener) {
4243
bus.allListeners = append(bus.allListeners, listener)
4344
}
4445

4546
type Subscription struct {
46-
Listener Listener
47-
SupportedEvents map[string]EventType
47+
Listener api.Listener
48+
SupportedEvents map[string]api.EventType
4849
}
4950

5051
func (bus *EventBus) Register(subscription Subscription) {
@@ -53,7 +54,7 @@ func (bus *EventBus) Register(subscription Subscription) {
5354
}
5455
}
5556

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

6364
listenersForType, ok := listenersForEvent[eventType]
6465
if !ok {
65-
listenersForType = make([]Listener, 0, 10)
66+
listenersForType = make([]api.Listener, 0, 10)
6667
}
6768

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

71-
func (bus *EventBus) DispatchEvent(event Event) (err error) {
72+
func (bus *EventBus) DispatchEvent(event api.Event) (err error) {
7273
errors := make([]error, 0, 10)
7374
listenersForEvent, ok := bus.listeners[event.Name]
74-
processedListeners := make([]Listener, 0, 10)
75+
processedListeners := make([]api.Listener, 0, 10)
7576
var abort bool
7677
if ok {
7778
listenersForType, ok := listenersForEvent[event.Type]
@@ -80,7 +81,7 @@ func (bus *EventBus) DispatchEvent(event Event) (err error) {
8081
listener := listenersForType[i]
8182
err := listener.OnEvent(event)
8283
if err != nil {
83-
if IsEventCausedAbort(err) {
84+
if api.IsEventCausedAbort(err) {
8485
abort = true
8586
return err
8687
}
@@ -96,7 +97,7 @@ func (bus *EventBus) DispatchEvent(event Event) (err error) {
9697
listener := bus.allListeners[i]
9798
err := listener.OnEvent(event)
9899
if err != nil {
99-
if IsEventCausedAbort(err) {
100+
if api.IsEventCausedAbort(err) {
100101
abort = true
101102
return err
102103
}
@@ -118,11 +119,11 @@ func (bus *EventBus) DispatchEvent(event Event) (err error) {
118119
return
119120
}
120121

121-
func revertProcessedListenersOnAbort(abort bool, err error, listeners []Listener) {
122+
func revertProcessedListenersOnAbort(abort bool, err error, listeners []api.Listener) {
122123
if abort {
123124

124125
for i := range listeners {
125-
listeners[i].OnAbort(err.(*EventCausedAbortError))
126+
listeners[i].OnAbort(err.(*api.EventCausedAbortError))
126127
}
127128
}
128129
}

pkg/odo/events/event.go

Lines changed: 0 additions & 33 deletions
This file was deleted.

pkg/odo/events/listener.go

Lines changed: 0 additions & 7 deletions
This file was deleted.

pkg/odo/genericclioptions/runnable.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package genericclioptions
22

33
import (
4+
api "github.com/metacosm/odo-event-api/odo/api/events"
45
"github.com/redhat-developer/odo/pkg/log"
56
"github.com/redhat-developer/odo/pkg/odo/events"
67
"github.com/redhat-developer/odo/pkg/odo/util"
@@ -15,17 +16,17 @@ type Runnable interface {
1516
}
1617

1718
func GenericRun(o Runnable, cmd *cobra.Command, args []string) {
18-
exitIfAbort(events.DispatchEvent(cmd, events.PreRun, args), cmd)
19+
exitIfAbort(events.DispatchEvent(cmd, api.PreRun, args), cmd)
1920
util.CheckError(o.Complete(cmd.Name(), cmd, args), "")
20-
exitIfAbort(events.DispatchEvent(cmd, events.PostComplete, o), cmd)
21+
exitIfAbort(events.DispatchEvent(cmd, api.PostComplete, o), cmd)
2122
util.CheckError(o.Validate(), "")
22-
exitIfAbort(events.DispatchEvent(cmd, events.PostValidate, o), cmd)
23+
exitIfAbort(events.DispatchEvent(cmd, api.PostValidate, o), cmd)
2324
util.CheckError(o.Run(), "")
24-
exitIfAbort(events.DispatchEvent(cmd, events.PostRun, o), cmd)
25+
exitIfAbort(events.DispatchEvent(cmd, api.PostRun, o), cmd)
2526
}
2627

2728
func exitIfAbort(err error, cmd *cobra.Command) {
28-
if events.IsEventCausedAbort(err) {
29+
if api.IsEventCausedAbort(err) {
2930
log.Errorf("Processing of %s command was aborted: %v", cmd.Name(), err)
3031
os.Exit(1)
3132
}

0 commit comments

Comments
 (0)