/
commenter.go
44 lines (35 loc) · 976 Bytes
/
commenter.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
package commenter
import (
"fmt"
"github.com/bivas/rivi/pkg/engine/actions"
"github.com/bivas/rivi/pkg/types"
"github.com/bivas/rivi/pkg/util/log"
"github.com/mitchellh/mapstructure"
"github.com/mitchellh/multistep"
"github.com/prometheus/client_golang/prometheus"
)
type action struct {
rule *rule
}
func (a *action) String() string {
return fmt.Sprintf("%T{rule: %+v}", *a, a.rule)
}
func (a *action) Apply(state multistep.StateBag) {
counter.Inc()
state.Get("data").(types.Data).AddComment(a.rule.Comment)
}
type factory struct {
}
func (*factory) BuildAction(config map[string]interface{}) actions.Action {
item := rule{}
if e := mapstructure.Decode(config, &item); e != nil {
log.Get("commenter").ErrorWith(log.MetaFields{log.E(e)}, "Unable to build action")
return nil
}
return &action{rule: &item}
}
var counter = actions.NewCounter("commenter")
func init() {
actions.RegisterAction("commenter", &factory{})
prometheus.Register(counter)
}