{"id":16534540,"url":"https://github.com/iamolegga/enviper","last_synced_at":"2025-03-21T09:32:08.068Z","repository":{"id":44934924,"uuid":"273199179","full_name":"iamolegga/enviper","owner":"iamolegga","description":"Consider environment variables while unmarshaling viper's config","archived":false,"fork":false,"pushed_at":"2023-10-02T12:56:44.000Z","size":21,"stargazers_count":24,"open_issues_count":4,"forks_count":9,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-03-01T05:05:37.088Z","etag":null,"topics":["config","configuration","go","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/iamolegga.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-18T09:39:39.000Z","updated_at":"2024-05-15T09:54:35.000Z","dependencies_parsed_at":"2024-06-18T18:38:03.590Z","dependency_job_id":"2891f018-0d4c-4acb-9bbb-1a7d65b94850","html_url":"https://github.com/iamolegga/enviper","commit_stats":null,"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iamolegga%2Fenviper","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iamolegga%2Fenviper/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iamolegga%2Fenviper/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iamolegga%2Fenviper/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/iamolegga","download_url":"https://codeload.github.com/iamolegga/enviper/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244130281,"owners_count":20402753,"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","configuration","go","golang"],"created_at":"2024-10-11T18:24:30.966Z","updated_at":"2025-03-21T09:32:07.710Z","avatar_url":"https://github.com/iamolegga.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# enviper\n\n[![PkgGoDev](https://pkg.go.dev/badge/github.com/iamolegga/enviper)](https://pkg.go.dev/github.com/iamolegga/enviper)\n[![Build Status](https://circleci.com/gh/iamolegga/enviper.svg?style=svg)](https://circleci.com/gh/iamolegga/enviper)\n[![Test Coverage](https://api.codeclimate.com/v1/badges/85fb13ce6638226a3732/test_coverage)](https://codeclimate.com/github/iamolegga/enviper/test_coverage)\n[![Maintainability](https://api.codeclimate.com/v1/badges/85fb13ce6638226a3732/maintainability)](https://codeclimate.com/github/iamolegga/enviper/maintainability)\n[![Go Report Card](https://goreportcard.com/badge/github.com/iamolegga/enviper)](https://goreportcard.com/report/github.com/iamolegga/enviper)\n\nPackage enviper is a helper/wrapper for [viper](http://github.com/spf13/viper) with the same API.\nIt makes it possible to unmarshal config to struct considering environment variables.\n\n## Problem\n\n[Viper](https://github.com/spf13/viper) package doesn't consider environment variables while unmarshaling.\nPlease, see: [188](https://github.com/spf13/viper/issues/188) and [761](https://github.com/spf13/viper/issues/761)\n\n## Solution\n\nJust wrap viper instance and use the same `Unmarshal` method as you did before:\n\n```go\ne := enviper.New(viper.New())\ne.Unmarshal(\u0026config)\n```\n\n## Example\n\n```go\npackage main\n\nimport (\n\t\"github.com/iamolegga/enviper\"\n\t\"github.com/spf13/viper\"\n)\n\ntype barry struct {\n    Bar int `mapstructure:\"bar\"`\n}\ntype bazzy struct {\n    Baz bool\n}\ntype quxxy struct {\n\tQux string\n}\ntype config struct {\n    Foo      string\n    Barry    barry\n    Barries  map[string]barry\n    Bazzy    bazzy `mapstructure:\",squash\"`\n    Quxxy    *quxxy\n}\n\n// For example this kind of structure can be unmarshaled with next yaml:\n//  Foo: foo\n//  Barry:\n//    bar: 42\n//  Baz: true\n//  Barries: \n//    key1:\n//      Bar: 255\n//    key2:\n//      Bar: 256\n//  Quxxy:\n//    Qux: \"lorem\"\n//\n// And then it could be overriden by next env variables:\n//  FOO=foo\n//  BARRY_BAR=42\n//  BAZ=true\n//  BARRIES_KEY1_BAR=42\n//  QUXXY_QUX=ipsum\n//\n// Or with prefix:\n//  MYAPP_FOO=foo\n//  MYAPP_BARRY_BAR=42\n//  MYAPP_BAZ=true\n//  MYAPP_BARRIES_KEY1_BAR=42\n//  MYAPP_QUXXY_QUX=ipsum\n\nfunc main() {    \n    var c config\n\n    e := enviper.New(viper.New())\n    e.SetEnvPrefix(\"MYAPP\")\n    e.AddConfigPath(\"/my/config/path\")\n    e.SetConfigName(\"config\")\n\n    e.Unmarshal(\u0026c)\n}\n```\n\n## Custom Tag Names\n\nIn case you want to use custom tag name (something different from `mapstructure`), you have to set it explicitly via `WithTagName` function.\nThe wrapper must know custom tag name in order to register all the env vars for viper so you can't just use `DecoderConfigOption`.\n\n## Credits\n\nThanks to\n[krak3n](https://github.com/krak3n) ([issuecomment-399884438](https://github.com/spf13/viper/issues/188#issuecomment-399884438))\nand\n[celian-garcia](https://github.com/celian-garcia) ([issuecomment-626122696](https://github.com/spf13/viper/issues/761#issuecomment-626122696))\nfor inspiring.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fiamolegga%2Fenviper","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fiamolegga%2Fenviper","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fiamolegga%2Fenviper/lists"}