add content requested and refactor python cataloger to use it

Signed-off-by: Alex Goodman <alex.goodman@anchore.com>
This commit is contained in:
Alex Goodman
2020-12-15 10:59:14 -05:00
parent 82c8a8e17b
commit e4a3e433b6
4 changed files with 132 additions and 73 deletions
+48
View File
@@ -0,0 +1,48 @@
package source
import "sync"
type ContentRequester struct {
request map[Location][]*FileData
lock sync.Mutex
}
func NewContentRequester(data ...*FileData) *ContentRequester {
requester := &ContentRequester{
request: make(map[Location][]*FileData),
}
for _, d := range data {
requester.Add(d)
}
return requester
}
func (b *ContentRequester) Add(data *FileData) {
b.lock.Lock()
defer b.lock.Unlock()
b.request[data.Location] = append(b.request[data.Location], data)
}
func (b *ContentRequester) Execute(resolver ContentResolver) error {
b.lock.Lock()
defer b.lock.Unlock()
var locations = make([]Location, len(b.request))
idx := 0
for l := range b.request {
locations[idx] = l
idx++
}
response, err := resolver.MultipleFileContentsByLocation(locations)
if err != nil {
return err
}
for l, contents := range response {
for i := range b.request[l] {
b.request[l][i].Contents = contents
}
}
return nil
}
+6
View File
@@ -0,0 +1,6 @@
package source
type FileData struct {
Location Location
Contents string
}