Skip to content

Commit

Permalink
Load previous mixin feed data when generating
Browse files Browse the repository at this point in the history
  • Loading branch information
carolynvs-msft committed Apr 23, 2019
1 parent ad1fdf5 commit 863e19d
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 12 deletions.
3 changes: 2 additions & 1 deletion pkg/mixin/feed/feed.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type MixinFeed struct {

func NewMixinFeed(cxt *context.Context) *MixinFeed {
return &MixinFeed{
Index: make(map[string]map[string]*MixinFileset),
Context:cxt,
}
}
Expand All @@ -39,7 +40,7 @@ func (feed *MixinFeed) Search(mixin string, version string) *MixinFileset {
type MixinFileset struct {
Mixin string
Version string
Files []MixinFile
Files []*MixinFile
}

func (f *MixinFileset) FindDownloadURL(os string, arch string) *url.URL {
Expand Down
28 changes: 26 additions & 2 deletions pkg/mixin/feed/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,20 @@ func (o *GenerateOptions) ValidateTemplateFile(cxt *context.Context) error {
}

func (feed *MixinFeed) Generate(opts GenerateOptions) error {
// Check if the atom file already exists, and load in the existing data first
existingFeed, err := feed.FileSystem.Exists(opts.AtomFile)
if err != nil {
return err
}
if existingFeed {
err := feed.Load(opts.AtomFile)
if err != nil {
return err
}
}

mixinRegex := regexp.MustCompile(`(.*/)?(.+)/([a-z]+)-(linux|windows|darwin)-(amd64)(\.exe)?`)

feed.Index = make(map[string]map[string]*MixinFileset)
return feed.FileSystem.Walk(opts.SearchDirectory, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
Expand All @@ -66,6 +77,7 @@ func (feed *MixinFeed) Generate(opts GenerateOptions) error {
version := matches[2]
mixin := matches[3]
filename := info.Name()
updated := info.ModTime()

versions, ok := feed.Index[mixin]
if !ok {
Expand All @@ -81,7 +93,17 @@ func (feed *MixinFeed) Generate(opts GenerateOptions) error {
}
versions[version] = fileset
}
fileset.Files = append(fileset.Files, MixinFile{File: filename, Updated: info.ModTime()})

// Check if the file is already in the feed
for _, file := range fileset.Files {
// The file is already in the feed, bump the timestamp and move on
if file.File == filename && file.Updated.After(updated) {
file.Updated = updated
return nil
}
}
// Add the file to the feed's index
fileset.Files = append(fileset.Files, &MixinFile{File: filename, Updated: updated})
}

return nil
Expand All @@ -105,6 +127,8 @@ func (feed *MixinFeed) Save(opts GenerateOptions) error {
}
sort.Sort(sort.Reverse(entries))

sort.Strings(mixins)

tmplData["Mixins"] = mixins
tmplData["Entries"] = entries
tmplData["Updated"] = entries[0].Updated()
Expand Down
42 changes: 39 additions & 3 deletions pkg/mixin/feed/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,24 +66,60 @@ func TestGenerate(t *testing.T) {
assert.Equal(t, wantXml, gotXml)
}

func TestGenerate_ExistingFeed(t *testing.T) {
tc := context.NewTestContext(t)
tc.AddTestFile("testdata/atom-template.xml", "template.xml")
tc.AddTestFile("testdata/atom-existing.xml", "atom.xml")

tc.FileSystem.Create("bin/v1.2.4/helm-darwin-amd64")
tc.FileSystem.Create("bin/v1.2.4/helm-linux-amd64")
tc.FileSystem.Create("bin/v1.2.4/helm-windows-amd64.exe")

up4, _ := time.Parse("2006-Jan-02", "2013-Feb-04")
tc.FileSystem.Chtimes("bin/v1.2.4/helm-darwin-amd64", up4, up4)
tc.FileSystem.Chtimes("bin/v1.2.4/helm-linux-amd64", up4, up4)
tc.FileSystem.Chtimes("bin/v1.2.4/helm-windows-amd64.exe", up4, up4)

opts := GenerateOptions{
AtomFile: "atom.xml",
SearchDirectory: "bin",
TemplateFile: "template.xml",
}
f := NewMixinFeed(tc.Context)
err := f.Generate(opts)
require.NoError(t, err)
err = f.Save(opts)
require.NoError(t, err)

b, err := tc.FileSystem.ReadFile("atom.xml")
require.NoError(t, err)
gotXml := string(b)

b, err = ioutil.ReadFile("testdata/atom.xml")
require.NoError(t, err)
wantXml := string(b)

assert.Equal(t, wantXml, gotXml)
}

func TestMixinEntries_Sort(t *testing.T) {
up2, _ := time.Parse("2006-Jan-02", "2013-Feb-02")
up3, _ := time.Parse("2006-Jan-02", "2013-Feb-03")
up4, _ := time.Parse("2006-Jan-02", "2013-Feb-04")

entries := MixinEntries{
{
Files: []MixinFile{
Files: []*MixinFile{
{Updated: up3},
},
},
{
Files: []MixinFile{
Files: []*MixinFile{
{Updated: up2},
},
},
{
Files: []MixinFile{
Files: []*MixinFile{
{Updated: up4},
},
},
Expand Down
7 changes: 4 additions & 3 deletions pkg/mixin/feed/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"fmt"
"net/url"
"path"

"github.com/mmcdole/gofeed/atom"
"github.com/pkg/errors"
Expand All @@ -30,7 +31,6 @@ func (feed *MixinFeed) Load(file string) error {
feed.Mixins = append(feed.Mixins, category.Term)
}

feed.Index = make(map[string]map[string]*MixinFileset)
for _, entry := range atomFeed.Entries {
fileset := &MixinFileset{}

Expand All @@ -56,7 +56,7 @@ func (feed *MixinFeed) Load(file string) error {
continue
}

fileset.Files = make([]MixinFile, 0, len(entry.Links))
fileset.Files = make([]*MixinFile, 0, len(entry.Links))
for _, link := range entry.Links {
if link.Rel == "download" {
if entry.UpdatedParsed == nil {
Expand All @@ -74,9 +74,10 @@ func (feed *MixinFeed) Load(file string) error {
continue
}

file := MixinFile{
file := &MixinFile{
URL: parsedUrl,
Updated: *entry.UpdatedParsed,
File: path.Base(parsedUrl.Path),
}
fileset.Files = append(fileset.Files, file)
}
Expand Down
32 changes: 32 additions & 0 deletions pkg/mixin/feed/testdata/atom-existing.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<feed xmlns="http://www.w3.org/2005/Atom">
<id>https://porter.sh/mixins</id>
<title>DeisLabs Mixins</title>
<updated>2013-02-03T00:00:00Z</updated>
<link rel="self" href="https://porter.sh/mixins/atom.xml"/>
<author>
<name>DeisLabs</name>
<uri>https://deislabs.io</uri>
</author>
<category term="exec"/>
<category term="helm"/>
<entry>
<id>https://porter.sh/mixins/v1.2.3/helm</id>
<title>helm @ v1.2.3</title>
<updated>2013-02-03T00:00:00Z</updated>
<category term="helm"/>
<content>v1.2.3</content>
<link rel="download" href="https://porter.sh/mixins/v1.2.3/helm-darwin-amd64" />
<link rel="download" href="https://porter.sh/mixins/v1.2.3/helm-linux-amd64" />
<link rel="download" href="https://porter.sh/mixins/v1.2.3/helm-windows-amd64.exe" />
</entry>
<entry>
<id>https://porter.sh/mixins/v1.2.3/exec</id>
<title>exec @ v1.2.3</title>
<updated>2013-02-02T00:00:00Z</updated>
<category term="exec"/>
<content>v1.2.3</content>
<link rel="download" href="https://porter.sh/mixins/v1.2.3/exec-darwin-amd64" />
<link rel="download" href="https://porter.sh/mixins/v1.2.3/exec-linux-amd64" />
<link rel="download" href="https://porter.sh/mixins/v1.2.3/exec-windows-amd64.exe" />
</entry>
</feed>
8 changes: 5 additions & 3 deletions pkg/porter/mixins.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,14 @@ func (p *Porter) InstallMixin(opts mixin.InstallOptions) error {
}

func (p *Porter) GenerateMixinFeed(opts feed.GenerateOptions) error {
f := feed.MixinFeed{}
err := f.Generate(opts, p.Context)
f := feed.NewMixinFeed(p.Context)

err := f.Generate(opts)
if err != nil {
return err
}
return f.Save(opts, p.Context)

return f.Save(opts)
}

func (p *Porter) CreateMixinFeedTemplate() error {
Expand Down

0 comments on commit 863e19d

Please sign in to comment.