/
utils.go
141 lines (124 loc) · 3.53 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
package portfoliodb
import (
"io/ioutil"
"net/url"
"os"
"path"
"path/filepath"
"regexp"
"strings"
"github.com/xeipuuv/gojsonschema"
)
// ReadFileBytes reads the content of ``filepath`` and returns the contents as a byte array
func ReadFileBytes(filepath string) ([]byte, error) {
file, err := os.Open(filepath)
if err != nil {
return []byte{}, err
}
defer file.Close()
b, err := ioutil.ReadAll(file)
return b, nil
}
// ReadFile reads the content of ``filepath`` and returns the contents as a string
func ReadFile(filepath string) (string, error) {
content, err := ReadFileBytes(filepath)
if err != nil {
return "", err
}
return string(content), nil
}
// WriteFile writes content to file filepath
func WriteFile(filename string, content []byte) error {
absfilepath, err := filepath.Abs(filename)
f, err := os.Create(absfilepath)
if err != nil {
return err
}
_, err = f.Write(content)
if err != nil {
f.Close()
return err
}
err = f.Close()
if err != nil {
return err
}
return nil
}
// ValidateWithJSONSchema checks if the JSON document ``document`` conforms to the JSON schema ``schema``
func ValidateWithJSONSchema(document string, schema string) (bool, []gojsonschema.ResultError, error) {
schemaLoader := gojsonschema.NewStringLoader(schema)
documentLoader := gojsonschema.NewStringLoader(document)
result, err := gojsonschema.Validate(schemaLoader, documentLoader)
if err != nil {
return false, nil, err
}
if result.Valid() {
return true, nil, nil
}
var errorMessages []gojsonschema.ResultError
for _, desc := range result.Errors() {
errorMessages = append(errorMessages, desc)
}
return false, errorMessages, nil
}
// FileExists checks if the file at ``filepath`` exists, and returns ``true`` if it exists or ``false`` otherwise
func FileExists(filepath string) bool {
if _, err := os.Stat(filepath); os.IsNotExist(err) {
return false
}
return true
}
// RegexpMatches checks if s matches the regex regex at least once
func RegexpMatches(regex string, s string) bool {
p := regexp.MustCompile(regex)
return p.MatchString(s)
}
// RegexpGroups returns all the capture groups' contents from the first match of regex regex in s. The first element [0] is the entire match. [1] is the first capture group's content, et cætera.
func RegexpGroups(regex string, s string) []string {
p := regexp.MustCompile(regex)
return p.FindStringSubmatch(s)
}
// IsValidURL tests a string to determine if it is a well-structured url or not.
func IsValidURL(URL string) bool {
_, err := url.ParseRequestURI(URL)
if err != nil {
return false
}
u, err := url.Parse(URL)
if err != nil || u.Scheme == "" || u.Host == "" {
return false
}
return true
}
// StringInSlice checks if needle is in haystack
func StringInSlice(haystack []string, needle string) bool {
for _, v := range haystack {
if v == needle {
return true
}
}
return false
}
// FilterSlice returns a slice of strings containing only the elements that return true when called with cond.
func FilterSlice(s []string, cond func(string) bool) []string {
filtered := make([]string, 0)
for _, item := range s {
if cond(item) {
filtered = append(filtered, item)
}
}
return filtered
}
// MapKeys returns a slice of strings containing the map's keys
func MapKeys(m map[string]string) []string {
keys := make([]string, 0)
for k := range m {
keys = append(keys, k)
}
return keys
}
// FilepathBaseNoExt returns the basename of pth with the extension removed
func FilepathBaseNoExt(pth string) string {
return strings.TrimSuffix(filepath.Base(pth), path.Ext(pth))
}