Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
* allows to parse Boolean env. variable, keeping errors, if any
  • Loading branch information
anthonydahanne committed Mar 20, 2023
1 parent 2ff479d commit 2c9d401
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 11 deletions.
22 changes: 18 additions & 4 deletions sherpa/env_var.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,29 @@ func GetEnvWithDefault(name string, def string) string {
// ResolveBool resolves a boolean value for a configuration option. Returns true for 1, t, T, TRUE, true, True. Returns
// false for all other values or unset.
func ResolveBool(name string) bool {
resolveBool, _ := ResolveBoolErr(name)
return resolveBool
}

// ResolveBoolErr resolves a boolean value for a configuration option.
// Returns true, nil for 1, t, T, TRUE, true, True.
// Returns false, nil for all other values or unset.
// Returns false, error is the value could not be parsed into a bool
func ResolveBoolErr(name string) (bool, error) {
s, ok := os.LookupEnv(name)
if !ok {
return false
return false, nil
}

t, err := strconv.ParseBool(s)
t := strings.TrimSpace(s)
p, err := strconv.ParseBool(t)
if err != nil {
return false
return false, fmt.Errorf(
"invalid value '%s' for key '%s': expected one of [1, t, T, TRUE, true, True, 0, f, F, FALSE, false, False]",
s,
name,
)
}

return t
return p, nil
}
37 changes: 30 additions & 7 deletions sherpa/env_var_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,12 @@ func testEnvVar(t *testing.T, context spec.G, it spec.S) {
})
})

context("ResolveBool", func() {
context("ResolveBoolErr", func() {
context("variable not set", func() {
it("returns false if not set", func() {
Expect(sherpa.ResolveBool("TEST_KEY")).To(BeFalse())
boolean, err := sherpa.ResolveBoolErr("TEST_KEY")
Expect(boolean).To(BeFalse())
Expect(err).ToNot(HaveOccurred())
})
})

Expand All @@ -117,9 +119,11 @@ func testEnvVar(t *testing.T, context spec.G, it spec.S) {
})

it("returns true", func() {
for _, form := range []string{"1", "t", "T", "TRUE", "true", "True"} {
for _, form := range []string{"1", "t", "T", "TRUE", "true", "True", "\t1\n"} {
Expect(os.Setenv("TEST_KEY", form))
Expect(sherpa.ResolveBool("TEST_KEY")).To(BeTrue())
boolean, err := sherpa.ResolveBoolErr("TEST_KEY")
Expect(boolean).To(BeTrue())
Expect(err).ToNot(HaveOccurred())
Expect(os.Unsetenv("TEST_KEY")).To(Succeed())
}
})
Expand All @@ -131,9 +135,11 @@ func testEnvVar(t *testing.T, context spec.G, it spec.S) {
})

it("returns false", func() {
for _, form := range []string{"0", "f", "F", "FALSE", "false", "False"} {
for _, form := range []string{"0", "f", "F", "FALSE", "false", "False", "\tF\n"} {
Expect(os.Setenv("TEST_KEY", form))
Expect(sherpa.ResolveBool("TEST_KEY")).To(BeFalse())
boolean, err := sherpa.ResolveBoolErr("TEST_KEY")
Expect(boolean).To(BeFalse())
Expect(err).ToNot(HaveOccurred())
Expect(os.Unsetenv("TEST_KEY")).To(Succeed())
}
})
Expand All @@ -146,10 +152,27 @@ func testEnvVar(t *testing.T, context spec.G, it spec.S) {

it("returns false", func() {
Expect(os.Setenv("TEST_KEY", "foo"))
Expect(sherpa.ResolveBool("TEST_KEY")).To(BeFalse())
boolean, err := sherpa.ResolveBoolErr("TEST_KEY")
Expect(boolean).To(BeFalse())
Expect(err).To(MatchError("invalid value 'foo' for key 'TEST_KEY': " +
"expected one of [1, t, T, TRUE, true, True, 0, f, F, FALSE, false, False]"))
Expect(os.Unsetenv("TEST_KEY")).To(Succeed())
})
})

})

context("ResolveBool", func() {
context("variable is set to an invalid value", func() {
it.After(func() {
Expect(os.Unsetenv("TEST_KEY")).To(Succeed())
})

it("returns false", func() {
Expect(os.Setenv("TEST_KEY", "foo"))
Expect(sherpa.ResolveBool("TEST_KEY")).To(BeFalse())
Expect(os.Unsetenv("TEST_KEY")).To(Succeed())
})
})
})
}

0 comments on commit 2c9d401

Please sign in to comment.