forked from heroku/shh
/
utils.go
204 lines (169 loc) · 4.39 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package shh
import (
"log"
"net/url"
"os"
"regexp"
"sort"
"strconv"
"strings"
"time"
"github.com/freeformz/filechan"
"github.com/heroku/slog"
)
var (
NonWord = regexp.MustCompile("\\W")
)
func FatalError(ctx slog.Context, err error, msg interface{}) {
ctx["error"] = err
ctx["message"] = msg
ErrLogger.Fatal(ctx)
}
func LogError(ctx slog.Context, err error, msg interface{}) {
ctx["error"] = err
ctx["message"] = msg
ErrLogger.Println(ctx)
delete(ctx, "error")
delete(ctx, "message")
}
func Fields(line string) []string {
var insideParens = false
return strings.FieldsFunc(line, func(s rune) bool {
switch s {
case '(':
insideParens = true
case ')':
insideParens = false
case ';', ':', ' ', '\t', '\n':
return insideParens == false
}
return false
})
}
func SliceContainsString(ss []string, s string) bool {
for _, v := range ss {
if v == s {
return true
}
}
return false
}
func Ui64toa(val uint64) string {
return strconv.FormatUint(val, 10)
}
func Atofloat64(s string) float64 {
val, err := strconv.ParseFloat(s, 64)
if err != nil {
FatalError(slog.Context{"fn": "Atofloat64", "input": s}, err, "converting string to float64")
}
return val
}
func PercentFormat(val float64) string {
return strconv.FormatFloat(val, 'f', 2, 64)
}
func Atouint64(s string) uint64 {
val, err := strconv.ParseUint(s, 10, 64)
if err != nil {
FatalError(slog.Context{"fn": "Atouint64", "input": s}, err, "converting string to uint64")
log.Fatal(err)
}
return val
}
// Checks to see if a path exists or not
func Exists(path string) bool {
_, err := os.Stat(path)
return !os.IsNotExist(err)
}
// Returns the value of $env from the OS and if it's empty, returns def
func GetEnvWithDefault(env string, def string) string {
tmp := os.Getenv(env)
if tmp == "" {
return def
}
return tmp
}
// Returns the value of $env from the OS and if it's empty, returns def
func GetEnvWithDefaultInt(env string, def int) int {
tmp := os.Getenv(env)
if tmp == "" {
return def
}
i, err := strconv.Atoi(tmp)
if err != nil {
FatalError(slog.Context{"fn": "GetEnvWithDefaultInt", "env": env, "def": def}, err, "converting atoi")
}
return i
}
// Returns the value of $env from the OS and if it's empty, returns def
func GetEnvWithDefaultBool(env string, def bool) bool {
tmp := os.Getenv(env)
if tmp == "" {
return def
}
b, err := strconv.ParseBool(tmp)
if err != nil {
FatalError(slog.Context{"fn": "GetEnvWithDefaultBool", "env": env, "def": def}, err, "converting atob")
}
return b
}
func GetEnvWithDefaultDuration(env string, def string) time.Duration {
tmp := os.Getenv(env)
if tmp == "" {
tmp = def
}
d, err := time.ParseDuration(tmp)
if err != nil {
FatalError(slog.Context{"fn": "GetEnvWithDefaultDuration", "env": env, "def": def}, err, "not a valid duration")
}
return d
}
// Returns a slice of sorted strings from the environment or default split on ,
// So "foo,bar" returns ["bar","foo"]
func GetEnvWithDefaultStrings(env string, def string) []string {
env = GetEnvWithDefault(env, def)
if env == "" {
return make([]string, 0)
}
tmp := strings.Split(env, ",")
if !sort.StringsAreSorted(tmp) {
sort.Strings(tmp)
}
return tmp
}
// Returns a *regexp.Regexp compiled from the env or default
func GetEnvWithDefaultRegexp(env string, def string) *regexp.Regexp {
env = GetEnvWithDefault(env, def)
re, err := regexp.Compile(env)
if err != nil {
FatalError(slog.Context{"fn": "GetEnvWithDefaultRegexp", "env": env, "def": def}, err, "not a valid regex")
}
return re
}
// Returns a *url.URL representing the given env, or nil if empty
func GetEnvWithDefaultURL(env string, def string) *url.URL {
env = GetEnvWithDefault(env, def)
if env == "" {
return nil
}
parsed, err := url.Parse(env)
if err != nil {
FatalError(slog.Context{"fn": "GetEnvWithDefaultURL", "env": env, "def": def}, err, "not a valid URL")
}
return parsed
}
// Small wrapper to handle errors on open
func FileLineChannel(fpath string) <-chan string {
c, err := filechan.FileLineChannel(fpath)
if err != nil {
FatalError(slog.Context{"fn": "FileLineChannel", "fpath": fpath}, err, "creating FileLineChannel")
}
return c
}
func FixUpName(name string) []string {
name = strings.ToLower(name)
name = strings.Replace(name, "(", ".", -1)
name = strings.Replace(name, ")", "", -1)
name = strings.Replace(name, "_", ".", -1)
name = strings.TrimLeft(name, ".")
return strings.Split(name, ".")
}