/
hook.go
84 lines (72 loc) · 1.71 KB
/
hook.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
package hook
import (
"bytes"
"reflect"
"strings"
"github.com/lucasgomide/snitch/config"
"github.com/lucasgomide/snitch/types"
"github.com/lucasgomide/snitch/utils"
)
// Execute runs all hooks that have been configured on
// config file with your options
func Execute(h types.Hook, t types.Tsuru) {
var err error
var deploy []types.Deploy
err = findLastDeploy(t, &deploy)
if err != nil {
utils.LogError(err.Error())
} else {
for hookName, conf := range config.Data() {
switch defineHookName(hookName.(string)) {
case "Slack":
h = &Slack{}
case "Sentry":
h = &Sentry{}
case "Rollbar":
h = &Rollbar{}
case "Newrelic":
h = &NewRelic{}
case "Hangouts Chat":
h = &HangoutsChat{}
default:
continue
}
if err := executeHook(h, deploy, conf); err != nil {
utils.LogError(err.Error())
}
}
}
}
func findLastDeploy(t types.Tsuru, deploy *[]types.Deploy) error {
return t.FindLastDeploy(deploy)
}
func callHook(h types.Hook, deploy []types.Deploy) error {
err := h.ValidatesFields()
if err == nil {
return h.CallHook(deploy)
}
return err
}
func executeHook(h types.Hook, deploy []types.Deploy, conf interface{}) error {
s := reflect.ValueOf(h).Elem()
for k, v := range conf.(map[interface{}]interface{}) {
var valueBuffer bytes.Buffer
for _, w := range strings.Split(k.(string), "_") {
valueBuffer.WriteString(strings.Title(w))
}
value := s.FieldByName(valueBuffer.String())
if value.IsValid() && value.CanAddr() {
value.SetString(utils.GetEnv(v.(string)))
}
}
err := callHook(h, deploy)
if err != nil {
return err
}
return nil
}
func defineHookName(name string) string {
return strings.Title(
strings.Join(strings.Split(name, "_"), " "),
)
}