-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
113 lines (95 loc) · 2.99 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package main
import (
"strings"
"github.com/kelseyhightower/envconfig"
log "github.com/sirupsen/logrus"
flag "github.com/spf13/pflag"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
"gorm.io/gorm/logger"
)
type Config struct {
DatabaseFile string `required:"false" split_words:"true"`
LogFormat string `required:"false" split_words:"true"`
NotifySlack bool `required:"false" split_words:"true"`
RapidApiKey string `required:"false" split_words:"true"`
Site string `required:"false" split_words:"true"`
SlackChannelID string `required:"true" split_words:"true"`
SlackToken string `required:"true" split_words:"true"`
Tag string `required:"true" split_words:"true"`
TwitterBearerToken string `required:"false" split_words:"true"`
}
func LoadConfig() *Config {
var config Config
err := envconfig.Process("", &config)
if err != nil {
panic(err)
}
return &config
}
func CreateDB(databaseFile string) (*gorm.DB, error) {
if databaseFile == "" {
databaseFile = "stackoverflow-notifications.db"
}
db, err := gorm.Open(sqlite.Open(databaseFile), &gorm.Config{
Logger: logger.Default.LogMode(logger.Silent),
})
if err != nil {
return db, err
}
return db, nil
}
type processor func(*Config, *gorm.DB) error
func main() {
services := flag.String("services", "", "comma-separated list of services to process")
notifySlack := flag.Bool("notify-slack", true, "whether to notify slack or not")
flag.Parse()
config := LoadConfig()
if config.LogFormat == "json" {
log.SetFormatter(&log.JSONFormatter{})
}
if !*notifySlack {
config.NotifySlack = false
}
if config.Tag == "" {
log.Fatal("No TAG environment variable specified")
}
db, err := CreateDB(config.DatabaseFile)
if err != nil {
log.WithError(err).Fatal("error creating db")
}
processorMap := map[string]processor{
"devto": processDevtoArticles,
"github": processGithubRepositories,
"hackernews_comment": processHackernewsComments,
"hackernews_story": processHackernewsStories,
"mastodon": processMastodon,
"medium": processMediumArticles,
"reddit": processRedditPosts,
"stackoverflow": processStackoverflow,
"twitter": processTwitter,
}
// allow disabling services
if len(*services) > 0 {
enabledServices := map[string]bool{}
for _, service := range strings.Split(*services, ",") {
enabledServices[service] = true
}
disabledServices := []string{}
for service := range processorMap {
if !enabledServices[service] {
disabledServices = append(disabledServices, service)
}
}
for _, service := range disabledServices {
log.WithField("service", service).Info("Disabling service")
delete(processorMap, service)
}
}
for service, processor := range processorMap {
log.WithField("service", service).Info("Processing service")
if err := processor(config, db); err != nil {
log.WithError(err).WithField("service", service).Fatal("error processing")
}
}
}