-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
64 lines (55 loc) · 1.06 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
package main
import (
"log"
"net/url"
"os"
)
func panicIf(err error) {
if err != nil {
panic(err)
}
}
func exitIf(err error) {
if err != nil {
log.Fatal(err)
}
}
func exitWith(message string) {
log.Fatal(message)
}
// fileExists() returns true if a file exists
func fileExists(filename string) bool {
if _, err := os.Stat(filename); os.IsNotExist(err) {
return false
}
return true
}
// stringIn() checks if str is present in the slice
func stringIn(str string, slice []string) bool {
for _, s := range slice {
if s == str {
return true
}
}
return false
}
// equalSlices checks if two string slices contain equal elements (order does not matter)
func equalSlices(s1 []string, s2 []string) bool {
if len(s1) != len(s2) {
return false
}
for _, s := range s1 {
if !stringIn(s, s2) {
return false
}
}
return true
}
// replaces the hostname part in a given URL with a new host
func replaceHostnameIn(urlString string, hostname string) string {
u, err := url.Parse(urlString)
if err == nil {
u.Host = hostname
}
return u.String()
}