{"id":36865565,"url":"https://github.com/gen0cide/cfx","last_synced_at":"2026-01-12T14:53:33.104Z","repository":{"id":57517800,"uuid":"247135588","full_name":"gen0cide/cfx","owner":"gen0cide","description":"An Fx (go.uber.org/fx) configuration provider that allows other components to load their configs in a standard way.","archived":false,"fork":false,"pushed_at":"2020-03-14T01:43:38.000Z","size":37,"stargazers_count":15,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-08-15T11:41:30.544Z","etag":null,"topics":["config","configuration","fx","go","golang","golang-package"],"latest_commit_sha":null,"homepage":null,"language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/gen0cide.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}},"created_at":"2020-03-13T18:18:32.000Z","updated_at":"2024-11-03T19:48:42.000Z","dependencies_parsed_at":"2022-09-26T18:01:32.336Z","dependency_job_id":null,"html_url":"https://github.com/gen0cide/cfx","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/gen0cide/cfx","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gen0cide%2Fcfx","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gen0cide%2Fcfx/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gen0cide%2Fcfx/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gen0cide%2Fcfx/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/gen0cide","download_url":"https://codeload.github.com/gen0cide/cfx/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gen0cide%2Fcfx/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28340411,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-12T12:22:26.515Z","status":"ssl_error","status_checked_at":"2026-01-12T12:22:10.856Z","response_time":98,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6: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","fx","go","golang","golang-package"],"created_at":"2026-01-12T14:53:32.304Z","updated_at":"2026-01-12T14:53:33.057Z","avatar_url":"https://github.com/gen0cide.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# cfx\n\nAn Fx (go.uber.org/fx) configuration provider that allows other components to load their configs in a standard way.\n\n## Usage\n\n```shell\ngo get github.com/gen0cide/cfx\n```\n\nSince this is an Fx module, all you need to do is include the cfx Module in your Fx options:\n\n```go\napp := fx.Option(\n  // FOO here is the provided ENV VAR prefix. All cfx assignable env vars\n  // will now be prefixed with \"FOO_\" when they're queried. I.e. CFX_ENVIRONMENT =\u003e FOO_ENVIRONMENT.\n  cfx.NewFXEnvContext(\"FOO\"),\n  cfx.Module,\n)\n```\n\nAfter that, the two main types you can use in your constructors are:\n\n- `cfx.EnvContext` - has information about the current environment and parses environment variables.\n- `cfx.Container` - Contains the merged and set YAML configuration objects. This allows you to quickly extract yaml sections dedicated to individual components.\n\n## How does it work\n\nThat's easy. `cfx` does two things to load configuration:\n\n1. determines the config directory\n2. loads configuration files\n\nLets take those one at a time.\n\n### Determining the config directory\n\nThe first thing `cfx` does is gathers information about the environment it's operating in. It does this\nthrough a combination of system info (hostname, GOOS, etc.) as well as environment variables.\n\nThis results in the `cfx.EnvContext` type [doc](https://pkg.go.dev/github.com/gen0cide/cfx?tab=doc#EnvContext).\n\nTwo properties of the `cfx.EnvContext` are the `AppPath` and `ConfigPath`. These are locations on the filesystem. By **DEFAULT**, `AppPath` will be the current working directory when the program is run. `ConfigPath` will simply be `AppPath` + \"config\". So if the app is running out of `/opt/foo`, the `ConfigPath` will be set to `/opt/foo/config`.\n\n`cfx` checks to make sure that both of these paths are valid folders and that the application has permissions\nto read them.\n\nYou can customize `AppPath` with the environment variable `CFX_APP_DIR` and if you wish to completely override `ConfigPath` you can use the environment variable `CFX_CONFIG_DIR`.\n\nLastly, `cfx` expects an `Environment` to be defined. By default, that is set to `development`, but can be overridden with the `CFX_ENVIRONMENT` environment variable. (Or more precisely, `${ENV_PREFIX}_ENVIRONMENT` where `ENV_PREFIX` is the value that you passed into the `cfx.NewFXEnvContext()` constructor.) Env Prefixes can be uppercase alpha numeric and include '\\_' characters, but it cannot start nor end with one.\n\nUsers can define their own environments, but they must conform to the following rules:\n\n1. lowercase alpha numeric characters only\n2. longer than 2 characters\n3. shorter than 64 characters\n\nThe exported function `cfx.ParseEnv` is what performs this validation.\n\n### Loading the Configuration File\n\nSo at this point, we've determined that theres a folder for configurations and an environment ID. You can now place your YAML configurations inside `${environment}.yaml` inside the config directory. `cfx` will attempt to load **at most 2** configurations - `base.yaml` and `${environment}.yaml`.\n\nThese configurations are loaded in order, and any changes found in `${environment}.yaml` are merged on top of the `base.yaml`. This makes defining base configurations easy, with overrides for individual environments.\n\n`cfx` doesn't care if there is no `base.yaml`, but will load it if it is present.\n\n### Populating your configuration structs\n\nLets say your YAML looks like this:\n\n```yaml\nfoo:\n  name: bob\n\nbar:\n  location: gym\n```\n\nUsing struct tags, you can define go types that corraspond to the structure under the top level YAML keys.\n\n```go\ntype Foo struct {\n  Name string `yaml:\"name\"`\n}\n\ntype Bar struct {\n  Location string `yaml:\"gym\"`\n}\n```\n\nTo unmarshal the configuration out of a `cfx.Container`, simply do this:\n\n```go\n\n_ = cfg\n// assume that cfg is the `cfx.Container` type\n\nf := Foo{}\nb := Bar{}\n\nerr := cfg.Populate(\"foo\", \u0026f)\nif err != nil {\n  // handle error\n}\n\nerr := cfg.Populate(\"bar\", \u0026b)\nif err != nil {\n  // handle error\n}\n\n// f.Name now equals \"bob\"\n// b.Location now equals \"gym\"\n```\n\nThose are easily setup in your fx constructors. Take a look at the example repo [here](https://github.com/gen0cide/cfx-example). It reproduces this exact example with a full main.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgen0cide%2Fcfx","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgen0cide%2Fcfx","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgen0cide%2Fcfx/lists"}