forked from security/hakurei
This is useful for testing, where report testdata is not available. Signed-off-by: Ophestra <cat@gensokyo.uk>
87 lines
2.0 KiB
Go
87 lines
2.0 KiB
Go
package main
|
|
|
|
import (
|
|
"cmp"
|
|
"slices"
|
|
"unique"
|
|
|
|
"hakurei.app/internal/pkg"
|
|
"hakurei.app/internal/rosa"
|
|
)
|
|
|
|
type SortOrders int
|
|
|
|
const (
|
|
DeclarationAscending SortOrders = iota
|
|
DeclarationDescending
|
|
NameAscending
|
|
NameDescending
|
|
limitSortOrders
|
|
)
|
|
|
|
type PackageIndex struct {
|
|
sorts [limitSortOrders][rosa.PresetUnexportedStart]*PackageIndexEntry
|
|
names map[string]*PackageIndexEntry
|
|
}
|
|
|
|
type PackageIndexEntry struct {
|
|
Name string `json:"name"`
|
|
Description string `json:"description,omitempty"`
|
|
Website string `json:"website,omitempty"`
|
|
Version string `json:"version"`
|
|
HasReport bool `json:"report,omitempty"`
|
|
ident unique.Handle[pkg.ID] `json:"-"`
|
|
status []byte `json:"-"`
|
|
}
|
|
|
|
func createPackageIndex(cache *pkg.Cache, report *rosa.Report) (index *PackageIndex, err error) {
|
|
index = new(PackageIndex)
|
|
index.names = make(map[string]*PackageIndexEntry, rosa.PresetUnexportedStart)
|
|
work := make([]PackageIndexEntry, rosa.PresetUnexportedStart)
|
|
if report != nil {
|
|
defer report.HandleAccess(&err)()
|
|
}
|
|
|
|
for p := range rosa.PresetUnexportedStart {
|
|
m := rosa.GetMetadata(p)
|
|
v := rosa.Std.Version(p)
|
|
a := rosa.Std.Load(p)
|
|
entry := PackageIndexEntry{
|
|
Name: m.Name,
|
|
Description: m.Description,
|
|
Website: m.Website,
|
|
Version: v,
|
|
}
|
|
|
|
if cache != nil && report != nil {
|
|
entry.ident = cache.Ident(a)
|
|
status, n := report.ArtifactOf(entry.ident)
|
|
if n >= 0 {
|
|
entry.HasReport = true
|
|
entry.status = status
|
|
}
|
|
}
|
|
|
|
work[p] = entry
|
|
index.names[m.Name] = &entry
|
|
}
|
|
for i, p := range work {
|
|
index.sorts[DeclarationAscending][i] = &p
|
|
}
|
|
slices.Reverse(work)
|
|
for i, p := range work {
|
|
index.sorts[DeclarationDescending][i] = &p
|
|
}
|
|
slices.SortFunc(work, func(a PackageIndexEntry, b PackageIndexEntry) int {
|
|
return cmp.Compare(a.Name, b.Name)
|
|
})
|
|
for i, p := range work {
|
|
index.sorts[NameAscending][i] = &p
|
|
}
|
|
slices.Reverse(work)
|
|
for i, p := range work {
|
|
index.sorts[NameDescending][i] = &p
|
|
}
|
|
return
|
|
}
|