This repository has been archived on 2024-07-22. You can view files and clone it, but cannot push or open issues or pull requests.
plug-sdk/config/config.go
2024-02-20 11:26:39 +01:00

37 lines
1.0 KiB
Go

package config
import (
"fmt"
"github.com/caarlos0/env/v10"
"github.com/go-playground/validator/v10"
)
type PlugConfig struct {
LogLevel string `validate:"required,eq=FATAL|eq=ERROR|eq=WARN|eq=INFO|eq=DEBUG|eq=TRACE" env:"LOG_LEVEL" envDefault:"INFO" json:"log_level"`
LogFormat string `validate:"required,eq=PLAIN|eq=JSON" env:"LOG_FORMAT" envDefault:"PLAIN" json:"log_format"`
}
type OtterSpaceConfig struct {
Type string `env:"DB_TYPE,required"`
Endpoint string `env:"DB_URL,required"`
Username string `env:"DB_USERNAME,required"`
Password string `env:"DB_PASSWORD,required,unset"`
Debug bool `env:"DB_DEBUG" envDefault:"FALSE"`
}
// LoadConfig loads the configuration from environment variables and validates it.
func LoadConfig[T any](cfg T) (T, error) {
if err := env.Parse(&cfg); err != nil {
return cfg, fmt.Errorf("config: error parsing configuration: %w", err)
}
validate := validator.New()
if err := validate.Struct(cfg); err != nil {
return cfg, fmt.Errorf("config: validation error: %w", err)
}
return cfg, nil
}