From 4324faf106f4de95a4fbe25296857de0eddb8c93 Mon Sep 17 00:00:00 2001 From: Felix Breidenstein Date: Thu, 9 Aug 2018 01:42:56 +0200 Subject: [PATCH 01/28] Interface for all Modules. Made all structs and functions private --- gitlab.go | 67 +++++++++++++++++++++++++++------------------- gitlab_test.go | 3 ++- main.go | 20 +++++++++++--- prometheus.go | 58 ++++++++++++++++++++++----------------- prometheus_test.go | 3 ++- simple.go | 15 +++++++++-- simple_test.go | 6 +++-- status.go | 22 ++++++++++++--- 8 files changed, 128 insertions(+), 66 deletions(-) diff --git a/gitlab.go b/gitlab.go index 95f746e..8646fd1 100644 --- a/gitlab.go +++ b/gitlab.go @@ -13,7 +13,11 @@ import ( "github.com/spf13/viper" ) -type Mapping struct { +type GitlabModule struct { + channelMapping mapping +} + +type mapping struct { DefaultChannel string `mapstructure:"default"` GroupMappings map[string][]string `mapstructure:"groups"` ExplicitMappings map[string][]string `mapstructure:"explicit"` @@ -28,20 +32,20 @@ func contains(mapping map[string][]string, entry string) bool { return false } -func sendMessage(message string, projectName string, namespace string, channelMapping Mapping) { +func (m GitlabModule) sendMessage(message string, projectName string, namespace string) { var channelNames []string - var full_projectName = namespace + "/" + projectName + var fullProjectName = namespace + "/" + projectName - if contains(channelMapping.ExplicitMappings, full_projectName) { // Check if explizit mapping exists - for _, channelName := range channelMapping.ExplicitMappings[full_projectName] { + if contains(m.channelMapping.ExplicitMappings, fullProjectName) { // Check if explizit mapping exists + for _, channelName := range m.channelMapping.ExplicitMappings[fullProjectName] { channelNames = append(channelNames, channelName) } - } else if contains(channelMapping.GroupMappings, namespace) { // Check if group mapping exists - for _, channelName := range channelMapping.GroupMappings[namespace] { + } else if contains(m.channelMapping.GroupMappings, namespace) { // Check if group mapping exists + for _, channelName := range m.channelMapping.GroupMappings[namespace] { channelNames = append(channelNames, channelName) } } else { // Fall back to default channel - channelNames = append(channelNames, channelMapping.DefaultChannel) + channelNames = append(channelNames, m.channelMapping.DefaultChannel) } for _, channelName := range channelNames { @@ -53,7 +57,15 @@ func sendMessage(message string, projectName string, namespace string, channelMa } -func gitlabHandler(c *viper.Viper) http.HandlerFunc { +func (m GitlabModule) getChannelList() []string { + return nil +} + +func (m GitlabModule) getEndpoint() string { + return "/gitlab" +} + +func (m GitlabModule) getHandler(c *viper.Viper) 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 }}" @@ -62,9 +74,9 @@ func gitlabHandler(c *viper.Viper) http.HandlerFunc { const commitString = "\x0315{{ .ShortID }}\x03 (\x0303+{{ .AddedFiles }}\x03|\x0308±{{ .ModifiedFiles }}\x03|\x0304-{{ .RemovedFiles }}\x03) \x0306{{ .Author.Name }}\x03: {{ .Message }}" const issueString = "[\x0312{{ .Project.Name }}\x03] {{ .User.Name }} {{ .Issue.Action }} issue \x0308#{{ .Issue.Iid }}\x03: {{ .Issue.Title }} {{ .Issue.URL }}" const mergeString = "[\x0312{{ .Project.Name }}\x03] {{ .User.Name }} {{ .Merge.Action }} merge request \x0308#{{ .Merge.Iid }}\x03: {{ .Merge.Title }} {{ .Merge.URL }}" - const pipelineCreateString = "[\x0312{{ .Project.Name }}\x03] Pipeline for commit {{ .Pipeline.Commit }} {{ .Pipeline.Status }} {{ .Project.WebURL }}/pipelines/{{ .Pipeline.Id }}" - const pipelineCompleteString = "[\x0312{{ .Project.Name }}\x03] Pipeline for commit {{ .Pipeline.Commit }} {{ .Pipeline.Status }} in {{ .Pipeline.Duration }} seconds {{ .Project.WebURL }}/pipelines/{{ .Pipeline.Id }}" - const jobCompleteString = "[\x0312{{ .Repository.Name }}\x03] Job \x0308{{ .Name }}\x03 for commit {{ .Commit }} {{ .Status }} in {{ .Duration }} seconds {{ .Repository.Homepage }}/-/jobs/{{ .Id }}" + const pipelineCreateString = "[\x0312{{ .Project.Name }}\x03] Pipeline for commit {{ .Pipeline.Commit }} {{ .Pipeline.Status }} {{ .Project.WebURL }}/pipelines/{{ .Pipeline.ID }}" + const pipelineCompleteString = "[\x0312{{ .Project.Name }}\x03] Pipeline for commit {{ .Pipeline.Commit }} {{ .Pipeline.Status }} in {{ .Pipeline.Duration }} seconds {{ .Project.WebURL }}/pipelines/{{ .Pipeline.ID }}" + const jobCompleteString = "[\x0312{{ .Repository.Name }}\x03] Job \x0308{{ .Name }}\x03 for commit {{ .Commit }} {{ .Status }} in {{ .Duration }} seconds {{ .Repository.Homepage }}/-/jobs/{{ .ID }}" JobStatus := map[string]string{ "pending": "is \x0315pending\x03", @@ -82,8 +94,7 @@ func gitlabHandler(c *viper.Viper) http.HandlerFunc { "merge": "merged", } - channelMapping := Mapping{} - err := c.Unmarshal(&channelMapping) + err := c.Unmarshal(&m.channelMapping) if err != nil { log.Fatal("Failed to unmarshal channelmapping into struct") } @@ -169,7 +180,7 @@ func gitlabHandler(c *viper.Viper) http.HandlerFunc { } type Commit struct { - Id string `json:"id"` + ID string `json:"id"` Message string `json:"message"` Added []string `json:"added"` Modified []string `json:"modified"` @@ -207,7 +218,7 @@ func gitlabHandler(c *viper.Viper) http.HandlerFunc { } type Pipeline struct { - Id int `json:"id"` + ID int `json:"id"` Commit string `json:"sha"` Status string `json:"status"` Duration float64 `json:"duration"` @@ -225,7 +236,7 @@ func gitlabHandler(c *viper.Viper) http.HandlerFunc { } type JobEvent struct { - Id int `json:"build_id"` + ID int `json:"build_id"` Name string `json:"build_name"` Status string `json:"build_status"` Duration float64 `json:"build_duration"` @@ -259,14 +270,14 @@ func gitlabHandler(c *viper.Viper) http.HandlerFunc { pipelineEvent.Pipeline.Status = JobStatus[pipelineEvent.Pipeline.Status] err = pipelineCreateTemplate.Execute(&buf, &pipelineEvent) - sendMessage(buf.String(), pipelineEvent.Project.Name, pipelineEvent.Project.Namespace, channelMapping) + m.sendMessage(buf.String(), pipelineEvent.Project.Name, pipelineEvent.Project.Namespace) } else if pipelineEvent.Pipeline.Status == "success" || pipelineEvent.Pipeline.Status == "failed" { // colorize status pipelineEvent.Pipeline.Status = JobStatus[pipelineEvent.Pipeline.Status] err = pipelineCompleteTemplate.Execute(&buf, &pipelineEvent) - sendMessage(buf.String(), pipelineEvent.Project.Name, pipelineEvent.Project.Namespace, channelMapping) + m.sendMessage(buf.String(), pipelineEvent.Project.Name, pipelineEvent.Project.Namespace) } case "Job Hook": @@ -292,7 +303,7 @@ func gitlabHandler(c *viper.Viper) http.HandlerFunc { jobEvent.Status = JobStatus[jobEvent.Status] err = jobCompleteTemplate.Execute(&buf, &jobEvent) - sendMessage(buf.String(), jobEvent.Repository.Name, namespace, channelMapping) + m.sendMessage(buf.String(), jobEvent.Repository.Name, namespace) case "Merge Request Hook", "Merge Request Event": log.Printf("Got Hook for a Merge Request") @@ -306,7 +317,7 @@ func gitlabHandler(c *viper.Viper) http.HandlerFunc { err = mergeTemplate.Execute(&buf, &mergeEvent) - sendMessage(buf.String(), mergeEvent.Project.Name, mergeEvent.Project.Namespace, channelMapping) + m.sendMessage(buf.String(), mergeEvent.Project.Name, mergeEvent.Project.Namespace) case "Issue Hook", "Issue Event": log.Printf("Got Hook for an Issue") @@ -320,7 +331,7 @@ func gitlabHandler(c *viper.Viper) http.HandlerFunc { err = issueTemplate.Execute(&buf, &issueEvent) - sendMessage(buf.String(), issueEvent.Project.Name, issueEvent.Project.Namespace, channelMapping) + m.sendMessage(buf.String(), issueEvent.Project.Name, issueEvent.Project.Namespace) case "Push Hook", "Push Event": log.Printf("Got Hook for a Push Event") @@ -336,13 +347,13 @@ func gitlabHandler(c *viper.Viper) http.HandlerFunc { // Branch was deleted var buf bytes.Buffer err = branchDeleteTemplate.Execute(&buf, &pushEvent) - sendMessage(buf.String(), pushEvent.Project.Name, pushEvent.Project.Namespace, channelMapping) + m.sendMessage(buf.String(), pushEvent.Project.Name, pushEvent.Project.Namespace) } else { if pushEvent.BeforeCommit == NullCommit { // Branch was created var buf bytes.Buffer err = branchCreateTemplate.Execute(&buf, &pushEvent) - sendMessage(buf.String(), pushEvent.Project.Name, pushEvent.Project.Namespace, channelMapping) + m.sendMessage(buf.String(), pushEvent.Project.Name, pushEvent.Project.Namespace) } if pushEvent.TotalCommits > 0 { @@ -356,7 +367,7 @@ func gitlabHandler(c *viper.Viper) http.HandlerFunc { err = pushCompareTemplate.Execute(&buf, &pushEvent) } - sendMessage(buf.String(), pushEvent.Project.Name, pushEvent.Project.Namespace, channelMapping) + m.sendMessage(buf.String(), pushEvent.Project.Name, pushEvent.Project.Namespace) // Limit number of commit meessages to 3 if pushEvent.TotalCommits > 3 { @@ -374,7 +385,7 @@ func gitlabHandler(c *viper.Viper) http.HandlerFunc { } context := CommitContext{ - ShortID: commit.Id[0:7], + ShortID: commit.ID[0:7], Message: html.UnescapeString(commit.Message), Author: commit.Author, AddedFiles: len(commit.Added), @@ -389,12 +400,12 @@ func gitlabHandler(c *viper.Viper) http.HandlerFunc { log.Printf("ERROR: %v", err) return } - sendMessage(buf.String(), pushEvent.Project.Name, pushEvent.Project.Namespace, channelMapping) + m.sendMessage(buf.String(), pushEvent.Project.Name, pushEvent.Project.Namespace) } if pushEvent.TotalCommits > 3 { var message = fmt.Sprintf("and %d more commits.", pushEvent.TotalCommits-3) - sendMessage(message, pushEvent.Project.Name, pushEvent.Project.Namespace, channelMapping) + m.sendMessage(message, pushEvent.Project.Name, pushEvent.Project.Namespace) } } } diff --git a/gitlab_test.go b/gitlab_test.go index 5dd564e..c324a54 100644 --- a/gitlab_test.go +++ b/gitlab_test.go @@ -32,7 +32,8 @@ func TestGitlabHandler(t *testing.T) { } rr := httptest.NewRecorder() - handler := http.HandlerFunc(gitlabHandler(viper.Sub("modules.gitlab"))) + var gitlabModule Module = GitlabModule{} + handler := http.HandlerFunc(gitlabModule.getHandler(viper.Sub("modules.gitlab"))) handler.ServeHTTP(rr, req) diff --git a/main.go b/main.go index 489db6d..bb895bf 100644 --- a/main.go +++ b/main.go @@ -11,11 +11,19 @@ import ( "github.com/spf13/viper" ) +// IRCMessage are send over the messageChannel from the different modules type IRCMessage struct { Messages []string Channel string } +// Module defines a common interface for all CptHook modules +type Module interface { + getChannelList() []string + getEndpoint() string + getHandler(*viper.Viper) http.HandlerFunc +} + var messageChannel = make(chan IRCMessage, 10) func main() { @@ -40,7 +48,8 @@ func main() { // Status module if moduleList.GetBool("status.enabled") { log.Println("Status module is active") - http.HandleFunc("/status", statusHandler) + var statusModule Module = StatusModule{} + http.HandleFunc(statusModule.getEndpoint(), statusModule.getHandler(nil)) } else { log.Println("Status module disabled of not configured") } @@ -48,19 +57,22 @@ func main() { // Prometheus module if moduleList.GetBool("prometheus.enabled") { log.Println("Prometheus module is active") - http.HandleFunc("/prometheus", prometheusHandler(viper.Sub("modules.prometheus"))) + var prometheusModule Module = PrometheusModule{} + http.HandleFunc(prometheusModule.getEndpoint(), prometheusModule.getHandler(viper.Sub("modules.prometheus"))) } // Gitlab module if moduleList.GetBool("gitlab.enabled") { log.Println("Gitlab module is active") - http.HandleFunc("/gitlab", gitlabHandler(viper.Sub("modules.gitlab"))) + var gitlabModule Module = GitlabModule{} + http.HandleFunc(gitlabModule.getEndpoint(), gitlabModule.getHandler(viper.Sub("modules.gitlab"))) } // Simple module if moduleList.GetBool("simple.enabled") { log.Println("Simple module is active") - http.HandleFunc("/simple", simpleHandler(viper.Sub("modules.simple"))) + var simpleModule Module = SimpleModule{} + http.HandleFunc(simpleModule.getEndpoint(), simpleModule.getHandler(viper.Sub("modules.simple"))) } // Start IRC connection diff --git a/prometheus.go b/prometheus.go index 2fae099..778e9a6 100644 --- a/prometheus.go +++ b/prometheus.go @@ -13,14 +13,17 @@ import ( "github.com/spf13/viper" ) -type Alert struct { +type PrometheusModule struct { +} + +type alert struct { Labels map[string]interface{} `json:"labels"` Annotations map[string]interface{} `json:"annotations"` StartsAt string `json:"startsAt"` EndsAt string `json:"endsAt"` } -type Notification struct { +type notification struct { Version string `json:"version"` GroupKey string `json:"groupKey"` Status string `json:"status"` @@ -29,24 +32,24 @@ type Notification struct { CommonLabels map[string]interface{} `json:"commonLabels"` CommonAnnotations map[string]interface{} `json:"commonAnnotations"` ExternalURL string `json:"externalURL"` - Alerts []Alert `json:"alerts"` + Alerts []alert `json:"alerts"` } -type NotificationContext struct { - Alert *Alert - Notification *Notification +type notificationContext struct { + Alert *alert + Notification *notification InstanceCount int Status string ColorStart string ColorEnd string } -type Instance struct { +type instance struct { Name string Value string } -func SortAlerts(alerts []Alert) (firing, resolved []Alert) { +func sortAlerts(alerts []alert) (firing, resolved []alert) { for _, alert := range alerts { tStart, _ := time.Parse(time.RFC3339, alert.StartsAt) tEnd, _ := time.Parse(time.RFC3339, alert.EndsAt) @@ -75,12 +78,19 @@ func shortenInstanceName(name string, pattern string) string { match := r.FindStringSubmatch(name) if len(match) > 1 { return match[1] - } else { - return name } + return name +} + +func (m PrometheusModule) getEndpoint() string { + return "/prometheus" +} + +func (m PrometheusModule) getChannelList() []string { + return []string{"foo", "bar"} } -func prometheusHandler(c *viper.Viper) http.HandlerFunc { +func (m PrometheusModule) getHandler(c *viper.Viper) http.HandlerFunc { const firingTemplateString = "[{{ .ColorStart }}{{ .Status }}{{ .ColorEnd }}:{{ .InstanceCount }}] {{ .Alert.Labels.alertname}} - {{ .Alert.Annotations.description}}" const resolvedTemplateString = "[{{ .ColorStart }}{{ .Status }}{{ .ColorEnd }}:{{ .InstanceCount }}] {{ .Alert.Labels.alertname}}" @@ -106,25 +116,25 @@ func prometheusHandler(c *viper.Viper) http.HandlerFunc { defer r.Body.Close() decoder := json.NewDecoder(r.Body) - var notification Notification + var n notification - if err := decoder.Decode(¬ification); err != nil { + if err := decoder.Decode(&n); err != nil { log.Println(err) return } - _, err := json.Marshal(¬ification) + _, err := json.Marshal(&n) if err != nil { log.Println(err) return } - var sortedAlerts = make(map[string][]Alert) - sortedAlerts["firing"], sortedAlerts["resolved"] = SortAlerts(notification.Alerts) + var sortedAlerts = make(map[string][]alert) + sortedAlerts["firing"], sortedAlerts["resolved"] = sortAlerts(n.Alerts) - var instance Instance - var instanceList []Instance + var inst instance + var instanceList []instance var buf bytes.Buffer for alertStatus, alertList := range sortedAlerts { @@ -138,16 +148,16 @@ func prometheusHandler(c *viper.Viper) http.HandlerFunc { name = shortenInstanceName(name, c.GetString("hostname_filter")) value, ok := alert.Annotations["value"].(string) if ok { - instance = Instance{Name: name, Value: value} + inst = instance{Name: name, Value: value} } else { - instance = Instance{Name: name} + inst = instance{Name: name} } - instanceList = append(instanceList, instance) + instanceList = append(instanceList, inst) } - context := NotificationContext{ - Alert: ¬ification.Alerts[0], - Notification: ¬ification, + context := notificationContext{ + Alert: &n.Alerts[0], + Notification: &n, Status: strings.ToUpper(alertStatus), InstanceCount: len(instanceList), ColorStart: getColorcode(alertStatus), diff --git a/prometheus_test.go b/prometheus_test.go index ee6589d..a50366b 100644 --- a/prometheus_test.go +++ b/prometheus_test.go @@ -31,7 +31,8 @@ func TestPrometheusHandler(t *testing.T) { } rr := httptest.NewRecorder() - handler := http.HandlerFunc(prometheusHandler(viper.Sub("modules.prometheus"))) + var pH Module = PrometheusModule{} + handler := http.HandlerFunc(pH.getHandler(viper.Sub("modules.prometheus"))) handler.ServeHTTP(rr, req) diff --git a/simple.go b/simple.go index 1330179..2260ae8 100644 --- a/simple.go +++ b/simple.go @@ -1,12 +1,23 @@ package main import ( + "bufio" "net/http" + "github.com/spf13/viper" - "bufio" ) -func simpleHandler(c *viper.Viper) http.HandlerFunc { +type SimpleModule struct{} + +func (m SimpleModule) getChannelList() []string { + return nil +} + +func (m SimpleModule) getEndpoint() string { + return "/simple" +} + +func (m SimpleModule) getHandler(c *viper.Viper) http.HandlerFunc { defaultChannel := c.GetString("default_channel") diff --git a/simple_test.go b/simple_test.go index a03ba69..5b66f54 100644 --- a/simple_test.go +++ b/simple_test.go @@ -6,8 +6,9 @@ import ( "net/http/httptest" "testing" - "github.com/spf13/viper" "strings" + + "github.com/spf13/viper" ) func TestSimpleHandler(t *testing.T) { @@ -26,7 +27,8 @@ func TestSimpleHandler(t *testing.T) { } rr := httptest.NewRecorder() - handler := http.HandlerFunc(simpleHandler(viper.Sub("modules.simple"))) + var simpleModule Module = SimpleModule{} + handler := http.HandlerFunc(simpleModule.getHandler(viper.Sub("modules.simple"))) handler.ServeHTTP(rr, req) diff --git a/status.go b/status.go index 2d471fe..246a7de 100644 --- a/status.go +++ b/status.go @@ -4,10 +4,24 @@ import ( "html/template" "log" "net/http" + + "github.com/spf13/viper" ) -func statusHandler(w http.ResponseWriter, r *http.Request) { - log.Println("Got http event for /status") - t, _ := template.ParseFiles("templates/status.html") - t.Execute(w, nil) +type StatusModule struct{} + +func (m StatusModule) getEndpoint() string { + return "/status" +} + +func (m StatusModule) getChannelList() []string { + return []string{} +} + +func (m StatusModule) getHandler(c *viper.Viper) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + log.Println("Got http event for /status") + t, _ := template.ParseFiles("templates/status.html") + t.Execute(w, nil) + } } From ca1030ea8d90a24ffdd93694e92eb22e419790be Mon Sep 17 00:00:00 2001 From: Felix Breidenstein Date: Tue, 14 Aug 2018 02:04:48 +0200 Subject: [PATCH 02/28] Added webhook notifications to travis.yml --- .travis.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.travis.yml b/.travis.yml index f3db8fb..209d245 100644 --- a/.travis.yml +++ b/.travis.yml @@ -26,3 +26,7 @@ deploy: script: bash scripts/deploy.sh on: branch: master + +notifications: + webhooks: + - http://webhooks.rainbownerds.de:12345 \ No newline at end of file From 3fe7cae6a35c717146bd363ac2758e74d359eb96 Mon Sep 17 00:00:00 2001 From: Felix Breidenstein Date: Tue, 21 Aug 2018 01:22:48 +0200 Subject: [PATCH 03/28] Added init() method to all modules --- gitlab.go | 30 +++++++++++++++++++++++------- gitlab_test.go | 3 ++- irc.go | 8 +++++--- main.go | 24 ++++++++++++++++-------- prometheus.go | 15 +++++++++++---- prometheus_test.go | 5 +++-- simple.go | 16 ++++++++++------ simple_test.go | 3 ++- status.go | 4 +++- 9 files changed, 75 insertions(+), 33 deletions(-) diff --git a/gitlab.go b/gitlab.go index 8646fd1..2d98d74 100644 --- a/gitlab.go +++ b/gitlab.go @@ -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 }}" @@ -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) diff --git a/gitlab_test.go b/gitlab_test.go index c324a54..8f59287 100644 --- a/gitlab_test.go +++ b/gitlab_test.go @@ -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) diff --git a/irc.go b/irc.go index 524ce17..04954af 100644 --- a/irc.go +++ b/irc.go @@ -14,7 +14,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"), @@ -79,7 +79,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 @@ -109,6 +111,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) } diff --git a/main.go b/main.go index bb895bf..51b9675 100644 --- a/main.go +++ b/main.go @@ -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) @@ -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)) diff --git a/prometheus.go b/prometheus.go index 778e9a6..f6c999c 100644 --- a/prometheus.go +++ b/prometheus.go @@ -14,6 +14,8 @@ import ( ) type PrometheusModule struct { + defaultChannel string + hostnameFilter string } type alert struct { @@ -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}}" @@ -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} @@ -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 } } diff --git a/prometheus_test.go b/prometheus_test.go index a50366b..eededa5 100644 --- a/prometheus_test.go +++ b/prometheus_test.go @@ -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) diff --git a/simple.go b/simple.go index 2260ae8..ef39be1 100644 --- a/simple.go +++ b/simple.go @@ -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() @@ -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 diff --git a/simple_test.go b/simple_test.go index 5b66f54..02e713d 100644 --- a/simple_test.go +++ b/simple_test.go @@ -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) diff --git a/status.go b/status.go index 246a7de..c638837 100644 --- a/status.go +++ b/status.go @@ -10,6 +10,8 @@ import ( type StatusModule struct{} +func (m StatusModule) init(c *viper.Viper) {} + func (m StatusModule) getEndpoint() string { return "/status" } @@ -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") From 6f507dd26f6bef4226046e515833e6fe7e05f9e5 Mon Sep 17 00:00:00 2001 From: Felix Breidenstein Date: Tue, 21 Aug 2018 15:16:47 +0200 Subject: [PATCH 04/28] Fixed small copy/paste error --- gitlab.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gitlab.go b/gitlab.go index 2d98d74..4361ede 100644 --- a/gitlab.go +++ b/gitlab.go @@ -72,7 +72,7 @@ func (m GitlabModule) getChannelList() []string { all = append(all, name) } } - for _, v := range m.channelMapping.ExplicitMappings { + for _, v := range m.channelMapping.GroupMappings { for _, name := range v { all = append(all, name) } From d35cbe3bcc2e7b161aee338f70078b8e370ef6be Mon Sep 17 00:00:00 2001 From: Martin Weinelt Date: Sat, 25 Aug 2018 00:18:01 +0200 Subject: [PATCH 05/28] Use text/template to render strings This fixes the unintentional encoding of characters as html entities when the the template was executed. Fixes #16. --- gitlab.go | 5 ++--- prometheus.go | 2 +- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/gitlab.go b/gitlab.go index 4361ede..20ad23f 100644 --- a/gitlab.go +++ b/gitlab.go @@ -4,11 +4,10 @@ import ( "bytes" "encoding/json" "fmt" - "html" - "html/template" "log" "net/http" "strings" + "text/template" "github.com/spf13/viper" ) @@ -402,7 +401,7 @@ func (m GitlabModule) getHandler() http.HandlerFunc { context := CommitContext{ ShortID: commit.ID[0:7], - Message: html.UnescapeString(commit.Message), + Message: commit.Message, Author: commit.Author, AddedFiles: len(commit.Added), ModifiedFiles: len(commit.Modified), diff --git a/prometheus.go b/prometheus.go index f6c999c..1f05ea3 100644 --- a/prometheus.go +++ b/prometheus.go @@ -3,11 +3,11 @@ package main import ( "bytes" "encoding/json" - "html/template" "log" "net/http" "regexp" "strings" + "text/template" "time" "github.com/spf13/viper" From 3cc86af87dc05f015706b932f4fbed84ee8e86dc Mon Sep 17 00:00:00 2001 From: Felix Breidenstein Date: Sun, 26 Aug 2018 22:48:20 +0200 Subject: [PATCH 06/28] Build tagged images. Fixes #8 --- .travis.yml | 11 +++++++++-- scripts/deploy.sh | 6 ++++-- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/.travis.yml b/.travis.yml index 209d245..bdba14f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -16,17 +16,24 @@ before_install: - dep ensure script: + - set -eu - cp cpthook.yml.example cpthook.yml - go test - CGO_ENABLED="0" GOARCH="amd64" GOOS="linux" go build -a -installsuffix cgo -o CptHook + - docker build -f Dockerfile -t $IMAGE . deploy: - provider: script skip_cleanup: true - script: bash scripts/deploy.sh + script: bash scripts/deploy.sh stable on: branch: master + - provider: script + skip_cleanup: true + script: bash scripts/deploy.sh development + on: + branch: development notifications: webhooks: - - http://webhooks.rainbownerds.de:12345 \ No newline at end of file + - http://webhooks.rainbownerds.de:12345 diff --git a/scripts/deploy.sh b/scripts/deploy.sh index 77193da..e713e4e 100644 --- a/scripts/deploy.sh +++ b/scripts/deploy.sh @@ -1,6 +1,8 @@ #! /usr/bin/env bash set -eu -docker build -f Dockerfile -t $IMAGE . +TAG=$1 + +docker tag $IMAGE $IMAGE:$TAG docker login -u $DOCKER_USER -p $DOCKER_PASS -docker push $IMAGE +docker push $IMAGE:$TAG From 80d4da2b1f7306c6bb517a9abdb5a68944571279 Mon Sep 17 00:00:00 2001 From: Felix Breidenstein Date: Sun, 26 Aug 2018 22:54:21 +0200 Subject: [PATCH 07/28] remove 'set -eu' because it breaks the build --- .travis.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index bdba14f..14f8790 100644 --- a/.travis.yml +++ b/.travis.yml @@ -16,7 +16,6 @@ before_install: - dep ensure script: - - set -eu - cp cpthook.yml.example cpthook.yml - go test - CGO_ENABLED="0" GOARCH="amd64" GOOS="linux" go build -a -installsuffix cgo -o CptHook From adecc2ad16b130569f69bc98fdc262dfdc916aba Mon Sep 17 00:00:00 2001 From: Felix Breidenstein Date: Tue, 28 Aug 2018 23:18:36 +0200 Subject: [PATCH 08/28] Use pointer for the struct functions so we can actually modify the object --- gitlab.go | 2 +- main.go | 8 ++++---- prometheus.go | 2 +- simple.go | 2 +- status.go | 2 +- 5 files changed, 8 insertions(+), 8 deletions(-) diff --git a/gitlab.go b/gitlab.go index 20ad23f..256ba89 100644 --- a/gitlab.go +++ b/gitlab.go @@ -56,7 +56,7 @@ func (m GitlabModule) sendMessage(message string, projectName string, namespace } -func (m GitlabModule) init(c *viper.Viper) { +func (m *GitlabModule) init(c *viper.Viper) { err := c.Unmarshal(&m.channelMapping) if err != nil { log.Fatal("Failed to unmarshal channelmapping into struct") diff --git a/main.go b/main.go index 51b9675..87d13bd 100644 --- a/main.go +++ b/main.go @@ -50,7 +50,7 @@ func main() { // Status module if moduleList.GetBool("status.enabled") { log.Println("Status module is active") - var statusModule Module = StatusModule{} + var statusModule Module = &StatusModule{} statusModule.init(viper.Sub("modules.status")) channelList = append(channelList, statusModule.getChannelList()...) http.HandleFunc(statusModule.getEndpoint(), statusModule.getHandler()) @@ -59,7 +59,7 @@ func main() { // Prometheus module if moduleList.GetBool("prometheus.enabled") { log.Println("Prometheus module is active") - var prometheusModule Module = PrometheusModule{} + var prometheusModule Module = &PrometheusModule{} prometheusModule.init(viper.Sub("modules.prometheus")) channelList = append(channelList, prometheusModule.getChannelList()...) http.HandleFunc(prometheusModule.getEndpoint(), prometheusModule.getHandler()) @@ -68,7 +68,7 @@ func main() { // Gitlab module if moduleList.GetBool("gitlab.enabled") { log.Println("Gitlab module is active") - var gitlabModule Module = GitlabModule{} + var gitlabModule Module = &GitlabModule{} gitlabModule.init(viper.Sub("modules.gitlab")) channelList = append(channelList, gitlabModule.getChannelList()...) http.HandleFunc(gitlabModule.getEndpoint(), gitlabModule.getHandler()) @@ -77,7 +77,7 @@ func main() { // Simple module if moduleList.GetBool("simple.enabled") { log.Println("Simple module is active") - var simpleModule Module = SimpleModule{} + var simpleModule Module = &SimpleModule{} simpleModule.init(viper.Sub("modules.simple")) channelList = append(channelList, simpleModule.getChannelList()...) http.HandleFunc(simpleModule.getEndpoint(), simpleModule.getHandler()) diff --git a/prometheus.go b/prometheus.go index 1f05ea3..8de2f2c 100644 --- a/prometheus.go +++ b/prometheus.go @@ -92,7 +92,7 @@ func (m PrometheusModule) getChannelList() []string { return []string{m.defaultChannel} } -func (m PrometheusModule) init(c *viper.Viper) { +func (m *PrometheusModule) init(c *viper.Viper) { m.defaultChannel = c.GetString("channel") m.hostnameFilter = c.GetString("hostname_filter") } diff --git a/simple.go b/simple.go index ef39be1..f864e5d 100644 --- a/simple.go +++ b/simple.go @@ -11,7 +11,7 @@ type SimpleModule struct { defaultChannel string } -func (m SimpleModule) init(c *viper.Viper) { +func (m *SimpleModule) init(c *viper.Viper) { m.defaultChannel = c.GetString("default_channel") } diff --git a/status.go b/status.go index c638837..d12715d 100644 --- a/status.go +++ b/status.go @@ -10,7 +10,7 @@ import ( type StatusModule struct{} -func (m StatusModule) init(c *viper.Viper) {} +func (m *StatusModule) init(c *viper.Viper) {} func (m StatusModule) getEndpoint() string { return "/status" From ba9ee91cc601c7b76f5dad77c8df6742c93f9ce2 Mon Sep 17 00:00:00 2001 From: Felix Breidenstein Date: Tue, 28 Aug 2018 23:31:06 +0200 Subject: [PATCH 09/28] Fixed tests --- gitlab_test.go | 2 +- prometheus_test.go | 2 +- simple_test.go | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/gitlab_test.go b/gitlab_test.go index 8f59287..0fa6c46 100644 --- a/gitlab_test.go +++ b/gitlab_test.go @@ -32,7 +32,7 @@ func TestGitlabHandler(t *testing.T) { } rr := httptest.NewRecorder() - var gitlabModule Module = GitlabModule{} + var gitlabModule Module = &GitlabModule{} gitlabModule.init(viper.Sub("modules.gitlab")) handler := http.HandlerFunc(gitlabModule.getHandler()) diff --git a/prometheus_test.go b/prometheus_test.go index eededa5..3231992 100644 --- a/prometheus_test.go +++ b/prometheus_test.go @@ -31,7 +31,7 @@ func TestPrometheusHandler(t *testing.T) { } rr := httptest.NewRecorder() - var prometheusModule Module = PrometheusModule{} + var prometheusModule Module = &PrometheusModule{} prometheusModule.init(viper.Sub("modules.prometheus")) handler := http.HandlerFunc(prometheusModule.getHandler()) diff --git a/simple_test.go b/simple_test.go index 02e713d..9ffeb39 100644 --- a/simple_test.go +++ b/simple_test.go @@ -27,7 +27,7 @@ func TestSimpleHandler(t *testing.T) { } rr := httptest.NewRecorder() - var simpleModule Module = SimpleModule{} + var simpleModule Module = &SimpleModule{} simpleModule.init(viper.Sub("modules.simple")) handler := http.HandlerFunc(simpleModule.getHandler()) From e59f1787e1f86a7a296a7f8c8eab9b49cdc88b8e Mon Sep 17 00:00:00 2001 From: Felix Breidenstein Date: Tue, 28 Aug 2018 23:44:11 +0200 Subject: [PATCH 10/28] Disable emails from travis --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index 14f8790..9d04ea1 100644 --- a/.travis.yml +++ b/.travis.yml @@ -36,3 +36,4 @@ deploy: notifications: webhooks: - http://webhooks.rainbownerds.de:12345 + email: false From 9858816950bf763dc03d6f0741cef2e073940853 Mon Sep 17 00:00:00 2001 From: Felix Breidenstein Date: Tue, 4 Sep 2018 01:06:52 +0200 Subject: [PATCH 11/28] Added more webhook config --- .travis.yml | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 9d04ea1..3c51509 100644 --- a/.travis.yml +++ b/.travis.yml @@ -35,5 +35,12 @@ deploy: notifications: webhooks: - - http://webhooks.rainbownerds.de:12345 + urls: + - http://webhooks.rainbownerds.de:12345 + on_success: always # default: always + on_failure: always # default: always + on_start: always # default: never + on_cancel: always # default: always + on_error: always # default: always + email: false From 6f6b61831f8936815c575882d208e90b013f1956 Mon Sep 17 00:00:00 2001 From: Felix Breidenstein Date: Tue, 4 Sep 2018 01:17:35 +0200 Subject: [PATCH 12/28] Breaking travis on purpose to get a failing webhook for development --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 3c51509..28d8bf9 100644 --- a/.travis.yml +++ b/.travis.yml @@ -16,7 +16,7 @@ before_install: - dep ensure script: - - cp cpthook.yml.example cpthook.yml + - cp cpthoooook.yml.example cpthook.yml - go test - CGO_ENABLED="0" GOARCH="amd64" GOOS="linux" go build -a -installsuffix cgo -o CptHook - docker build -f Dockerfile -t $IMAGE . From 3e644422c763af8d75cf8e7d2bc9ae7f9bca25bb Mon Sep 17 00:00:00 2001 From: Felix Breidenstein Date: Sun, 16 Sep 2018 20:00:51 +0200 Subject: [PATCH 13/28] Revert "Breaking travis on purpose to get a failing webhook for development" This reverts commit 6f6b61831f8936815c575882d208e90b013f1956. --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 28d8bf9..3c51509 100644 --- a/.travis.yml +++ b/.travis.yml @@ -16,7 +16,7 @@ before_install: - dep ensure script: - - cp cpthoooook.yml.example cpthook.yml + - cp cpthook.yml.example cpthook.yml - go test - CGO_ENABLED="0" GOARCH="amd64" GOOS="linux" go build -a -installsuffix cgo -o CptHook - docker build -f Dockerfile -t $IMAGE . From db38200badb5b43a00f1a09d5f09cc63f531480f Mon Sep 17 00:00:00 2001 From: Felix Breidenstein Date: Tue, 18 Sep 2018 00:07:12 +0200 Subject: [PATCH 14/28] Add GoReleaser --- .goreleaser.yml | 31 +++++++++++++++++++++++++++++++ .travis.yml | 9 +++++++++ 2 files changed, 40 insertions(+) create mode 100644 .goreleaser.yml diff --git a/.goreleaser.yml b/.goreleaser.yml new file mode 100644 index 0000000..e77ec5e --- /dev/null +++ b/.goreleaser.yml @@ -0,0 +1,31 @@ +# This is an example goreleaser.yaml file with some sane defaults. +# Make sure to check the documentation at http://goreleaser.com +builds: +- + env: + - CGO_ENABLED=0 + goos: + - linux + - darwin + goarch: + - amd64 + - arm64 + +archive: + replacements: + linux: Linux + amd64: x86_64 + +checksum: + name_template: 'checksums.txt' + +snapshot: + name_template: "{{ .Tag }}-next" + +changelog: + sort: asc + filters: + exclude: + - '^docs:' + - '^test:' + diff --git a/.travis.yml b/.travis.yml index 3c51509..5533892 100644 --- a/.travis.yml +++ b/.travis.yml @@ -22,16 +22,25 @@ script: - docker build -f Dockerfile -t $IMAGE . deploy: +# Build production Docker container - provider: script skip_cleanup: true script: bash scripts/deploy.sh stable on: branch: master +# Build development Docker container - provider: script skip_cleanup: true script: bash scripts/deploy.sh development on: branch: development +# Build release files with goreleaser + - provider: script + skip_cleanup: true + script: curl -sL https://git.io/goreleaser | bash + on: + tags: true + condition: $TRAVIS_OS_NAME = linux notifications: webhooks: From a42b4d90b781db6719077fe9125d26cc1dd010bc Mon Sep 17 00:00:00 2001 From: Felix Breidenstein Date: Mon, 24 Sep 2018 23:28:50 +0200 Subject: [PATCH 15/28] Added logrus This allows for better logging with different levels and colors. --- Gopkg.lock | 61 +++++++++++++++++++++++++++++++++++++++++---- cpthook.yml.example | 3 +++ gitlab.go | 4 ++- gitlab_test.go | 8 +++--- irc.go | 16 ++++++------ main.go | 37 +++++++++++++++++++++------ prometheus.go | 5 ++-- prometheus_test.go | 8 +++--- simple.go | 3 +++ simple_test.go | 6 ++--- status.go | 5 ++-- 11 files changed, 121 insertions(+), 35 deletions(-) diff --git a/Gopkg.lock b/Gopkg.lock index daca7e6..a9b445c 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -2,13 +2,16 @@ [[projects]] + digest = "1:abeb38ade3f32a92943e5be54f55ed6d6e3b6602761d74b4aab4c9dd45c18abd" name = "github.com/fsnotify/fsnotify" packages = ["."] + pruneopts = "UT" revision = "c2828203cd70a50dcccfb2761f8b1f8ceef9a8e9" version = "v1.4.7" [[projects]] branch = "master" + digest = "1:a361611b8c8c75a1091f00027767f7779b29cb37c456a71b8f2604c88057ab40" name = "github.com/hashicorp/hcl" packages = [ ".", @@ -20,74 +23,115 @@ "hcl/token", "json/parser", "json/scanner", - "json/token" + "json/token", ] + pruneopts = "UT" revision = "ef8a98b0bbce4a65b5aa4c368430a80ddc533168" [[projects]] branch = "master" + digest = "1:13cf326ea1042c7c5ea33459016237e6cc1c1840c49b602c072b702fbd995aae" name = "github.com/lrstanley/girc" packages = ["."] + pruneopts = "UT" revision = "102f17f86306c2152a8c6188f9bb8b0e7288de31" [[projects]] + digest = "1:c568d7727aa262c32bdf8a3f7db83614f7af0ed661474b24588de635c20024c7" name = "github.com/magiconair/properties" packages = ["."] + pruneopts = "UT" revision = "c2353362d570a7bfa228149c62842019201cfb71" version = "v1.8.0" [[projects]] branch = "master" + digest = "1:e730597b38a4d56e2361e0b6236cb800e52c73cace2ff91396f4ff35792ddfa7" name = "github.com/mitchellh/mapstructure" packages = ["."] + pruneopts = "UT" revision = "bb74f1db0675b241733089d5a1faa5dd8b0ef57b" [[projects]] + digest = "1:95741de3af260a92cc5c7f3f3061e85273f5a81b5db20d4bd68da74bd521675e" name = "github.com/pelletier/go-toml" packages = ["."] + pruneopts = "UT" revision = "c01d1270ff3e442a8a57cddc1c92dc1138598194" version = "v1.2.0" [[projects]] + digest = "1:d867dfa6751c8d7a435821ad3b736310c2ed68945d05b50fb9d23aee0540c8cc" + name = "github.com/sirupsen/logrus" + packages = ["."] + pruneopts = "UT" + revision = "3e01752db0189b9157070a0e1668a620f9a85da2" + version = "v1.0.6" + +[[projects]] + digest = "1:bd1ae00087d17c5a748660b8e89e1043e1e5479d0fea743352cda2f8dd8c4f84" name = "github.com/spf13/afero" packages = [ ".", - "mem" + "mem", ] + pruneopts = "UT" revision = "787d034dfe70e44075ccc060d346146ef53270ad" version = "v1.1.1" [[projects]] + digest = "1:516e71bed754268937f57d4ecb190e01958452336fa73dbac880894164e91c1f" name = "github.com/spf13/cast" packages = ["."] + pruneopts = "UT" revision = "8965335b8c7107321228e3e3702cab9832751bac" version = "v1.2.0" [[projects]] branch = "master" + digest = "1:080e5f630945ad754f4b920e60b4d3095ba0237ebf88dc462eb28002932e3805" name = "github.com/spf13/jwalterweatherman" packages = ["."] + pruneopts = "UT" revision = "7c0cea34c8ece3fbeb2b27ab9b59511d360fb394" [[projects]] + digest = "1:9424f440bba8f7508b69414634aef3b2b3a877e522d8a4624692412805407bb7" name = "github.com/spf13/pflag" packages = ["."] + pruneopts = "UT" revision = "583c0c0531f06d5278b7d917446061adc344b5cd" version = "v1.0.1" [[projects]] + digest = "1:59e7dceb53b4a1e57eb1eb0bf9951ff0c25912df7660004a789b62b4e8cdca3b" name = "github.com/spf13/viper" packages = ["."] + pruneopts = "UT" revision = "b5e8006cbee93ec955a89ab31e0e3ce3204f3736" version = "v1.0.2" [[projects]] branch = "master" + digest = "1:3f3a05ae0b95893d90b9b3b5afdb79a9b3d96e4e36e099d841ae602e4aca0da8" + name = "golang.org/x/crypto" + packages = ["ssh/terminal"] + pruneopts = "UT" + revision = "0e37d006457bf46f9e6692014ba72ef82c33022c" + +[[projects]] + branch = "master" + digest = "1:50e49f00c462e4531c6987ab12ab81a9a9f76bc0c3235c6e9cf9b75c2b5ff638" name = "golang.org/x/sys" - packages = ["unix"] + packages = [ + "unix", + "windows", + ] + pruneopts = "UT" revision = "7138fd3d9dc8335c567ca206f4333fb75eb05d56" [[projects]] + digest = "1:8029e9743749d4be5bc9f7d42ea1659471767860f0cdc34d37c3111bd308a295" name = "golang.org/x/text" packages = [ "internal/gen", @@ -95,20 +139,27 @@ "internal/ucd", "transform", "unicode/cldr", - "unicode/norm" + "unicode/norm", ] + pruneopts = "UT" revision = "f21a4dfb5e38f5895301dc265a8def02365cc3d0" version = "v0.3.0" [[projects]] + digest = "1:342378ac4dcb378a5448dd723f0784ae519383532f5e70ade24132c4c8693202" name = "gopkg.in/yaml.v2" packages = ["."] + pruneopts = "UT" revision = "5420a8b6744d3b0345ab293f6fcba19c978f1183" version = "v2.2.1" [solve-meta] analyzer-name = "dep" analyzer-version = 1 - inputs-digest = "cd0b9a55c610da3a0418424c508d763f1080f1a63ecf2ef2437735f4d51dc2bb" + input-imports = [ + "github.com/lrstanley/girc", + "github.com/sirupsen/logrus", + "github.com/spf13/viper", + ] solver-name = "gps-cdcl" solver-version = 1 diff --git a/cpthook.yml.example b/cpthook.yml.example index a1cd44b..8b40b2c 100644 --- a/cpthook.yml.example +++ b/cpthook.yml.example @@ -1,6 +1,9 @@ http: listen: ":8086" +logging: + level: "INFO" + irc: host: "irc.hackint.eu" port: 6667 diff --git a/gitlab.go b/gitlab.go index 256ba89..bc92f17 100644 --- a/gitlab.go +++ b/gitlab.go @@ -4,11 +4,12 @@ import ( "bytes" "encoding/json" "fmt" - "log" "net/http" "strings" "text/template" + log "github.com/sirupsen/logrus" + "github.com/spf13/viper" ) @@ -167,6 +168,7 @@ func (m GitlabModule) getHandler() http.HandlerFunc { } return func(wr http.ResponseWriter, req *http.Request) { + log.Debug("Got a request for the GitlabModule") defer req.Body.Close() decoder := json.NewDecoder(req.Body) diff --git a/gitlab_test.go b/gitlab_test.go index 0fa6c46..8972623 100644 --- a/gitlab_test.go +++ b/gitlab_test.go @@ -1,12 +1,13 @@ package main import ( - "fmt" "net/http" "net/http/httptest" "os" "testing" + log "github.com/sirupsen/logrus" + "github.com/spf13/viper" ) @@ -15,13 +16,12 @@ func TestGitlabHandler(t *testing.T) { viper.AddConfigPath(".") err := viper.ReadInConfig() if err != nil { - panic(fmt.Errorf("Fatal error config file: %s", err)) + log.Panic(err) } file, e := os.Open("./tests/gitlab.json") if e != nil { - fmt.Printf("File error: %v\n", e) - os.Exit(1) + log.Fatal(e) } req, err := http.NewRequest("POST", "/", file) diff --git a/irc.go b/irc.go index d7a2426..504a5ae 100644 --- a/irc.go +++ b/irc.go @@ -3,12 +3,12 @@ package main import ( "crypto/tls" "crypto/x509" - "fmt" "io/ioutil" - "log" "strings" "time" + log "github.com/sirupsen/logrus" + "github.com/lrstanley/girc" "github.com/spf13/viper" ) @@ -90,8 +90,8 @@ func ircConnection(config *viper.Viper, channelList []string) { for { if err := client.Connect(); err != nil { - log.Printf("Connection to %s terminated: %s", client.Server(), err) - log.Printf("Reconnecting to %s in 30 seconds...", client.Server()) + log.Warn("Connection to %s terminated: %s", client.Server(), err) + log.Warn("Reconnecting to %s in 30 seconds...", client.Server()) time.Sleep(30 * time.Second) } } @@ -99,10 +99,10 @@ func ircConnection(config *viper.Viper, channelList []string) { } func channelReceiver() { - log.Println("ChannelReceiver started") + log.Info("ChannelReceiver started") for elem := range messageChannel { - fmt.Println("Took IRC event out of channel.") + log.Debug("Took IRC event out of channel.") joinChannel(elem.Channel) for _, message := range elem.Messages { client.Cmd.Message(elem.Channel, message) @@ -116,6 +116,8 @@ func joinChannel(newChannel string) { return } } - fmt.Printf("Need to join new channel %q\n", newChannel) + log.WithFields(log.Fields{ + "channel": newChannel, + }).Debug("Need to join new channel %q\n", newChannel) client.Cmd.Join(newChannel) } diff --git a/main.go b/main.go index 87d13bd..7d0a478 100644 --- a/main.go +++ b/main.go @@ -2,12 +2,12 @@ package main import ( "flag" - "fmt" - "log" "net/http" "path" "strings" + log "github.com/sirupsen/logrus" + "github.com/spf13/viper" ) @@ -27,6 +27,24 @@ type Module interface { var messageChannel = make(chan IRCMessage, 10) +func configureLogLevel() { + if l := viper.GetString("logging.level"); l != "" { + level, err := log.ParseLevel(l) + if err != nil { + log.WithFields(log.Fields{ + "level": l, + }).Fatal("Uknown loglevel defined in configuration.") + } + log.WithFields(log.Fields{ + "level": level, + }).Info("Setting loglevel defined by configuration") + log.SetLevel(level) + return + } + log.Info("Loglevel not defined in configuration. Defaulting to ERROR") + log.SetLevel(log.ErrorLevel) +} + func main() { confDirPtr := flag.String("config", "/etc/cpthook.yml", "Path to the configfile") flag.Parse() @@ -41,15 +59,17 @@ func main() { } err := viper.ReadInConfig() if err != nil { - panic(fmt.Errorf("fatal error config file: %s", err)) + log.Fatal(err) } + configureLogLevel() + var moduleList = viper.Sub("modules") var channelList = []string{} // Status module if moduleList.GetBool("status.enabled") { - log.Println("Status module is active") + log.Info("Status module is active") var statusModule Module = &StatusModule{} statusModule.init(viper.Sub("modules.status")) channelList = append(channelList, statusModule.getChannelList()...) @@ -58,7 +78,7 @@ func main() { // Prometheus module if moduleList.GetBool("prometheus.enabled") { - log.Println("Prometheus module is active") + log.Info("Prometheus module is active") var prometheusModule Module = &PrometheusModule{} prometheusModule.init(viper.Sub("modules.prometheus")) channelList = append(channelList, prometheusModule.getChannelList()...) @@ -67,7 +87,7 @@ func main() { // Gitlab module if moduleList.GetBool("gitlab.enabled") { - log.Println("Gitlab module is active") + log.Info("Gitlab module is active") var gitlabModule Module = &GitlabModule{} gitlabModule.init(viper.Sub("modules.gitlab")) channelList = append(channelList, gitlabModule.getChannelList()...) @@ -76,7 +96,7 @@ func main() { // Simple module if moduleList.GetBool("simple.enabled") { - log.Println("Simple module is active") + log.Info("Simple module is active") var simpleModule Module = &SimpleModule{} simpleModule.init(viper.Sub("modules.simple")) channelList = append(channelList, simpleModule.getChannelList()...) @@ -87,6 +107,9 @@ func main() { go ircConnection(viper.Sub("irc"), channelList) // Start HTTP server + log.WithFields(log.Fields{ + "listen": viper.GetString("http.listen"), + }).Info("Started HTTP Server") log.Fatal(http.ListenAndServe(viper.GetString("http.listen"), nil)) } diff --git a/prometheus.go b/prometheus.go index 8de2f2c..5fd5630 100644 --- a/prometheus.go +++ b/prometheus.go @@ -3,13 +3,14 @@ package main import ( "bytes" "encoding/json" - "log" "net/http" "regexp" "strings" "text/template" "time" + log "github.com/sirupsen/logrus" + "github.com/spf13/viper" ) @@ -119,7 +120,7 @@ func (m PrometheusModule) getHandler() http.HandlerFunc { } return func(w http.ResponseWriter, r *http.Request) { - log.Println("Got http event for /prometheus") + log.Debug("Got a request for the PrometheusModule") defer r.Body.Close() decoder := json.NewDecoder(r.Body) diff --git a/prometheus_test.go b/prometheus_test.go index 3231992..e248f1c 100644 --- a/prometheus_test.go +++ b/prometheus_test.go @@ -1,12 +1,13 @@ package main import ( - "fmt" "net/http" "net/http/httptest" "os" "testing" + log "github.com/sirupsen/logrus" + "github.com/spf13/viper" ) @@ -15,13 +16,12 @@ func TestPrometheusHandler(t *testing.T) { viper.AddConfigPath(".") err := viper.ReadInConfig() if err != nil { - panic(fmt.Errorf("Fatal error config file: %s", err)) + log.Fatal(err) } file, e := os.Open("./tests/prometheus.json") if e != nil { - fmt.Printf("File error: %v\n", e) - os.Exit(1) + log.Fatal(e) } req, err := http.NewRequest("POST", "/", file) diff --git a/simple.go b/simple.go index f864e5d..8fe7435 100644 --- a/simple.go +++ b/simple.go @@ -4,6 +4,8 @@ import ( "bufio" "net/http" + log "github.com/sirupsen/logrus" + "github.com/spf13/viper" ) @@ -26,6 +28,7 @@ func (m SimpleModule) getEndpoint() string { func (m SimpleModule) getHandler() http.HandlerFunc { return func(wr http.ResponseWriter, req *http.Request) { + log.Debug("Got a request for the SimpleModule") defer req.Body.Close() query := req.URL.Query() diff --git a/simple_test.go b/simple_test.go index 9ffeb39..d44666a 100644 --- a/simple_test.go +++ b/simple_test.go @@ -1,12 +1,12 @@ package main import ( - "fmt" "net/http" "net/http/httptest" + "strings" "testing" - "strings" + log "github.com/sirupsen/logrus" "github.com/spf13/viper" ) @@ -16,7 +16,7 @@ func TestSimpleHandler(t *testing.T) { viper.AddConfigPath(".") err := viper.ReadInConfig() if err != nil { - panic(fmt.Errorf("Fatal error config file: %s", err)) + log.Fatal(err) } body := strings.NewReader("Hello, World!") diff --git a/status.go b/status.go index d12715d..bea7dbf 100644 --- a/status.go +++ b/status.go @@ -2,9 +2,10 @@ package main import ( "html/template" - "log" "net/http" + log "github.com/sirupsen/logrus" + "github.com/spf13/viper" ) @@ -22,7 +23,7 @@ func (m StatusModule) getChannelList() []string { func (m StatusModule) getHandler() http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - log.Println("Got http event for /status") + log.Debug("Got a request for the StatusModule") t, _ := template.ParseFiles("templates/status.html") t.Execute(w, nil) } From c52ffa5c1c782105c855d798c8c1478779794ca1 Mon Sep 17 00:00:00 2001 From: Felix Breidenstein Date: Fri, 5 Oct 2018 00:42:31 +0200 Subject: [PATCH 16/28] Better module loading * Move modules into subfolder * Iterate over config to avoid code reuse * Remove status module cause it had no functionality --- .gitignore | 1 + Gopkg.lock | 60 +++++++------ gitlab.go => input/gitlab.go | 26 +++--- gitlab_test.go => input/gitlab_test.go | 4 +- input/helper.go | 21 +++++ prometheus.go => input/prometheus.go | 14 +-- .../prometheus_test.go | 4 +- simple.go => input/simple.go | 14 +-- simple_test.go => input/simple_test.go | 4 +- irc.go | 6 +- main.go | 90 +++++++++---------- status.go | 30 ------- templates/status.html | 16 ---- 13 files changed, 138 insertions(+), 152 deletions(-) rename gitlab.go => input/gitlab.go (97%) rename gitlab_test.go => input/gitlab_test.go (92%) create mode 100644 input/helper.go rename prometheus.go => input/prometheus.go (93%) rename prometheus_test.go => input/prometheus_test.go (91%) rename simple.go => input/simple.go (70%) rename simple_test.go => input/simple_test.go (91%) delete mode 100644 status.go delete mode 100644 templates/status.html diff --git a/.gitignore b/.gitignore index 0a88fb8..fe438ec 100644 --- a/.gitignore +++ b/.gitignore @@ -16,6 +16,7 @@ cpthook.yml .idea/ *.crt vendor/ +.vendor-new/ # Binarys CptHook diff --git a/Gopkg.lock b/Gopkg.lock index a9b445c..db73d28 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -10,8 +10,7 @@ version = "v1.4.7" [[projects]] - branch = "master" - digest = "1:a361611b8c8c75a1091f00027767f7779b29cb37c456a71b8f2604c88057ab40" + digest = "1:c0d19ab64b32ce9fe5cf4ddceba78d5bc9807f0016db6b1183599da3dcc24d10" name = "github.com/hashicorp/hcl" packages = [ ".", @@ -26,15 +25,24 @@ "json/token", ] pruneopts = "UT" - revision = "ef8a98b0bbce4a65b5aa4c368430a80ddc533168" + revision = "8cb6e5b959231cc1119e43259c4a608f9c51a241" + version = "v1.0.0" + +[[projects]] + digest = "1:0a69a1c0db3591fcefb47f115b224592c8dfa4368b7ba9fae509d5e16cdc95c8" + name = "github.com/konsorten/go-windows-terminal-sequences" + packages = ["."] + pruneopts = "UT" + revision = "5c8c8bd35d3832f5d134ae1e1e375b69a4d25242" + version = "v1.0.1" [[projects]] branch = "master" - digest = "1:13cf326ea1042c7c5ea33459016237e6cc1c1840c49b602c072b702fbd995aae" + digest = "1:9a55521f220cd824a256f61daf7e284e1d41871fc21d7ed42ffd069b521a1555" name = "github.com/lrstanley/girc" packages = ["."] pruneopts = "UT" - revision = "102f17f86306c2152a8c6188f9bb8b0e7288de31" + revision = "3ed14e1a7d17523fa75b28f0d95640162dc8c4ef" [[projects]] digest = "1:c568d7727aa262c32bdf8a3f7db83614f7af0ed661474b24588de635c20024c7" @@ -45,12 +53,12 @@ version = "v1.8.0" [[projects]] - branch = "master" - digest = "1:e730597b38a4d56e2361e0b6236cb800e52c73cace2ff91396f4ff35792ddfa7" + digest = "1:53bc4cd4914cd7cd52139990d5170d6dc99067ae31c56530621b18b35fc30318" name = "github.com/mitchellh/mapstructure" packages = ["."] pruneopts = "UT" - revision = "bb74f1db0675b241733089d5a1faa5dd8b0ef57b" + revision = "3536a929edddb9a5b34bd6861dc4a9647cb459fe" + version = "v1.1.2" [[projects]] digest = "1:95741de3af260a92cc5c7f3f3061e85273f5a81b5db20d4bd68da74bd521675e" @@ -61,23 +69,23 @@ version = "v1.2.0" [[projects]] - digest = "1:d867dfa6751c8d7a435821ad3b736310c2ed68945d05b50fb9d23aee0540c8cc" + digest = "1:3f53e9e4dfbb664cd62940c9c4b65a2171c66acd0b7621a1a6b8e78513525a52" name = "github.com/sirupsen/logrus" packages = ["."] pruneopts = "UT" - revision = "3e01752db0189b9157070a0e1668a620f9a85da2" - version = "v1.0.6" + revision = "ad15b42461921f1fb3529b058c6786c6a45d5162" + version = "v1.1.1" [[projects]] - digest = "1:bd1ae00087d17c5a748660b8e89e1043e1e5479d0fea743352cda2f8dd8c4f84" + digest = "1:6a4a11ba764a56d2758899ec6f3848d24698d48442ebce85ee7a3f63284526cd" name = "github.com/spf13/afero" packages = [ ".", "mem", ] pruneopts = "UT" - revision = "787d034dfe70e44075ccc060d346146ef53270ad" - version = "v1.1.1" + revision = "d40851caa0d747393da1ffb28f7f9d8b4eeffebd" + version = "v1.1.2" [[projects]] digest = "1:516e71bed754268937f57d4ecb190e01958452336fa73dbac880894164e91c1f" @@ -88,28 +96,28 @@ version = "v1.2.0" [[projects]] - branch = "master" - digest = "1:080e5f630945ad754f4b920e60b4d3095ba0237ebf88dc462eb28002932e3805" + digest = "1:68ea4e23713989dc20b1bded5d9da2c5f9be14ff9885beef481848edd18c26cb" name = "github.com/spf13/jwalterweatherman" packages = ["."] pruneopts = "UT" - revision = "7c0cea34c8ece3fbeb2b27ab9b59511d360fb394" + revision = "4a4406e478ca629068e7768fc33f3f044173c0a6" + version = "v1.0.0" [[projects]] - digest = "1:9424f440bba8f7508b69414634aef3b2b3a877e522d8a4624692412805407bb7" + digest = "1:c1b1102241e7f645bc8e0c22ae352e8f0dc6484b6cb4d132fa9f24174e0119e2" name = "github.com/spf13/pflag" packages = ["."] pruneopts = "UT" - revision = "583c0c0531f06d5278b7d917446061adc344b5cd" - version = "v1.0.1" + revision = "298182f68c66c05229eb03ac171abe6e309ee79a" + version = "v1.0.3" [[projects]] - digest = "1:59e7dceb53b4a1e57eb1eb0bf9951ff0c25912df7660004a789b62b4e8cdca3b" + digest = "1:214775c11fd26da94a100111a62daa25339198a4f9c57cb4aab352da889f5b93" name = "github.com/spf13/viper" packages = ["."] pruneopts = "UT" - revision = "b5e8006cbee93ec955a89ab31e0e3ce3204f3736" - version = "v1.0.2" + revision = "2c12c60302a5a0e62ee102ca9bc996277c2f64f5" + version = "v1.2.1" [[projects]] branch = "master" @@ -117,18 +125,18 @@ name = "golang.org/x/crypto" packages = ["ssh/terminal"] pruneopts = "UT" - revision = "0e37d006457bf46f9e6692014ba72ef82c33022c" + revision = "0c41d7ab0a0ee717d4590a44bcb987dfd9e183eb" [[projects]] branch = "master" - digest = "1:50e49f00c462e4531c6987ab12ab81a9a9f76bc0c3235c6e9cf9b75c2b5ff638" + digest = "1:56e0fb6e56fa651d9183abd2db5f29d05da203a7c50bf9b331259e5087aca683" name = "golang.org/x/sys" packages = [ "unix", "windows", ] pruneopts = "UT" - revision = "7138fd3d9dc8335c567ca206f4333fb75eb05d56" + revision = "8e24a49d80f82323e1c4db1b5da3e0f31171a151" [[projects]] digest = "1:8029e9743749d4be5bc9f7d42ea1659471767860f0cdc34d37c3111bd308a295" diff --git a/gitlab.go b/input/gitlab.go similarity index 97% rename from gitlab.go rename to input/gitlab.go index bc92f17..e331968 100644 --- a/gitlab.go +++ b/input/gitlab.go @@ -1,4 +1,4 @@ -package main +package input import ( "bytes" @@ -15,6 +15,7 @@ import ( type GitlabModule struct { channelMapping mapping + channel chan IRCMessage } type mapping struct { @@ -32,6 +33,14 @@ func contains(mapping map[string][]string, entry string) bool { return false } +func (m *GitlabModule) Init(c *viper.Viper, channel *chan IRCMessage) { + err := c.Unmarshal(&m.channelMapping) + if err != nil { + log.Fatal("Failed to unmarshal channelmapping into struct") + } + m.channel = *channel +} + func (m GitlabModule) sendMessage(message string, projectName string, namespace string) { var channelNames []string var fullProjectName = namespace + "/" + projectName @@ -52,19 +61,12 @@ func (m GitlabModule) sendMessage(message string, projectName string, namespace var event IRCMessage event.Messages = append(event.Messages, message) event.Channel = channelName - messageChannel <- event + m.channel <- event } } -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 { +func (m GitlabModule) GetChannelList() []string { var all []string for _, v := range m.channelMapping.ExplicitMappings { @@ -82,11 +84,11 @@ func (m GitlabModule) getChannelList() []string { return all } -func (m GitlabModule) getEndpoint() string { +func (m GitlabModule) GetEndpoint() string { return "/gitlab" } -func (m GitlabModule) getHandler() 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 }}" diff --git a/gitlab_test.go b/input/gitlab_test.go similarity index 92% rename from gitlab_test.go rename to input/gitlab_test.go index 8972623..a3a85a6 100644 --- a/gitlab_test.go +++ b/input/gitlab_test.go @@ -1,4 +1,4 @@ -package main +package input import ( "net/http" @@ -33,7 +33,7 @@ func TestGitlabHandler(t *testing.T) { rr := httptest.NewRecorder() var gitlabModule Module = &GitlabModule{} - gitlabModule.init(viper.Sub("modules.gitlab")) + gitlabModule.init(viper.Sub("modules.gitlab"), nil) handler := http.HandlerFunc(gitlabModule.getHandler()) handler.ServeHTTP(rr, req) diff --git a/input/helper.go b/input/helper.go new file mode 100644 index 0000000..7096b27 --- /dev/null +++ b/input/helper.go @@ -0,0 +1,21 @@ +package input + +import ( + "net/http" + + "github.com/spf13/viper" +) + +// Module defines a common interface for all CptHook modules +type Module interface { + Init(c *viper.Viper, channel *chan IRCMessage) + GetChannelList() []string + GetEndpoint() string + GetHandler() http.HandlerFunc +} + +// IRCMessage are send over the inputChannel from the different modules +type IRCMessage struct { + Messages []string + Channel string +} diff --git a/prometheus.go b/input/prometheus.go similarity index 93% rename from prometheus.go rename to input/prometheus.go index 5fd5630..1ca8373 100644 --- a/prometheus.go +++ b/input/prometheus.go @@ -1,4 +1,4 @@ -package main +package input import ( "bytes" @@ -16,6 +16,7 @@ import ( type PrometheusModule struct { defaultChannel string + channel chan IRCMessage hostnameFilter string } @@ -85,20 +86,21 @@ func shortenInstanceName(name string, pattern string) string { return name } -func (m PrometheusModule) getEndpoint() string { +func (m PrometheusModule) GetEndpoint() string { return "/prometheus" } -func (m PrometheusModule) getChannelList() []string { +func (m PrometheusModule) GetChannelList() []string { return []string{m.defaultChannel} } -func (m *PrometheusModule) init(c *viper.Viper) { +func (m *PrometheusModule) Init(c *viper.Viper, channel *chan IRCMessage) { m.defaultChannel = c.GetString("channel") + m.channel = *channel m.hostnameFilter = c.GetString("hostname_filter") } -func (m PrometheusModule) getHandler() http.HandlerFunc { +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}}" @@ -186,7 +188,7 @@ func (m PrometheusModule) getHandler() http.HandlerFunc { _ = hostListTemplate.Execute(&buf, &instanceList) event.Messages = append(event.Messages, buf.String()) event.Channel = m.defaultChannel - messageChannel <- event + m.channel <- event } } } diff --git a/prometheus_test.go b/input/prometheus_test.go similarity index 91% rename from prometheus_test.go rename to input/prometheus_test.go index e248f1c..9f57ca8 100644 --- a/prometheus_test.go +++ b/input/prometheus_test.go @@ -1,4 +1,4 @@ -package main +package input import ( "net/http" @@ -32,7 +32,7 @@ func TestPrometheusHandler(t *testing.T) { rr := httptest.NewRecorder() var prometheusModule Module = &PrometheusModule{} - prometheusModule.init(viper.Sub("modules.prometheus")) + prometheusModule.init(viper.Sub("modules.prometheus"), nil) handler := http.HandlerFunc(prometheusModule.getHandler()) handler.ServeHTTP(rr, req) diff --git a/simple.go b/input/simple.go similarity index 70% rename from simple.go rename to input/simple.go index 8fe7435..e7f76cd 100644 --- a/simple.go +++ b/input/simple.go @@ -1,4 +1,4 @@ -package main +package input import ( "bufio" @@ -11,21 +11,23 @@ import ( type SimpleModule struct { defaultChannel string + channel chan IRCMessage } -func (m *SimpleModule) init(c *viper.Viper) { +func (m *SimpleModule) Init(c *viper.Viper, channel *chan IRCMessage) { m.defaultChannel = c.GetString("default_channel") + m.channel = *channel } -func (m SimpleModule) getChannelList() []string { +func (m SimpleModule) GetChannelList() []string { return []string{m.defaultChannel} } -func (m SimpleModule) getEndpoint() string { +func (m SimpleModule) GetEndpoint() string { return "/simple" } -func (m SimpleModule) getHandler() http.HandlerFunc { +func (m SimpleModule) GetHandler() http.HandlerFunc { return func(wr http.ResponseWriter, req *http.Request) { log.Debug("Got a request for the SimpleModule") @@ -47,7 +49,7 @@ func (m SimpleModule) getHandler() http.HandlerFunc { } // Send message - messageChannel <- IRCMessage{ + m.channel <- IRCMessage{ Messages: lines, Channel: channel, } diff --git a/simple_test.go b/input/simple_test.go similarity index 91% rename from simple_test.go rename to input/simple_test.go index d44666a..4bd9413 100644 --- a/simple_test.go +++ b/input/simple_test.go @@ -1,4 +1,4 @@ -package main +package input import ( "net/http" @@ -28,7 +28,7 @@ func TestSimpleHandler(t *testing.T) { rr := httptest.NewRecorder() var simpleModule Module = &SimpleModule{} - simpleModule.init(viper.Sub("modules.simple")) + simpleModule.init(viper.Sub("modules.simple"), nil) handler := http.HandlerFunc(simpleModule.getHandler()) handler.ServeHTTP(rr, req) diff --git a/irc.go b/irc.go index 504a5ae..acf5790 100644 --- a/irc.go +++ b/irc.go @@ -101,7 +101,7 @@ func ircConnection(config *viper.Viper, channelList []string) { func channelReceiver() { log.Info("ChannelReceiver started") - for elem := range messageChannel { + for elem := range inputChannel { log.Debug("Took IRC event out of channel.") joinChannel(elem.Channel) for _, message := range elem.Messages { @@ -116,8 +116,10 @@ func joinChannel(newChannel string) { return } } + log.WithFields(log.Fields{ "channel": newChannel, - }).Debug("Need to join new channel %q\n", newChannel) + }).Debug("Need to join new channel") + client.Cmd.Join(newChannel) } diff --git a/main.go b/main.go index 7d0a478..b58a492 100644 --- a/main.go +++ b/main.go @@ -2,30 +2,18 @@ package main import ( "flag" + "fmt" "net/http" "path" "strings" log "github.com/sirupsen/logrus" + "github.com/fleaz/CptHook/input" "github.com/spf13/viper" ) -// IRCMessage are send over the messageChannel from the different modules -type IRCMessage struct { - Messages []string - Channel string -} - -// Module defines a common interface for all CptHook modules -type Module interface { - init(c *viper.Viper) - getChannelList() []string - getEndpoint() string - getHandler() http.HandlerFunc -} - -var messageChannel = make(chan IRCMessage, 10) +var inputChannel = make(chan input.IRCMessage, 10) func configureLogLevel() { if l := viper.GetString("logging.level"); l != "" { @@ -45,6 +33,31 @@ func configureLogLevel() { log.SetLevel(log.ErrorLevel) } +type Configuration struct { + Modules map[string]InputModule `yaml:"modules"` +} + +type InputModule struct { + Enalbed string `yaml:"enabled"` +} + +func createModuleObject(name string) (input.Module, error) { + var m input.Module + var e error + switch name { + case "gitlab": + m = &input.GitlabModule{} + case "prometheus": + m = &input.PrometheusModule{} + case "simple": + m = &input.SimpleModule{} + default: + e = fmt.Errorf("found configuration for unknown module: %q", name) + } + + return m, e +} + func main() { confDirPtr := flag.String("config", "/etc/cpthook.yml", "Path to the configfile") flag.Parse() @@ -61,46 +74,27 @@ func main() { if err != nil { log.Fatal(err) } - configureLogLevel() - var moduleList = viper.Sub("modules") var channelList = []string{} + var configurtaion = Configuration{} - // Status module - if moduleList.GetBool("status.enabled") { - log.Info("Status module is active") - var statusModule Module = &StatusModule{} - 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.Info("Prometheus module is active") - var prometheusModule Module = &PrometheusModule{} - prometheusModule.init(viper.Sub("modules.prometheus")) - channelList = append(channelList, prometheusModule.getChannelList()...) - http.HandleFunc(prometheusModule.getEndpoint(), prometheusModule.getHandler()) + err = viper.Unmarshal(&configurtaion) + if err != nil { + log.Fatal(err) } - // Gitlab module - if moduleList.GetBool("gitlab.enabled") { - log.Info("Gitlab module is active") - var gitlabModule Module = &GitlabModule{} - gitlabModule.init(viper.Sub("modules.gitlab")) - channelList = append(channelList, gitlabModule.getChannelList()...) - http.HandleFunc(gitlabModule.getEndpoint(), gitlabModule.getHandler()) - } + for moduleName := range configurtaion.Modules { + module, err := createModuleObject(moduleName) + if err != nil { + log.Error(err) + } + log.Infof("Loaded module %q", moduleName) + configPath := fmt.Sprintf("modules.%s", moduleName) + module.Init(viper.Sub(configPath), &inputChannel) + channelList = append(channelList, module.GetChannelList()...) + http.HandleFunc(module.GetEndpoint(), module.GetHandler()) - // Simple module - if moduleList.GetBool("simple.enabled") { - log.Info("Simple module is active") - var simpleModule Module = &SimpleModule{} - simpleModule.init(viper.Sub("modules.simple")) - channelList = append(channelList, simpleModule.getChannelList()...) - http.HandleFunc(simpleModule.getEndpoint(), simpleModule.getHandler()) } // Start IRC connection diff --git a/status.go b/status.go deleted file mode 100644 index bea7dbf..0000000 --- a/status.go +++ /dev/null @@ -1,30 +0,0 @@ -package main - -import ( - "html/template" - "net/http" - - log "github.com/sirupsen/logrus" - - "github.com/spf13/viper" -) - -type StatusModule struct{} - -func (m *StatusModule) init(c *viper.Viper) {} - -func (m StatusModule) getEndpoint() string { - return "/status" -} - -func (m StatusModule) getChannelList() []string { - return []string{} -} - -func (m StatusModule) getHandler() http.HandlerFunc { - return func(w http.ResponseWriter, r *http.Request) { - log.Debug("Got a request for the StatusModule") - t, _ := template.ParseFiles("templates/status.html") - t.Execute(w, nil) - } -} diff --git a/templates/status.html b/templates/status.html deleted file mode 100644 index 658fb32..0000000 --- a/templates/status.html +++ /dev/null @@ -1,16 +0,0 @@ - - - - - - - Webhook-GW Status - - - - - -

Hi

- Hello {{ .Name }} - - \ No newline at end of file From c352b4000d7fc22a138b72316940d99560105493 Mon Sep 17 00:00:00 2001 From: Felix Breidenstein Date: Sat, 3 Nov 2018 23:06:52 +0100 Subject: [PATCH 17/28] Remove deleted templates folder from Dockerfile --- Dockerfile | 1 - 1 file changed, 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 10a3441..784f9ae 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,7 +2,6 @@ FROM alpine LABEL maintainer mail@fleaz.me RUN apk add --no-cache ca-certificates -COPY templates/ /templates COPY CptHook / EXPOSE 8086 CMD ["/CptHook"] From 9221101c0653ae32ebe6f7dfce8670fd0ce7bbca Mon Sep 17 00:00:00 2001 From: Felix Breidenstein Date: Sat, 3 Nov 2018 23:09:09 +0100 Subject: [PATCH 18/28] Fix logging in irc.go --- irc.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/irc.go b/irc.go index acf5790..21e2947 100644 --- a/irc.go +++ b/irc.go @@ -90,8 +90,8 @@ func ircConnection(config *viper.Viper, channelList []string) { for { if err := client.Connect(); err != nil { - log.Warn("Connection to %s terminated: %s", client.Server(), err) - log.Warn("Reconnecting to %s in 30 seconds...", client.Server()) + log.Warnf("Connection to %s terminated: %s", client.Server(), err) + log.Warn("Reconnecting to in 30 seconds...") time.Sleep(30 * time.Second) } } From 0eb124220200f833ab03a89d035e464afdfae747 Mon Sep 17 00:00:00 2001 From: Felix Breidenstein Date: Sat, 3 Nov 2018 23:09:28 +0100 Subject: [PATCH 19/28] Correctly call all tests in subfolders --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 5533892..18ee653 100644 --- a/.travis.yml +++ b/.travis.yml @@ -17,7 +17,7 @@ before_install: script: - cp cpthook.yml.example cpthook.yml - - go test + - go test ./... - CGO_ENABLED="0" GOARCH="amd64" GOOS="linux" go build -a -installsuffix cgo -o CptHook - docker build -f Dockerfile -t $IMAGE . From 124d96b82a7c28a1c544783d02748ec58c08fd60 Mon Sep 17 00:00:00 2001 From: Felix Breidenstein Date: Sun, 4 Nov 2018 00:47:52 +0100 Subject: [PATCH 20/28] Moved testdata into subfolder --- {tests => input/test_data}/gitlab.json | 0 {tests => input/test_data}/prometheus.json | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename {tests => input/test_data}/gitlab.json (100%) rename {tests => input/test_data}/prometheus.json (100%) diff --git a/tests/gitlab.json b/input/test_data/gitlab.json similarity index 100% rename from tests/gitlab.json rename to input/test_data/gitlab.json diff --git a/tests/prometheus.json b/input/test_data/prometheus.json similarity index 100% rename from tests/prometheus.json rename to input/test_data/prometheus.json From 1a015ecd95162bbf3a22c1d03e0fc4b86358fc49 Mon Sep 17 00:00:00 2001 From: Felix Breidenstein Date: Sun, 4 Nov 2018 00:50:28 +0100 Subject: [PATCH 21/28] Correctly load config file from parent dir --- input/gitlab_test.go | 2 +- input/prometheus_test.go | 2 +- input/simple_test.go | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/input/gitlab_test.go b/input/gitlab_test.go index a3a85a6..896fb70 100644 --- a/input/gitlab_test.go +++ b/input/gitlab_test.go @@ -13,7 +13,7 @@ import ( func TestGitlabHandler(t *testing.T) { viper.SetConfigName("cpthook") - viper.AddConfigPath(".") + viper.AddConfigPath("../") err := viper.ReadInConfig() if err != nil { log.Panic(err) diff --git a/input/prometheus_test.go b/input/prometheus_test.go index 9f57ca8..4323084 100644 --- a/input/prometheus_test.go +++ b/input/prometheus_test.go @@ -13,7 +13,7 @@ import ( func TestPrometheusHandler(t *testing.T) { viper.SetConfigName("cpthook") - viper.AddConfigPath(".") + viper.AddConfigPath("../") err := viper.ReadInConfig() if err != nil { log.Fatal(err) diff --git a/input/simple_test.go b/input/simple_test.go index 4bd9413..06336d9 100644 --- a/input/simple_test.go +++ b/input/simple_test.go @@ -13,7 +13,7 @@ import ( func TestSimpleHandler(t *testing.T) { viper.SetConfigName("cpthook") - viper.AddConfigPath(".") + viper.AddConfigPath("../") err := viper.ReadInConfig() if err != nil { log.Fatal(err) From 6d2cf42b8ec1b08a12855f2c6bd1346478811b3d Mon Sep 17 00:00:00 2001 From: Felix Breidenstein Date: Sun, 4 Nov 2018 00:51:10 +0100 Subject: [PATCH 22/28] Fix function names and new test file dir --- input/gitlab_test.go | 7 ++++--- input/prometheus_test.go | 7 ++++--- input/simple_test.go | 4 ++-- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/input/gitlab_test.go b/input/gitlab_test.go index 896fb70..78ba1bc 100644 --- a/input/gitlab_test.go +++ b/input/gitlab_test.go @@ -19,7 +19,7 @@ func TestGitlabHandler(t *testing.T) { log.Panic(err) } - file, e := os.Open("./tests/gitlab.json") + file, e := os.Open("./test_data/gitlab.json") if e != nil { log.Fatal(e) } @@ -33,8 +33,9 @@ func TestGitlabHandler(t *testing.T) { rr := httptest.NewRecorder() var gitlabModule Module = &GitlabModule{} - gitlabModule.init(viper.Sub("modules.gitlab"), nil) - handler := http.HandlerFunc(gitlabModule.getHandler()) + c := make(chan IRCMessage, 1) + gitlabModule.Init(viper.Sub("modules.gitlab"), &c) + handler := http.HandlerFunc(gitlabModule.GetHandler()) handler.ServeHTTP(rr, req) diff --git a/input/prometheus_test.go b/input/prometheus_test.go index 4323084..adec168 100644 --- a/input/prometheus_test.go +++ b/input/prometheus_test.go @@ -19,7 +19,7 @@ func TestPrometheusHandler(t *testing.T) { log.Fatal(err) } - file, e := os.Open("./tests/prometheus.json") + file, e := os.Open("./test_data/prometheus.json") if e != nil { log.Fatal(e) } @@ -32,8 +32,9 @@ func TestPrometheusHandler(t *testing.T) { rr := httptest.NewRecorder() var prometheusModule Module = &PrometheusModule{} - prometheusModule.init(viper.Sub("modules.prometheus"), nil) - handler := http.HandlerFunc(prometheusModule.getHandler()) + c := make(chan IRCMessage, 1) + prometheusModule.Init(viper.Sub("modules.prometheus"), &c) + handler := http.HandlerFunc(prometheusModule.GetHandler()) handler.ServeHTTP(rr, req) diff --git a/input/simple_test.go b/input/simple_test.go index 06336d9..611f079 100644 --- a/input/simple_test.go +++ b/input/simple_test.go @@ -28,8 +28,8 @@ func TestSimpleHandler(t *testing.T) { rr := httptest.NewRecorder() var simpleModule Module = &SimpleModule{} - simpleModule.init(viper.Sub("modules.simple"), nil) - handler := http.HandlerFunc(simpleModule.getHandler()) + simpleModule.Init(viper.Sub("modules.simple"), nil) + handler := http.HandlerFunc(simpleModule.GetHandler()) handler.ServeHTTP(rr, req) From c9c42b8bfc019774f4c59529a8a3badd99642559 Mon Sep 17 00:00:00 2001 From: Felix Breidenstein Date: Sun, 4 Nov 2018 00:58:56 +0100 Subject: [PATCH 23/28] Cleanup notifications from travisci --- .travis.yml | 9 --------- 1 file changed, 9 deletions(-) diff --git a/.travis.yml b/.travis.yml index 5533892..1b0c04d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -43,13 +43,4 @@ deploy: condition: $TRAVIS_OS_NAME = linux notifications: - webhooks: - urls: - - http://webhooks.rainbownerds.de:12345 - on_success: always # default: always - on_failure: always # default: always - on_start: always # default: never - on_cancel: always # default: always - on_error: always # default: always - email: false From 4da46f7b1b870159f5fc590ebbb541d4b82c79b9 Mon Sep 17 00:00:00 2001 From: Felix Breidenstein Date: Tue, 6 Nov 2018 23:24:29 +0100 Subject: [PATCH 24/28] Include exampleconfig into release archives This fixes #22 --- .goreleaser.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.goreleaser.yml b/.goreleaser.yml index e77ec5e..f6386ae 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -15,6 +15,9 @@ archive: replacements: linux: Linux amd64: x86_64 + files: + - LICENSE + - cpthook.yml.example checksum: name_template: 'checksums.txt' From 605171a82c25ffa2c11dac73024a768feac9dcfb Mon Sep 17 00:00:00 2001 From: Felix Breidenstein Date: Tue, 6 Nov 2018 23:39:02 +0100 Subject: [PATCH 25/28] Build tagged container. Fixes #23 --- .travis.yml | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/.travis.yml b/.travis.yml index 1b0c04d..26f02e3 100644 --- a/.travis.yml +++ b/.travis.yml @@ -28,6 +28,13 @@ deploy: script: bash scripts/deploy.sh stable on: branch: master +# Build tagged Docker container + - provider: script + skip_cleanup: true + script: bash scripts/deploy.sh $TARVIS_TAG + on: + tags: true + branch: master # Build development Docker container - provider: script skip_cleanup: true From baee9ebf4bc820680e2b27d7270f7a9d52fcd51a Mon Sep 17 00:00:00 2001 From: Felix Breidenstein Date: Tue, 20 Nov 2018 17:30:39 +0100 Subject: [PATCH 26/28] Give the tests a larger channel buffer During testing we don't have a consumer on the other side of the channel so it will block when multiple messages are generated and the tests run in a timeout. --- input/gitlab_test.go | 2 +- input/prometheus_test.go | 2 +- input/simple_test.go | 3 ++- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/input/gitlab_test.go b/input/gitlab_test.go index 78ba1bc..2a8e3d8 100644 --- a/input/gitlab_test.go +++ b/input/gitlab_test.go @@ -33,7 +33,7 @@ func TestGitlabHandler(t *testing.T) { rr := httptest.NewRecorder() var gitlabModule Module = &GitlabModule{} - c := make(chan IRCMessage, 1) + c := make(chan IRCMessage, 10) gitlabModule.Init(viper.Sub("modules.gitlab"), &c) handler := http.HandlerFunc(gitlabModule.GetHandler()) diff --git a/input/prometheus_test.go b/input/prometheus_test.go index adec168..c43fd6f 100644 --- a/input/prometheus_test.go +++ b/input/prometheus_test.go @@ -32,7 +32,7 @@ func TestPrometheusHandler(t *testing.T) { rr := httptest.NewRecorder() var prometheusModule Module = &PrometheusModule{} - c := make(chan IRCMessage, 1) + c := make(chan IRCMessage, 10) prometheusModule.Init(viper.Sub("modules.prometheus"), &c) handler := http.HandlerFunc(prometheusModule.GetHandler()) diff --git a/input/simple_test.go b/input/simple_test.go index 611f079..9005dfc 100644 --- a/input/simple_test.go +++ b/input/simple_test.go @@ -28,7 +28,8 @@ func TestSimpleHandler(t *testing.T) { rr := httptest.NewRecorder() var simpleModule Module = &SimpleModule{} - simpleModule.Init(viper.Sub("modules.simple"), nil) + c := make(chan IRCMessage, 10) + simpleModule.Init(viper.Sub("modules.simple"), &c) handler := http.HandlerFunc(simpleModule.GetHandler()) handler.ServeHTTP(rr, req) From aeb9d920f9cc19703c01f07d18b717c86434b751 Mon Sep 17 00:00:00 2001 From: Felix Breidenstein Date: Tue, 20 Nov 2018 17:38:45 +0100 Subject: [PATCH 27/28] Fix typo in .travis.yml --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 18ecdb1..1fa6cc1 100644 --- a/.travis.yml +++ b/.travis.yml @@ -31,7 +31,7 @@ deploy: # Build tagged Docker container - provider: script skip_cleanup: true - script: bash scripts/deploy.sh $TARVIS_TAG + script: bash scripts/deploy.sh $TRAVIS_TAG on: tags: true branch: master From 2b84cd42a383cfddce7207da9b9e12b7ef4d4f66 Mon Sep 17 00:00:00 2001 From: Felix Breidenstein Date: Thu, 22 Nov 2018 09:50:04 +0100 Subject: [PATCH 28/28] Updated all dependencys --- Gopkg.lock | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/Gopkg.lock b/Gopkg.lock index db73d28..cb03980 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -38,11 +38,11 @@ [[projects]] branch = "master" - digest = "1:9a55521f220cd824a256f61daf7e284e1d41871fc21d7ed42ffd069b521a1555" + digest = "1:21fa85390115cb08fb3cfee3b016525e704fff7e0d361b8d943c4c55842c8f90" name = "github.com/lrstanley/girc" packages = ["."] pruneopts = "UT" - revision = "3ed14e1a7d17523fa75b28f0d95640162dc8c4ef" + revision = "3aee8c249519e71856c3aadd1749c0455c822626" [[projects]] digest = "1:c568d7727aa262c32bdf8a3f7db83614f7af0ed661474b24588de635c20024c7" @@ -69,12 +69,12 @@ version = "v1.2.0" [[projects]] - digest = "1:3f53e9e4dfbb664cd62940c9c4b65a2171c66acd0b7621a1a6b8e78513525a52" + digest = "1:69b1cc331fca23d702bd72f860c6a647afd0aa9fcbc1d0659b1365e26546dd70" name = "github.com/sirupsen/logrus" packages = ["."] pruneopts = "UT" - revision = "ad15b42461921f1fb3529b058c6786c6a45d5162" - version = "v1.1.1" + revision = "bcd833dfe83d3cebad139e4a29ed79cb2318bf95" + version = "v1.2.0" [[projects]] digest = "1:6a4a11ba764a56d2758899ec6f3848d24698d48442ebce85ee7a3f63284526cd" @@ -88,12 +88,12 @@ version = "v1.1.2" [[projects]] - digest = "1:516e71bed754268937f57d4ecb190e01958452336fa73dbac880894164e91c1f" + digest = "1:08d65904057412fc0270fc4812a1c90c594186819243160dc779a402d4b6d0bc" name = "github.com/spf13/cast" packages = ["."] pruneopts = "UT" - revision = "8965335b8c7107321228e3e3702cab9832751bac" - version = "v1.2.0" + revision = "8c9545af88b134710ab1cd196795e7f2388358d7" + version = "v1.3.0" [[projects]] digest = "1:68ea4e23713989dc20b1bded5d9da2c5f9be14ff9885beef481848edd18c26cb" @@ -125,18 +125,18 @@ name = "golang.org/x/crypto" packages = ["ssh/terminal"] pruneopts = "UT" - revision = "0c41d7ab0a0ee717d4590a44bcb987dfd9e183eb" + revision = "3d3f9f413869b949e48070b5bc593aa22cc2b8f2" [[projects]] branch = "master" - digest = "1:56e0fb6e56fa651d9183abd2db5f29d05da203a7c50bf9b331259e5087aca683" + digest = "1:f343f077a5b0bc3a3788b3a04e24dd417e3e25b2acb529c413e212d2c42416ef" name = "golang.org/x/sys" packages = [ "unix", "windows", ] pruneopts = "UT" - revision = "8e24a49d80f82323e1c4db1b5da3e0f31171a151" + revision = "0cf1ed9e522b7dbb416f080a5c8003de9b702bf4" [[projects]] digest = "1:8029e9743749d4be5bc9f7d42ea1659471767860f0cdc34d37c3111bd308a295"