{"id":13713270,"url":"https://github.com/jinzhu/configor","last_synced_at":"2025-05-14T15:11:39.677Z","repository":{"id":1213949,"uuid":"41674061","full_name":"jinzhu/configor","owner":"jinzhu","description":"Golang Configuration tool that support YAML, JSON, TOML, Shell Environment","archived":false,"fork":false,"pushed_at":"2024-04-27T09:45:17.000Z","size":72,"stargazers_count":1769,"open_issues_count":32,"forks_count":206,"subscribers_count":22,"default_branch":"master","last_synced_at":"2025-04-04T05:01:38.925Z","etag":null,"topics":["go","golang","golang-configuration"],"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/jinzhu.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":"2015-08-31T12:27:22.000Z","updated_at":"2025-03-26T02:20:26.000Z","dependencies_parsed_at":"2024-01-13T22:55:16.316Z","dependency_job_id":"d28303a3-106d-4a64-ae6a-8e8b9b143e69","html_url":"https://github.com/jinzhu/configor","commit_stats":{"total_commits":60,"total_committers":15,"mean_commits":4.0,"dds":0.3666666666666667,"last_synced_commit":"f7a0fc7c9fc697269b1749c1cc472d49baa8d33c"},"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jinzhu%2Fconfigor","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jinzhu%2Fconfigor/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jinzhu%2Fconfigor/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jinzhu%2Fconfigor/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jinzhu","download_url":"https://codeload.github.com/jinzhu/configor/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248384704,"owners_count":21094762,"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":["go","golang","golang-configuration"],"created_at":"2024-08-02T23:01:31.202Z","updated_at":"2025-04-11T11:36:38.053Z","avatar_url":"https://github.com/jinzhu.png","language":"Go","funding_links":[],"categories":["开源类库","Go","Repositories","golang"],"sub_categories":["配置"],"readme":"# Configor\n\nGolang Configuration tool that support YAML, JSON, TOML, Shell Environment (Supports Go 1.10+)\n\n[![test status](https://github.com/jinzhu/configor/workflows/tests/badge.svg?branch=master \"test status\")](https://github.com/jinzhu/configor/actions)\n\n## Usage\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\t\"github.com/jinzhu/configor\"\n)\n\nvar Config = struct {\n\tAPPName string `default:\"app name\"`\n\n\tDB struct {\n\t\tName     string\n\t\tUser     string `default:\"root\"`\n\t\tPassword string `required:\"true\" env:\"DBPassword\"`\n\t\tPort     uint   `default:\"3306\"`\n\t}\n\n\tContacts []struct {\n\t\tName  string\n\t\tEmail string `required:\"true\"`\n\t}\n}{}\n\nfunc main() {\n\tconfigor.Load(\u0026Config, \"config.yml\")\n\tfmt.Printf(\"config: %#v\", Config)\n}\n```\n\nWith configuration file *config.yml*:\n\n```yaml\nappname: test\n\ndb:\n    name:     test\n    user:     test\n    password: test\n    port:     1234\n\ncontacts:\n- name: i test\n  email: test@test.com\n```\n\n## Debug Mode \u0026 Verbose Mode\n\nDebug/Verbose mode is helpful when debuging your application, `debug mode` will let you know how `configor` loaded your configurations, like from which file, shell env, `verbose mode` will tell you even more, like those shell environments `configor` tried to load.\n\n```go\n// Enable debug mode or set env `CONFIGOR_DEBUG_MODE` to true when running your application\nconfigor.New(\u0026configor.Config{Debug: true}).Load(\u0026Config, \"config.json\")\n\n// Enable verbose mode or set env `CONFIGOR_VERBOSE_MODE` to true when running your application\nconfigor.New(\u0026configor.Config{Verbose: true}).Load(\u0026Config, \"config.json\")\n```\n\n## Auto Reload Mode\n\nConfigor can auto reload configuration based on time\n\n```go\n// auto reload configuration every second\nconfigor.New(\u0026configor.Config{AutoReload: true}).Load(\u0026Config, \"config.json\")\n\n// auto reload configuration every minute\nconfigor.New(\u0026configor.Config{AutoReload: true, AutoReloadInterval: time.Minute}).Load(\u0026Config, \"config.json\")\n```\n\nAuto Reload Callback\n\n```go\nconfigor.New(\u0026configor.Config{AutoReload: true, AutoReloadCallback: func(config interface{}) {\n    fmt.Printf(\"%v changed\", config)\n}}).Load(\u0026Config, \"config.json\")\n```\n\n# Advanced Usage\n\n* Load mutiple configurations\n\n```go\n// Earlier configurations have higher priority\nconfigor.Load(\u0026Config, \"application.yml\", \"database.json\")\n```\n\n* Return error on unmatched keys\n\nReturn an error on finding keys in the config file that do not match any fields in the config struct.\nIn the example below, an error will be returned if config.toml contains keys that do not match any fields in the ConfigStruct struct.\nIf ErrorOnUnmatchedKeys is not set, it defaults to false.\n\nNote that for json files, setting ErrorOnUnmatchedKeys to true will have an effect only if using go 1.10 or later.\n\n```go\nerr := configor.New(\u0026configor.Config{ErrorOnUnmatchedKeys: true}).Load(\u0026ConfigStruct, \"config.toml\")\n```\n\n* Load configuration by environment\n\nUse `CONFIGOR_ENV` to set environment, if `CONFIGOR_ENV` not set, environment will be `development` by default, and it will be `test` when running tests with `go test`\n\n```go\n// config.go\nconfigor.Load(\u0026Config, \"config.json\")\n\n$ go run config.go\n// Will load `config.json`, `config.development.json` if it exists\n// `config.development.json` will overwrite `config.json`'s configuration\n// You could use this to share same configuration across different environments\n\n$ CONFIGOR_ENV=production go run config.go\n// Will load `config.json`, `config.production.json` if it exists\n// `config.production.json` will overwrite `config.json`'s configuration\n\n$ go test\n// Will load `config.json`, `config.test.json` if it exists\n// `config.test.json` will overwrite `config.json`'s configuration\n\n$ CONFIGOR_ENV=production go test\n// Will load `config.json`, `config.production.json` if it exists\n// `config.production.json` will overwrite `config.json`'s configuration\n```\n\n```go\n// Set environment by config\nconfigor.New(\u0026configor.Config{Environment: \"production\"}).Load(\u0026Config, \"config.json\")\n```\n\n* Example Configuration\n\n```go\n// config.go\nconfigor.Load(\u0026Config, \"config.yml\")\n\n$ go run config.go\n// Will load `config.example.yml` automatically if `config.yml` not found and print warning message\n```\n\n* Load From Shell Environment\n\n```go\n$ CONFIGOR_APPNAME=\"hello world\" CONFIGOR_DB_NAME=\"hello world\" go run config.go\n// Load configuration from shell environment, it's name is {{prefix}}_FieldName\n```\n\n```go\n// You could overwrite the prefix with environment CONFIGOR_ENV_PREFIX, for example:\n$ CONFIGOR_ENV_PREFIX=\"WEB\" WEB_APPNAME=\"hello world\" WEB_DB_NAME=\"hello world\" go run config.go\n\n// Set prefix by config\nconfigor.New(\u0026configor.Config{ENVPrefix: \"WEB\"}).Load(\u0026Config, \"config.json\")\n```\n\n* Anonymous Struct\n\nAdd the `anonymous:\"true\"` tag to an anonymous, embedded struct to NOT include the struct name in the environment\nvariable of any contained fields.  For example:\n\n```go\ntype Details struct {\n\tDescription string\n}\n\ntype Config struct {\n\tDetails `anonymous:\"true\"`\n}\n```\n\nWith the `anonymous:\"true\"` tag specified, the environment variable for the `Description` field is `CONFIGOR_DESCRIPTION`.\nWithout the `anonymous:\"true\"`tag specified, then environment variable would include the embedded struct name and be `CONFIGOR_DETAILS_DESCRIPTION`.\n\n* With flags\n\n```go\nfunc main() {\n\tconfig := flag.String(\"file\", \"config.yml\", \"configuration file\")\n\tflag.StringVar(\u0026Config.APPName, \"name\", \"\", \"app name\")\n\tflag.StringVar(\u0026Config.DB.Name, \"db-name\", \"\", \"database name\")\n\tflag.StringVar(\u0026Config.DB.User, \"db-user\", \"root\", \"database user\")\n\tflag.Parse()\n\n\tos.Setenv(\"CONFIGOR_ENV_PREFIX\", \"-\")\n\tconfigor.Load(\u0026Config, *config)\n\t// configor.Load(\u0026Config) // only load configurations from shell env \u0026 flag\n}\n```\n\n## Contributing\n\nYou can help to make the project better, check out [http://gorm.io/contribute.html](http://gorm.io/contribute.html) for things you can do.\n\n## Author\n\n**jinzhu**\n\n* \u003chttp://github.com/jinzhu\u003e\n* \u003cwosmvp@gmail.com\u003e\n* \u003chttp://twitter.com/zhangjinzhu\u003e\n\n## License\n\nReleased under the MIT License\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjinzhu%2Fconfigor","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjinzhu%2Fconfigor","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjinzhu%2Fconfigor/lists"}