Files
golib/ezconf/README.md

162 lines
3.9 KiB
Markdown

# EZConf - v0.1.0
A unified configuration management system for loading and managing environment-based configurations across multiple packages in Go.
## Features
- Load configurations from multiple packages using their ConfigFromEnv functions
- Parse package source code to extract environment variable documentation from struct comments
- Generate and update .env files with all required environment variables
- Print environment variable lists with descriptions and current values
- Track additional custom environment variables
- Support for both inline and doc comments in ENV format
- Automatic environment variable value population
- Preserve existing values when updating .env files
## Installation
```bash
go get git.haelnorr.com/h/golib/ezconf
```
## Quick Start
### Easy Integration (Recommended)
```go
package main
import (
"log"
"os"
"git.haelnorr.com/h/golib/ezconf"
"git.haelnorr.com/h/golib/hlog"
"git.haelnorr.com/h/golib/hws"
"git.haelnorr.com/h/golib/hwsauth"
)
func main() {
// Create a new configuration loader
loader := ezconf.New()
// Register packages using built-in integrations
loader.RegisterIntegrations(
hlog.NewEZConfIntegration(),
hws.NewEZConfIntegration(),
hwsauth.NewEZConfIntegration(),
)
// Load all configurations
if err := loader.Load(); err != nil {
log.Fatal(err)
}
// Get configurations
hlogCfg, _ := loader.GetConfig("hlog")
cfg := hlogCfg.(*hlog.Config)
// Use configuration
logger, _ := hlog.NewLogger(cfg, os.Stdout)
logger.Info().Msg("Application started")
}
```
### Manual Integration
```go
package main
import (
"log"
"os"
"git.haelnorr.com/h/golib/ezconf"
"git.haelnorr.com/h/golib/hlog"
"git.haelnorr.com/h/golib/hws"
)
func main() {
// Create a new configuration loader
loader := ezconf.New()
// Add package paths to parse for ENV comments
loader.AddPackagePath("vendor/git.haelnorr.com/h/golib/hlog")
loader.AddPackagePath("vendor/git.haelnorr.com/h/golib/hws")
// Add configuration loaders
loader.AddConfigFunc("hlog", func() (interface{}, error) {
return hlog.ConfigFromEnv()
})
loader.AddConfigFunc("hws", func() (interface{}, error) {
return hws.ConfigFromEnv()
})
// Load all configurations
if err := loader.Load(); err != nil {
log.Fatal(err)
}
// Get a specific configuration
hlogCfg, ok := loader.GetConfig("hlog")
if ok {
cfg := hlogCfg.(*hlog.Config)
// Use configuration...
}
// Print all environment variables
if err := loader.PrintEnvVarsStdout(false); err != nil {
log.Fatal(err)
}
// Generate a .env file
if err := loader.GenerateEnvFile(".env", false); err != nil {
log.Fatal(err)
}
}
```
## Documentation
For detailed documentation, see the [EZConf Wiki](../golib-wiki/EZConf.md).
Additional API documentation is available at [GoDoc](https://pkg.go.dev/git.haelnorr.com/h/golib/ezconf).
## ENV Comment Format
EZConf parses struct field comments in the following format:
```go
type Config struct {
// ENV LOG_LEVEL: Log level for the application (default: info)
LogLevel string
// ENV DATABASE_URL: Database connection string (required)
DatabaseURL string
// Inline comments also work
Port int // ENV PORT: Server port (default: 8080)
}
```
The format is:
- `ENV ENV_VAR_NAME: Description (optional modifiers)`
- `(required)` or `(required if condition)` - marks variable as required
- `(default: value)` - specifies default value
## License
This project is licensed under the MIT License - see the LICENSE file for details.
## Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
## Related Projects
- [hlog](https://git.haelnorr.com/h/golib/hlog) - Structured logging package with ConfigFromEnv
- [hws](https://git.haelnorr.com/h/golib/hws) - HTTP web server with ConfigFromEnv
- [hwsauth](https://git.haelnorr.com/h/golib/hwsauth) - Authentication middleware with ConfigFromEnv
- [env](https://git.haelnorr.com/h/golib/env) - Environment variable helpers