-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
252 lines (206 loc) · 5.96 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
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
package main
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"net/http"
"os"
"strconv"
"strings"
"sync"
"time"
"github.com/pkg/errors"
"crypto/md5"
"github.com/PuerkitoBio/goquery"
"github.com/oirik/gosubcommand"
)
var (
version string
revision string
)
const maxCheckCount = 5
func main() {
gosubcommand.AppName = "wcc"
gosubcommand.Version = fmt.Sprintf("version: %s\nrevision: %s", version, revision)
gosubcommand.Summary = "wcc is a command-line tool which checks whether a website is changed or not. \nThis tool creates a data file named `wcc.dat` to save the registered websites in a current direcotry."
gosubcommand.Register("add", &addCommand{})
gosubcommand.Register("rm", &rmCommand{})
gosubcommand.Register("list", &listCommand{})
gosubcommand.Register("check", &checkCommand{})
os.Exit(int(gosubcommand.Execute()))
}
type addCommand struct{}
func (cmd *addCommand) Summary() string { return "Add new website which you want to check updates." }
func (cmd *addCommand) SetFlag(fs *flag.FlagSet) {}
func (cmd *addCommand) Execute(fs *flag.FlagSet) gosubcommand.ExitCode {
url := fs.Arg(0)
if url == "" {
fmt.Fprintln(os.Stderr, "url argument is missing.")
return gosubcommand.ExitCodeUsageError
}
selector := fs.Arg(1)
wcc, err := open()
if err != nil {
fmt.Fprintln(os.Stderr, "failed to open data file.", err)
return gosubcommand.ExitCodeError
}
hash, err := getHash(url, selector)
if err != nil {
fmt.Fprintln(os.Stderr, "failed to access with the url+selector", err)
return gosubcommand.ExitCodeError
}
wcc.Websites = append(wcc.Websites, &wccWebsite{URL: url, Selector: selector, Hash: hash})
err = wcc.save()
if err != nil {
fmt.Fprintln(os.Stderr, "failed to save data file.", err)
return gosubcommand.ExitCodeError
}
wcc.fprint(os.Stdout)
return gosubcommand.ExitCodeSuccess
}
type rmCommand struct{}
func (cmd *rmCommand) Summary() string { return "Remove website from the registered list." }
func (cmd *rmCommand) SetFlag(fs *flag.FlagSet) {}
func (cmd *rmCommand) Execute(fs *flag.FlagSet) gosubcommand.ExitCode {
index, err := strconv.ParseInt(fs.Arg(0), 10, 32)
if err != nil {
fmt.Fprintln(os.Stderr, "argument is not integer.", err)
return gosubcommand.ExitCodeUsageError
}
wcc, err := open()
if err != nil {
fmt.Fprintln(os.Stderr, "failed to open data file.", err)
return gosubcommand.ExitCodeError
}
if index <= 0 || int(index) > len(wcc.Websites) {
fmt.Fprintln(os.Stderr, "index is out of range.")
return gosubcommand.ExitCodeUsageError
}
wcc.Websites = append(wcc.Websites[:index-1], wcc.Websites[index:]...)
err = wcc.save()
if err != nil {
fmt.Fprintln(os.Stderr, "failed to save data file.", err)
return gosubcommand.ExitCodeError
}
wcc.fprint(os.Stdout)
return gosubcommand.ExitCodeSuccess
}
type listCommand struct{}
func (cmd *listCommand) Summary() string { return "Show list of the registered websites." }
func (cmd *listCommand) SetFlag(fs *flag.FlagSet) {}
func (cmd *listCommand) Execute(fs *flag.FlagSet) gosubcommand.ExitCode {
wcc, err := open()
if err != nil {
fmt.Fprintln(os.Stderr, "failed to open data file.", err)
return gosubcommand.ExitCodeError
}
wcc.fprint(os.Stdout)
return gosubcommand.ExitCodeSuccess
}
type checkCommand struct {
slack string
}
func (cmd *checkCommand) Summary() string { return "Check updates of the registered websites." }
func (cmd *checkCommand) SetFlag(fs *flag.FlagSet) {
fs.StringVar(&cmd.slack, "slack", "", "Set the slack's incoming webhook URL if you want to be notified when found updated")
}
func (cmd *checkCommand) Execute(fs *flag.FlagSet) gosubcommand.ExitCode {
wcc, err := open()
if err != nil {
fmt.Fprintln(os.Stderr, "failed to open data file.", err)
return gosubcommand.ExitCodeError
}
if len(wcc.Websites) == 0 {
return gosubcommand.ExitCodeSuccess
}
limit := make(chan struct{}, maxCheckCount)
var wg sync.WaitGroup
for i := range wcc.Websites {
wg.Add(1)
go func(i int) {
limit <- struct{}{}
defer wg.Done()
now := time.Now()
ws := wcc.Websites[i]
ws.LastChecked = now
hash, err := getHash(ws.URL, ws.Selector)
if err != nil {
ws.Status = statusError
ws.Error = err
return
}
ws.Error = nil
if hash == ws.Hash {
ws.Status = statusNoChange
return
}
ws.Status = statusUpdated
ws.LastUpdated = now
ws.Hash = hash
<-limit
}(i)
}
wg.Wait()
err = wcc.save()
if err != nil {
fmt.Fprintln(os.Stderr, "failed to save data file.", err)
return gosubcommand.ExitCodeError
}
var errorCount, updatedCount int
for _, ws := range wcc.Websites {
switch ws.Status {
case statusError:
errorCount++
case statusUpdated:
updatedCount++
}
}
if errorCount > 0 || updatedCount > 0 {
builder := new(strings.Builder)
if errorCount > 0 {
fmt.Fprintf(builder, "%d websites got error.\n\n", errorCount)
}
if updatedCount > 0 {
fmt.Fprintf(builder, "%d websites have been updated.\n\n", updatedCount)
}
fmt.Fprintln(builder, "```")
wcc.fprint(builder)
fmt.Fprintln(builder, "```")
message := builder.String()
fmt.Println(message)
if cmd.slack != "" {
if err = notifySlack(message, cmd.slack); err != nil {
panic(err)
}
}
}
return gosubcommand.ExitCodeSuccess
}
func notifySlack(message string, url string) error {
jsonBytes, _ := json.Marshal(map[string]string{
"text": message,
})
resp, err := http.Post(url, "application/json", bytes.NewBuffer(jsonBytes))
if err != nil {
return errors.Wrap(err, "failed to notify slack")
}
defer resp.Body.Close()
return nil
}
func getHash(url string, selector string) (hash string, err error) {
doc, err := goquery.NewDocument(url)
if err != nil {
return "", errors.Wrap(err, "failed to get the website")
}
var s *goquery.Selection
if selector == "" {
s = doc.First()
} else {
s = doc.Find(selector).First()
}
if s == nil {
return "", errors.New("this selector don't find anything")
}
return fmt.Sprintf("%x", md5.Sum([]byte(s.Text()))), nil
}