{"id":43612652,"url":"https://github.com/sxwebdev/xconfig","last_synced_at":"2026-02-04T12:11:49.102Z","repository":{"id":315297053,"uuid":"1058937071","full_name":"sxwebdev/xconfig","owner":"sxwebdev","description":"A lightweight, zero-dependency, and highly extensible configuration management library for Go applications.","archived":false,"fork":false,"pushed_at":"2026-02-03T19:24:08.000Z","size":108,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"master","last_synced_at":"2026-02-04T08:40:18.021Z","etag":null,"topics":["config","configuration-management","go"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sxwebdev.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2025-09-17T18:51:56.000Z","updated_at":"2026-02-03T19:22:56.000Z","dependencies_parsed_at":"2025-09-17T21:10:14.494Z","dependency_job_id":null,"html_url":"https://github.com/sxwebdev/xconfig","commit_stats":null,"previous_names":["sxwebdev/xconfig"],"tags_count":9,"template":false,"template_full_name":null,"purl":"pkg:github/sxwebdev/xconfig","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sxwebdev%2Fxconfig","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sxwebdev%2Fxconfig/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sxwebdev%2Fxconfig/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sxwebdev%2Fxconfig/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sxwebdev","download_url":"https://codeload.github.com/sxwebdev/xconfig/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sxwebdev%2Fxconfig/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29084097,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-04T03:31:03.593Z","status":"ssl_error","status_checked_at":"2026-02-04T03:29:50.742Z","response_time":62,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["config","configuration-management","go"],"created_at":"2026-02-04T12:11:48.359Z","updated_at":"2026-02-04T12:11:49.089Z","avatar_url":"https://github.com/sxwebdev.png","language":"Go","readme":"# xConfig\n\n[![Go Reference](https://pkg.go.dev/badge/github.com/sxwebdev/xconfig.svg)](https://pkg.go.dev/github.com/sxwebdev/xconfig)\n[![Go Version](https://img.shields.io/badge/go-1.25-blue)](https://go.dev/)\n[![Go Report Card](https://goreportcard.com/badge/github.com/sxwebdev/xconfig)](https://goreportcard.com/report/github.com/sxwebdev/xconfig)\n[![License](https://img.shields.io/github/license/sxwebdev/xconfig)](LICENSE)\n\nA lightweight, zero-dependency, and highly extensible configuration management library for Go applications.\n\n## Features\n\n- **Zero Dependencies** - No external dependencies in the core library\n- **Plugin-Based Architecture** - Mix and match only the configuration sources you need\n- **Type-Safe** - Strongly typed configuration with struct tags\n- **Multiple Sources** - Support for defaults, environment variables, command-line flags, and config files\n- **Nested Structures** - Full support for nested configuration structs\n- **Rich Type Support** - All basic Go types, `time.Duration`, and custom types via `encoding.TextUnmarshaler`\n- **Validation** - Built-in validation support through plugins\n- **Documentation Generation** - Auto-generate markdown documentation for your configuration\n\n## Installation\n\n```bash\ngo get github.com/sxwebdev/xconfig\n```\n\n## Quick Start\n\n```go\npackage main\n\nimport (\n    \"fmt\"\n    \"log\"\n\n    \"github.com/sxwebdev/xconfig\"\n)\n\ntype Config struct {\n    Host     string `default:\"localhost\" env:\"HOST\" flag:\"host\" usage:\"Server host address\"`\n    Port     int    `default:\"8080\" env:\"PORT\" flag:\"port\" usage:\"Server port\"`\n    Debug    bool   `default:\"false\" env:\"DEBUG\" flag:\"debug\" usage:\"Enable debug mode\"`\n    Database struct {\n        Host     string `default:\"localhost\" env:\"DB_HOST\" usage:\"Database host\"`\n        Port     int    `default:\"5432\" env:\"DB_PORT\" usage:\"Database port\"`\n        Name     string `default:\"myapp\" env:\"DB_NAME\" usage:\"Database name\"`\n        Password string `secret:\"DB_PASSWORD\" usage:\"Database password\"`\n    }\n}\n\nfunc main() {\n    cfg := \u0026Config{}\n\n    // Load configuration from defaults, env vars, and flags\n    _, err := xconfig.Load(cfg)\n    if err != nil {\n        log.Fatal(err)\n    }\n\n    fmt.Printf(\"Server: %s:%d\\n\", cfg.Host, cfg.Port)\n    fmt.Printf(\"Database: %s:%d/%s\\n\", cfg.Database.Host, cfg.Database.Port, cfg.Database.Name)\n}\n```\n\n## Usage\n\n### Basic Configuration Loading\n\nThe `Load` function provides the most common configuration pattern, automatically enabling:\n\n1. Default values from struct tags\n2. Custom defaults via `SetDefaults()` method\n3. Configuration files (if provided)\n4. Environment variables\n5. Command-line flags\n\n```go\ntype AppConfig struct {\n    APIKey    string `default:\"dev-key\" env:\"API_KEY\" flag:\"api-key\"`\n    Timeout   int    `default:\"30\" env:\"TIMEOUT\" flag:\"timeout\"`\n    EnableLog bool   `default:\"true\" env:\"ENABLE_LOG\" flag:\"enable-log\"`\n}\n\ncfg := \u0026AppConfig{}\n_, err := xconfig.Load(cfg)\n```\n\n### Loading from Configuration Files\n\nxConfig supports multiple configuration file formats through decoders:\n\n```go\nimport (\n    \"encoding/json\"\n\n    \"github.com/sxwebdev/xconfig\"\n    \"github.com/sxwebdev/xconfig/plugins/loader\"\n    \"github.com/sxwebdev/xconfig/decoders/xconfigyaml\"\n    \"github.com/sxwebdev/xconfig/decoders/xconfigdotenv\"\n)\n\ntype Config struct {\n    Server struct {\n        Host string `json:\"host\"`\n        Port int    `json:\"port\"`\n    } `json:\"server\"`\n}\n\ncfg := \u0026Config{}\n\n// Create loader with JSON decoder\nl, err := loader.NewLoader(map[string]loader.Unmarshal{\n    \"json\": json.Unmarshal,\n    \"yaml\": xconfigyaml.New().Unmarshal,\n    \"env\":  xconfigdotenv.New().Unmarshal,\n})\nif err != nil {\n    log.Fatal(err)\n}\n\n// Add configuration file (optional=false means file must exist)\nerr = l.AddFile(\"config.json\", false)\nif err != nil {\n    log.Fatal(err)\n}\n\n_, err = xconfig.Load(cfg, xconfig.WithLoader(l))\n```\n\n### Environment Variables with Prefix\n\n```go\ntype Config struct {\n    APIKey string `env:\"API_KEY\"`\n    Secret string `env:\"SECRET\"`\n}\n\ncfg := \u0026Config{}\n\n// All env vars will be prefixed with \"MYAPP_\"\n// So it will look for: MYAPP_API_KEY and MYAPP_SECRET\n_, err := xconfig.Load(cfg, xconfig.WithEnvPrefix(\"MYAPP\"))\n```\n\n### Custom Defaults with SetDefaults\n\nImplement the `SetDefaults()` method to programmatically set default values:\n\n```go\ntype Config struct {\n    Host string\n    Port int\n    URLs []string\n}\n\nfunc (c *Config) SetDefaults() {\n    c.Host = \"localhost\"\n    c.Port = 8080\n    c.URLs = []string{\"https://api.example.com\", \"https://backup.example.com\"}\n}\n\ncfg := \u0026Config{}\n_, err := xconfig.Load(cfg)\n// cfg.Host will be \"localhost\" unless overridden by env or flags\n```\n\n### Secret Management\n\nUse the `secret` tag for sensitive data that should be loaded from a secret provider:\n\n```go\nimport (\n    \"github.com/sxwebdev/xconfig\"\n    \"github.com/sxwebdev/xconfig/plugins/secret\"\n)\n\ntype Config struct {\n    DBPassword string `secret:\"DATABASE_PASSWORD\"`\n    APIToken   string `secret:\"API_TOKEN\"`\n}\n\n// Custom secret provider (e.g., AWS Secrets Manager, HashiCorp Vault, etc.)\nsecretProvider := func(name string) (string, error) {\n    // Implement your secret fetching logic here\n    switch name {\n    case \"DATABASE_PASSWORD\":\n        return fetchFromVault(name)\n    case \"API_TOKEN\":\n        return fetchFromAWS(name)\n    default:\n        return \"\", fmt.Errorf(\"secret not found: %s\", name)\n    }\n}\n\ncfg := \u0026Config{}\n_, err := xconfig.Load(cfg, xconfig.WithPlugins(secret.New(secretProvider)))\n```\n\n### Validation\n\nAdd validation to ensure your configuration meets requirements:\n\n```go\nimport (\n    \"fmt\"\n    \"github.com/sxwebdev/xconfig\"\n    \"github.com/sxwebdev/xconfig/plugins/validate\"\n)\n\ntype Config struct {\n    Port int    `default:\"8080\"`\n    Host string `default:\"localhost\"`\n}\n\n// Implement Validate method\nfunc (c *Config) Validate() error {\n    if c.Port \u003c 1 || c.Port \u003e 65535 {\n        return fmt.Errorf(\"port must be between 1 and 65535\")\n    }\n    if c.Host == \"\" {\n        return fmt.Errorf(\"host cannot be empty\")\n    }\n    return nil\n}\n\ncfg := \u0026Config{}\n\n// Validation happens automatically after loading\n_, err := xconfig.Load(cfg)\nif err != nil {\n    log.Fatal(err) // Will fail if validation fails\n}\n```\n\nYou can also use external validators:\n\n```go\nimport (\n    \"github.com/go-playground/validator/v10\"\n    \"github.com/sxwebdev/xconfig/plugins/validate\"\n)\n\ntype Config struct {\n    Email string `validate:\"required,email\"`\n    Age   int    `validate:\"gte=0,lte=130\"`\n}\n\ncfg := \u0026Config{}\n\nv := validator.New()\n_, err := xconfig.Load(cfg, xconfig.WithPlugins(\n    validate.New(func(a any) error {\n        return v.Struct(a)\n    }),\n))\n```\n\n### Selective Plugin Loading\n\nControl which plugins are enabled:\n\n```go\ncfg := \u0026Config{}\n\n// Skip certain plugins\n_, err := xconfig.Load(cfg,\n    xconfig.WithSkipDefaults(),         // Don't load from 'default' tags\n    xconfig.WithSkipEnv(),              // Don't load from environment\n    xconfig.WithSkipFlags(),            // Don't load from command-line flags\n    xconfig.WithSkipCustomDefaults(),   // Don't call SetDefaults()\n    xconfig.WithDisallowUnknownFields(), // Fail if config files contain unknown fields\n)\n```\n\n**Unknown Fields Validation**: Enable `WithDisallowUnknownFields()` to detect typos and configuration errors in JSON/YAML files. When enabled, loading will fail if any fields in the config files don't match your struct definition. Use `xconfig.GetUnknownFields()` to retrieve unknown fields without failing.\n\n### Documentation Generation\n\nGenerate markdown documentation for your configuration:\n\n```go\ntype Config struct {\n    Host   string `default:\"localhost\" usage:\"Server host address\"`\n    Port   int    `default:\"8080\" usage:\"Server port number\"`\n    APIKey string `secret:\"API_KEY\" usage:\"API authentication key\"`\n}\n\ncfg := \u0026Config{}\n\nmarkdown, err := xconfig.GenerateMarkdown(cfg)\nif err != nil {\n    log.Fatal(err)\n}\n\n// Save to file\nos.WriteFile(\"CONFIG.md\", []byte(markdown), 0644)\n```\n\n### Usage Information\n\nGet runtime configuration information:\n\n```go\ncfg := \u0026Config{}\nc, err := xconfig.Load(cfg)\nif err != nil {\n    log.Fatal(err)\n}\n\nusage, err := c.Usage()\nif err != nil {\n    log.Fatal(err)\n}\n\nfmt.Println(usage)\n```\n\n## Available Struct Tags\n\n| Tag       | Description                           | Example                 |\n| --------- | ------------------------------------- | ----------------------- |\n| `default` | Default value for the field           | `default:\"8080\"`        |\n| `env`     | Environment variable name             | `env:\"PORT\"`            |\n| `flag`    | Command-line flag name                | `flag:\"port\"`           |\n| `secret`  | Secret identifier for secret provider | `secret:\"DB_PASSWORD\"`  |\n| `usage`   | Description for documentation/help    | `usage:\"Server port\"`   |\n| `xconfig` | Override field name in flat structure | `xconfig:\"custom_name\"` |\n\n## Available Plugins\n\n| Plugin             | Description                                             |\n| ------------------ | ------------------------------------------------------- |\n| **defaults**       | Load values from `default` struct tags                  |\n| **customdefaults** | Call `SetDefaults()` method if implemented              |\n| **env**            | Load values from environment variables                  |\n| **flag**           | Load values from command-line flags                     |\n| **loader**         | Load values from configuration files (JSON, YAML, etc.) |\n| **secret**         | Load sensitive values from secret providers             |\n| **validate**       | Validate configuration after loading                    |\n\n## Custom Plugins\n\nCreate your own plugins by implementing the `Plugin` interface with either `Walker` or `Visitor`:\n\n```go\nimport (\n    \"github.com/sxwebdev/xconfig/flat\"\n    \"github.com/sxwebdev/xconfig/plugins\"\n)\n\ntype myPlugin struct {\n    fields flat.Fields\n}\n\n// Visitor interface - called once with all fields\nfunc (p *myPlugin) Visit(fields flat.Fields) error {\n    p.fields = fields\n    // Setup phase: register metadata, validate structure, etc.\n    return nil\n}\n\n// Parse is called to actually load configuration\nfunc (p *myPlugin) Parse() error {\n    for _, field := range p.fields {\n        // Load configuration for each field\n    }\n    return nil\n}\n\n// Use your custom plugin\ncfg := \u0026Config{}\n_, err := xconfig.Custom(cfg, \u0026myPlugin{})\n```\n\n## Supported Types\n\n- All basic Go types: `string`, `bool`, `int`, `int8`, `int16`, `int32`, `int64`, `uint`, `uint8`, `uint16`, `uint32`, `uint64`, `float32`, `float64`\n- `time.Duration`\n- Slices of supported types: `[]string`, `[]int`, etc.\n- Any type implementing `encoding.TextUnmarshaler`\n\n## Examples\n\nSee the [examples](https://github.com/sxwebdev/xconfig/tree/master/examples) directory for more complete examples.\n\n## License\n\nMIT License\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsxwebdev%2Fxconfig","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsxwebdev%2Fxconfig","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsxwebdev%2Fxconfig/lists"}