|
1
|
|
|
package dictionary |
|
2
|
|
|
|
|
3
|
|
|
import ( |
|
4
|
|
|
"bufio" |
|
5
|
|
|
"io" |
|
6
|
|
|
"net/http" |
|
7
|
|
|
"net/url" |
|
8
|
|
|
"os" |
|
9
|
|
|
|
|
10
|
|
|
"github.com/pkg/errors" |
|
11
|
|
|
) |
|
12
|
|
|
|
|
13
|
|
|
const commentPrefix = "#" |
|
14
|
|
|
|
|
15
|
|
|
func NewDictionaryFrom(path string, doer Doer) ([]string, error) { |
|
|
|
|
|
|
16
|
|
|
_, err := url.ParseRequestURI(path) |
|
17
|
|
|
if err != nil { |
|
18
|
|
|
return newDictionaryFromLocalFile(path) |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
return newDictionaryFromRemoteFile(path, doer) |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
func newDictionaryFromLocalFile(path string) ([]string, error) { |
|
25
|
|
|
file, err := os.Open(path) |
|
26
|
|
|
if err != nil { |
|
27
|
|
|
return nil, errors.Wrapf(err, "dictionary: unable to open: %s", path) |
|
28
|
|
|
} |
|
29
|
|
|
defer file.Close() |
|
30
|
|
|
|
|
31
|
|
|
return dictionaryFromReader(file), nil |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
func dictionaryFromReader(reader io.Reader) []string { |
|
35
|
|
|
entries := make([]string, 0) |
|
36
|
|
|
scanner := bufio.NewScanner(reader) |
|
37
|
|
|
for scanner.Scan() { |
|
38
|
|
|
line := scanner.Text() |
|
39
|
|
|
if isAComment(line) { |
|
40
|
|
|
continue |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
entries = append(entries, line) |
|
44
|
|
|
} |
|
45
|
|
|
return entries |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
func newDictionaryFromRemoteFile(path string, doer Doer) ([]string, error) { |
|
49
|
|
|
req, err := http.NewRequest(http.MethodGet, path, nil) |
|
50
|
|
|
if err != nil { |
|
51
|
|
|
return nil, errors.Wrapf(err, "dictionary: failed to build request for `%s`", path) |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
res, err := doer.Do(req) |
|
55
|
|
|
if err != nil { |
|
56
|
|
|
return nil, errors.Wrapf(err, "dictionary: failed to get `%s`", path) |
|
57
|
|
|
} |
|
58
|
|
|
defer res.Body.Close() |
|
59
|
|
|
|
|
60
|
|
|
statusCode := res.StatusCode |
|
61
|
|
|
if statusCode > 299 || statusCode < 200 { |
|
62
|
|
|
return nil, errors.Errorf( |
|
63
|
|
|
"dictionary: failed to retrieve from `%s`, status code %d", |
|
64
|
|
|
path, |
|
65
|
|
|
statusCode, |
|
66
|
|
|
) |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
return dictionaryFromReader(res.Body), nil |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
func isAComment(line string) bool { |
|
73
|
|
|
return line[0:1] == commentPrefix |
|
74
|
|
|
} |
|
75
|
|
|
|