Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Add env defaulting funcs to operatorpkg #127

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions env/env.go
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
}