Skip to content

Commit

Permalink
Adds Candidates function to generic emitter
Browse files Browse the repository at this point in the history
- Adds functionality that deduplicates printing entries with the same
version source and version to help decluter logging
  • Loading branch information
ForestEckhardt authored and sophiewigmore committed Feb 5, 2021
1 parent ed7ff1b commit 3d9483e
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 0 deletions.
42 changes: 42 additions & 0 deletions scribe/emitter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package scribe

import (
"io"
"strconv"
"time"

"github.com/paketo-buildpacks/packit"
Expand Down Expand Up @@ -41,3 +42,44 @@ func (e Emitter) SelectedDependency(entry packit.BuildpackPlanEntry, dependency
}
e.Break()
}

func (e Emitter) Candidates(entries []packit.BuildpackPlanEntry) {
e.Subprocess("Candidate version sources (in priority order):")

var (
sources [][2]string
maxLen int
)

Entries:
for _, entry := range entries {
versionSource, ok := entry.Metadata["version-source"].(string)
if !ok {
versionSource = "<unknown>"
}

if len(versionSource) > maxLen {
maxLen = len(versionSource)
}

version, ok := entry.Metadata["version"].(string)
if !ok {
version = ""
}

source := [2]string{versionSource, version}
for _, s := range sources {
if s == source {
continue Entries
}
}

sources = append(sources, source)
}

for _, source := range sources {
e.Action(("%-" + strconv.Itoa(maxLen) + "s -> %q"), source[0], source[1])
}

e.Break()
}
71 changes: 71 additions & 0 deletions scribe/emitter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,75 @@ func testEmitter(t *testing.T, context spec.G, it spec.S) {
})
})
})

context("Candidates", func() {
it("logs the candidate entries", func() {
emitter.Candidates([]packit.BuildpackPlanEntry{
{
Metadata: map[string]interface{}{
"version-source": "some-source",
"version": "some-version",
},
},
{
Metadata: map[string]interface{}{
"version": "other-version",
},
},
})
Expect(buffer.String()).To(Equal(` Candidate version sources (in priority order):
some-source -> "some-version"
<unknown> -> "other-version"
`))
})

context("when there are deuplicate version sources with the same version", func() {
it("logs the candidate entries and removes duplicates", func() {
emitter.Candidates([]packit.BuildpackPlanEntry{
{
Metadata: map[string]interface{}{
"version-source": "some-source",
"version": "some-version",
},
},
{
Metadata: map[string]interface{}{
"version-source": "some-source",
"version": "other-version",
},
},
{
Metadata: map[string]interface{}{
"version-source": "other-source",
"version": "some-version",
},
},
{
Metadata: map[string]interface{}{
"version": "other-version",
},
},
{
Metadata: map[string]interface{}{
"version": "other-version",
},
},
{
Metadata: map[string]interface{}{
"version-source": "some-source",
"version": "some-version",
},
},
})
Expect(buffer.String()).To(Equal(` Candidate version sources (in priority order):
some-source -> "some-version"
some-source -> "other-version"
other-source -> "some-version"
<unknown> -> "other-version"
`), buffer.String())
})
})
})
}

0 comments on commit 3d9483e

Please sign in to comment.