-
Notifications
You must be signed in to change notification settings - Fork 207
/
load.go
98 lines (84 loc) · 2.37 KB
/
load.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
98
package feed
import (
"bytes"
"fmt"
"net/url"
"path"
"github.com/mmcdole/gofeed/atom"
"github.com/pkg/errors"
)
func (feed *MixinFeed) Load(file string) error {
contents, err := feed.FileSystem.ReadFile(file)
if err != nil {
return errors.Wrapf(err, "error reading mixin feed at %s", file)
}
p := atom.Parser{}
atomFeed, err := p.Parse(bytes.NewReader(contents))
if err != nil {
if feed.Debug {
fmt.Fprintln(feed.Err, string(contents))
}
return errors.Wrap(err, "error parsing the mixin feed as an atom xml file")
}
feed.Updated = atomFeed.UpdatedParsed
for _, category := range atomFeed.Categories {
feed.Mixins = append(feed.Mixins, category.Term)
}
for _, entry := range atomFeed.Entries {
fileset := &MixinFileset{}
if len(entry.Categories) == 0 {
if feed.Debug {
fmt.Fprintf(feed.Err, "skipping invalid entry %s, missing category (mixin name)", entry.ID)
}
continue
}
fileset.Mixin = entry.Categories[0].Term
if fileset.Mixin == "" {
if feed.Debug {
fmt.Fprintf(feed.Err, "skipping invalid entry %s, empty category (mixin name)", entry.ID)
}
continue
}
fileset.Version = entry.Content.Value
if fileset.Version == "" {
if feed.Debug {
fmt.Fprintf(feed.Err, "skipping invalid entry %s, empty content (version)", entry.ID)
}
continue
}
fileset.Files = make([]*MixinFile, 0, len(entry.Links))
for _, link := range entry.Links {
if link.Rel == "download" {
if entry.UpdatedParsed == nil {
if feed.Debug {
fmt.Fprintf(feed.Err, "skipping invalid entry %s, invalid updated %q could not be parsed as RFC3339", entry.ID, entry.Updated)
}
continue
}
parsedUrl, err := url.Parse(link.Href)
if err != nil || link.Href == "" {
if feed.Debug {
fmt.Fprintf(feed.Err, "skipping invalid entry %s, invalid link.href %q", entry.ID, link.Href)
}
continue
}
file := &MixinFile{
URL: parsedUrl,
Updated: *entry.UpdatedParsed,
File: path.Base(parsedUrl.Path),
}
fileset.Files = append(fileset.Files, file)
}
}
versions, ok := feed.Index[fileset.Mixin]
if !ok {
versions = map[string]*MixinFileset{}
feed.Index[fileset.Mixin] = versions
}
indexedFileset, ok := versions[fileset.Version]
if !ok || fileset.GetLastUpdated().After(indexedFileset.GetLastUpdated()) {
versions[fileset.Version] = fileset
}
}
return nil
}