major: remove slack context
This commit is contained in:
@@ -11,14 +11,12 @@ import (
|
||||
|
||||
"github.com/digitalocean/godo"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/slack-go/slack"
|
||||
)
|
||||
|
||||
const createdAtFormat = "2006-01-02T15:04:05Z"
|
||||
|
||||
type snapshotterContext struct {
|
||||
DoContext *DigitalOceanContext
|
||||
SlackContext *SlackContext
|
||||
DoContext *DigitalOceanContext
|
||||
}
|
||||
|
||||
func initLogging() {
|
||||
@@ -36,19 +34,19 @@ func main() {
|
||||
|
||||
DOToken, present := os.LookupEnv("DO_TOKEN")
|
||||
|
||||
if present == false {
|
||||
if !present {
|
||||
log.Fatal("Missing enviroment variable \"DO_TOKEN\"")
|
||||
}
|
||||
|
||||
volumesEnv, present := os.LookupEnv("DO_VOLUMES")
|
||||
|
||||
if present == false {
|
||||
if !present {
|
||||
log.Fatal("Missing enviroment variable \"DO_VOLUMES\"")
|
||||
}
|
||||
|
||||
snapshotCountEnv, present := os.LookupEnv("DO_SNAPSHOT_COUNT")
|
||||
|
||||
if present == false {
|
||||
if !present {
|
||||
log.Fatal("Missing enviroment variable \"DO_SNAPSHOT_COUNT\"")
|
||||
}
|
||||
|
||||
@@ -58,29 +56,11 @@ func main() {
|
||||
log.Fatal("Enviroment variable \"DO_SNAPSHOT_COUNT\" is not an integer")
|
||||
}
|
||||
|
||||
slackEnv := os.Getenv("SLACK_TOKEN")
|
||||
|
||||
var slackContext *SlackContext = nil
|
||||
|
||||
if slackEnv != "" {
|
||||
channelID, present := os.LookupEnv("SLACK_CHANNEL_ID")
|
||||
|
||||
if present == false {
|
||||
log.Fatal("Missing enviroment variable \"SLACK_CHANNEL_ID\"")
|
||||
}
|
||||
|
||||
slackContext = &SlackContext{
|
||||
client: slack.New(slackEnv),
|
||||
channelID: channelID,
|
||||
}
|
||||
}
|
||||
|
||||
ctx := snapshotterContext{
|
||||
DoContext: &DigitalOceanContext{
|
||||
client: godo.NewFromToken(DOToken),
|
||||
ctx: context.TODO(),
|
||||
},
|
||||
SlackContext: slackContext,
|
||||
}
|
||||
|
||||
volumeIDs := strings.Split(volumesEnv, ",")
|
||||
@@ -95,6 +75,7 @@ func main() {
|
||||
VolumeID: volume.ID,
|
||||
Name: time.Now().Format("2006-01-02T15:04:05"),
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
handleError(ctx, err, true)
|
||||
}
|
||||
@@ -103,6 +84,10 @@ func main() {
|
||||
|
||||
snapshots, _, err := ctx.DoContext.ListSnapshots(volume.ID, nil)
|
||||
|
||||
if err != nil {
|
||||
handleError(ctx, err, true)
|
||||
}
|
||||
|
||||
snapshotLength := len(snapshots)
|
||||
|
||||
if snapshotLength > snapshotCount {
|
||||
@@ -135,24 +120,11 @@ func main() {
|
||||
}
|
||||
}
|
||||
|
||||
if ctx.SlackContext != nil {
|
||||
err = ctx.SlackContext.SendEvent(fmt.Sprintf("Successfully created Backup for %d Volumes", len(volumeIDs)), log.InfoLevel)
|
||||
if err != nil {
|
||||
handleError(ctx, err, false)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func handleError(ctx snapshotterContext, err error, fatal bool) {
|
||||
errString := err.Error()
|
||||
|
||||
if ctx.SlackContext != nil {
|
||||
err = ctx.SlackContext.SendEvent(errString, log.ErrorLevel)
|
||||
if err != nil {
|
||||
log.Error(fmt.Sprintf("Error while trying to send error to Slack: %s", err.Error()))
|
||||
}
|
||||
}
|
||||
|
||||
if fatal {
|
||||
log.Fatal(errString)
|
||||
}
|
||||
|
||||
@@ -1,30 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/slack-go/slack"
|
||||
)
|
||||
|
||||
// OutputSource is an abstraction for outputting specific events to other services (e.g. Discord, Slack or Whatsapp)
|
||||
type OutputSource interface {
|
||||
SendEvent(string, log.Level) error
|
||||
}
|
||||
|
||||
// SendEvent forwards event to Slack
|
||||
func (s SlackContext) SendEvent(content string, level log.Level) error {
|
||||
|
||||
color := "#00FF00"
|
||||
|
||||
if level == log.ErrorLevel {
|
||||
color = "#FF0000"
|
||||
}
|
||||
|
||||
return s.SendMessageWithEmbed(slack.Attachment{
|
||||
Color: color,
|
||||
AuthorName: "DigitalOceanSnapshotter",
|
||||
AuthorIcon: "https://cdn.top.gg/icons/DO_Logo_icon_blue.png",
|
||||
Text: content,
|
||||
Title: "DigitalOceanSnapshotter",
|
||||
TitleLink: "https://github.com/top-gg/DigitalOceanSnapshotter",
|
||||
})
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
package main
|
||||
|
||||
import "github.com/slack-go/slack"
|
||||
|
||||
// SlackContext is an helper struct to acess slack actions
|
||||
type SlackContext struct {
|
||||
client *slack.Client
|
||||
channelID string
|
||||
}
|
||||
|
||||
// SendMessageWithContent sends a message with content to the pre defined channel
|
||||
func (s SlackContext) SendMessageWithContent(content string) error {
|
||||
_, _, _, err := s.client.SendMessage(s.channelID, slack.MsgOptionText(content, true))
|
||||
return err
|
||||
}
|
||||
|
||||
// SendMessageWithEmbed sends a message with a Rich Embed to the pre defined channel
|
||||
func (s SlackContext) SendMessageWithEmbed(attachment slack.Attachment) error {
|
||||
_, _, _, err := s.client.SendMessage(s.channelID, slack.MsgOptionAttachments(attachment))
|
||||
return err
|
||||
}
|
||||
Reference in New Issue
Block a user