-
Notifications
You must be signed in to change notification settings - Fork 1
/
bot.go
166 lines (137 loc) · 3.73 KB
/
bot.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package main
import (
"encoding/json"
"errors"
"flag"
"fmt"
"github.com/garyburd/redigo/redis"
"github.com/hugbotme/bot-go/config"
"io/ioutil"
"log"
"os"
"os/signal"
"strconv"
"time"
)
var (
flagConfigFile *string
flagTestFile *string
flagStopWordsFile *string
flagProbableWordsFile *string
flagPidFile *string
flagVersion *bool
)
const (
majorVersion = 1
minorVersion = 0
patchVersion = 0
)
type Hug struct {
TweetID string
URL string
PullRequestId int
}
func init() {
flagConfigFile = flag.String("config", "", "Configuration file")
flagPidFile = flag.String("pidfile", "", "Write the process id into a given file")
flagTestFile = flag.String("testfile", "", "Spell check test file")
flagStopWordsFile = flag.String("stopwords", "", "Stop words to ignore when spell checking")
flagProbableWordsFile = flag.String("prefer", "", "Word list to prefer")
flagVersion = flag.Bool("version", false, "Outputs the version number and exits")
}
func FetchFromQueue(client redis.Conn) (*Hug, error) {
values, err := redis.Values(client.Do("BLPOP", "hug:queue", 0))
if err != nil {
return nil, err
}
bytes, err := redis.Bytes(values[1], nil)
if err != nil {
log.Print("Something broke in bytes", err)
return nil, errors.New("Something broke in bytes")
}
var hug Hug
err = json.Unmarshal(bytes, &hug)
if err != nil {
return nil, err
}
return &hug, nil
}
func ConnectRedis(url string, auth string) redis.Conn {
redisClient, err := redis.Dial("tcp", url)
if err != nil {
log.Fatal("Redis client init (connect) failed:", err)
}
if len(auth) == 0 {
return redisClient
}
if _, err := redisClient.Do("AUTH", auth); err != nil {
redisClient.Close()
log.Fatal("Redis client init (auth) failed:", err)
}
return redisClient
}
func main() {
flag.Parse()
// Output the version and exit
if *flagVersion {
fmt.Printf("bot v%d.%d.%d\n", majorVersion, minorVersion, patchVersion)
return
}
// Check for configuration file
if len(*flagConfigFile) <= 0 {
log.Fatal("No configuration file found. Please add the --config parameter")
}
// Check for stop words file
if len(*flagStopWordsFile) <= 0 {
log.Fatal("No stop words file found. Please add the --stopwords parameter")
}
// Check for preferred file
if len(*flagProbableWordsFile) <= 0 {
log.Fatal("No preferences file found. Please add the --prefer parameter")
}
// PID-File
if len(*flagPidFile) > 0 {
ioutil.WriteFile(*flagPidFile, []byte(strconv.Itoa(os.Getpid())), 0644)
}
fmt.Println("Hi, i am hugbot. And now i start to fix your typos.")
config, err := config.NewConfiguration(flagConfigFile)
if err != nil {
log.Fatal("Configuration initialisation failed:", err)
}
if len(*flagTestFile) > 0 {
// jvt: @todo error handling?
testFile, _ := ioutil.ReadFile(*flagTestFile)
processor, _ := newSpellCheckFileProcessor(*flagStopWordsFile, *flagProbableWordsFile)
ioutil.WriteFile("new-line-test.txt", []byte(processor.processContent(testFile)), 0644)
os.Exit(1)
}
// capture ctrl+c and stop execution
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
jobs := make(chan *Hug)
go func() {
time.Sleep(time.Second * 1)
redisClient := ConnectRedis(config.Redis.Url, config.Redis.Auth)
defer redisClient.Close()
for {
hug, err := FetchFromQueue(redisClient)
fmt.Println(hug, err)
if err == nil {
jobs <- hug
}
}
}()
// jvt: run as long as no interrupt is sent
go func() {
for sig := range c {
fmt.Printf("captured %v, notifying everyone...\n", sig)
fmt.Println("exiting...")
os.Exit(1)
}
}()
// jvt: check for new job
for job := range jobs {
fmt.Println("got new job: ", job)
go processHug(job, config, *flagStopWordsFile, *flagStopWordsFile)
}
}