Documentation
¶
Index ¶
- func FormatString(config interface{}) string
- func LoadEnv(config interface{}) error
- func RegisterEnvType[T EnvTypeInterface]()
- func SetOracle(oracleFunc func(string) string)
- func UnmarshalEnvSlogLevel(string string) (interface{}, error)
- type EnvNotFoundError
- type EnvParseError
- type EnvType
- type EnvTypeInterface
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func FormatString ¶
func FormatString(config interface{}) string
TODO maybe an extra tag that ensures a field is not printed, handy for passwords for example
func LoadEnv ¶
func LoadEnv(config interface{}) error
LoadEnv loads environment variables into the provided config struct. It uses the "env" struct tag to determine which environment variable corresponds to each field. If an environment variable is not found, and it does not have a default value provided in the tag, it returns an error.
Example:
type DBConfig struct {
Host string `env:"DB_HOST"`
Port int `env:"DB_PORT"`
User string `env:"DB_USER;default:user"`
Password string `env:"DB_PASSWORD;default:password"`
}
type Config struct {
Port float64 `env:"PORT;default:8080"`
LogLevel string `env:"LOG_LEVEL;optional"`
DB DBConfig
}
func LoadConfig() error {
cfg := Config{}
err := config.LoadEnv(&cfg)
if err != nil {
return err
}
fmt.Printf("Config: %+v\n", cfg)
return nil
}
func main() {
err := godotenv.Load(".env")
if err != nil {
return cfg, fmt.Errorf("could not load environment files: %w", err)
}
err := LoadConfig()
if err != nil {
fmt.Printf("Error loading config: %v\n", err)
return
}
}
TODO: allow for format string defaults, function return defaults?
func RegisterEnvType ¶
func RegisterEnvType[T EnvTypeInterface]()
func UnmarshalEnvSlogLevel ¶
Types ¶
type EnvNotFoundError ¶
type EnvNotFoundError struct {
Env string
}
EnvNotFoundError represents an error when an expected environment variable is not found.
func (*EnvNotFoundError) Error ¶
func (e *EnvNotFoundError) Error() string
Error returns a string representation of the EnvNotFoundError.
type EnvParseError ¶
type EnvParseError struct {
// contains filtered or unexported fields
}
func (*EnvParseError) Error ¶
func (e *EnvParseError) Error() string
type EnvTypeInterface ¶
Click to show internal directories.
Click to hide internal directories.