128 lines
4.0 KiB
Go
128 lines
4.0 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 config struct tags 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 config structs:
|
|
//
|
|
// loader := ezconf.New()
|
|
//
|
|
// // Add config struct for tag parsing
|
|
// loader.AddConfigStruct(&mypackage.Config{}, "MyPackage")
|
|
//
|
|
// // Add configuration loaders
|
|
// loader.AddConfigFunc("mypackage", func() (interface{}, error) {
|
|
// return mypackage.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",
|
|
// })
|
|
//
|
|
// # Struct Tag Format
|
|
//
|
|
// ezconf uses struct tags to define environment variable metadata:
|
|
//
|
|
// type Config struct {
|
|
// LogLevel string `ezconf:"LOG_LEVEL,description:Log level for the application,default:info"`
|
|
// DatabaseURL string `ezconf:"DATABASE_URL,description:Database connection string,required"`
|
|
// LogDir string `ezconf:"LOG_DIR,description:Directory for log files,required:when LOG_OUTPUT is file"`
|
|
// }
|
|
//
|
|
// Tag components (comma-separated):
|
|
// - First value: environment variable name (required)
|
|
// - description:...: Description of the variable
|
|
// - default:...: Default value
|
|
// - required: Marks the variable as required
|
|
// - required:condition: Marks as required with a condition description
|
|
//
|
|
// # Integration
|
|
//
|
|
// Packages can implement the Integration interface to provide automatic
|
|
// registration with ezconf. The interface requires:
|
|
// - Name() string: Registration key for the config
|
|
// - ConfigPointer() any: Pointer to config struct for tag parsing
|
|
// - ConfigFunc() func() (any, error): Function to load config from env
|
|
// - GroupName() string: Display name for grouping env vars
|
|
//
|
|
// ezconf integrates with:
|
|
// - All golib packages that follow the ConfigFromEnv pattern
|
|
// - Any custom configuration structs with ezconf struct tags
|
|
// - Standard .env file format
|
|
package ezconf
|