{"id":15890628,"url":"https://github.com/dimfeld/goconfig","last_synced_at":"2025-03-20T11:36:44.754Z","repository":{"id":57528444,"uuid":"19877042","full_name":"dimfeld/goconfig","owner":"dimfeld","description":"Loads configuration from TOML files, allowing environment variables to override.","archived":false,"fork":false,"pushed_at":"2014-05-18T22:33:16.000Z","size":156,"stargazers_count":9,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-10-07T07:07:01.368Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/dimfeld.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":"2014-05-17T03:08:36.000Z","updated_at":"2019-07-18T08:54:59.000Z","dependencies_parsed_at":"2022-08-30T14:40:39.218Z","dependency_job_id":null,"html_url":"https://github.com/dimfeld/goconfig","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dimfeld%2Fgoconfig","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dimfeld%2Fgoconfig/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dimfeld%2Fgoconfig/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dimfeld%2Fgoconfig/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dimfeld","download_url":"https://codeload.github.com/dimfeld/goconfig/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":221756926,"owners_count":16875865,"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":[],"created_at":"2024-10-06T07:07:11.241Z","updated_at":"2024-10-28T01:02:10.355Z","avatar_url":"https://github.com/dimfeld.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"goconfig [![Build Status](https://travis-ci.org/dimfeld/goconfig.png?branch=master)](https://travis-ci.org/dimfeld/goconfig) [![GoDoc](http://godoc.org/github.com/dimfeld/goconfig?status.png)](http://godoc.org/github.com/dimfeld/goconfig)\n========\n\nLoads configuration from TOML files, allowing environment variables to override.\n\nThe package exposes a single function: `Load(configObj interface{}, reader io.Reader, envPrefix string)`.\n\nconfigObj contains a pointer to a configuration structure. The reader should supply TOML data, or can be nil for Load to just read from the environment. The environment variables corresponding to a field has the form ENVPREFIX_FIELDNAME.\n\n## Field Tags\nA field in the configuration structure can be tagged with `goconfigenv:\"VARNAME\"`. When this is present, the system will query the environment variable `VARNAME` for the value. Note that the passed envPrefix is not appended in this case.  `goconfigenv:\"-\"` can be used to indicate that a particular member should never be loaded from the environment.\n\nThe TOML parser used by this package also supports tags. The presence of a `toml:\"name\"` tag causes the parser to look the the corresponding key in the TOML data to assign to the given member. As above, `toml:\"-\"` can be used to indicate that a particular key should not be loaded from TOML. Note that if `toml:\"-\"` is added to a member, this package will also be unable to get a value for it from the environment.\n\n## Example\n\n````go\nfunc ExampleLoad() {\n    type exampleConfigStruct struct {\n        B bool\n        I int\n    }\n\n    // Note that the fields must be exported for the TOML parser to see them.\n    // The type itself does not have to be exported though.\n    type exampleConfig struct {\n        // Overridden TOML key\n        B bool `toml:\"boolean\"`\n        // Overridden TOML key and environment variable\n        I int `toml:\"SomeInt\" goconfigenv:\"SYSTEMINT\"`\n        // Overridden environment variable\n        S       string `goconfigenv:\"IMPORTANTSTRING\"`\n        IntList []int\n        Str     exampleConfigStruct\n        StrList []exampleConfigStruct\n    }\n\n    tomlExample := `\nboolean = true\nSomeint = 1\ns = \"ABC\"\nintList = [1, 2, 3]\n[str]\nb = true\ni = 2\n\n[[strList]]\nb = false\ni = 3\n\n[[strList]]\nb = true\ni = 4\n`\n    config := exampleConfig{\n        B: true,\n        S: \"default value string\",\n    }\n    reader := bytes.NewReader([]byte(tomlExample))\n    Load(\u0026config, reader, \"EXAMPLE\")\n    fmt.Println(\"Original data loaded\")\n    fmt.Println(config)\n\n    // The environment still uses the member name.\n    os.Setenv(\"EXAMPLE_B\", \"false\")\n    // Overridden environment variable name. Note that this doesn't use the prefix.\n    os.Setenv(\"SYSTEMINT\", \"3\")\n    // The parser will automatically add quotes around a string variable if\n    // not present in the environment.\n    os.Setenv(\"IMPORTANTSTRING\", \"DEF\")\n    // Lists of primitives follow the TOML format\n    os.Setenv(\"EXAMPLE_INTLIST\", \"[3,4,5]\")\n    // Single newline separates structure items\n    os.Setenv(\"EXAMPLE_STR\", \"b = false\\ni = 4\")\n    // Single newline separate structure items\n    // Double newline separates list items\n    os.Setenv(\"EXAMPLE_STRLIST\", \"b = true\\ni=6\\n\\nb = false\\ni = 7\\n\\nb=true\\ni=8\")\n    Load(\u0026config, reader, \"EXAMPLE\")\n\n    fmt.Println(\"Data overridden by environment\")\n    fmt.Println(config)\n\n    // Output:\n    // Original data loaded\n    // {true 1 ABC [1 2 3] {true 2} [{false 3} {true 4}]}\n    // Data overridden by environment\n    // {false 3 DEF [3 4 5] {false 4} [{true 6} {false 7} {true 8}]}\n}\n````\n\n## Acknowledgements\n\nThis package uses the TOML parser from [github.com/BurntSushi/toml](https://github.com/BurntSushi/toml)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdimfeld%2Fgoconfig","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdimfeld%2Fgoconfig","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdimfeld%2Fgoconfig/lists"}