Skip to content

Commit

Permalink
Adds method ResolveBoolWithDefault
Browse files Browse the repository at this point in the history
This allows you to fetch a boolean configuration and change the default, like if you want the default to be true. Not used often, but it is helpful for a couple of buildpacks.

Signed-off-by: Daniel Mikusa <dan@mikusa.com>
  • Loading branch information
dmikusa committed Dec 31, 2024
1 parent 790a8df commit 97b416f
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions sherpa/env_var.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,7 @@ func ResolveBoolErr(name string) (bool, error) {
return false, nil
}

t := strings.TrimSpace(s)
p, err := strconv.ParseBool(t)
p, err := strconv.ParseBool(strings.TrimSpace(s))
if err != nil {
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]",
Expand All @@ -81,3 +80,18 @@ func ResolveBoolErr(name string) (bool, error) {

return p, nil
}

// ResolveBoolWithDefault resolves a boolean value for a configuration option or returns the default value
func ResolveBoolWithDefault(name string, defaultVal bool) bool {
s, ok := os.LookupEnv(name)
if !ok {
return defaultVal
}

p, err := strconv.ParseBool(strings.TrimSpace(s))
if err != nil {
return false
}

return p
}

0 comments on commit 97b416f

Please sign in to comment.