-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.go
91 lines (79 loc) · 1.91 KB
/
util.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
package main
import (
"fmt"
log "github.com/sirupsen/logrus"
"os"
"path/filepath"
)
func StringInSlice(s string, slice []string) bool {
for _, i := range slice {
if s == i {
return true
}
}
return false
}
func PrintArrayOnNewlines(a []string) {
for _, v := range a {
fmt.Println(v)
}
}
type walk struct {
prefix string
abstractArray *[]string
key string
searchArgs map[string]Constraint
}
type fmwalk struct {
fm map[string][]string
fullFilepath string
}
func (w walk) WalkFrontMatter(f func(fmwalk), params ...interface{}) error {
walkGenerator := func(...interface{}) filepath.WalkFunc {
return func(fullFilepath string, info os.FileInfo, err error) error {
shouldSkip := ShouldSkipFile(info, err)
if shouldSkip {
return nil
}
recipeFile, err := ParseFile(fullFilepath)
if err != nil {
log.WithFields(log.Fields{
"file": fullFilepath,
}).Warn(err.Error())
return nil
}
frontMatter, err := ParseFrontMatter(recipeFile.FrontMatter)
if err != nil {
log.WithFields(log.Fields{
"file": fullFilepath,
}).Warn(fmt.Sprintf("An error occurred while parsing front matter: %s", err.Error()))
}
fmwalkData := fmwalk{fm: frontMatter, fullFilepath: fullFilepath}
f(fmwalkData)
return nil
}
}
return filepath.Walk(w.prefix, walkGenerator(params))
}
func (w walk) ListKeys(data fmwalk) {
for k, v := range data.fm {
key := fmt.Sprintf("%s (%T)", k, v)
if StringInSlice(key, *w.abstractArray) == false {
*w.abstractArray = append(*w.abstractArray, key)
}
}
}
func (w walk) ListValuesForKey(data fmwalk) {
valueOfKey := data.fm[w.key]
for _, v := range valueOfKey {
if StringInSlice(v, *w.abstractArray) == false {
*w.abstractArray = append(*w.abstractArray, v)
}
}
}
func (w walk) SearchWithArgs(data fmwalk) {
isMatch := Match(w.searchArgs, data.fm)
if isMatch {
fmt.Println(GetBasenameWithoutExt(data.fullFilepath))
}
}