{"id":13410913,"url":"https://github.com/antham/envh","last_synced_at":"2025-07-24T20:34:19.855Z","repository":{"id":37548485,"uuid":"78738671","full_name":"antham/envh","owner":"antham","description":"Go helpers to manage environment variables","archived":false,"fork":false,"pushed_at":"2024-11-26T06:59:22.000Z","size":116,"stargazers_count":100,"open_issues_count":0,"forks_count":2,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-06-28T23:43:39.477Z","etag":null,"topics":["configuration-management","environment","golang","tree"],"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/antham.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","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":"2017-01-12T11:25:48.000Z","updated_at":"2025-02-23T17:57:34.000Z","dependencies_parsed_at":"2024-01-08T14:30:54.245Z","dependency_job_id":"65648319-3104-48f1-8714-c43e20951344","html_url":"https://github.com/antham/envh","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"purl":"pkg:github/antham/envh","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/antham%2Fenvh","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/antham%2Fenvh/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/antham%2Fenvh/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/antham%2Fenvh/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/antham","download_url":"https://codeload.github.com/antham/envh/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/antham%2Fenvh/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":266902205,"owners_count":24003637,"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","status":"online","status_checked_at":"2025-07-24T02:00:09.469Z","response_time":99,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"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":["configuration-management","environment","golang","tree"],"created_at":"2024-07-30T20:01:10.171Z","updated_at":"2025-07-24T20:34:19.793Z","avatar_url":"https://github.com/antham.png","language":"Go","readme":"# Envh [![codecov](https://codecov.io/gh/antham/envh/branch/master/graph/badge.svg)](https://codecov.io/gh/antham/envh) [![Go Report Card](https://goreportcard.com/badge/github.com/antham/envh)](https://goreportcard.com/report/github.com/antham/envh) [![GoDoc](https://godoc.org/github.com/antham/envh?status.svg)](http://godoc.org/github.com/antham/envh) [![GitHub tag](https://img.shields.io/github/tag/antham/envh.svg)]()\n\nThis library is made up of two parts :\n\n- Env object : it wraps your environments variables in an object and provides convenient helpers.\n- Env tree object : it manages environment variables through a tree structure to store a config the same way as in a yaml file or whatever format allows to store a config hierarchically\n\n## Install\n\n    go get github.com/antham/envh\n\n## How it works\n\nCheck [the godoc](http://godoc.org/github.com/antham/envh), there are many examples provided.\n\n## Example with a tree dumped in a config struct\n\n```go\npackage envh\n\nimport (\n\t\"encoding/json\"\n\t\"fmt\"\n\t\"os\"\n\t\"strings\"\n)\n\ntype CONFIG2 struct {\n\tDB struct {\n\t\tUSERNAME   string\n\t\tPASSWORD   string\n\t\tHOST       string\n\t\tNAME       string\n\t\tPORT       int\n\t\tURL        string\n\t\tUSAGELIMIT float32\n\t}\n\tMAILER struct {\n\t\tHOST     string\n\t\tUSERNAME string\n\t\tPASSWORD string\n\t\tENABLED  bool\n\t}\n\tMAP map[string]string\n}\n\nfunc (c *CONFIG2) Walk(tree *EnvTree, keyChain []string) (bool, error) {\n\tif setter, ok := map[string]func(*EnvTree, []string) error{\n\t\t\"CONFIG2_DB_URL\": c.setURL,\n\t\t\"CONFIG2_MAP\":    c.setMap,\n\t}[strings.Join(keyChain, \"_\")]; ok {\n\t\treturn true, setter(tree, keyChain)\n\t}\n\n\treturn false, nil\n}\n\nfunc (c *CONFIG2) setMap(tree *EnvTree, keyChain []string) error {\n\tdatas := map[string]string{}\n\n\tkeys, err := tree.FindChildrenKeys(keyChain...)\n\n\tif err != nil {\n\t\treturn err\n\t}\n\n\tfor _, key := range keys {\n\t\tvalue, err := tree.FindString(append(keyChain, key)...)\n\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\n\t\tdatas[key] = value\n\t}\n\n\tc.MAP = datas\n\n\treturn nil\n}\n\nfunc (c *CONFIG2) setURL(tree *EnvTree, keyChain []string) error {\n\tdatas := map[string]string{}\n\n\tfor _, key := range []string{\"USERNAME\", \"PASSWORD\", \"HOST\", \"NAME\"} {\n\t\tvalue, err := tree.FindString(\"CONFIG2\", \"DB\", key)\n\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\n\t\tdatas[key] = value\n\t}\n\n\tport, err := tree.FindInt(\"CONFIG2\", \"DB\", \"PORT\")\n\n\tif err != nil {\n\t\treturn err\n\t}\n\n\tc.DB.URL = fmt.Sprintf(\"jdbc:mysql://%s:%d/%s?user=%s\u0026password=%s\", datas[\"HOST\"], port, datas[\"NAME\"], datas[\"USERNAME\"], datas[\"PASSWORD\"])\n\n\treturn nil\n}\n\nfunc ExampleStructWalker_customFieldSet() {\n\tos.Clearenv()\n\tsetEnv(\"CONFIG2_DB_USERNAME\", \"foo\")\n\tsetEnv(\"CONFIG2_DB_PASSWORD\", \"bar\")\n\tsetEnv(\"CONFIG2_DB_HOST\", \"localhost\")\n\tsetEnv(\"CONFIG2_DB_NAME\", \"my-db\")\n\tsetEnv(\"CONFIG2_DB_PORT\", \"3306\")\n\tsetEnv(\"CONFIG2_DB_USAGELIMIT\", \"95.6\")\n\tsetEnv(\"CONFIG2_MAILER_HOST\", \"127.0.0.1\")\n\tsetEnv(\"CONFIG2_MAILER_USERNAME\", \"foo\")\n\tsetEnv(\"CONFIG2_MAILER_PASSWORD\", \"bar\")\n\tsetEnv(\"CONFIG2_MAILER_ENABLED\", \"true\")\n\tsetEnv(\"CONFIG2_MAP_KEY1\", \"value1\")\n\tsetEnv(\"CONFIG2_MAP_KEY2\", \"value2\")\n\tsetEnv(\"CONFIG2_MAP_KEY3\", \"value3\")\n\n\tenv, err := NewEnvTree(\"^CONFIG2\", \"_\")\n\n\tif err != nil {\n\t\treturn\n\t}\n\n\ts := CONFIG2{}\n\n\terr = env.PopulateStruct(\u0026s)\n\n\tif err != nil {\n\t\treturn\n\t}\n\n\tb, err := json.Marshal(s)\n\n\tif err != nil {\n\t\treturn\n\t}\n\n\tfmt.Println(string(b))\n\t// Output:\n\t// {\"DB\":{\"USERNAME\":\"foo\",\"PASSWORD\":\"bar\",\"HOST\":\"localhost\",\"NAME\":\"my-db\",\"PORT\":3306,\"URL\":\"jdbc:mysql://localhost:3306/my-db?user=foo\\u0026password=bar\",\"USAGELIMIT\":95.6},\"MAILER\":{\"HOST\":\"127.0.0.1\",\"USERNAME\":\"foo\",\"PASSWORD\":\"bar\",\"ENABLED\":true},\"MAP\":{\"KEY1\":\"value1\",\"KEY2\":\"value2\",\"KEY3\":\"value3\"}}\n}\n```\n","funding_links":[],"categories":["配置","Uncategorized","Configuration","配置管理","\u003cspan id=\"组态-configuration\"\u003e组态 Configuration\u003c/span\u003e","配置管理 `配置解析库`"],"sub_categories":["标准CLI","Advanced Console UIs","Standard CLI","标准 CLI","\u003cspan id=\"高级控制台用户界面-advanced-console-uis\"\u003e高级控制台用户界面 Advanced Console UIs\u003c/span\u003e"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fantham%2Fenvh","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fantham%2Fenvh","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fantham%2Fenvh/lists"}