{"id":18986039,"url":"https://github.com/deanpdx/dotconfig","last_synced_at":"2025-07-27T12:05:35.475Z","repository":{"id":257790221,"uuid":"860974096","full_name":"DeanPDX/dotconfig","owner":"DeanPDX","description":"Simplify configuration of microservices.","archived":false,"fork":false,"pushed_at":"2025-03-31T20:12:25.000Z","size":72,"stargazers_count":5,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-16T19:26:46.473Z","etag":null,"topics":["config","golang"],"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/DeanPDX.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}},"created_at":"2024-09-21T16:57:32.000Z","updated_at":"2025-03-30T18:28:23.000Z","dependencies_parsed_at":"2024-09-22T05:46:24.826Z","dependency_job_id":"e8ec32cc-9302-4012-8ba5-f94f1dd98fc9","html_url":"https://github.com/DeanPDX/dotconfig","commit_stats":null,"previous_names":["deanpdx/dotconfig"],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DeanPDX%2Fdotconfig","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DeanPDX%2Fdotconfig/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DeanPDX%2Fdotconfig/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DeanPDX%2Fdotconfig/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/DeanPDX","download_url":"https://codeload.github.com/DeanPDX/dotconfig/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249795146,"owners_count":21326776,"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":["config","golang"],"created_at":"2024-11-08T16:29:19.132Z","updated_at":"2025-04-19T20:35:20.341Z","avatar_url":"https://github.com/DeanPDX.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Dotconfig [![Go Reference](https://pkg.go.dev/badge/github.com/DeanPDX/dotconfig.svg)](https://pkg.go.dev/github.com/DeanPDX/dotconfig) [![Go Report Card](https://goreportcard.com/badge/github.com/DeanPDX/dotconfig)](https://goreportcard.com/report/github.com/DeanPDX/dotconfig) [![Codecov](https://img.shields.io/codecov/c/github/DeanPDX/dotconfig)](https://app.codecov.io/gh/DeanPDX/dotconfig/)\n\nThis package aims to simplify configuration from environment variables. In local development, we can supply a `.env` file with key/value pairs. When deployed, values come from a secret manager. This is similar to [joho/godotenv](https://github.com/joho/godotenv) but the aim here is to not only read the `.env` file but use reflection to produce a config struct. We also support optional/required fields in the struct and default values.\n\n## Usage\nCreate a `.env` file in your current working directory with the following contents:\n\n```shell\nMAX_BYTES_PER_REQUEST='1024'\n# Double quotes are fine\nAPI_VERSION=\"1.19\"\n# All of these are valie for booleans:\n# 1, t, T, TRUE, true, True, 0, f, F, FALSE, false, False\nIS_DEV='1'\n# Raw values with no quotes are also fine\nSTRIPE_SECRET=sk_test_insertkeyhere\n# Right now supporting newlines via \"\\n\" in strings:\nWELCOME_MESSAGE='Hello,\\nWelcome to the app!\\n-The App Dev Team'\n```\n\nYou can read from this file and initialize your config with values with the following code:\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\n\t\"github.com/DeanPDX/dotconfig\"\n)\n\n// Our AppConfig with env struct tags:\ntype AppConfig struct {\n\tMaxBytesPerRequest int     `env:\"MAX_BYTES_PER_REQUEST\" default:\"2048\"` // Defaults to 2048\n\tAPIVersion         float64 `env:\"API_VERSION,required\"` // Required to be present and not empty string\n\tIsDev              bool    `env:\"IS_DEV,optional\"` // Optional: defaults to zero value\n\tStripeSecret       string  `env:\"STRIPE_SECRET\"`\n\tWelcomeMessage     string  `env:\"WELCOME_MESSAGE\"`\n}\n\nfunc Main() {\n\tconfig, err := dotconfig.FromFileName[AppConfig](\".env\")\n\tif err != nil {\n\t\tfmt.Printf(\"Error: %v.\", err)\n\t}\n\t// Config is ready to use. Don't print to console in a real \n\t// app. But for the purposes of testing:\n\tfmt.Println(config)\n}\n```\n\nSo for local dev we can use this `.env` file. But when you deploy your app, you set these values from environment variables / secret managers. Your app that consumes this config struct doesn't have to concern itself with where the values came from.\n\nIf your key value pairs are coming from a source other than a file, or you want to control file IO yourself, you can call `FromReader` instead and pass in a `io.Reader`. There is [a runnable example of that in the godoc](https://pkg.go.dev/github.com/DeanPDX/dotconfig#example-FromReader).\n\n## Error Handling and Options\n\nBy default, file IO errors in `dotconfig.FromFileName` won't produce an error. This is because when you are running in the cloud with a secret manager, not finding a `.env` file is the happy path. If you want to return errors from `os.Open` you can do so with an option:\n\n```go\nconfig, err := dotconfig.FromFileName[AppConfig](\".env\", dotconfig.ReturnFileErrors)\n```\n\nBy default, if your struct contains fields that don't have an `env:\"MY_ENV\"` tag, we assume you want us to ignore those fields. If you want missing `env` tags to produce errors, use the `dotconfig.EnforceStructTags` option:\n\n```\nconfig, err := dotconfig.FromFileName[AppConfig](\".env\", dotconfig.EnforceStructTags)\n```\n\n`dotconfig.FromFileName` and `dotconfig.FromReader` both return multiple wrapped errors. If you want to print all errors to the console you can do that:\n\n```go\ntype AppConfig struct {\n\tForgotToAddStructTag string\n\tUnsupportedType   \t complex64 `env:\"UNSUPPORTED_TYPE\"`\n}\nconfig, err := dotconfig.FromFileName[AppConfig](\".env\", dotconfig.EnforceStructTags)\nif err != nil {\n\tfmt.Printf(\"Error %v.\", err)\n}\n// Output:\n// Error: multiple errors:\n//  - missing struct tag on field: ForgotToAddStructTag\n//  - unsupported field type: complex64\n```\n\nSometimes you want more fine-grained control of error handling (because certain states you can recover from). If you want to handle each error type, you can use `dotconfig.Errors` in conjunction with `errors.Unwrap` and `errors.Is`. Here's an example where each error type is being handled:\n\n```go\ntype MyConfig struct {}\n_, err := dotconfig.FromFileName[MyConfig](\".env\", dotconfig.EnforceStructTags)\nif err != nil {\n\t// Get error slice from err\n\terrs := dotconfig.Errors(err)\n\tfor _, err := range errs {\n\t\t// Handle various error types however you want\n\t\tswitch {\n\t\tcase errors.Is(errors.Unwrap(err), dotconfig.ErrMissingEnvVar):\n\t\t\t// Handle missing environment variable\n\t\tcase errors.Is(errors.Unwrap(err), dotconfig.ErrMissingStructTag):\n\t\t\t// Handle missing struct tag\n\t\tcase errors.Is(errors.Unwrap(err), dotconfig.ErrUnsupportedFieldType):\n\t\t\t// Handle unsupported field type\n\t\tcase errors.Is(errors.Unwrap(err), dotconfig.ErrMissingRequiredField):\n\t\t\t// Handle missing required field\n\t\t}\n\t}\n}\n```\n\n## Contributing\nContributions are always welcome. Have a new idea or find a bug? Submit a pull request or create an issue! \n\nThis package is being used in production and any future updates should maintain backwards compatibility. This is why we have options; to allow us to introduce optional new features while maintaining backwards compatibility.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdeanpdx%2Fdotconfig","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdeanpdx%2Fdotconfig","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdeanpdx%2Fdotconfig/lists"}