Commit ceb025d5 authored by Kirill Garbar's avatar Kirill Garbar
Browse files

[+] init

parents
### JetBrains ###
# User-specific stuff
.idea
**/.DS_Store
# File-based project format
*.iws
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib
# Test binary, build with `go test -c`
*.test
coverage.txt
# Output of the go coverage tool, specifically when used with LiteIDE
*.out
# compiled builds
/bin
## Configuration
### `MATTERMOST_HOOK_URL`
Mattermost | Slack hook URL
### `APP_NAME`
Application name for notifications
### `ENV`
Current system environment. Supported options are:
* `DEV`
* `STG`
* `PROD`
module yn.ee/facktoreal/slack
go 1.12
require github.com/romana/rlog v0.0.0-20171115192701-f018bc92e7d7
package slack
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"os"
"github.com/romana/rlog"
)
// Message ...
type Message struct {
Text string `json:"text"`
Username string `json:"username,omitempty"`
IconEmoji string `json:"icon_emoji,omitempty"`
Channel string `json:"channel,omitempty"`
Attachments []Attachment `json:"attachments,omitempty"`
}
// Attachment ...
type Attachment struct {
Text string `json:"text,omitempty"`
Fallback string `json:"fallback,omitempty"`
Pretext string `json:"pretext,omitempty"`
Color string `json:"color,omitempty"`
AuthorName string `json:"author_name,omitempty"`
AuthorLink string `json:"author_link,omitempty"`
AuthorIcon string `json:"author_icon,omitempty"`
Title string `json:"title,omitempty"`
TitleLink string `json:"title_link,omitempty"`
Fields []Field `json:"fields,omitempty"`
ImageURL string `json:"image_url,omitempty"`
ThumbURL string `json:"thumb_url,omitempty"`
}
// Field ...
type Field struct {
Title string `json:"title,omitempty"`
Value string `json:"value,omitempty"`
Short bool `json:"short,omitempty"`
}
// AddAttachment ...
func (m *Message) AddAttachment(attachment Attachment) *Message {
m.Attachments = append(m.Attachments, attachment)
return m
}
// SlackService ...
type SlackService interface {
Send() error
Add(attachment Attachment)
Dump(body interface{})
Debugf(format string, args ...interface{})
Infof(format string, args ...interface{})
Errorf(format string, args ...interface{})
Warningf(format string, args ...interface{})
Success(format string, args ...interface{})
}
type slackService struct {
msg Message
ctx context.Context
}
// NewSlackService ...
func NewSlackService(ctx context.Context, name string) SlackService {
return &slackService{
ctx: ctx,
msg: Message{
Text: name,
Username: fmt.Sprintf("%s-%s", os.Getenv("APP_NAME"), os.Getenv("ENV")),
},
}
}
// Add ...
func (s *slackService) Add(attachment Attachment) {
s.msg.AddAttachment(attachment)
}
// Dump ...
func (s *slackService) Dump(body interface{}) {
rlog.Println(body)
b, err := json.MarshalIndent(body, "", " ")
if err != nil {
rlog.Errorf("Err: %s", err.Error())
return
}
s.msg.AddAttachment(Attachment{
Text: fmt.Sprintf("%s", b),
})
}
// Debugf ...
func (s *slackService) Debugf(format string, args ...interface{}) {
rlog.Debugf(format, args...)
s.msg.AddAttachment(Attachment{
Text: fmt.Sprintf(format, args...),
})
}
// Infof ...
func (s *slackService) Infof(format string, args ...interface{}) {
rlog.Infof(format, args...)
s.msg.AddAttachment(Attachment{
Text: fmt.Sprintf(format, args...),
})
}
// Success ...
func (s *slackService) Success(format string, args ...interface{}) {
rlog.Infof(format, args...)
s.msg.AddAttachment(Attachment{
Color: "#2B7A1F",
Text: fmt.Sprintf(format, args...),
})
}
// Warningf ...
func (s *slackService) Warningf(format string, args ...interface{}) {
rlog.Warnf(format, args...)
s.msg.AddAttachment(Attachment{
Color: "#FC6501",
Text: fmt.Sprintf(format, args...),
})
}
// Errorf ...
func (s *slackService) Errorf(format string, args ...interface{}) {
rlog.Errorf(format, args...)
s.msg.AddAttachment(Attachment{
Color: "#FF3232",
Text: fmt.Sprintf(format, args...),
})
}
// Send ...
func (s *slackService) Send() error {
payloadBytes, err := json.Marshal(s.msg)
if err != nil {
rlog.Errorf("Unable to marshal %s", err.Error())
return err
}
body := bytes.NewReader(payloadBytes)
req, err := http.NewRequest("POST", os.Getenv("MATTERMOST_HOOK_URL"), body)
if err != nil {
rlog.Errorf("Unable to notify slack, err: %s", err.Error())
return err
}
req.Header.Set("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
rlog.Errorf("Unable to notify slack, err: %s", err.Error())
return err
}
resp.Body.Close()
return nil
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment