-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathoverrides.go
66 lines (57 loc) · 1.57 KB
/
overrides.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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
//
// Contributor:
// - Brandon Myers [email protected]
package main
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
)
type override struct {
cidr string
city string
country string
latitude float64
longitude float64
}
func readOverrides(path string) (overrides []override, err error) {
file, err := os.Open(path)
if err != nil {
fmt.Fprintf(os.Stderr, "Error opening file: %v\n", err)
return nil, err
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line_contents := scanner.Text()
if strings.HasPrefix(line_contents, "#") {
// The line is a comment, so we'll skip it
continue
}
elements := strings.Split(line_contents, ",")
elements_len := len(elements)
if elements_len != 5 {
return nil, fmt.Errorf("Line must have 5 comma separated elements: %v", line_contents)
}
cidr := elements[0]
city := elements[1]
country := elements[2]
latitude, lat_err := strconv.ParseFloat(elements[3], 64)
if lat_err != nil {
fmt.Fprintf(os.Stderr, "Error converting latitude: %v\n", lat_err)
return nil, lat_err
}
longitude, long_err := strconv.ParseFloat(elements[4], 64)
if long_err != nil {
fmt.Fprintf(os.Stderr, "Error converting longitude: %v\n", long_err)
return nil, long_err
}
overrides = append(overrides, override{cidr, city, country, latitude, longitude})
}
return overrides, nil
}