-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
65 lines (53 loc) · 1.07 KB
/
utils.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
package main
import (
"bufio"
"fmt"
"io"
"net/http"
"reflect"
)
func isLookupInSliceEqual(a []Lookup) []Lookup {
var l []Lookup
for _, v := range a {
original := v
v.Hostname = a[0].Hostname
v.Time = a[0].Time
if !reflect.DeepEqual(v, a[0]) {
l = append(l, original)
}
}
return l
}
func unwrapLookups(s, f []Lookup) (success, fail string) {
for _, s := range s {
success += fmt.Sprintf("+%+v\n", s)
}
for _, f := range f {
fail += fmt.Sprintf("-%+v\n", f)
}
return success, fail
}
func urlToLines(url string, logger io.Writer) ([]string, error) {
resp, err := http.Get(url)
if err != nil {
return nil, err
}
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
_, _ = fmt.Fprint(logger, fmt.Errorf("error: %w", err))
}
}(resp.Body)
return linesFromReader(resp.Body)
}
func linesFromReader(r io.Reader) ([]string, error) {
var lines []string
scanner := bufio.NewScanner(r)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
if err := scanner.Err(); err != nil {
return nil, err
}
return lines, nil
}