-
Notifications
You must be signed in to change notification settings - Fork 0
/
actions.go
40 lines (34 loc) · 933 Bytes
/
actions.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
package rest
import (
"errors"
"fmt"
"net/http"
"strings"
"github.com/dulli/deichwave/pkg/common"
)
var ErrActionNotReady = errors.New("actions can not be performed yet")
var ErrActionFailed = errors.New("action could not be performed")
func (s Server) DoAction(action string) error {
if s.port == 0 {
return ErrActionNotReady
}
response, err := http.Post(fmt.Sprintf("http://127.0.0.1:%d/%s/%s", s.port, s.apiBase, action), "application/json", nil)
if err == nil && response.StatusCode != 200 {
err = ErrActionFailed
}
return err
}
func (s Server) AddHooks(cfg common.Config) {
for key, actions := range cfg.Hooks {
listenHook(s, strings.Split(key, "-"), actions)
}
}
func listenHook(s Server, target []string, actions []string) {
common.EventListen(func(ev common.Event) {
if ev.Origin == target[0] && ev.Type == target[1] {
for _, action := range actions {
_ = s.DoAction(action)
}
}
})
}