{"id":23721070,"url":"https://github.com/nhas/confy","last_synced_at":"2026-02-08T02:32:02.562Z","repository":{"id":261730190,"uuid":"885074806","full_name":"NHAS/confy","owner":"NHAS","description":"A one stop shop for config file parsing, cli and environment variables","archived":false,"fork":false,"pushed_at":"2024-11-10T04:56:55.000Z","size":81,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-06-14T00:06:35.300Z","etag":null,"topics":["automagic","config","easy","golang","parsing"],"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/NHAS.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"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}},"created_at":"2024-11-07T22:57:07.000Z","updated_at":"2024-11-10T04:56:58.000Z","dependencies_parsed_at":"2024-11-08T05:38:10.098Z","dependency_job_id":null,"html_url":"https://github.com/NHAS/confy","commit_stats":null,"previous_names":["nhas/confy"],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/NHAS/confy","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NHAS%2Fconfy","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NHAS%2Fconfy/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NHAS%2Fconfy/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NHAS%2Fconfy/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/NHAS","download_url":"https://codeload.github.com/NHAS/confy/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NHAS%2Fconfy/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":260933186,"owners_count":23084949,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":["automagic","config","easy","golang","parsing"],"created_at":"2024-12-30T22:16:53.304Z","updated_at":"2026-02-08T02:31:57.539Z","avatar_url":"https://github.com/NHAS.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Confy\n\n`confy` is your one stop shop for configuration, it allows configuration from multiple sources including command-line arguments, environment variables, and configuration files in various formats (`JSON`, `YAML`, `TOML`).\n\n## Features\n\n- **Multiple Configuration Sources**: Populate settings from *CLI flags*, *environment variables*, and *config files*.\n- **Automatic CLI Flag and Env Variable Mapping**: Fields are mapped automatically and can be renamed with the `confy:\"\"` tag\n- **Support for YAML, JSON, and TOML**: Automatically detect and parse configuration file format, no added effort to switch to a new format\n\n## Installation\n\n```sh\ngo get github.com/NHAS/confy\n```\n\n## Usage\n\n### Tags\n- `confy:\"field_name;sensitive\"`: Customize field names for env variables, CLI flags, and config files.\n- `confy_description:\"Field Description here\"`: Set field descriptions for CLI parsing and help messages.\n\n### Basic Examples\n\n`config.json`:\n```json\n{\n  \"Database\": {\n    \"Host\": \"localhost\",\n    \"Port\": 5432,\n    \"User\": \"dbuser\",\n    \"Password\": \"securepassword\"\n  },\n  \"new_name\": \"some_value\"\n}\n```\n\n`config.yaml`:\n```yaml\nDatabase:\n  Host: \"localhost\"\n  Port: 5432\n  User: \"dbuser\"\n  Password: \"securepassword\"\n\nnew_name: \"some_value\"\n```\n\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\t\"github.com/NHAS/confy\"\n)\n\ntype Config struct {\n\tDatabase struct {\n\t\tHost     string `confy_description:\"Database host address\"`\n\t\tPort     int\n\t\tUser     string\n\t\tPassword string\n\t}\n\n    Renamed string `confy:\"new_name\"`\n}\n\nfunc main() {\n    // Defaults will also load from env and cli!\n\tloadedConfig, _, err := confy.Config[Config](confy.Defaults(\"config\", \"config.json\"))\n\tif err != nil {\n\t\tif !errors.Is(err, flag.ErrHelp) {\n\t\t\tfmt.Println(\"Error loading config:\", err)\n\t\t}\n\t\treturn\n\t}\n\n\tfmt.Printf(\"Loaded JSON config: %+v\\n\", loadedConfig)\n\n    yamlLoadedConfig, _, err := confy.Config[Config](confy.Defaults(\"config\", \"config.yaml\"))\n\tif err != nil {\n\t\tfmt.Println(\"Error loading config:\", err)\n\t\treturn\n\t}\n\n    fmt.Printf(\"Loaded YAML config: %+v\\n\", yamlLoadedConfig)\n\n    // They're the same!\n}\n```\n\nOutput\n```sh\n$ ./program\nLoaded JSON config: {Database:{Host:localhost Port:5432 User:dbuser Password:securepassword} Renamed:some_value}\nLoaded YAML config: {Database:{Host:localhost Port:5432 User:dbuser Password:securepassword} Renamed:some_value}\n\n$ ./program -h\nUsage of ./testconfy:\n  -config string\n        config file path (default \"config.json\")\nStructure options: \n  -Database.Host string\n        Database host address\n  -Database.Password string\n        A string value, Database.Password (Database.Password)\n  -Database.Port int\n        A int value, Database.Port (Database.Port)\n  -Database.User string\n        A string value, Database.User (Database.User)\n  -config string\n        config file path (default \"config.json\")\n  -new_name string\n        A string value, Renamed (new_name)\n  -struct-help\n        Print command line flags generated by confy (default true)\n```\n\n\n### Environment Variables\nFor struct:\n```go\npackage main\n\nimport (\n    \"fmt\"\n    \"github.com/NHAS/confy\"\n)\n\ntype Config struct {\n    Server struct {\n        Host string\n    }\n}\n\nfunc main() {\n\tpopulatedConfig, _, err := confy.Config[Config](confy.FromEnvs(confy.ENVDelimiter))\n\tif err != nil {\n\t\tfmt.Println(\"Error loading config:\", err)\n\t\treturn\n\t}\n\n    fmt.Println(populatedConfig.Server.Host)\n}\n```\nExpected environment variable:\n\n```sh\nexport Server_Host=\"localhost\"\n$ ./test\nlocalhost\n```\n\n### CLI Flags only\nFor struct:\n```go\npackage main\n\nimport (\n    \"fmt\"\n    \"github.com/NHAS/confy\"\n)\n\ntype Config struct {\n    Database struct {\n        Port int\n    }\n}\n\nfunc main() {\n\tpopulatedConfig, _, err := confy.Config[Config](confy.FromCli(confy.CLIDelimiter))\n\tif err != nil {\n\t\tfmt.Println(\"Error loading config:\", err)\n\t\treturn\n\t}\n\n    fmt.Println(populatedConfig.Database.Port)\n}\n\n```\nExpected CLI flag:\n```sh\n$ ./app -Database.Port=5432\n5432\n```\n\n\n## Logging\n\nConfy has logging capabilities using the `slog` package. Use the `WithLogLevel` option to adjust verbosity or disable logging.\n\n## Configuration Options\n\nConfy offers a variety of options for configuring your application's settings.\n\n| Option | Description |\n|----------|-------------|\n| `Defaults(...)` | Loads configurations in the order: config file -\u003e environment variables -\u003e CLI flags. This sets a non-strict parsing mode for unknown fields in the config file. |\n| `FromConfigFile(...)` | Load configuration from a file. Supports `YAML`, `JSON`, and `TOML`. |\n| `FromConfigBytes(...)` | Load configuration from raw bytes, ideal for embedding configuration in code. |\n| `FromConfigURL(...)` | Load configuration from URL. Supports `YAML`, `JSON`, and `TOML`, use extension or content type to specify type when using auto keyword|\n| `FromConfigFileFlagPath(...)` | Load configuration from file with filepath specified as cli flag |\n| `WithStrictParsing(...)` | Parse config files in a strict way, do not allow unknown fields |\n| `FromCli(...)` | Load configuration from CLI flags. Set a delimiter for nested struct parsing. |\n| `WithLogLevel(...)` | Set logging level to control output verbosity. Useful for debugging. |\n| `WithCliTransform(...)` | Takes a function to run against the generated CLI flag name, allows you to modify the flag name |\n| `WithEnvTransform(...)` | Takes a function to run against the generated ENV variable name, allows you to modify the ENV name |\n\n## Notes\n- Complex structures must implement `encoding.TextUnmarshaler` and `encoding.TextMarshaler` for CLI/ENV parsing.\n- CLI flags and environment variables use the delimiters (`.` for CLI, `_` for ENV by default) when handling nested fields.\n\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnhas%2Fconfy","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnhas%2Fconfy","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnhas%2Fconfy/lists"}