-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathbuilder.go
More file actions
118 lines (91 loc) · 2.98 KB
/
builder.go
File metadata and controls
118 lines (91 loc) · 2.98 KB
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
package slackscot
import (
"github.com/alexandre-normand/slackscot/config"
"github.com/spf13/viper"
"io"
)
// Builder holds a slackscot instance to build
type Builder struct {
bot *Slackscot
err error
}
// NewBot returns a new Builder used to set up a new slackscot
func NewBot(name string, v *viper.Viper, options ...Option) (sb *Builder) {
sb = new(Builder)
sb.bot, sb.err = New(name, v, options...)
return sb
}
// PluginInstantiator creates a new instance of a plugin given a PluginConfig
type PluginInstantiator func(c *config.PluginConfig) (p *Plugin, err error)
// CloserPluginInstantiator creates a new instance of a closer plugin given a PluginConfig
type CloserPluginInstantiator func(c *config.PluginConfig) (closer io.Closer, p *Plugin, err error)
// WithPlugin adds a plugin to the slackscot instance
func (sb *Builder) WithPlugin(p *Plugin) *Builder {
if sb.err != nil {
return sb
}
sb.bot.RegisterPlugin(p)
return sb
}
// WithPluginErr adds a plugin that has a creation function returning (Plugin, error) to the slackscot instance
func (sb *Builder) WithPluginErr(p *Plugin, err error) *Builder {
if sb.err == nil && err != nil {
sb.err = err
}
if sb.err != nil {
return sb
}
sb.bot.RegisterPlugin(p)
return sb
}
// WithPluginCloserErr adds a plugin that has a creation function returning (io.Closer, Plugin, error) to the slackscot instance
func (sb *Builder) WithPluginCloserErr(closer io.Closer, p *Plugin, err error) *Builder {
if sb.err == nil && err != nil {
sb.err = err
}
if sb.err != nil {
return sb
}
sb.bot.RegisterPlugin(p)
if closer != nil {
sb.bot.closers = append(sb.bot.closers, closer)
}
return sb
}
// WithConfigurablePluginErr adds a plugin to the slackscot instance by first checking and
// getting its configuration
func (sb *Builder) WithConfigurablePluginErr(name string, newInstance PluginInstantiator) *Builder {
if pc, exists := sb.loadPluginConfig(name); exists {
return sb.WithPluginErr(newInstance(pc))
}
return sb
}
// WithConfigurablePluginCloserErr adds a closer plugin to the slackscot instance by first checking and
// getting its configuration
func (sb *Builder) WithConfigurablePluginCloserErr(name string, newInstance CloserPluginInstantiator) *Builder {
if pc, exists := sb.loadPluginConfig(name); exists {
return sb.WithPluginCloserErr(newInstance(pc))
}
return sb
}
// loadPluginConfig gets the plugin configuration and returns it along with its validity. If false,
// there is no config for the plugin
func (sb *Builder) loadPluginConfig(name string) (pc *config.PluginConfig, valid bool) {
if sb.err != nil {
return nil, false
}
pc, err := config.GetPluginConfig(sb.bot.config, name)
if err != nil {
sb.err = err
return nil, false
}
return pc, true
}
// Build returns the built slackscot instance. If there was an error during
// setup, the error is returned along with a nil slackscot
func (sb *Builder) Build() (s *Slackscot, err error) {
if sb.err != nil {
return nil, sb.err
}
return sb.bot, sb.err
}