diff --git a/go.mod b/go.mod index f34972e..7e8859f 100644 --- a/go.mod +++ b/go.mod @@ -17,6 +17,7 @@ require ( github.com/onsi/ginkgo v1.8.0 // indirect github.com/onsi/gomega v1.5.0 // indirect github.com/pkg/errors v0.8.1 + github.com/rs/zerolog v1.20.0 github.com/slack-go/slack v0.6.5 github.com/ssor/bom v0.0.0-20170718123548-6386211fdfcf // indirect gomodules.xyz/envconfig v1.3.1-0.20190308184047-426f31af0d45 diff --git a/go.sum b/go.sum index 16a4784..57c4d47 100644 --- a/go.sum +++ b/go.sum @@ -7,6 +7,7 @@ github.com/cenkalti/backoff v2.1.1+incompatible h1:tKJnvO2kl0zmb/jA5UKAt4VoEVw1q github.com/cenkalti/backoff v2.1.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM= github.com/codegangsta/inject v0.0.0-20150114235600-33e0aa1cb7c0/go.mod h1:4Zcjuz89kmFXt9morQgcfYZAYZ5n8WHjt81YYWIwtTM= github.com/codeskyblue/go-sh v0.0.0-20190412065543-76bd3d59ff27/go.mod h1:VQx0hjo2oUeQkQUET7wRwradO6f+fN5jzXgB/zROxxE= +github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4= github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= @@ -74,6 +75,9 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/rogpeppe/go-internal v1.1.0 h1:g0fH8RicVgNl+zVZDCDfbdWxAWoAEJyI7I3TZYXFiig= github.com/rogpeppe/go-internal v1.1.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= +github.com/rs/xid v1.2.1/go.mod h1:+uKXf+4Djp6Md1KODXJxgGQPKngRmWyn10oCKFzNHOQ= +github.com/rs/zerolog v1.20.0 h1:38k9hgtUBdxFwE34yS8rTHmHBa4eN16E4DJlv177LNs= +github.com/rs/zerolog v1.20.0/go.mod h1:IzD0RJ65iWH0w97OQQebJEvTZYvsCUm9WVLWBQrJRjo= github.com/slack-go/slack v0.6.5 h1:IkDKtJ2IROJNoe3d6mW870/NRKvq2fhLB/Q5XmzWk00= github.com/slack-go/slack v0.6.5/go.mod h1:FGqNzJBmxIsZURAxh2a8D21AnOVvvXZvGligs4npPUM= github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc= @@ -96,6 +100,8 @@ golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73r golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3 h1:0GoQqolDA55aaLxZyTzK/Y2ePZzZTUrRacwib7cNsYQ= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859 h1:R/3boaszxrf1GEUWTVDzSKVwLmSJpwZ1yqXm8j0v2QI= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f h1:wMNYb4v58l5UBM7MYRLPG6ZhfOqbKu7X5eyFl8ZhKvA= golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190423024810-112230192c58 h1:8gQV6CLnAEikrhgkHFbMAEhagSSnXWGV915qUMm9mrU= @@ -109,6 +115,8 @@ golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= +golang.org/x/tools v0.0.0-20190828213141-aed303cbaa74/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= gomodules.xyz/envconfig v1.3.1-0.20190308184047-426f31af0d45 h1:juzzlx91nWAOsHuOVfXZPMXHtJEKouZvY9bBbwlOeYs= gomodules.xyz/envconfig v1.3.1-0.20190308184047-426f31af0d45/go.mod h1:41y72mzHT7+jFNgyBpJRrZWuZJcLmLrTpq6iGgOFJMQ= gomodules.xyz/version v0.1.0/go.mod h1:Y8xuV02mL/45psyPKG3NCVOwvAOy6T5Kx0l3rCjKSjU= diff --git a/slack/chat.go b/slack/chat.go index 8dae65f..0e4b832 100644 --- a/slack/chat.go +++ b/slack/chat.go @@ -3,6 +3,11 @@ package slack import ( "context" "errors" + "fmt" + "net/http" + "net/url" + + "github.com/rs/zerolog/log" "github.com/slack-go/slack" "gomodules.xyz/envconfig" "gomodules.xyz/notify" @@ -13,11 +18,15 @@ const UID = "slack" type Options struct { AuthToken string `envconfig:"AUTH_TOKEN" required:"true"` Channel []string `envconfig:"CHANNEL"` + ProxyHost string `envconfig:"PROXY_HOST"` + ProxyPort int `envconfig:"PROXY_PORT"` } type client struct { - opt Options - body string + opt Options + body string + msg slack.MsgOption + clientOpt *http.Client } var _ notify.ByChat = &client{} @@ -53,6 +62,20 @@ func (c client) WithBody(body string) notify.ByChat { return &c } +func (c client) WithMsgOption(msgOption slack.MsgOption) notify.ByChat { + c.msg = msgOption + return &c +} + +func (c client) WithProxy(proxyHost string, proxyPort int) notify.ByChat { + proxyURL, err := url.Parse(fmt.Sprintf("%v:%v", proxyHost, proxyPort)) + if err != nil { + log.Error().Err(err).Msg("failed to parse URL") + } + c.clientOpt = &http.Client{Transport: &http.Transport{Proxy: http.ProxyURL(proxyURL)}} + return &c +} + func (c client) To(to string, cc ...string) notify.ByChat { c.opt.Channel = append([]string{to}, cc...) return &c @@ -74,3 +97,20 @@ func (c *client) Send() error { } return nil } + +func (c *client) SendAsBlockMessage() error { + if len(c.opt.Channel) == 0 { + return errors.New("missing to") + } + s := slack.New(c.opt.AuthToken, slack.OptionHTTPClient(c.clientOpt)) + + for _, channel := range c.opt.Channel { + if _, _, err := s.PostMessageContext( + context.TODO(), + channel, + c.msg); err != nil { + return err + } + } + return nil +} diff --git a/types.go b/types.go index cdb0a78..5df2f36 100644 --- a/types.go +++ b/types.go @@ -1,5 +1,7 @@ package notify +import "github.com/slack-go/slack" + type ByEmail interface { UID() string From(from string) ByEmail @@ -29,8 +31,11 @@ type ByVoice interface { type ByChat interface { UID() string WithBody(body string) ByChat + WithProxy(proxyHost string, proxyPort int) ByChat + WithMsgOption(msgOption slack.MsgOption) ByChat To(to string, cc ...string) ByChat Send() error + SendAsBlockMessage() error } type ByPush interface {