generated from SAP/repository-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
findtemplates.go
69 lines (61 loc) · 1.53 KB
/
findtemplates.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
package generate
import (
"fmt"
"os"
"sort"
"strings"
"github.com/SAP/configuration-tools-for-gitops/v2/pkg/files"
)
func findTemplates(
basepath, tmplIdentifier string, includeFilters, excludeFilters []string,
) (map[string][]template, error) {
include := []string{tmplIdentifier}
include = append(include, includeFilters...)
list, err := files.New(basepath).
Include(files.AND, include).
Exclude(files.OR, excludeFilters).
Execute()
if err != nil {
return nil, err
}
filteredTemplates := list.Content()
res := make(map[string][]template, len(filteredTemplates))
for path, file := range filteredTemplates {
if file.IsDir {
continue
}
addTemplate(res, path, tmplIdentifier)
}
for k, v := range res {
sort.Slice(v, func(i, j int) bool {
return v[i].source < v[j].source
})
res[k] = v
}
return res, nil
}
func addTemplate(res map[string][]template, path, tmplIdentifier string) {
pathSlice := strings.Split(path, string(os.PathSeparator))
var i int
for i = len(pathSlice) - 1; i >= 0; i-- {
if strings.Contains(pathSlice[i], tmplIdentifier) {
break
}
}
b := strings.Join(pathSlice[:i], string(os.PathSeparator))
s := ""
if i < len(pathSlice)-1 {
s = fmt.Sprintf("%s%s",
string(os.PathSeparator),
strings.Join(pathSlice[i+1:], string(os.PathSeparator)),
)
}
pre := strings.Replace(pathSlice[i], tmplIdentifier, "", 1)
t := template{source: path, basepath: b, namePrefix: pre, subpath: s}
templates, ok := res[b]
if ok {
res[b] = append(templates, t)
} else {
res[b] = []template{t}
}
}