-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlabels.go
46 lines (36 loc) · 947 Bytes
/
labels.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
package main
import (
"errors"
"os"
"path/filepath"
"strings"
)
func readLabelsFromFile(path string) []string {
fileBytes, err := fsutil.ReadFile(path)
if err != nil {
return []string{""}
}
return strings.Split(string(fileBytes), "\n")
}
func pathToLabel(path string) string {
// Strip leading label directory (defaults to 'labels/') from the file path
return strings.Replace(path, labelDir+"/", "", 1)
}
// Construct a label map in the form of label: [dependent labels...] gleaned
// from the filesystem
func buildPossibleLabelMap() (map[string][]string, error) {
labelMap := make(map[string][]string)
err := filepath.Walk(labelDir, func(path string, info os.FileInfo, err error) error {
if !info.IsDir() {
labelMap[pathToLabel(path)] = readLabelsFromFile(path)
}
return nil
})
if err != nil {
return nil, err
}
if len(labelMap) == 0 {
return nil, errors.New("no labels found")
}
return labelMap, nil
}