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

Add flag env helpers #7

Merged
merged 1 commit into from
Nov 27, 2023
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
47 changes: 47 additions & 0 deletions pkg/flagenv/flagenv.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package flagenv

import (
"os"
"strconv"
"time"
)

func StringEnvWithDefault(key, defaultValue string) string {
if envValue, ok := os.LookupEnv(key); !ok {
return defaultValue
} else {
return envValue
}
}

func BoolEnvWithDefault(key string, defaultValue bool) bool {
if envValue, ok := os.LookupEnv(key); !ok {
return defaultValue
} else {
return envValue == "true"
}
}

func IntEnvWithDefault(key string, defaultValue int) int {
if envValue, ok := os.LookupEnv(key); !ok {
return defaultValue
} else {
v, err := strconv.Atoi(envValue)
if err != nil {
panic("invalid int env:" + key)
}
return v
}
}

func DurationEnvWithDefault(key string, defaultValue time.Duration) time.Duration {
if envValue, ok := os.LookupEnv(key); !ok {
return defaultValue
} else {
v, err := time.ParseDuration(envValue)
if err != nil {
panic("invalid duation env:" + key)
}
return v
}
}