Skip to content

Commit

Permalink
Merge pull request #81 from paketo-buildpacks/bindings
Browse files Browse the repository at this point in the history
Add Logging of Bindings Information for better UX
  • Loading branch information
pivotal-david-osullivan authored Sep 6, 2021
2 parents cb97503 + e9ffc88 commit e01c381
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 2 deletions.
34 changes: 34 additions & 0 deletions bindings/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,13 @@ package bindings

import (
"fmt"
"os"
"sort"
"strings"

"github.com/heroku/color"
"github.com/paketo-buildpacks/libpak/bard"

"github.com/buildpacks/libcnb"
)

Expand All @@ -45,17 +50,46 @@ func OfProvider(p string) Predicate {
// Resolve returns all bindings from binds that match every Predicate in predicates.
func Resolve(binds libcnb.Bindings, predicates ...Predicate) libcnb.Bindings {
var result libcnb.Bindings
logger := bard.NewLogger(os.Stdout)

// deep copy
for _, bind := range binds {
result = append(result, bind)
}

// filter on predicates
for _, p := range predicates {
result = filter(result, p)
}

LogBindings(result, logger)
return result
}

func LogBindings(bindings libcnb.Bindings, logger bard.Logger) {
f := color.New(color.Bold)

if len(bindings) > 0 {
logger.Header(f.Sprint("Bindings Identified:"))
} else {
logger.Header(f.Sprint("No Bindings Found"))
}

var s []string
for _, binding := range bindings {

for k := range binding.Secret {
s = append(s, k)
}
sort.Strings(s)

logger.Bodyf("Name: %s", binding.Name)
logger.Bodyf("Keys: %s", s)
s = nil
}

}

// ResolveOne returns a single binding from bindings that match every Predicate if present. If exactly one match is found
// ResolveOne returns the binding and true. If zero matches are found, ResolveOne returns an empty binding and false.
// An error is returned if multiple matches are found.
Expand Down
46 changes: 44 additions & 2 deletions bindings/resolve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@
package bindings_test

import (
"bytes"
"testing"

"github.com/paketo-buildpacks/libpak/bard"

"github.com/buildpacks/libcnb"
. "github.com/onsi/gomega"
"github.com/sclevine/spec"
Expand All @@ -29,8 +32,9 @@ import (
func testResolve(t *testing.T, context spec.G, it spec.S) {
var (
Expect = NewWithT(t).Expect

binds libcnb.Bindings
b *bytes.Buffer
logger bard.Logger
binds libcnb.Bindings
)

it.Before(func() {
Expand Down Expand Up @@ -163,4 +167,42 @@ func testResolve(t *testing.T, context spec.G, it spec.S) {
})
})
})

context("bindings are logged", func() {

it.Before(func() {
b = bytes.NewBuffer(nil)
logger = bard.NewLogger(b)
binds = []libcnb.Binding{
{
Name: "name1",
Type: "some-type",
Provider: "some-provider",
Secret: map[string]string{"my-key1": "my-sec1"},
},
{
Name: "name2",
Type: "some-type",
Provider: "some-provider",
Secret: map[string]string{"my-key2": "my-sec2"},
},
}
})

it("checks that binding info is logged", func() {

bindings.LogBindings(binds, logger)
Expect(b.String()).To(ContainSubstring("name1"))
Expect(b.String()).To(ContainSubstring("my-key1"))

Expect(b.String()).To(ContainSubstring("name2"))
Expect(b.String()).To(ContainSubstring("my-key2"))
})

it("checks that nothing is logged is no bindings found", func() {
binds = []libcnb.Binding{}
bindings.LogBindings(binds, logger)
Expect(b.String()).To(ContainSubstring("No Bindings Found"))
})
})
}

0 comments on commit e01c381

Please sign in to comment.