Skip to content

Commit

Permalink
Moved all root files to separate packages. (#27)
Browse files Browse the repository at this point in the history
Signed-off-by: Bartek Plotka <bwplotka@gmail.com>
  • Loading branch information
bwplotka authored Mar 19, 2019
1 parent 243f19e commit efb01aa
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 42 deletions.
4 changes: 2 additions & 2 deletions cmd/jiralert/content.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"html/template"
"net/http"

"github.com/free/jiralert"
"github.com/free/jiralert/pkg/config"
)

const (
Expand Down Expand Up @@ -93,7 +93,7 @@ func HomeHandlerFunc() func(http.ResponseWriter, *http.Request) {
}

// ConfigHandlerFunc is the HTTP handler for the `/config` page. It outputs the configuration marshaled in YAML format.
func ConfigHandlerFunc(config *jiralert.Config) func(http.ResponseWriter, *http.Request) {
func ConfigHandlerFunc(config *config.Config) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
configTemplate.Execute(w, &tdata{
DocsUrl: docsUrl,
Expand Down
13 changes: 8 additions & 5 deletions cmd/jiralert/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@ import (
"runtime"
"strconv"

"github.com/free/jiralert/pkg/alertmanager"
"github.com/free/jiralert/pkg/config"
"github.com/free/jiralert/pkg/notify"
"github.com/free/jiralert/pkg/template"

_ "net/http/pprof"

"github.com/free/jiralert"
"github.com/free/jiralert/alertmanager"
log "github.com/golang/glog"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
Expand Down Expand Up @@ -44,12 +47,12 @@ func main() {

log.Infof("Starting JIRAlert version %s", Version)

config, _, err := jiralert.LoadConfigFile(*configFile)
config, _, err := config.LoadFile(*configFile)
if err != nil {
log.Fatalf("Error loading configuration: %s", err)
}

tmpl, err := jiralert.LoadTemplate(config.Template)
tmpl, err := template.LoadTemplate(config.Template)
if err != nil {
log.Fatalf("Error loading templates from %s: %s", config.Template, err)
}
Expand Down Expand Up @@ -80,7 +83,7 @@ func main() {
}

if len(data.Alerts) > 0 {
r, err := jiralert.NewReceiver(conf, tmpl)
r, err := notify.NewReceiver(conf, tmpl)
if err != nil {
errorHandler(w, http.StatusInternalServerError, err, conf.Name, &data)
return
Expand Down
File renamed without changes.
File renamed without changes.
7 changes: 2 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
module jiralert
module github.com/free/jiralert

require (
github.com/BurntSushi/toml v0.3.1 // indirect
github.com/andygrunwald/go-jira v0.0.0-20181018203616-bbce4afa5493
github.com/beorn7/perks v0.0.0-20160804104726-4c0e84591b9a // indirect
github.com/fatih/structs v0.0.0-20171020064819-f5faa72e7309 // indirect
github.com/free/jiralert v0.0.0-20190308154351-21520699b386
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b
github.com/golang/protobuf v0.0.0-20171021043952-1643683e1b54 // indirect
github.com/google/go-querystring v0.0.0-20170111101155-53e6ce116135 // indirect
Expand All @@ -18,8 +16,7 @@ require (
github.com/prometheus/procfs v0.0.0-20171017214025-a6e9df898b13 // indirect
github.com/trivago/tgo v1.0.1
golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6 // indirect
golang.org/x/tools v0.0.0-20190308174544-00c44ba9c14f // indirect
golang.org/x/tools v0.0.0-20190315214010-f0bfdbff1f9c // indirect
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
gopkg.in/yaml.v2 v2.0.0-20170812160011-eb3733d160e7
honnef.co/go/tools v0.0.0-20190215041234-466a0476246c // indirect
)
17 changes: 6 additions & 11 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/andygrunwald/go-jira v0.0.0-20181018203616-bbce4afa5493 h1:7bKcEt+LMTt7yH4jBzmbxdAKnmi8khplBu0dcZJBRxI=
github.com/andygrunwald/go-jira v0.0.0-20181018203616-bbce4afa5493/go.mod h1:yNYQrX3nGSrVdcVsM2mWz2pm7tTeDtYfRyVEkc3VUiY=
github.com/beorn7/perks v0.0.0-20160804104726-4c0e84591b9a h1:BtpsbiV638WQZwhA98cEZw2BsbnQJrbd0BI7tsy0W1c=
github.com/beorn7/perks v0.0.0-20160804104726-4c0e84591b9a/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
github.com/fatih/structs v0.0.0-20171020064819-f5faa72e7309 h1:yetGKN1jYaaVt+q69KPz+V2Z64OyTw/KfTNQS90n/tU=
github.com/fatih/structs v0.0.0-20171020064819-f5faa72e7309/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga6PJ7M=
github.com/free/jiralert v0.0.0-20190308154351-21520699b386 h1:7xAiWhlM8DcNRq6jJCTQRCgI9vhRIEvCjOPlDoVJqR8=
github.com/free/jiralert v0.0.0-20190308154351-21520699b386/go.mod h1:MQhvb9wm5+MJhEO+XFg4TcfUr1sLQMjMxtcudFsYOvQ=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b h1:VKtxabqXZkF25pY9ekfRL6a582T4P37/31XEstQ5p58=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
github.com/golang/protobuf v0.0.0-20171021043952-1643683e1b54 h1:nRNJXiJvemchkOTn0V4U11TZkvacB94gTzbTZbSA7Rw=
Expand All @@ -33,16 +29,15 @@ github.com/prometheus/procfs v0.0.0-20171017214025-a6e9df898b13 h1:leRfx9kcgnSDk
github.com/prometheus/procfs v0.0.0-20171017214025-a6e9df898b13/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=
github.com/trivago/tgo v1.0.1 h1:bxatjJIXNIpV18bucU4Uk/LaoxvxuOlp/oowRHyncLQ=
github.com/trivago/tgo v1.0.1/go.mod h1:w4dpD+3tzNIIiIfkWWa85w5/B77tlvdZckQ+6PkFnhc=
golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6 h1:bjcUS9ztw9kFmmIxJInhon/0Is3p+EHBKNgquIzo1OI=
golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/tools v0.0.0-20190308174544-00c44ba9c14f h1:SUQ6L9W8e5xt2GFO9s+i18JGITAfem+a0AQuFU8Ls74=
golang.org/x/tools v0.0.0-20190308174544-00c44ba9c14f/go.mod h1:25r3+/G6/xytQM8iWZKq3Hn0kr0rgFKPUNVEL/dr3z4=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20190315214010-f0bfdbff1f9c h1:KQ2sRfnx/Xk0E4v13yE9v3gCXAn6qieU1aiQOsbmpQg=
golang.org/x/tools v0.0.0-20190315214010-f0bfdbff1f9c/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.0.0-20170812160011-eb3733d160e7 h1:+t9dhfO+GNOIGJof6kPOAenx7YgrZMTdRPV+EsnPabk=
gopkg.in/yaml.v2 v2.0.0-20170812160011-eb3733d160e7/go.mod h1:JAlM8MvJe8wmxCU4Bli9HhUf9+ttbYbLASfIpnQbh74=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099 h1:XJP7lxbSxWLOMNdBE4B/STaqVy6L73o0knwj2vIlxnw=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190215041234-466a0476246c h1:z+UFwlQ7KVwdlQTE5JjvDvfZmyyAVrEiiwau20b7X8k=
honnef.co/go/tools v0.0.0-20190215041234-466a0476246c/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
File renamed without changes.
12 changes: 6 additions & 6 deletions config.go → pkg/config/config.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package jiralert
package config

import (
"fmt"
Expand Down Expand Up @@ -32,8 +32,8 @@ func (s *Secret) UnmarshalYAML(unmarshal func(interface{}) error) error {
return unmarshal((*plain)(s))
}

// LoadConfig parses the YAML input into a Config.
func LoadConfig(s string) (*Config, error) {
// Load parses the YAML input into a Config.
func Load(s string) (*Config, error) {
cfg := &Config{}
err := yaml.Unmarshal([]byte(s), cfg)
if err != nil {
Expand All @@ -43,14 +43,14 @@ func LoadConfig(s string) (*Config, error) {
return cfg, nil
}

// LoadConfigFile parses the given YAML file into a Config.
func LoadConfigFile(filename string) (*Config, []byte, error) {
// LoadFile parses the given YAML file into a Config.
func LoadFile(filename string) (*Config, []byte, error) {
log.V(1).Infof("Loading configuration from %q", filename)
content, err := ioutil.ReadFile(filename)
if err != nil {
return nil, nil, err
}
cfg, err := LoadConfig(string(content))
cfg, err := Load(string(content))
if err != nil {
return nil, nil, err
}
Expand Down
26 changes: 14 additions & 12 deletions notify.go → pkg/notify/notify.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package jiralert
package notify

import (
"bytes"
Expand All @@ -8,21 +8,24 @@ import (
"strings"
"time"

"github.com/free/jiralert/pkg/config"
"github.com/free/jiralert/pkg/template"

"github.com/andygrunwald/go-jira"
"github.com/free/jiralert/alertmanager"
"github.com/free/jiralert/pkg/alertmanager"
log "github.com/golang/glog"
"github.com/trivago/tgo/tcontainer"
)

// Receiver wraps a JIRA client corresponding to a specific Alertmanager receiver, with its configuration and templates.
type Receiver struct {
conf *ReceiverConfig
tmpl *Template
conf *config.ReceiverConfig
tmpl *template.Template
client *jira.Client
}

// NewReceiver creates a Receiver using the provided configuration and template.
func NewReceiver(c *ReceiverConfig, t *Template) (*Receiver, error) {
func NewReceiver(c *config.ReceiverConfig, t *template.Template) (*Receiver, error) {
tp := jira.BasicAuthTransport{
Username: c.User,
Password: string(c.Password),
Expand All @@ -38,9 +41,8 @@ func NewReceiver(c *ReceiverConfig, t *Template) (*Receiver, error) {
// Notify implements the Notifier interface.
func (r *Receiver) Notify(data *alertmanager.Data) (bool, error) {
project := r.tmpl.Execute(r.conf.Project, data)
// check errors from r.tmpl.Execute()
if r.tmpl.err != nil {
return false, r.tmpl.err
if err := r.tmpl.Err(); err != nil {
return false, err
}
// Looks like an ALERT metric name, with spaces removed.
issueLabel := toIssueLabel(data.GroupLabels)
Expand Down Expand Up @@ -106,9 +108,9 @@ func (r *Receiver) Notify(data *alertmanager.Data) (bool, error) {
for key, value := range r.conf.Fields {
issue.Fields.Unknowns[key] = deepCopyWithTemplate(value, r.tmpl, data)
}
// check errors from r.tmpl.Execute()
if r.tmpl.err != nil {
return false, r.tmpl.err

if err := r.tmpl.Err(); err != nil {
return false, err
}
retry, err = r.create(issue)
if err == nil {
Expand All @@ -120,7 +122,7 @@ func (r *Receiver) Notify(data *alertmanager.Data) (bool, error) {
// deepCopyWithTemplate returns a deep copy of a map/slice/array/string/int/bool or combination thereof, executing the
// provided template (with the provided data) on all string keys or values. All maps are connverted to
// map[string]interface{}, with all non-string keys discarded.
func deepCopyWithTemplate(value interface{}, tmpl *Template, data interface{}) interface{} {
func deepCopyWithTemplate(value interface{}, tmpl *template.Template, data interface{}) interface{} {
if value == nil {
return value
}
Expand Down
6 changes: 5 additions & 1 deletion template.go → pkg/template/template.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package jiralert
package template

import (
"bytes"
Expand Down Expand Up @@ -41,6 +41,10 @@ func LoadTemplate(path string) (*Template, error) {
return &Template{tmpl: tmpl}, nil
}

func (t *Template) Err() error {
return t.err
}

// Execute parses the provided text (or returns it unchanged if not a Go template), associates it with the templates
// defined in t.tmpl (so they may be referenced and used) and applies the resulting template to the specified data
// object, returning the output as a string.
Expand Down

0 comments on commit efb01aa

Please sign in to comment.