Documentation
¶
Overview ¶
Package xconfig provides a lightweight, zero-dependency, and highly extensible configuration management library for Go applications.
Overview ¶
xconfig enables you to build type-safe configuration for your applications using a plugin-based architecture. Mix and match only the configuration sources you need: defaults, environment variables, command-line flags, configuration files, secret providers, and more.
Quick Start ¶
Define your configuration as a struct with tags:
type Config struct {
Host string `default:"localhost" env:"HOST" flag:"host" usage:"Server host"`
Port int `default:"8080" env:"PORT" flag:"port" usage:"Server port"`
Debug bool `default:"false" env:"DEBUG" flag:"debug" usage:"Debug mode"`
Database struct {
Host string `default:"localhost" env:"DB_HOST" usage:"Database host"`
Port int `default:"5432" env:"DB_PORT" usage:"Database port"`
Password string `secret:"DB_PASSWORD" usage:"Database password"`
}
}
Load configuration from multiple sources:
cfg := &Config{}
_, err := xconfig.Load(cfg)
if err != nil {
log.Fatal(err)
}
The Load function automatically processes configuration in this order:
- Default values from struct tags
- Custom defaults via SetDefaults() method
- Configuration files (if provided)
- Environment variables
- Command-line flags
Configuration Sources ¶
## Default Values
Use the "default" tag to specify default values:
type Config struct {
Port int `default:"8080"`
Timeout int `default:"30"`
Enabled bool `default:"true"`
}
## Environment Variables
Use the "env" tag to bind fields to environment variables:
type Config struct {
APIKey string `env:"API_KEY"`
Secret string `env:"SECRET"`
}
Add a prefix to all environment variables:
_, err := xconfig.Load(cfg, xconfig.WithEnvPrefix("MYAPP"))
// Will look for: MYAPP_API_KEY, MYAPP_SECRET
## Command-Line Flags
Use the "flag" tag to bind fields to command-line flags:
type Config struct {
Host string `flag:"host" usage:"Server hostname"`
Port int `flag:"port" usage:"Server port"`
}
## Configuration Files
Load configuration from JSON, YAML, TOML, or any other format:
import (
"encoding/json"
"github.com/sxwebdev/xconfig/plugins/loader"
)
l, err := loader.NewLoader(map[string]loader.Unmarshal{
".json": json.Unmarshal,
".yaml": yaml.Unmarshal,
})
if err != nil {
log.Fatal(err)
}
err = l.AddFile("config.json", false)
if err != nil {
log.Fatal(err)
}
_, err = xconfig.Load(cfg, xconfig.WithLoader(l))
## Custom Defaults
Implement SetDefaults() for programmatic default values:
type Config struct {
URLs []string
Port int
}
func (c *Config) SetDefaults() {
c.URLs = []string{"https://api.example.com"}
c.Port = 8080
}
Secret Management ¶
Use the secret plugin to load sensitive data from secret providers like AWS Secrets Manager, HashiCorp Vault, or Kubernetes secrets:
import "github.com/sxwebdev/xconfig/plugins/secret"
type Config struct {
DBPassword string `secret:"DATABASE_PASSWORD"`
APIToken string `secret:"API_TOKEN"`
}
secretProvider := func(name string) (string, error) {
// Fetch from your secret backend
return fetchFromVault(name)
}
_, err := xconfig.Load(cfg, xconfig.WithPlugins(secret.New(secretProvider)))
Validation ¶
Add validation by implementing the Validate() method:
type Config struct {
Port int `default:"8080"`
}
func (c *Config) Validate() error {
if c.Port < 1 || c.Port > 65535 {
return fmt.Errorf("invalid port: %d", c.Port)
}
return nil
}
Or use external validators with the validate plugin:
import (
"github.com/go-playground/validator/v10"
"github.com/sxwebdev/xconfig/plugins/validate"
)
type Config struct {
Email string `validate:"required,email"`
Age int `validate:"gte=0,lte=130"`
}
v := validator.New()
_, err := xconfig.Load(cfg, xconfig.WithPlugins(
validate.New(func(a any) error {
return v.Struct(a)
}),
))
Available Tags ¶
The following struct tags are supported:
- default: Default value for the field
- env: Environment variable name
- flag: Command-line flag name
- secret: Secret identifier for secret provider
- usage: Description for documentation and help text
- xconfig: Override field name in flat structure
Supported Types ¶
xconfig supports all basic Go types, time.Duration, slices of basic types, and any type implementing encoding.TextUnmarshaler:
- string, bool
- int, int8, int16, int32, int64
- uint, uint8, uint16, uint32, uint64
- float32, float64
- time.Duration
- []string, []int, []float64, etc.
- Custom types via encoding.TextUnmarshaler
Custom Plugins ¶
Create custom plugins by implementing the Plugin interface with either Walker or Visitor:
import (
"github.com/sxwebdev/xconfig/flat"
"github.com/sxwebdev/xconfig/plugins"
)
type myPlugin struct {
fields flat.Fields
}
func (p *myPlugin) Visit(fields flat.Fields) error {
p.fields = fields
return nil
}
func (p *myPlugin) Parse() error {
// Load configuration for each field
return nil
}
// Use your plugin
_, err := xconfig.Custom(cfg, &myPlugin{})
Documentation Generation ¶
Generate markdown documentation for your configuration:
markdown, err := xconfig.GenerateMarkdown(cfg)
if err != nil {
log.Fatal(err)
}
os.WriteFile("CONFIG.md", []byte(markdown), 0644)
Options ¶
Control which plugins are enabled:
_, err := xconfig.Load(cfg,
xconfig.WithSkipDefaults(), // Skip 'default' tags
xconfig.WithSkipEnv(), // Skip environment variables
xconfig.WithSkipFlags(), // Skip command-line flags
xconfig.WithEnvPrefix("MYAPP"), // Add prefix to env vars
xconfig.WithPlugins(myPlugin), // Add custom plugins
)
For more information and examples, see: https://github.com/sxwebdev/xconfig
Package xconfig provides advanced command line flags supporting defaults, env vars, and config structs.
Index ¶
- Variables
- func GenerateMarkdown(cfg any, opts ...Option) (string, error)
- func GetUnknownFields(c Config) map[string][]string
- type Config
- type Option
- func WithDisallowUnknownFields() Option
- func WithEnvPrefix(prefix string) Option
- func WithLoader(loader *loader.Loader) Option
- func WithPlugins(plugins ...plugins.Plugin) Option
- func WithSkipCustomDefaults() Option
- func WithSkipDefaults() Option
- func WithSkipEnv() Option
- func WithSkipFiles() Option
- func WithSkipFlags() Option
Constants ¶
This section is empty.
Variables ¶
var ErrUsage = plugins.ErrUsage
Functions ¶
func GetUnknownFields ¶
GetUnknownFields returns all unknown fields found in configuration files. Returns a map where keys are file paths and values are slices of unknown field paths. This function is useful for debugging configuration issues or logging warnings about extra fields that are not used.
Types ¶
type Config ¶
type Config interface {
// Parse will call the parse method of all the added pluginss in the order
// that the pluginss were registered, it will return early as soon as any
// plugins fails.
// You must call this before using the config value.
Parse() error
// Usage provides a simple usage message based on the meta data registered
// by the pluginss.
Usage() (string, error)
// Options returns the options for the config.
Options() *options
// Fields returns the flat fields that have been processed by plugins.
Fields() flat.Fields
// contains filtered or unexported methods
}
Config is the config manager.
type Option ¶
type Option func(*options)
func WithDisallowUnknownFields ¶
func WithDisallowUnknownFields() Option
func WithEnvPrefix ¶
func WithLoader ¶
func WithPlugins ¶
func WithSkipCustomDefaults ¶
func WithSkipCustomDefaults() Option
func WithSkipDefaults ¶
func WithSkipDefaults() Option
func WithSkipEnv ¶
func WithSkipEnv() Option
func WithSkipFiles ¶
func WithSkipFiles() Option
func WithSkipFlags ¶
func WithSkipFlags() Option
Directories
¶
| Path | Synopsis |
|---|---|
|
decoders
|
|
|
xconfigdotenv
module
|
|
|
xconfigyaml
module
|
|
|
Package flat provides a flat view of an arbitrary nested structs.
|
Package flat provides a flat view of an arbitrary nested structs. |
|
internal
|
|
|
f
Package f provides simple test fixtures for xconfig.
|
Package f provides simple test fixtures for xconfig. |
|
utils
original package located here https://github.com/mcuadros/go-lookup
|
original package located here https://github.com/mcuadros/go-lookup |
|
Package plugins describes the xconfig provider interface.
|
Package plugins describes the xconfig provider interface. |
|
customdefaults
Package defaults provides default values for xconfig
|
Package defaults provides default values for xconfig |
|
defaults
Package defaults provides default values for xconfig
|
Package defaults provides default values for xconfig |
|
env
Package env provides environment variables support for xconfig
|
Package env provides environment variables support for xconfig |
|
flag
Package flag provides flags support for xconfig
|
Package flag provides flags support for xconfig |
|
loader
Package file provides config loader support for xconfig
|
Package file provides config loader support for xconfig |
|
secret
Package secret enable xconfig to integrate with secret plugins.
|
Package secret enable xconfig to integrate with secret plugins. |