diff options
Diffstat (limited to 'six/helpers.go')
-rw-r--r-- | six/helpers.go | 112 |
1 files changed, 112 insertions, 0 deletions
diff --git a/six/helpers.go b/six/helpers.go new file mode 100644 index 0000000..694a79e --- /dev/null +++ b/six/helpers.go | |||
@@ -0,0 +1,112 @@ | |||
1 | // SPDX-License-Identifier: GPL-2.0-only | ||
2 | // Copyright (C) 2020 Michael Crute <mike@crute.us>. All rights reserved. | ||
3 | // | ||
4 | // Use of this source code is governed by a license that can be found in the | ||
5 | // LICENSE file. | ||
6 | |||
7 | package six | ||
8 | |||
9 | import ( | ||
10 | "net" | ||
11 | "strconv" | ||
12 | "strings" | ||
13 | "time" | ||
14 | ) | ||
15 | |||
16 | func mustParseInt(a string) int { | ||
17 | a = strings.TrimSpace(a) | ||
18 | |||
19 | if a == "" { | ||
20 | return 0 | ||
21 | } | ||
22 | |||
23 | i, err := strconv.Atoi(a) | ||
24 | if err != nil { | ||
25 | panic(err) | ||
26 | } | ||
27 | |||
28 | return i | ||
29 | } | ||
30 | |||
31 | func parseYesNo(b string) bool { | ||
32 | return strings.ToLower(strings.TrimSpace(b)) == "yes" | ||
33 | } | ||
34 | |||
35 | func parseIP(i string) *net.IP { | ||
36 | o := net.ParseIP(i) | ||
37 | return &o | ||
38 | } | ||
39 | |||
40 | func parseIPNetFromCIDR(i string) *net.IPNet { | ||
41 | _, ipnet, _ := net.ParseCIDR(strings.TrimSpace(i)) | ||
42 | return ipnet | ||
43 | } | ||
44 | |||
45 | func mustParseTime(t string) *time.Time { | ||
46 | t = strings.TrimSpace(t) | ||
47 | |||
48 | if t == "" { | ||
49 | return nil | ||
50 | } | ||
51 | |||
52 | i, err := time.Parse("2006-01-02", t) | ||
53 | if err != nil { | ||
54 | panic(err) | ||
55 | } | ||
56 | |||
57 | return &i | ||
58 | } | ||
59 | |||
60 | func mustParseLongTime(t string) *time.Time { | ||
61 | t = strings.TrimSpace(t) | ||
62 | |||
63 | if t == "" { | ||
64 | return nil | ||
65 | } | ||
66 | |||
67 | i, err := time.Parse("2006-01-02 15:04:05 MST", t) | ||
68 | if err != nil { | ||
69 | panic(err) | ||
70 | } | ||
71 | |||
72 | return &i | ||
73 | } | ||
74 | |||
75 | func mustParseLongTimeNoZone(t string) *time.Time { | ||
76 | t = strings.TrimSpace(t) | ||
77 | |||
78 | if t == "" { | ||
79 | return nil | ||
80 | } | ||
81 | |||
82 | i, err := time.Parse("2006-01-02 15:04:05", t) | ||
83 | if err != nil { | ||
84 | panic(err) | ||
85 | } | ||
86 | |||
87 | return &i | ||
88 | } | ||
89 | |||
90 | func allEmpty(d []string) bool { | ||
91 | for _, v := range d { | ||
92 | if strings.TrimSpace(v) != "" { | ||
93 | return false | ||
94 | } | ||
95 | } | ||
96 | return true | ||
97 | } | ||
98 | |||
99 | func parseASPath(p string) []int { | ||
100 | out := []int{} | ||
101 | |||
102 | for _, i := range strings.Split(p, " ") { | ||
103 | ii, err := strconv.Atoi(strings.TrimSpace(i)) | ||
104 | // Some AS paths contain { and } which are not valid and need to be discard | ||
105 | if err != nil { | ||
106 | continue | ||
107 | } | ||
108 | out = append(out, ii) | ||
109 | } | ||
110 | |||
111 | return out | ||
112 | } | ||