Files
golib/ezconf
2026-02-25 21:52:57 +11:00
..
2026-02-25 21:52:57 +11:00
2026-02-25 21:52:57 +11:00
2026-02-25 21:52:57 +11:00
2026-02-25 21:52:57 +11:00
2026-02-25 21:52:57 +11:00
2026-01-21 19:23:12 +11:00
2026-01-21 19:23:12 +11:00
2026-02-25 21:52:57 +11:00
2026-02-25 21:52:57 +11:00

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

go get git.haelnorr.com/h/golib/ezconf

Quick Start

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

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.

Additional API documentation is available at GoDoc.

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
	
	// 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.

  • hlog - Structured logging package with ConfigFromEnv
  • hws - HTTP web server with ConfigFromEnv
  • hwsauth - Authentication middleware with ConfigFromEnv
  • env - Environment variable helpers