-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31 from paketo-buildpacks/helper-helpers
Helper Helpers
- Loading branch information
Showing
5 changed files
with
197 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 variable if it exists, otherwise 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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")) | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters