forked from muesli/beehive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfactories.go
135 lines (114 loc) · 3.81 KB
/
factories.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
* Copyright (C) 2014-2017 Christian Muehlhaeuser
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Authors:
* Christian Muehlhaeuser <[email protected]>
*/
// Package bees is Beehive's central module system.
package bees
import (
"errors"
"golang.org/x/oauth2"
)
// A BeeFactory is the base struct to be embedded by other BeeFactories.
type BeeFactory struct {
}
// Image returns an empty image filename per default.
func (factory *BeeFactory) Image() string {
return ""
}
// LogoColor returns the default logo color.
func (factory *BeeFactory) LogoColor() string {
return "#010000"
}
// OAuth2AccessToken returns the oauth2 access token.
func (factory *BeeFactory) OAuth2AccessToken(id, secret, code string) (*oauth2.Token, error) {
return nil, errors.New("This Hive does not implement OAuth2")
}
// Options returns the default empty options set.
func (factory *BeeFactory) Options() []BeeOptionDescriptor {
return []BeeOptionDescriptor{}
}
// States returns the default empty states set.
func (factory *BeeFactory) States() []StateDescriptor {
return []StateDescriptor{}
}
// Events returns the default empty events set.
func (factory *BeeFactory) Events() []EventDescriptor {
return []EventDescriptor{}
}
// Actions returns the default empty actions set.
func (factory *BeeFactory) Actions() []ActionDescriptor {
return []ActionDescriptor{}
}
// A BeeFactoryInterface is the interface that gets implemented by a BeeFactory.
type BeeFactoryInterface interface {
// ID of the module
ID() string
// Name of the module
Name() string
// Description of the module
Description() string
// An image url for the module
Image() string
// A logo color for the module
LogoColor() string
// OAuth2AccessToken returns the oauth2 access token.
OAuth2AccessToken(id, secret, code string) (*oauth2.Token, error)
// Options supported by module
Options() []BeeOptionDescriptor
// States provided by module
States() []StateDescriptor
// Events defined by module
Events() []EventDescriptor
// Actions supported by module
Actions() []ActionDescriptor
New(name, description string, options BeeOptions) BeeInterface
}
// RegisterFactory gets called by BeeFactories to register themselves.
func RegisterFactory(factory BeeFactoryInterface) {
// log.Println("Bee Factory ready:", factory.ID(), "-", factory.Description())
/* for _, ev := range factory.Events() {
log.Println("\tProvides event:", ev.Name, "-", ev.Description)
for _, opt := range ev.Options {
log.Println("\t\tPlaceholder:", opt.Name, "-", opt.Description)
}
}
for _, ac := range factory.Actions() {
log.Println("\tOffers action:", ac.Name, "-", ac.Description)
for _, opt := range ac.Options {
log.Println("\t\tPlaceholder:", opt.Name, "-", opt.Description)
}
}
log.Println() */
factories[factory.ID()] = &factory
}
// GetFactory returns the factory with a specific name.
func GetFactory(identifier string) *BeeFactoryInterface {
factory, ok := factories[identifier]
if ok {
return factory
}
return nil
}
// GetFactories returns all known bee factories.
func GetFactories() []*BeeFactoryInterface {
r := []*BeeFactoryInterface{}
for _, factory := range factories {
r = append(r, factory)
}
return r
}