121 lines
3.4 KiB
Go
121 lines
3.4 KiB
Go
// Package ezconf provides a unified configuration management system for loading
|
|
// and managing environment-based configurations across multiple packages.
|
|
//
|
|
// ezconf allows you to:
|
|
// - Load configurations from multiple packages using their ConfigFromEnv functions
|
|
// - Parse package source code to extract environment variable documentation
|
|
// - Generate and update .env files with all required environment variables
|
|
// - Print environment variable lists with descriptions and current values
|
|
// - Track additional custom environment variables
|
|
//
|
|
// # Basic Usage
|
|
//
|
|
// Create a configuration loader and register packages using built-in integrations (recommended):
|
|
//
|
|
// import (
|
|
// "git.haelnorr.com/h/golib/ezconf"
|
|
// "git.haelnorr.com/h/golib/hlog"
|
|
// "git.haelnorr.com/h/golib/hws"
|
|
// "git.haelnorr.com/h/golib/hwsauth"
|
|
// )
|
|
//
|
|
// 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 a specific configuration
|
|
// hlogCfg, ok := loader.GetConfig("hlog")
|
|
// if ok {
|
|
// cfg := hlogCfg.(*hlog.Config)
|
|
// // Use configuration...
|
|
// }
|
|
//
|
|
// Alternatively, you can manually register packages:
|
|
//
|
|
// loader := ezconf.New()
|
|
//
|
|
// // Add package paths to parse for ENV comments
|
|
// loader.AddPackagePath("/path/to/golib/hlog")
|
|
//
|
|
// // Add configuration loaders
|
|
// loader.AddConfigFunc("hlog", func() (interface{}, error) {
|
|
// return hlog.ConfigFromEnv()
|
|
// })
|
|
//
|
|
// loader.Load()
|
|
//
|
|
// # Printing Environment Variables
|
|
//
|
|
// Print all environment variables with their descriptions:
|
|
//
|
|
// // Print without values (useful for documentation)
|
|
// if err := loader.PrintEnvVarsStdout(false); err != nil {
|
|
// log.Fatal(err)
|
|
// }
|
|
//
|
|
// // Print with current values
|
|
// if err := loader.PrintEnvVarsStdout(true); err != nil {
|
|
// log.Fatal(err)
|
|
// }
|
|
//
|
|
// # Generating .env Files
|
|
//
|
|
// Generate a new .env file with all environment variables:
|
|
//
|
|
// // Generate with default values
|
|
// err := loader.GenerateEnvFile(".env", false)
|
|
//
|
|
// // Generate with current environment values
|
|
// err := loader.GenerateEnvFile(".env", true)
|
|
//
|
|
// Update an existing .env file:
|
|
//
|
|
// // Update existing file, preserving existing values
|
|
// err := loader.UpdateEnvFile(".env", true)
|
|
//
|
|
// # Adding Custom Environment Variables
|
|
//
|
|
// You can add additional environment variables that aren't in package configs:
|
|
//
|
|
// loader.AddEnvVar(ezconf.EnvVar{
|
|
// Name: "DATABASE_URL",
|
|
// Description: "PostgreSQL connection string",
|
|
// Required: true,
|
|
// Default: "postgres://localhost/mydb",
|
|
// })
|
|
//
|
|
// # ENV Comment Format
|
|
//
|
|
// ezconf parses struct field comments in the following format:
|
|
//
|
|
// type Config struct {
|
|
// // ENV LOG_LEVEL: Log level for the application (default: info)
|
|
// LogLevel string
|
|
//
|
|
// // ENV DATABASE_URL: Database connection string (required)
|
|
// DatabaseURL string
|
|
// }
|
|
//
|
|
// The format is:
|
|
// - ENV ENV_VAR_NAME: Description (optional modifiers)
|
|
// - (required) or (required if condition) - marks variable as required
|
|
// - (default: value) - specifies default value
|
|
//
|
|
// # Integration
|
|
//
|
|
// ezconf integrates with:
|
|
// - All golib packages that follow the ConfigFromEnv pattern
|
|
// - Any custom configuration structs with ENV comments
|
|
// - Standard .env file format
|
|
package ezconf
|