diff --git a/bindings/resolve.go b/bindings/resolve.go index 61b1a5a..c1af42b 100644 --- a/bindings/resolve.go +++ b/bindings/resolve.go @@ -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" ) @@ -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. diff --git a/bindings/resolve_test.go b/bindings/resolve_test.go index c200235..989da52 100644 --- a/bindings/resolve_test.go +++ b/bindings/resolve_test.go @@ -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" @@ -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() { @@ -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")) + }) + }) }