{"id":13411118,"url":"https://github.com/kelseyhightower/envconfig","last_synced_at":"2025-05-14T11:03:04.905Z","repository":{"id":11670029,"uuid":"14179252","full_name":"kelseyhightower/envconfig","owner":"kelseyhightower","description":"Golang library for managing configuration data from environment variables","archived":false,"fork":false,"pushed_at":"2024-07-16T07:32:27.000Z","size":121,"stargazers_count":5205,"open_issues_count":55,"forks_count":386,"subscribers_count":37,"default_branch":"master","last_synced_at":"2025-05-07T10:52:31.306Z","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/kelseyhightower.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":"2013-11-06T17:01:55.000Z","updated_at":"2025-05-07T05:05:40.000Z","dependencies_parsed_at":"2024-06-18T10:51:17.681Z","dependency_job_id":"38a8ba21-d8c6-4450-95d7-f35716163619","html_url":"https://github.com/kelseyhightower/envconfig","commit_stats":{"total_commits":108,"total_committers":50,"mean_commits":2.16,"dds":0.8148148148148149,"last_synced_commit":"10e87fe9eaec671f89425dc366f004a9336bcc8f"},"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kelseyhightower%2Fenvconfig","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kelseyhightower%2Fenvconfig/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kelseyhightower%2Fenvconfig/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kelseyhightower%2Fenvconfig/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kelseyhightower","download_url":"https://codeload.github.com/kelseyhightower/envconfig/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254129467,"owners_count":22019628,"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-07-30T20:01:11.520Z","updated_at":"2025-05-14T11:03:04.840Z","avatar_url":"https://github.com/kelseyhightower.png","language":"Go","funding_links":[],"categories":["配置","Configurations","开源类库","Configuration","Go","Open source library","others","配置管理","Uncategorized","配置管理 `配置解析库`","Go 🐹"],"sub_categories":["Advanced Console UIs","标准CLI","配置","Standard CLI","Construction","标准 CLI"],"readme":"# envconfig\n\n[![Build Status](https://travis-ci.org/kelseyhightower/envconfig.svg)](https://travis-ci.org/kelseyhightower/envconfig)\n\n```Go\nimport \"github.com/kelseyhightower/envconfig\"\n```\n\n## Documentation\n\nSee [godoc](http://godoc.org/github.com/kelseyhightower/envconfig)\n\n## Usage\n\nSet some environment variables:\n\n```Bash\nexport MYAPP_DEBUG=false\nexport MYAPP_PORT=8080\nexport MYAPP_USER=Kelsey\nexport MYAPP_RATE=\"0.5\"\nexport MYAPP_TIMEOUT=\"3m\"\nexport MYAPP_USERS=\"rob,ken,robert\"\nexport MYAPP_COLORCODES=\"red:1,green:2,blue:3\"\n```\n\nWrite some code:\n\n```Go\npackage main\n\nimport (\n    \"fmt\"\n    \"log\"\n    \"time\"\n\n    \"github.com/kelseyhightower/envconfig\"\n)\n\ntype Specification struct {\n    Debug       bool\n    Port        int\n    User        string\n    Users       []string\n    Rate        float32\n    Timeout     time.Duration\n    ColorCodes  map[string]int\n}\n\nfunc main() {\n    var s Specification\n    err := envconfig.Process(\"myapp\", \u0026s)\n    if err != nil {\n        log.Fatal(err.Error())\n    }\n    format := \"Debug: %v\\nPort: %d\\nUser: %s\\nRate: %f\\nTimeout: %s\\n\"\n    _, err = fmt.Printf(format, s.Debug, s.Port, s.User, s.Rate, s.Timeout)\n    if err != nil {\n        log.Fatal(err.Error())\n    }\n\n    fmt.Println(\"Users:\")\n    for _, u := range s.Users {\n        fmt.Printf(\"  %s\\n\", u)\n    }\n\n    fmt.Println(\"Color codes:\")\n    for k, v := range s.ColorCodes {\n        fmt.Printf(\"  %s: %d\\n\", k, v)\n    }\n}\n```\n\nResults:\n\n```Bash\nDebug: false\nPort: 8080\nUser: Kelsey\nRate: 0.500000\nTimeout: 3m0s\nUsers:\n  rob\n  ken\n  robert\nColor codes:\n  red: 1\n  green: 2\n  blue: 3\n```\n\n## Struct Tag Support\n\nEnvconfig supports the use of struct tags to specify alternate, default, and required\nenvironment variables.\n\nFor example, consider the following struct:\n\n```Go\ntype Specification struct {\n    ManualOverride1 string `envconfig:\"manual_override_1\"`\n    DefaultVar      string `default:\"foobar\"`\n    RequiredVar     string `required:\"true\"`\n    IgnoredVar      string `ignored:\"true\"`\n    AutoSplitVar    string `split_words:\"true\"`\n    RequiredAndAutoSplitVar    string `required:\"true\" split_words:\"true\"`\n}\n```\n\nEnvconfig has automatic support for CamelCased struct elements when the\n`split_words:\"true\"` tag is supplied. Without this tag, `AutoSplitVar` above\nwould look for an environment variable called `MYAPP_AUTOSPLITVAR`. With the\nsetting applied it will look for `MYAPP_AUTO_SPLIT_VAR`. Note that numbers\nwill get globbed into the previous word. If the setting does not do the\nright thing, you may use a manual override.\n\nEnvconfig will process value for `ManualOverride1` by populating it with the\nvalue for `MYAPP_MANUAL_OVERRIDE_1`. Without this struct tag, it would have\ninstead looked up `MYAPP_MANUALOVERRIDE1`. With the `split_words:\"true\"` tag\nit would have looked up `MYAPP_MANUAL_OVERRIDE1`.\n\n```Bash\nexport MYAPP_MANUAL_OVERRIDE_1=\"this will be the value\"\n\n# export MYAPP_MANUALOVERRIDE1=\"and this will not\"\n```\n\nIf envconfig can't find an environment variable value for `MYAPP_DEFAULTVAR`,\nit will populate it with \"foobar\" as a default value.\n\nIf envconfig can't find an environment variable value for `MYAPP_REQUIREDVAR`,\nit will return an error when asked to process the struct.  If\n`MYAPP_REQUIREDVAR` is present but empty, envconfig will not return an error.\n\nIf envconfig can't find an environment variable in the form `PREFIX_MYVAR`, and there\nis a struct tag defined, it will try to populate your variable with an environment\nvariable that directly matches the envconfig tag in your struct definition:\n\n```shell\nexport SERVICE_HOST=127.0.0.1\nexport MYAPP_DEBUG=true\n```\n```Go\ntype Specification struct {\n    ServiceHost string `envconfig:\"SERVICE_HOST\"`\n    Debug       bool\n}\n```\n\nEnvconfig won't process a field with the \"ignored\" tag set to \"true\", even if a corresponding\nenvironment variable is set.\n\n## Supported Struct Field Types\n\nenvconfig supports these struct field types:\n\n  * string\n  * int8, int16, int32, int64\n  * bool\n  * float32, float64\n  * slices of any supported type\n  * maps (keys and values of any supported type)\n  * [encoding.TextUnmarshaler](https://golang.org/pkg/encoding/#TextUnmarshaler)\n  * [encoding.BinaryUnmarshaler](https://golang.org/pkg/encoding/#BinaryUnmarshaler)\n  * [time.Duration](https://golang.org/pkg/time/#Duration)\n\nEmbedded structs using these fields are also supported.\n\n## Custom Decoders\n\nAny field whose type (or pointer-to-type) implements `envconfig.Decoder` can\ncontrol its own deserialization:\n\n```Bash\nexport DNS_SERVER=8.8.8.8\n```\n\n```Go\ntype IPDecoder net.IP\n\nfunc (ipd *IPDecoder) Decode(value string) error {\n    *ipd = IPDecoder(net.ParseIP(value))\n    return nil\n}\n\ntype DNSConfig struct {\n    Address IPDecoder `envconfig:\"DNS_SERVER\"`\n}\n```\n\nExample for decoding the environment variables into map[string][]structName type\n\n```Bash\nexport SMS_PROVIDER_WITH_WEIGHT= `IND=[{\"name\":\"SMSProvider1\",\"weight\":70},{\"name\":\"SMSProvider2\",\"weight\":30}];US=[{\"name\":\"SMSProvider1\",\"weight\":100}]`\n```\n\n```GO\ntype providerDetails struct {\n\tName   string\n\tWeight int\n}\n\ntype SMSProviderDecoder map[string][]providerDetails\n\nfunc (sd *SMSProviderDecoder) Decode(value string) error {\n\tsmsProvider := map[string][]providerDetails{}\n\tpairs := strings.Split(value, \";\")\n\tfor _, pair := range pairs {\n\t\tproviderdata := []providerDetails{}\n\t\tkvpair := strings.Split(pair, \"=\")\n\t\tif len(kvpair) != 2 {\n\t\t\treturn fmt.Errorf(\"invalid map item: %q\", pair)\n\t\t}\n\t\terr := json.Unmarshal([]byte(kvpair[1]), \u0026providerdata)\n\t\tif err != nil {\n\t\t\treturn fmt.Errorf(\"invalid map json: %w\", err)\n\t\t}\n\t\tsmsProvider[kvpair[0]] = providerdata\n\n\t}\n\t*sd = SMSProviderDecoder(smsProvider)\n\treturn nil\n}\n\ntype SMSProviderConfig struct {\n    ProviderWithWeight SMSProviderDecoder `envconfig:\"SMS_PROVIDER_WITH_WEIGHT\"`\n}\n```\n\nAlso, envconfig will use a `Set(string) error` method like from the\n[flag.Value](https://godoc.org/flag#Value) interface if implemented.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkelseyhightower%2Fenvconfig","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkelseyhightower%2Fenvconfig","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkelseyhightower%2Fenvconfig/lists"}