Skip to content

Commit

Permalink
Helper Helpers
Browse files Browse the repository at this point in the history
This change adds some more helper functions to sherpa that are particularly
useful in the environment that exec.d's run in.

Signed-off-by: Ben Hale <bhale@vmware.com>
  • Loading branch information
nebhale committed Nov 13, 2020
1 parent efdd029 commit f6770eb
Show file tree
Hide file tree
Showing 5 changed files with 245 additions and 0 deletions.
30 changes: 30 additions & 0 deletions bindings/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,36 @@ import (
// Predicate should return true if it matches a given binding.
type Predicate func(bind libcnb.Binding) bool

// And combines multiple predicates logical and-ing their results together.
func And(predicates ...Predicate) Predicate {
return func(bind libcnb.Binding) bool {
if len(predicates) < 1 {
return false
}

v := predicates[0](bind)
for _, p := range predicates[1:] {
v = v && p(bind)
}
return v
}
}

// Or combines multiple predicates logical or-ing their results together.
func Or(predicates ...Predicate) Predicate {
return func(bind libcnb.Binding) bool {
if len(predicates) < 1 {
return false
}

v := predicates[0](bind)
for _, p := range predicates[1:] {
v = v || p(bind)
}
return v
}
}

// OfType returns a Predicate that returns true if a given binding has Type that matches t. The comparison is
// case-insensitive.
func OfType(t string) Predicate {
Expand Down
56 changes: 56 additions & 0 deletions bindings/resolve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,62 @@ func testResolve(t *testing.T, context spec.G, it spec.S) {
}
})

context("And", func() {
var (
tr = func(bind libcnb.Binding) bool {
return true
}

fa = func(bind libcnb.Binding) bool {
return false
}
)

it("returns false with no predicates", func() {
Expect(bindings.And()(libcnb.Binding{})).To(BeFalse())
})

it("returns value of single predicate", func() {
Expect(bindings.And(tr)(libcnb.Binding{})).To(BeTrue())
Expect(bindings.And(fa)(libcnb.Binding{})).To(BeFalse())
})

it("returns and-ed value of multiple predicates", func() {
Expect(bindings.And(tr, tr)(libcnb.Binding{})).To(BeTrue())
Expect(bindings.And(tr, fa)(libcnb.Binding{})).To(BeFalse())
Expect(bindings.And(fa, tr)(libcnb.Binding{})).To(BeFalse())
Expect(bindings.And(fa, fa)(libcnb.Binding{})).To(BeFalse())
})
})

context("Or", func() {
var (
tr = func(bind libcnb.Binding) bool {
return true
}

fa = func(bind libcnb.Binding) bool {
return false
}
)

it("returns false with no predicates", func() {
Expect(bindings.Or()(libcnb.Binding{})).To(BeFalse())
})

it("returns value of single predicate", func() {
Expect(bindings.Or(tr)(libcnb.Binding{})).To(BeTrue())
Expect(bindings.Or(fa)(libcnb.Binding{})).To(BeFalse())
})

it("returns or-ed value of multiple predicates", func() {
Expect(bindings.Or(tr, tr)(libcnb.Binding{})).To(BeTrue())
Expect(bindings.Or(tr, fa)(libcnb.Binding{})).To(BeTrue())
Expect(bindings.Or(fa, tr)(libcnb.Binding{})).To(BeTrue())
Expect(bindings.Or(fa, fa)(libcnb.Binding{})).To(BeFalse())
})
})

context("Resolve", func() {
context("no predicate", func() {
it("returns all bindings", func() {
Expand Down
52 changes: 52 additions & 0 deletions sherpa/env_var.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright 2018-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package sherpa

import (
"fmt"
"os"
"strings"
)

// AppendToEnvVar appends a collection of values to an env var separated by a delimiter. If the env var does not already
// exist, joins the values with the delimiter and returns the result.
func AppendToEnvVar(name string, delimiter string, values ...string) string {
var e []string
if s, ok := os.LookupEnv(name); ok {
e = append(e, s)
}
e = append(e, values...)
return strings.Join(e, delimiter)
}

// GetEnvRequired returns the value of an environment varible if it exists, otherwire returns an error with a
// predictable message.
func GetEnvRequired(name string) (string, error) {
if s, ok := os.LookupEnv(name); ok {
return s, nil
}

return "", fmt.Errorf("$%s must be set", name)
}

// GetEnvWithWithDefault returns the value of an environment variable if it exists, otherwise returns the default.
func GetEnvWithDefault(name string, def string) string {
if s, ok := os.LookupEnv(name); ok {
return s
}
return def
}
106 changes: 106 additions & 0 deletions sherpa/env_var_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
* Copyright 2018-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package sherpa_test

import (
"os"
"testing"

. "github.com/onsi/gomega"
"github.com/sclevine/spec"

"github.com/paketo-buildpacks/libpak/sherpa"
)

func testEnvVar(t *testing.T, context spec.G, it spec.S) {
var (
Expect = NewWithT(t).Expect
)

context("AppendToEnvVar", func() {

context("No Existing", func() {

it("append one", func() {
Expect(sherpa.AppendToEnvVar("TEST_KEY", "|", "test-value-2")).
To(Equal("test-value-2"))
})

it("appends multiple", func() {
Expect(sherpa.AppendToEnvVar("TEST_KEY", "|", "test-value-2", "test-value-3")).
To(Equal("test-value-2|test-value-3"))
})
})

context("With Existing", func() {
it.Before(func() {
Expect(os.Setenv("TEST_KEY", "test-value-1")).To(Succeed())
})

it.After(func() {
Expect(os.Unsetenv("TEST_KEY")).To(Succeed())
})

it("append one", func() {
Expect(sherpa.AppendToEnvVar("TEST_KEY", "|", "test-value-2")).
To(Equal("test-value-1|test-value-2"))
})

it("appends multiple", func() {
Expect(sherpa.AppendToEnvVar("TEST_KEY", "|", "test-value-2", "test-value-3")).
To(Equal("test-value-1|test-value-2|test-value-3"))
})
})
})

context("GetEnvRequired", func() {
it.Before(func() {
Expect(os.Setenv("TEST_KEY", "test-value")).To(Succeed())
})

it.After(func() {
Expect(os.Unsetenv("TEST_KEY")).To(Succeed())
})

it("returns value if set", func() {
Expect(sherpa.GetEnvRequired("TEST_KEY")).To(Equal("test-value"))
})

it("returns error if not set", func() {
_, err := sherpa.GetEnvRequired("ANOTHER_KEY")
Expect(err).To(MatchError("$ANOTHER_KEY must be set"))
})
})

context("GetEnvWithDefault", func() {
it.Before(func() {
Expect(os.Setenv("TEST_KEY", "test-value")).To(Succeed())
})

it.After(func() {
Expect(os.Unsetenv("TEST_KEY")).To(Succeed())
})

it("returns value if set", func() {
Expect(sherpa.GetEnvWithDefault("TEST_KEY", "default-value")).To(Equal("test-value"))
})

it("returns default value if not set", func() {
Expect(sherpa.GetEnvWithDefault("ANOTHER_KEY", "default-value")).To(Equal("default-value"))
})
})
}
1 change: 1 addition & 0 deletions sherpa/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
func TestUnit(t *testing.T) {
suite := spec.New("libpak/sherpa", spec.Report(report.Terminal{}))
suite("CopyFile", testCopyFile)
suite("EnvVar", testEnvVar)
suite("FileListing", testFileListing)
suite("NodeJS", testNodeJS)
suite("Sherpa", testSherpa)
Expand Down

0 comments on commit f6770eb

Please sign in to comment.