-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
80 lines (64 loc) · 1.71 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package main
import (
"os"
"strings"
"time"
log "github.com/sirupsen/logrus"
)
const defaultInterval time.Duration = time.Minute
func setLogLevel() {
level, exists := os.LookupEnv("LOG_LEVEL")
if !exists {
log.Info("LOG_LEVEL not set, defaulting to warning")
log.SetLevel(log.WarnLevel)
return
}
parsedLevel, err := log.ParseLevel(strings.ToLower(level))
if err != nil {
log.Warnf("Invalid LOG_LEVEL '%s', defaulting to warning", level)
log.SetLevel(log.WarnLevel)
return
}
log.SetLevel(parsedLevel)
log.Infof("Logging level set to %s", parsedLevel)
}
func init() {
log.SetOutput(os.Stdout)
setLogLevel()
}
func main() {
requiredVars := []string{"COSMOS_ENDPOINT", "SLACK_WEBHOOK_URL", "CHAIN_ID"}
for _, varName := range requiredVars {
if os.Getenv(varName) == "" {
log.Panicf("Error: %s is not defined\n", varName)
}
}
cosmosEndpoint := os.Getenv("COSMOS_ENDPOINT")
slackWebhookURL := os.Getenv("SLACK_WEBHOOK_URL")
chainID := os.Getenv("CHAIN_ID")
interval, err := time.ParseDuration(os.Getenv("FETCH_INTERVAL"))
if err != nil {
log.Errorf("Error parsing FETCH_INTERVAL: %s, defaulting to 1m\n", err)
interval = defaultInterval
}
var lastProposalId string
firstIteration := true
for {
latestProposal, err := getLatestProposal(cosmosEndpoint)
if err != nil {
log.Errorf("Error fetching proposal: %s\n", err)
continue
}
// post to slack only if didn't posted before
if !firstIteration && lastProposalId != latestProposal.ID {
err = postToSlack(chainID, *latestProposal, slackWebhookURL)
if err != nil {
log.Errorf("Error posting to slack: %s\n", err)
continue
}
}
lastProposalId = latestProposal.ID
firstIteration = false
time.Sleep(interval)
}
}