-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add: env package to read and parse env for basic datatypes
- Loading branch information
1 parent
8ee3819
commit f8753ca
Showing
1 changed file
with
62 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package env | ||
|
||
import ( | ||
"os" | ||
"strconv" | ||
) | ||
|
||
// Bool looks up for boolean env variables and returns it. | ||
// This returns false if the key is not found or the value if non-boolean | ||
func Bool(key string, fallback bool) bool { | ||
value, ok := os.LookupEnv(key) | ||
if !ok { | ||
return fallback | ||
} | ||
|
||
parseBool, err := strconv.ParseBool(value) | ||
if err != nil { | ||
return fallback | ||
} | ||
|
||
return parseBool | ||
} | ||
|
||
// String looks up for a string env variables and returns it. | ||
func String(key, fallback string) string { | ||
value, ok := os.LookupEnv(key) | ||
if !ok { | ||
return fallback | ||
} | ||
|
||
return value | ||
} | ||
|
||
// Int looks up for a integer env variables and returns it. | ||
func Int(key string, fallback int) int { | ||
value, ok := os.LookupEnv(key) | ||
if !ok { | ||
return fallback | ||
} | ||
|
||
parseInt, err := strconv.Atoi(value) | ||
if err != nil { | ||
return fallback | ||
} | ||
|
||
return parseInt | ||
} | ||
|
||
// Float looks up for a float64 env variables and returns it. | ||
func Float(key string, fallback float64) float64 { | ||
value, ok := os.LookupEnv(key) | ||
if !ok { | ||
return fallback | ||
} | ||
|
||
parseFloat, err := strconv.ParseFloat(value, 64) | ||
if err != nil { | ||
return fallback | ||
} | ||
|
||
return parseFloat | ||
} |