Passed
Pull Request — master (#21)
by Stefano
02:13
created

dictionary.isAComment   A

Complexity

Conditions 1

Size

Total Lines 2
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nop 1
dl 0
loc 2
rs 10
c 0
b 0
f 0
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) {
0 ignored issues
show
introduced by
exported function NewDictionaryFrom should have comment or be unexported
Loading history...
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