-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.go
172 lines (147 loc) · 4.24 KB
/
main.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package main
import (
"flag"
"fmt"
"os"
"os/signal"
"strings"
"sync"
log "github.com/Sirupsen/logrus"
"github.com/agonzalezro/botella/adapter"
"github.com/agonzalezro/botella/config"
"github.com/agonzalezro/botella/plugin"
)
func init() {
if os.Getenv("DEBUG") != "" {
log.SetLevel(log.DebugLevel)
}
}
func inferConfigPath() (string, error) {
paths := []string{"botella.yml", "botella.yaml"}
for _, path := range paths {
if _, err := os.Stat(path); err == nil {
return path, nil
}
}
return "", fmt.Errorf("No %s file found!\n", strings.Join(paths, " or "))
}
// ensureVolumeHasMountPoint will return a list of volumes in the form `hostPath:containerPath` adding the `containerPath` in case it's missing.
func ensureVolumeHasMountPoint(vs []string) []string {
volumes := []string{}
for _, v := range vs {
fragments := strings.Split(v, ":")
if len(fragments) < 2 {
v = fmt.Sprintf("%s:%s", fragments[0], fragments[0])
}
volumes = append(volumes, v)
}
return volumes
}
func loadPlugins(config *config.Config) ([]*plugin.Plugin, error) {
var plugins []*plugin.Plugin
for _, pluginConfig := range config.Plugins {
plugin, err := plugin.New(
pluginConfig.Image,
pluginConfig.Environment,
ensureVolumeHasMountPoint(pluginConfig.Volumes),
)
if err != nil {
return nil, fmt.Errorf("Error loading plugin (image: %s): %v", pluginConfig.Image, err)
}
// TODO: this is a little bit ugly
plugin.RunOnlyOnChannels = pluginConfig.OnlyChannels
plugin.RunOnlyOnDirectMessages = pluginConfig.OnlyDirectMessages
plugin.RunOnlyOnMentions = pluginConfig.OnlyMentions
log.Infof("Plugin (%s) loaded.", pluginConfig.Image)
log.Debugf("Plugin (%s) config: %+v", pluginConfig.Image, pluginConfig)
plugins = append(plugins, plugin)
}
return plugins, nil
}
func loadAdapters(config *config.Config) ([]adapter.Adapter, error) {
var adapters []adapter.Adapter
for _, adapterConfig := range config.Adapters {
adapter, err := adapter.New(adapterConfig.Name, adapterConfig.Environment)
if err != nil {
return nil, fmt.Errorf("Error loading adapter (%s): %v", adapterConfig.Name, err)
}
log.Infof("Adapter (%s) loaded.", adapterConfig.Name)
log.Debugf("Adapter (%s) config: %+v", adapterConfig.Name, adapterConfig)
adapters = append(adapters, adapter)
}
return adapters, nil
}
func listenAndReply(adapters []adapter.Adapter, plugins []*plugin.Plugin) {
var wg sync.WaitGroup
signalsCh := make(chan os.Signal, 1)
signal.Notify(signalsCh, os.Interrupt)
for _, a := range adapters {
wg.Add(1)
stdinCh, stdoutCh, stderrCh := a.RunAndAttach()
go func(a adapter.Adapter, stdinCh, stdoutCh chan adapter.Message, stderrCh chan error) {
for {
select {
case m := <-stdinCh:
log.Debugf("Message received: %+v", m)
for _, p := range plugins {
if !a.ShouldRun(p, &m) {
log.Debugf("Not running plugin (%s) for: %+v", p.Image, m)
continue
}
log.Debugf("Running plugin (%s) for: %+v", p.Image, m)
stdout, stderr, err := p.Run(plugin.NewInput(m.Emitter, m.Receiver, m.Body))
if err != nil {
stderrCh <- err
continue
}
stdout = strings.TrimSuffix(stdout, "\n")
log.Debugf("Plugin (%s) response: %s", p.Image, stdout)
if stderr != "" {
log.Errorf("Plugin (%s) threw an error: %s", p.Image, stderr)
}
stdoutCh <- adapter.Message{Receiver: m.Receiver, Body: stdout}
}
case err := <-stderrCh:
log.Error(err)
case <-signalsCh:
for i := 0; i < len(adapters); i++ {
wg.Done()
}
}
}
}(a, stdinCh, stdoutCh, stderrCh)
}
wg.Wait()
log.Info("Teardown...")
for _, plugin := range plugins {
plugin.Stop()
}
}
func main() {
configPath := flag.String("f", "", "Use a different file for the config. By default: botella.y{,a}ml")
flag.Parse()
if *configPath == "" {
inferedPath, err := inferConfigPath()
if err != nil {
log.Error(err)
os.Exit(-1)
}
configPath = &inferedPath
}
config, err := config.NewFromFile(*configPath)
if err != nil {
log.Error(err)
os.Exit(-1)
}
adapters, err := loadAdapters(config)
if err != nil {
log.Error(err)
os.Exit(-1)
}
plugins, err := loadPlugins(config)
if err != nil {
log.Error(err)
os.Exit(-1)
}
listenAndReply(adapters, plugins)
}