major: remove slack context
This commit is contained in:
		
							parent
							
								
									8adfa89575
								
							
						
					
					
						commit
						4e2a9a4b89
					
				| @ -11,14 +11,12 @@ import ( | |||||||
| 
 | 
 | ||||||
| 	"github.com/digitalocean/godo" | 	"github.com/digitalocean/godo" | ||||||
| 	log "github.com/sirupsen/logrus" | 	log "github.com/sirupsen/logrus" | ||||||
| 	"github.com/slack-go/slack" |  | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| const createdAtFormat = "2006-01-02T15:04:05Z" | const createdAtFormat = "2006-01-02T15:04:05Z" | ||||||
| 
 | 
 | ||||||
| type snapshotterContext struct { | type snapshotterContext struct { | ||||||
| 	DoContext *DigitalOceanContext | 	DoContext *DigitalOceanContext | ||||||
| 	SlackContext *SlackContext |  | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func initLogging() { | func initLogging() { | ||||||
| @ -36,19 +34,19 @@ func main() { | |||||||
| 
 | 
 | ||||||
| 	DOToken, present := os.LookupEnv("DO_TOKEN") | 	DOToken, present := os.LookupEnv("DO_TOKEN") | ||||||
| 
 | 
 | ||||||
| 	if present == false { | 	if !present { | ||||||
| 		log.Fatal("Missing enviroment variable \"DO_TOKEN\"") | 		log.Fatal("Missing enviroment variable \"DO_TOKEN\"") | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	volumesEnv, present := os.LookupEnv("DO_VOLUMES") | 	volumesEnv, present := os.LookupEnv("DO_VOLUMES") | ||||||
| 
 | 
 | ||||||
| 	if present == false { | 	if !present { | ||||||
| 		log.Fatal("Missing enviroment variable \"DO_VOLUMES\"") | 		log.Fatal("Missing enviroment variable \"DO_VOLUMES\"") | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	snapshotCountEnv, present := os.LookupEnv("DO_SNAPSHOT_COUNT") | 	snapshotCountEnv, present := os.LookupEnv("DO_SNAPSHOT_COUNT") | ||||||
| 
 | 
 | ||||||
| 	if present == false { | 	if !present { | ||||||
| 		log.Fatal("Missing enviroment variable \"DO_SNAPSHOT_COUNT\"") | 		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") | 		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{ | 	ctx := snapshotterContext{ | ||||||
| 		DoContext: &DigitalOceanContext{ | 		DoContext: &DigitalOceanContext{ | ||||||
| 			client: godo.NewFromToken(DOToken), | 			client: godo.NewFromToken(DOToken), | ||||||
| 			ctx:    context.TODO(), | 			ctx:    context.TODO(), | ||||||
| 		}, | 		}, | ||||||
| 		SlackContext: slackContext, |  | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	volumeIDs := strings.Split(volumesEnv, ",") | 	volumeIDs := strings.Split(volumesEnv, ",") | ||||||
| @ -95,6 +75,7 @@ func main() { | |||||||
| 			VolumeID: volume.ID, | 			VolumeID: volume.ID, | ||||||
| 			Name:     time.Now().Format("2006-01-02T15:04:05"), | 			Name:     time.Now().Format("2006-01-02T15:04:05"), | ||||||
| 		}) | 		}) | ||||||
|  | 
 | ||||||
| 		if err != nil { | 		if err != nil { | ||||||
| 			handleError(ctx, err, true) | 			handleError(ctx, err, true) | ||||||
| 		} | 		} | ||||||
| @ -103,6 +84,10 @@ func main() { | |||||||
| 
 | 
 | ||||||
| 		snapshots, _, err := ctx.DoContext.ListSnapshots(volume.ID, nil) | 		snapshots, _, err := ctx.DoContext.ListSnapshots(volume.ID, nil) | ||||||
| 
 | 
 | ||||||
|  | 		if err != nil { | ||||||
|  | 			handleError(ctx, err, true) | ||||||
|  | 		} | ||||||
|  | 
 | ||||||
| 		snapshotLength := len(snapshots) | 		snapshotLength := len(snapshots) | ||||||
| 
 | 
 | ||||||
| 		if snapshotLength > snapshotCount { | 		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) { | func handleError(ctx snapshotterContext, err error, fatal bool) { | ||||||
| 	errString := err.Error() | 	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 { | 	if fatal { | ||||||
| 		log.Fatal(errString) | 		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 |  | ||||||
| } |  | ||||||
							
								
								
									
										1
									
								
								go.mod
									
									
									
									
									
								
							
							
						
						
									
										1
									
								
								go.mod
									
									
									
									
									
								
							| @ -5,5 +5,4 @@ go 1.15 | |||||||
| require ( | require ( | ||||||
| 	github.com/digitalocean/godo v1.57.0 | 	github.com/digitalocean/godo v1.57.0 | ||||||
| 	github.com/sirupsen/logrus v1.7.0 | 	github.com/sirupsen/logrus v1.7.0 | ||||||
| 	github.com/slack-go/slack v0.8.0 |  | ||||||
| ) | ) | ||||||
|  | |||||||
							
								
								
									
										9
									
								
								go.sum
									
									
									
									
									
								
							
							
						
						
									
										9
									
								
								go.sum
									
									
									
									
									
								
							| @ -1,28 +1,19 @@ | |||||||
| cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= | cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= | ||||||
| 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.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||||||
| github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= | github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= | ||||||
| github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||||||
| github.com/digitalocean/godo v1.57.0 h1:uCpe0sRIZ/sJWxWDsJyBPBjUfSvxop+WHkHiSf+tjjM= | github.com/digitalocean/godo v1.57.0 h1:uCpe0sRIZ/sJWxWDsJyBPBjUfSvxop+WHkHiSf+tjjM= | ||||||
| github.com/digitalocean/godo v1.57.0/go.mod h1:p7dOjjtSBqCTUksqtA5Fd3uaKs9kyTq2xcz76ulEJRU= | github.com/digitalocean/godo v1.57.0/go.mod h1:p7dOjjtSBqCTUksqtA5Fd3uaKs9kyTq2xcz76ulEJRU= | ||||||
| github.com/go-test/deep v1.0.4 h1:u2CU3YKy9I2pmu9pX0eq50wCgjfGIt539SqR7FbHiho= |  | ||||||
| github.com/go-test/deep v1.0.4/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA= |  | ||||||
| github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= | github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= | ||||||
| github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= | github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= | ||||||
| github.com/golang/protobuf v1.3.5 h1:F768QJ1E9tib+q5Sc8MkdJi1RxLTbRcTf8LJV56aRls= | github.com/golang/protobuf v1.3.5 h1:F768QJ1E9tib+q5Sc8MkdJi1RxLTbRcTf8LJV56aRls= | ||||||
| github.com/golang/protobuf v1.3.5/go.mod h1:6O5/vntMXwX2lRkT1hjjk0nAC1IDOTvTlVgjlRvqsdk= | github.com/golang/protobuf v1.3.5/go.mod h1:6O5/vntMXwX2lRkT1hjjk0nAC1IDOTvTlVgjlRvqsdk= | ||||||
| github.com/google/go-querystring v1.0.0 h1:Xkwi/a1rcvNg1PPYe5vI8GbeBY/jrVuDX5ASuANWTrk= | github.com/google/go-querystring v1.0.0 h1:Xkwi/a1rcvNg1PPYe5vI8GbeBY/jrVuDX5ASuANWTrk= | ||||||
| github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck= | github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck= | ||||||
| github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc= |  | ||||||
| github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= |  | ||||||
| github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw= |  | ||||||
| github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= |  | ||||||
| github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= | github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= | ||||||
| github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||||||
| github.com/sirupsen/logrus v1.7.0 h1:ShrD1U9pZB12TX0cVy0DtePoCH97K8EtX+mg7ZARUtM= | github.com/sirupsen/logrus v1.7.0 h1:ShrD1U9pZB12TX0cVy0DtePoCH97K8EtX+mg7ZARUtM= | ||||||
| github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0= | github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0= | ||||||
| github.com/slack-go/slack v0.8.0 h1:ANyLY5KHLV+MxLJDQum2IuHTLwbCbDtaWY405X1EU9U= |  | ||||||
| github.com/slack-go/slack v0.8.0/go.mod h1:FGqNzJBmxIsZURAxh2a8D21AnOVvvXZvGligs4npPUM= |  | ||||||
| github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= | github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= | ||||||
| github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= | github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= | ||||||
| github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= | github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user