-
Notifications
You must be signed in to change notification settings - Fork 0
/
reservations.go
66 lines (57 loc) · 1.24 KB
/
reservations.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
package namegrind
import (
"bufio"
"github.com/cheggaaa/pb/v3"
"github.com/mitchellh/go-homedir"
"io"
"net/http"
"os"
"path"
)
const ReservationsURL = "https://gist.githubusercontent.com/mslipper/d1a9989a8a175272f51da5e2f43e8d15/raw/72b07b1e29f08575d2feebef21c9da09c5e167cc/namehashes"
func ReservationsPath() string {
dir, err := homedir.Dir()
if err != nil {
panic(err)
}
return path.Join(dir, "reservations.json")
}
func ReservationsExist() (bool, error) {
_, err := os.Stat(ReservationsPath())
if os.IsNotExist(err) {
return false, nil
}
if err != nil {
return false, err
}
return true, nil
}
func FetchReservations() error {
out, err := os.Create(ReservationsPath())
defer out.Close()
res, err := http.Get(ReservationsURL)
if err != nil {
return err
}
defer res.Body.Close()
bar := pb.Full.Start64(res.ContentLength)
if _, err := io.Copy(out, bar.NewProxyReader(res.Body)); err != nil {
return err
}
bar.Finish()
return nil
}
func ParseReservations() (map[string]bool, error) {
out := make(map[string]bool)
f, err := os.Open(ReservationsPath())
if err != nil {
return nil, err
}
defer f.Close()
scan := bufio.NewScanner(f)
for scan.Scan() {
line := scan.Text()
out[line] = true
}
return out, nil
}