-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add env defaulting funcs to operatorpkg
- Loading branch information
1 parent
5fbad4f
commit 673a66a
Showing
1 changed file
with
73 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,73 @@ | ||
package env | ||
|
||
import ( | ||
"os" | ||
"strconv" | ||
"time" | ||
) | ||
|
||
// WithDefaultInt returns the int value of the supplied environment variable or, if not present, | ||
// the supplied default value. If the int conversion fails, returns the default | ||
func WithDefaultInt(key string, def int) int { | ||
val, ok := os.LookupEnv(key) | ||
if !ok { | ||
return def | ||
} | ||
i, err := strconv.Atoi(val) | ||
if err != nil { | ||
return def | ||
} | ||
return i | ||
} | ||
|
||
// WithDefaultInt64 returns the int value of the supplied environment variable or, if not present, | ||
// the supplied default value. If the int conversion fails, returns the default | ||
func WithDefaultInt64(key string, def int64) int64 { | ||
val, ok := os.LookupEnv(key) | ||
if !ok { | ||
return def | ||
} | ||
i, err := strconv.ParseInt(val, 10, 64) | ||
if err != nil { | ||
return def | ||
} | ||
return i | ||
} | ||
|
||
// WithDefaultString returns the string value of the supplied environment variable or, if not present, | ||
// the supplied default value. | ||
func WithDefaultString(key string, def string) string { | ||
val, ok := os.LookupEnv(key) | ||
if !ok { | ||
return def | ||
} | ||
return val | ||
} | ||
|
||
// WithDefaultBool returns the boolean value of the supplied environment variable or, if not present, | ||
// the supplied default value. | ||
func WithDefaultBool(key string, def bool) bool { | ||
val, ok := os.LookupEnv(key) | ||
if !ok { | ||
return def | ||
} | ||
parsedVal, err := strconv.ParseBool(val) | ||
if err != nil { | ||
return def | ||
} | ||
return parsedVal | ||
} | ||
|
||
// WithDefaultDuration returns the duration value of the supplied environment variable or, if not present, | ||
// the supplied default value. | ||
func WithDefaultDuration(key string, def time.Duration) time.Duration { | ||
val, ok := os.LookupEnv(key) | ||
if !ok { | ||
return def | ||
} | ||
parsedVal, err := time.ParseDuration(val) | ||
if err != nil { | ||
return def | ||
} | ||
return parsedVal | ||
} |