Skip to content

Commit

Permalink
Added init() method to all modules
Browse files Browse the repository at this point in the history
  • Loading branch information
fleaz committed Sep 17, 2018
1 parent 2bfa877 commit 5e08a73
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 33 deletions.
30 changes: 23 additions & 7 deletions gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,36 @@ func (m GitlabModule) sendMessage(message string, projectName string, namespace

}

func (m GitlabModule) init(c *viper.Viper) {
err := c.Unmarshal(&m.channelMapping)
if err != nil {
log.Fatal("Failed to unmarshal channelmapping into struct")
}
}

func (m GitlabModule) getChannelList() []string {
return nil
var all []string

for _, v := range m.channelMapping.ExplicitMappings {
for _, name := range v {
all = append(all, name)
}
}
for _, v := range m.channelMapping.ExplicitMappings {
for _, name := range v {
all = append(all, name)
}
}
all = append(all, m.channelMapping.DefaultChannel)

return all
}

func (m GitlabModule) getEndpoint() string {
return "/gitlab"
}

func (m GitlabModule) getHandler(c *viper.Viper) http.HandlerFunc {
func (m GitlabModule) getHandler() http.HandlerFunc {

const pushCompareString = "[\x0312{{ .Project.Name }}\x03] {{ .UserName }} pushed {{ .TotalCommits }} commits to \x0305{{ .Branch }}\x03 {{ .Project.WebURL }}/compare/{{ .BeforeCommit }}...{{ .AfterCommit }}"
const pushCommitLogString = "[\x0312{{ .Project.Name }}\x03] {{ .UserName }} pushed {{ .TotalCommits }} commits to \x0305{{ .Branch }}\x03 {{ .Project.WebURL }}/commits/{{ .Branch }}"
Expand Down Expand Up @@ -94,11 +115,6 @@ func (m GitlabModule) getHandler(c *viper.Viper) http.HandlerFunc {
"merge": "merged",
}

err := c.Unmarshal(&m.channelMapping)
if err != nil {
log.Fatal("Failed to unmarshal channelmapping into struct")
}

const NullCommit = "0000000000000000000000000000000000000000"

pushCompareTemplate, err := template.New("push notification").Parse(pushCompareString)
Expand Down
3 changes: 2 additions & 1 deletion gitlab_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ func TestGitlabHandler(t *testing.T) {

rr := httptest.NewRecorder()
var gitlabModule Module = GitlabModule{}
handler := http.HandlerFunc(gitlabModule.getHandler(viper.Sub("modules.gitlab")))
gitlabModule.init(viper.Sub("modules.gitlab"))
handler := http.HandlerFunc(gitlabModule.getHandler())

handler.ServeHTTP(rr, req)

Expand Down
8 changes: 5 additions & 3 deletions irc.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (

var client *girc.Client

func ircConnection(config *viper.Viper) {
func ircConnection(config *viper.Viper, channelList []string) {
clientConfig := girc.Config{
Server: config.GetString("host"),
Port: config.GetInt("port"),
Expand Down Expand Up @@ -80,7 +80,9 @@ func ircConnection(config *viper.Viper) {
client = girc.New(clientConfig)

client.Handlers.Add(girc.CONNECTED, func(c *girc.Client, e girc.Event) {
c.Cmd.Whois(clientConfig.Nick)
for _, name := range channelList {
joinChannel(name)
}
})

// Start thread to process message queue
Expand Down Expand Up @@ -114,6 +116,6 @@ func joinChannel(newChannel string) {
return
}
}
fmt.Printf("Need to join new channel %s\n", newChannel)
fmt.Printf("Need to join new channel %q\n", newChannel)
client.Cmd.Join(newChannel)
}
24 changes: 16 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ type IRCMessage struct {

// Module defines a common interface for all CptHook modules
type Module interface {
init(c *viper.Viper)
getChannelList() []string
getEndpoint() string
getHandler(*viper.Viper) http.HandlerFunc
getHandler() http.HandlerFunc
}

var messageChannel = make(chan IRCMessage, 10)
Expand All @@ -44,39 +45,46 @@ func main() {
}

var moduleList = viper.Sub("modules")
var channelList = []string{}

// Status module
if moduleList.GetBool("status.enabled") {
log.Println("Status module is active")
var statusModule Module = StatusModule{}
http.HandleFunc(statusModule.getEndpoint(), statusModule.getHandler(nil))
} else {
log.Println("Status module disabled of not configured")
statusModule.init(viper.Sub("modules.status"))
channelList = append(channelList, statusModule.getChannelList()...)
http.HandleFunc(statusModule.getEndpoint(), statusModule.getHandler())
}

// Prometheus module
if moduleList.GetBool("prometheus.enabled") {
log.Println("Prometheus module is active")
var prometheusModule Module = PrometheusModule{}
http.HandleFunc(prometheusModule.getEndpoint(), prometheusModule.getHandler(viper.Sub("modules.prometheus")))
prometheusModule.init(viper.Sub("modules.prometheus"))
channelList = append(channelList, prometheusModule.getChannelList()...)
http.HandleFunc(prometheusModule.getEndpoint(), prometheusModule.getHandler())
}

// Gitlab module
if moduleList.GetBool("gitlab.enabled") {
log.Println("Gitlab module is active")
var gitlabModule Module = GitlabModule{}
http.HandleFunc(gitlabModule.getEndpoint(), gitlabModule.getHandler(viper.Sub("modules.gitlab")))
gitlabModule.init(viper.Sub("modules.gitlab"))
channelList = append(channelList, gitlabModule.getChannelList()...)
http.HandleFunc(gitlabModule.getEndpoint(), gitlabModule.getHandler())
}

// Simple module
if moduleList.GetBool("simple.enabled") {
log.Println("Simple module is active")
var simpleModule Module = SimpleModule{}
http.HandleFunc(simpleModule.getEndpoint(), simpleModule.getHandler(viper.Sub("modules.simple")))
simpleModule.init(viper.Sub("modules.simple"))
channelList = append(channelList, simpleModule.getChannelList()...)
http.HandleFunc(simpleModule.getEndpoint(), simpleModule.getHandler())
}

// Start IRC connection
go ircConnection(viper.Sub("irc"))
go ircConnection(viper.Sub("irc"), channelList)

// Start HTTP server
log.Fatal(http.ListenAndServe(viper.GetString("http.listen"), nil))
Expand Down
15 changes: 11 additions & 4 deletions prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (
)

type PrometheusModule struct {
defaultChannel string
hostnameFilter string
}

type alert struct {
Expand Down Expand Up @@ -87,10 +89,15 @@ func (m PrometheusModule) getEndpoint() string {
}

func (m PrometheusModule) getChannelList() []string {
return []string{"foo", "bar"}
return []string{m.defaultChannel}
}

func (m PrometheusModule) getHandler(c *viper.Viper) http.HandlerFunc {
func (m PrometheusModule) init(c *viper.Viper) {
m.defaultChannel = c.GetString("channel")
m.hostnameFilter = c.GetString("hostname_filter")
}

func (m PrometheusModule) getHandler() http.HandlerFunc {

const firingTemplateString = "[{{ .ColorStart }}{{ .Status }}{{ .ColorEnd }}:{{ .InstanceCount }}] {{ .Alert.Labels.alertname}} - {{ .Alert.Annotations.description}}"
const resolvedTemplateString = "[{{ .ColorStart }}{{ .Status }}{{ .ColorEnd }}:{{ .InstanceCount }}] {{ .Alert.Labels.alertname}}"
Expand Down Expand Up @@ -145,7 +152,7 @@ func (m PrometheusModule) getHandler(c *viper.Viper) http.HandlerFunc {

for _, alert := range alertList {
name := alert.Labels["instance"].(string)
name = shortenInstanceName(name, c.GetString("hostname_filter"))
name = shortenInstanceName(name, m.hostnameFilter)
value, ok := alert.Annotations["value"].(string)
if ok {
inst = instance{Name: name, Value: value}
Expand Down Expand Up @@ -177,7 +184,7 @@ func (m PrometheusModule) getHandler(c *viper.Viper) http.HandlerFunc {
buf.Reset()
_ = hostListTemplate.Execute(&buf, &instanceList)
event.Messages = append(event.Messages, buf.String())
event.Channel = c.GetString("channel")
event.Channel = m.defaultChannel
messageChannel <- event
}
}
Expand Down
5 changes: 3 additions & 2 deletions prometheus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ func TestPrometheusHandler(t *testing.T) {
}

rr := httptest.NewRecorder()
var pH Module = PrometheusModule{}
handler := http.HandlerFunc(pH.getHandler(viper.Sub("modules.prometheus")))
var prometheusModule Module = PrometheusModule{}
prometheusModule.init(viper.Sub("modules.prometheus"))
handler := http.HandlerFunc(prometheusModule.getHandler())

handler.ServeHTTP(rr, req)

Expand Down
16 changes: 10 additions & 6 deletions simple.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,23 @@ import (
"github.com/spf13/viper"
)

type SimpleModule struct{}
type SimpleModule struct {
defaultChannel string
}

func (m SimpleModule) init(c *viper.Viper) {
m.defaultChannel = c.GetString("default_channel")
}

func (m SimpleModule) getChannelList() []string {
return nil
return []string{m.defaultChannel}
}

func (m SimpleModule) getEndpoint() string {
return "/simple"
}

func (m SimpleModule) getHandler(c *viper.Viper) http.HandlerFunc {

defaultChannel := c.GetString("default_channel")
func (m SimpleModule) getHandler() http.HandlerFunc {

return func(wr http.ResponseWriter, req *http.Request) {
defer req.Body.Close()
Expand All @@ -29,7 +33,7 @@ func (m SimpleModule) getHandler(c *viper.Viper) http.HandlerFunc {
// Get channel to send to
channel := query.Get("channel")
if channel == "" {
channel = defaultChannel
channel = m.defaultChannel
}

// Split body into lines
Expand Down
3 changes: 2 additions & 1 deletion simple_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ func TestSimpleHandler(t *testing.T) {

rr := httptest.NewRecorder()
var simpleModule Module = SimpleModule{}
handler := http.HandlerFunc(simpleModule.getHandler(viper.Sub("modules.simple")))
simpleModule.init(viper.Sub("modules.simple"))
handler := http.HandlerFunc(simpleModule.getHandler())

handler.ServeHTTP(rr, req)

Expand Down
4 changes: 3 additions & 1 deletion status.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (

type StatusModule struct{}

func (m StatusModule) init(c *viper.Viper) {}

func (m StatusModule) getEndpoint() string {
return "/status"
}
Expand All @@ -18,7 +20,7 @@ func (m StatusModule) getChannelList() []string {
return []string{}
}

func (m StatusModule) getHandler(c *viper.Viper) http.HandlerFunc {
func (m StatusModule) getHandler() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
log.Println("Got http event for /status")
t, _ := template.ParseFiles("templates/status.html")
Expand Down

0 comments on commit 5e08a73

Please sign in to comment.