-
Notifications
You must be signed in to change notification settings - Fork 0
/
scrape.go
57 lines (46 loc) · 941 Bytes
/
scrape.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
package main
import (
"context"
"log"
"os"
"os/signal"
"github.com/FurqanSoftware/blink/site"
"github.com/spf13/cobra"
)
var scrapeCmd = &cobra.Command{
Use: "scrape",
Short: "Scrape scrapes programming language references",
Run: func(cmd *cobra.Command, args []string) {
exitCh := make(chan struct{})
errCh := make(chan error)
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
defer cancel()
siteCpp := site.Get(args[0])
go func() {
err := site.Scraper{}.Run(ctx, siteCpp)
if err != nil {
errCh <- err
}
close(exitCh)
}()
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, os.Interrupt)
select {
case err := <-errCh:
log.Fatal(err)
case <-exitCh:
case sig := <-sigCh:
log.Printf("Received %s; exiting", sig)
cancel()
select {
case <-exitCh:
case <-sigCh:
}
}
log.Print("Bye")
},
}
func init() {
rootCmd.AddCommand(scrapeCmd)
}