// Package config contains small, dependency-free environment helpers shared // by the Go services. It intentionally does not define an application's full // configuration struct. package config import ( "fmt" "os" "strconv" "strings" "time" ) // String returns the environment value or fallback when the variable is empty. func String(key, fallback string) string { if value := strings.TrimSpace(os.Getenv(key)); value != "" { return value } return fallback } // Required returns a trimmed environment value or an actionable error. func Required(key string) (string, error) { value := strings.TrimSpace(os.Getenv(key)) if value == "" { return "", fmt.Errorf("%s is required", key) } return value, nil } // Bool returns a parsed boolean environment value or fallback for an unset or // invalid value. Invalid values are deliberately non-fatal for optional flags. func Bool(key string, fallback bool) bool { value := strings.TrimSpace(os.Getenv(key)) if value == "" { return fallback } parsed, err := strconv.ParseBool(value) if err != nil { return fallback } return parsed } // Int returns a parsed integer environment value or fallback for an unset or // invalid value. Callers should validate application-specific ranges. func Int(key string, fallback int) int { value := strings.TrimSpace(os.Getenv(key)) if value == "" { return fallback } parsed, err := strconv.Atoi(value) if err != nil { return fallback } return parsed } // Duration returns a parsed duration environment value or fallback for an // unset or invalid value. func Duration(key string, fallback time.Duration) time.Duration { value := strings.TrimSpace(os.Getenv(key)) if value == "" { return fallback } parsed, err := time.ParseDuration(value) if err != nil { return fallback } return parsed }