@@ -45,85 +45,43 @@ type Store struct {
4545}
4646
4747// Load reads in all the records serialized in the provided [io.Reader].
48- func Load (ctx context.Context , r io.Reader ) (* Loader , error ) {
49- l := Loader {
50- dec : json . NewDecoder ( r ),
51- cur : uuid . Nil ,
48+ func Load (ctx context.Context , r io.Reader ) (* Store , error ) {
49+ s , err := New ()
50+ if err != nil {
51+ return nil , err
5252 }
53- return & l , nil
54- }
55-
56- // Loader is an iterator that returns a series of [Entry].
57- //
58- // Users should call [*Loader.Next] until it reports false, then check for
59- // errors via [*Loader.Err].
60- type Loader struct {
61- err error
62- e * Entry
63-
64- dec * json.Decoder
65- next * Entry
66- de diskEntry
67- cur uuid.UUID
68- }
6953
70- // Next reports whether there's an [Entry] to be processed.
71- func (l * Loader ) Next () bool {
72- if l .err != nil {
73- return false
54+ l , err := NewLoader (r )
55+ if err != nil {
56+ return nil , err
7457 }
7558
76- for l . err = l . dec . Decode ( & l . de ); l . err == nil ; l . err = l . dec . Decode ( & l . de ) {
77- id := l . de . Ref
78- // If we just hit a new Entry, promote the current one.
79- if id != l . cur {
80- l . e = l . next
81- l . next = & Entry {}
82- l . next . Updater = l . de . Updater
83- l . next . Fingerprint = l . de . Fingerprint
84- l . next . Date = l . de . Date
59+ // TODO(DO NOT MERGE): ~~This implementation might be a bit naive. Currently,
60+ // it basically copies [OfflineImport]. We could probably do some custom
61+ // parsing such that it basically just decodes the json into the
62+ // appropriate [Store] fields.~~
63+ // Actually, this might be the way.
64+ // [OfflineImport]: https://github.com/quay/claircore/blob/126f688bb11220fb34708719be91952dc32ff7b1/libvuln/updates.go#L17-L74
65+ for l . Next () {
66+ if err := ctx . Err (); err != nil {
67+ return nil , err
8568 }
86- switch l .de .Kind {
87- case driver .VulnerabilityKind :
88- vuln := claircore.Vulnerability {}
89- if err := json .Unmarshal (l .de .Vuln .buf , & vuln ); err != nil {
90- l .err = err
91- return false
69+ e := l .Entry ()
70+ if e .Enrichment != nil {
71+ if _ , err = s .UpdateEnrichments (ctx , e .Updater , e .Fingerprint , e .Enrichment ); err != nil {
72+ return nil , fmt .Errorf ("updating enrichements: %w" , err )
9273 }
93- l .next .Vuln = append (l .next .Vuln , & vuln )
94- case driver .EnrichmentKind :
95- en := driver.EnrichmentRecord {}
96- if err := json .Unmarshal (l .de .Enrichment .buf , & en ); err != nil {
97- l .err = err
98- return false
99- }
100- l .next .Enrichment = append (l .next .Enrichment , en )
10174 }
102- // If this was an initial diskEntry, promote the ref.
103- if id != l .cur {
104- l .cur = id
105- // If we have an Entry ready, report that.
106- if l .e != nil {
107- return true
75+ if e .Vuln != nil {
76+ if _ , err = s .UpdateVulnerabilities (ctx , e .Updater , e .Fingerprint , e .Vuln ); err != nil {
77+ return nil , fmt .Errorf ("updating vulnerabilities: %w" , err )
10878 }
10979 }
11080 }
111- l .e = l .next
112- return true
113- }
114-
115- // Entry returns the latest loaded [Entry].
116- func (l * Loader ) Entry () * Entry {
117- return l .e
118- }
119-
120- // Err is the latest encountered error.
121- func (l * Loader ) Err () error {
122- // Don't report EOF as an error.
123- if errors .Is (l .err , io .EOF ) {
124- return nil
81+ if err := l .Err (); err != nil {
82+ return nil , err
12583 }
126- return l . err
84+ return s , nil
12785}
12886
12987// Store writes out the contents of the receiver to the provided [io.Writer].
0 commit comments