-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfactory.go
97 lines (82 loc) · 1.92 KB
/
factory.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
92
93
94
95
96
97
package omnitrail
import (
"io/fs"
"path/filepath"
"sort"
)
type factoryImpl struct {
Options *Options
envelope *Envelope
Plugins []Plugin
AllowList []string
}
func (factory *factoryImpl) Add(originalPath string) error {
// Convert the path to an absolute path
absPath, err := filepath.Abs(originalPath)
if err != nil {
return err
}
// Add the absolute path to the allow list
factory.AllowList = append(factory.AllowList, absPath)
// For each plugin, add the allow list
for _, plugin := range factory.Plugins {
plugin.SetAllowList(factory.AllowList)
}
// check if path already exists in the envelope, if so, return
if _, ok := factory.envelope.Mapping[originalPath]; ok {
return nil
}
err = filepath.WalkDir(originalPath, func(path string, d fs.DirEntry, err error) error {
path, err = filepath.Abs(path)
if err != nil {
return err
}
for _, plugin := range factory.Plugins {
err := plugin.Add(path)
if err != nil {
return err
}
}
return nil
})
if err != nil {
return err
}
// Generate ADGs for directories
// collect all keys in the map
var keys []string
for k := range factory.envelope.Mapping {
keys = append(keys, k)
}
// sort keys by lexical order
sort.Strings(keys)
// stable sort keys by length
sort.SliceStable(keys, func(i, j int) bool {
return len(keys[i]) > len(keys[j])
})
for _, plugin := range factory.Plugins {
err := plugin.Store(factory.envelope)
if err != nil {
return err
}
}
return nil
}
func (factory *factoryImpl) Sha1ADGs() map[string]string {
m := make(map[string]string)
for _, plugin := range factory.Plugins {
plugin.Sha1ADG(m)
}
return m
}
// Sha256ADGs return sha256 omnibor objects
func (factory *factoryImpl) Sha256ADGs() map[string]string {
m := make(map[string]string)
for _, plugin := range factory.Plugins {
plugin.Sha256ADG(m)
}
return m
}
func (factory *factoryImpl) Envelope() *Envelope {
return factory.envelope
}