-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
validate.go
81 lines (66 loc) · 1.39 KB
/
validate.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
package postmortems
import (
"fmt"
"net/url"
"os"
"path/filepath"
)
// ValidateDir takes a directory path and validates every file in there.
func ValidateDir(d string) ([]*Postmortem, error) {
var ret []*Postmortem
err := filepath.Walk(d, func(path string, info os.FileInfo, err error) error {
// Failed to open path
if err != nil {
return err
}
if !info.IsDir() {
p, err := ValidateFile(path)
if err != nil {
return err
}
ret = append(ret, p)
}
return nil
})
if err != nil {
return nil, err
}
return ret, err
}
// ValidateFile takes a file path and validates just that file.
func ValidateFile(filename string) (*Postmortem, error) {
f, err := os.Open(filename)
if err != nil {
return nil, err
}
p, err := Parse(f)
if err != nil {
return nil, err
}
if p.URL == "" {
return nil, fmt.Errorf("%s: url is empty", filename)
}
_, err = url.Parse(p.URL)
if err != nil {
return nil, err
}
for _, cat := range p.Categories {
if !CategoriesContain(cat) {
return nil, fmt.Errorf("%s: %s is not a valid category", filename, cat)
}
}
if p.Description == "" {
return nil, fmt.Errorf("%s: description is empty", filename)
}
return p, nil
}
// CategoriesContain takes a string and decides
// if it is in the category whitelist.
func CategoriesContain(e string) bool {
for _, a := range Categories {
if a == e {
return true
}
}
return false
}