-
Notifications
You must be signed in to change notification settings - Fork 6
/
read.go
61 lines (57 loc) · 1.72 KB
/
read.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
package templates
import (
"encoding/json"
"io/fs"
"mvdan.cc/xurls/v2"
"os"
"path/filepath"
"strings"
)
func readFileContents(path string, info fs.FileInfo, err error) ([]byte, error) {
if err != nil || info.IsDir() {
return []byte{}, err
}
fileBytes, err := os.ReadFile(path)
if err != nil {
return []byte{}, err
}
return fileBytes, nil
}
func WalkTemplates(processTemplate func(template *Template)) {
_ = filepath.Walk(GetOsdServiceLogTemplatesDir(), func(path string, info fs.FileInfo, err error) error {
fileBytes, err := readFileContents(path, info, err)
if err != nil || len(fileBytes) == 0 {
return err
}
template := &Template{SourcePath: GetRelativePathForManagedNotifications(path)}
err = json.Unmarshal(fileBytes, template)
if err == nil {
processTemplate(template)
}
return nil
})
}
func FindReferencingV4SOPs() map[string][]string {
urls := map[string][]string{}
_ = filepath.Walk(GetOpsSOPDir()+string(os.PathSeparator)+"v4"+string(os.PathSeparator)+"alerts", func(path string, info fs.FileInfo, err error) error {
fileContents, err := readFileContents(path, info, err)
if err != nil {
return err
}
rxStrict := xurls.Strict()
for _, link := range rxStrict.FindAllString(string(fileContents), -1) {
if strings.Contains(link, managedNotificationsDirName) {
normalizedManagedNotificationPath := GetRelativePathForManagedNotifications(link)
if _, ok := urls[normalizedManagedNotificationPath]; !ok {
urls[normalizedManagedNotificationPath] = make([]string, 0, 1)
}
urls[normalizedManagedNotificationPath] = append(
urls[normalizedManagedNotificationPath],
strings.TrimSuffix(info.Name(), filepath.Ext(info.Name())),
)
}
}
return nil
})
return urls
}