Last active
March 8, 2019 18:43
-
-
Save craicoverflow/f487b83a5bae318a7a94ba6d326219c1 to your computer and use it in GitHub Desktop.
Config struct that handles strings, bools, integers and slices
This file contains hidden or 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
package config | |
import ( | |
"os" | |
"strconv" | |
"strings" | |
) | |
type GitHubConfig struct { | |
Username string | |
APIKey string | |
} | |
type Config struct { | |
GitHub GitHubConfig | |
DebugMode bool | |
UserRoles []string | |
MaxUsers int | |
} | |
// New returns a new Config struct | |
func New() *Config { | |
return &Config{ | |
GitHub: GitHubConfig{ | |
Username: getEnv("GITHUB_USERNAME", ""), | |
APIKey: getEnv("GITHUB_API_KEY", ""), | |
}, | |
DebugMode: getEnvAsBool("DEBUG_MODE", true), | |
UserRoles: getEnvAsSlice("USER_ROLES", []string{"admin"}, ","), | |
MaxUsers: getEnvAsInt("MAX_USERS", 1), | |
} | |
} | |
// Simple helper function to read an environment or return a default value | |
func getEnv(key string, defaultVal string) string { | |
if value, exists := os.LookupEnv(key); exists { | |
return value | |
} | |
return defaultVal | |
} | |
// Simple helper function to read an environment variable into integer or return a default value | |
func getEnvAsInt(name string, defaultVal int) int { | |
valueStr := getEnv(name, "") | |
if value, err := strconv.Atoi(valueStr); err == nil { | |
return value | |
} | |
return defaultVal | |
} | |
// Helper to read an environment variable into a bool or return default value | |
func getEnvAsBool(name string, defaultVal bool) bool { | |
valStr := getEnv(name, "") | |
if val, err := strconv.ParseBool(valStr); err == nil { | |
return val | |
} | |
return defaultVal | |
} | |
// Helper to read an environment variable into a string slice or return default value | |
func getEnvAsSlice(name string, defaultVal []string, sep string) []string { | |
valStr := getEnv(name, "") | |
if valStr == "" { | |
return defaultVal | |
} | |
val := strings.Split(valStr, sep) | |
return val | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment