Skip to content

Commit

Permalink
Add function envflags.GetBoolDefault
Browse files Browse the repository at this point in the history
  • Loading branch information
joreetz-otto committed Nov 8, 2023
1 parent 9199723 commit 381fc01
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

## v0.0.39 More options for envflags
- Add function `envflags.GetBoolDefault`

## v0.0.38 Further options for ErrorReporting
- Add WithErrorReportCallback for testing Services.

Expand Down
17 changes: 17 additions & 0 deletions pkg/envflags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"os"
"strconv"
"strings"
)

// GetStringDefault looks up Environment Variable for a key.
Expand Down Expand Up @@ -33,3 +34,19 @@ func GetIntDefault(envKey string, def int) int {
}
return def
}

// GetBoolDefault looks up Environment Variable for a key.
// Variable value will be checked for a non case sensitive
// variation of the words true or false.
// Panics if no variation found
func GetBoolDefault(envKey string, def bool) bool {
v := os.Getenv(envKey)
if v == "" {
return def
} else if strings.ToLower(v) == "true" {
return true
} else if strings.ToLower(v) == "false" {
return false
}
panic(fmt.Sprintf("Provided value for Environment Variable %q is not a valid value for type bool: %s", envKey, v))
}

0 comments on commit 381fc01

Please sign in to comment.