{"id":13410742,"url":"https://github.com/cristalhq/aconfig","last_synced_at":"2025-05-16T00:06:21.988Z","repository":{"id":38782632,"uuid":"275234652","full_name":"cristalhq/aconfig","owner":"cristalhq","description":"Simple, useful and opinionated config loader.","archived":false,"fork":false,"pushed_at":"2025-04-20T07:33:02.000Z","size":280,"stargazers_count":586,"open_issues_count":16,"forks_count":38,"subscribers_count":8,"default_branch":"main","last_synced_at":"2025-05-15T18:52:03.511Z","etag":null,"topics":["command-line","config","configuration","configuration-files","dependency-free","environment","environment-variables","go","golang","json","toml","yaml"],"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/cristalhq.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}},"created_at":"2020-06-26T19:43:20.000Z","updated_at":"2025-05-14T16:06:32.000Z","dependencies_parsed_at":"2023-12-17T10:26:11.730Z","dependency_job_id":"f956ea35-674f-4861-bae0-3fdc777004eb","html_url":"https://github.com/cristalhq/aconfig","commit_stats":{"total_commits":163,"total_committers":18,"mean_commits":9.055555555555555,"dds":0.49079754601227,"last_synced_commit":"7111e04773005c1271da579d77dceccd4a5f3651"},"previous_names":[],"tags_count":86,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cristalhq%2Faconfig","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cristalhq%2Faconfig/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cristalhq%2Faconfig/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cristalhq%2Faconfig/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cristalhq","download_url":"https://codeload.github.com/cristalhq/aconfig/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254442854,"owners_count":22071878,"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":["command-line","config","configuration","configuration-files","dependency-free","environment","environment-variables","go","golang","json","toml","yaml"],"created_at":"2024-07-30T20:01:08.819Z","updated_at":"2025-05-16T00:06:21.968Z","avatar_url":"https://github.com/cristalhq.png","language":"Go","funding_links":[],"categories":["Configuration","配置","Go","yaml","Uncategorized","配置管理 `配置解析库`","配置管理"],"sub_categories":["Standard CLI","Advanced Console UIs","标准CLI","标准 CLI"],"readme":"# aconfig\r\n\r\n[![build-img]][build-url]\r\n[![pkg-img]][pkg-url]\r\n[![version-img]][version-url]\r\n\r\nSimple, useful and opinionated config loader.\r\n\r\n## Rationale\r\n\r\nThere are many solutions regarding configuration loading in Go. I was looking for a simple loader that is as easy to use and understand as possible. The goal was to load config from 4 places: defaults (in the code), files, environment variables, command-line flags. This library works with all of these sources.\r\n\r\n## Features\r\n\r\n* Simple API.\r\n* Clean and tested code.\r\n* Automatic fields mapping.\r\n* Supports different sources:\r\n  * defaults in the code\r\n  * files (JSON, YAML, TOML, DotENV, HCL)\r\n  * environment variables\r\n  * command-line flags\r\n* Dependency-free (file parsers are optional).\r\n* Ability to walk over configuration fields.\r\n\r\n## Install\r\n\r\nGo version 1.14+\r\n\r\n```\r\ngo get github.com/cristalhq/aconfig\r\n```\r\n\r\n## Example\r\n\r\n```go\r\ntype MyConfig struct {\r\n\tPort int `default:\"1111\" usage:\"just give a number\"`\r\n\tAuth struct {\r\n\t\tUser string `required:\"true\"`\r\n\t\tPass string `required:\"true\"`\r\n\t}\r\n\tPass string `default:\"\" env:\"SECRET\" flag:\"sec_ret\"`\r\n}\r\n\r\nvar cfg MyConfig\r\nloader := aconfig.LoaderFor(\u0026cfg, aconfig.Config{\r\n\t// feel free to skip some steps :)\r\n\t// SkipDefaults: true,\r\n\t// SkipFiles:    true,\r\n\t// SkipEnv:      true,\r\n\t// SkipFlags:    true,\r\n\tEnvPrefix:       \"APP\",\r\n\tFlagPrefix:      \"app\",\r\n\tFiles:           []string{\"/var/opt/myapp/config.json\", \"ouch.yaml\"},\r\n\tFileDecoders: map[string]aconfig.FileDecoder{\r\n\t\t// from `aconfigyaml` submodule\r\n\t\t// see submodules in repo for more formats\r\n\t\t\".yaml\": aconfigyaml.New(),\r\n\t},\r\n})\r\n\r\n// IMPORTANT: define your own flags with `flagSet`\r\nflagSet := loader.Flags()\r\n\r\nif err := loader.Load(); err != nil {\r\n\tpanic(err)\r\n}\r\n\r\n// configuration fields will be loaded from (in order):\r\n//\r\n// 1. defaults set in structure tags (see MyConfig defenition)\r\n// 2. loaded from files `file.json` if not `ouch.yaml` will be used\r\n// 3. from corresponding environment variables with the prefix `APP_`\r\n// 4. command-line flags with the prefix `app.` if they are\r\n```\r\n\r\nAlso see examples: [examples_test.go](https://github.com/cristalhq/aconfig/blob/master/example_test.go).\r\n\r\nIntegration with `spf13/cobra` [playground](https://play.golang.org/p/OsCR8qTCN0H).\r\n\r\n## Documentation\r\n\r\nSee [these docs][pkg-url].\r\n\r\n## License\r\n\r\n[MIT License](LICENSE).\r\n\r\n[build-img]: https://github.com/cristalhq/aconfig/workflows/build/badge.svg\r\n[build-url]: https://github.com/cristalhq/aconfig/actions\r\n[pkg-img]: https://pkg.go.dev/badge/cristalhq/aconfig\r\n[pkg-url]: https://pkg.go.dev/github.com/cristalhq/aconfig\r\n[version-img]: https://img.shields.io/github/v/release/cristalhq/aconfig\r\n[version-url]: https://github.com/cristalhq/aconfig/releases\r\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcristalhq%2Faconfig","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcristalhq%2Faconfig","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcristalhq%2Faconfig/lists"}