{"id":13410788,"url":"https://github.com/num30/config","last_synced_at":"2025-04-09T17:26:41.411Z","repository":{"id":39632834,"uuid":"487575088","full_name":"num30/config","owner":"num30","description":"Declarative configuration for Go","archived":false,"fork":false,"pushed_at":"2025-02-23T19:24:21.000Z","size":148,"stargazers_count":56,"open_issues_count":4,"forks_count":4,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-02T15:09:23.576Z","etag":null,"topics":["config","configuration","environment-variables","go","golang","hacktoberfest","json","toml","yaml"],"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/num30.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}},"created_at":"2022-05-01T15:39:36.000Z","updated_at":"2025-03-28T11:55:36.000Z","dependencies_parsed_at":"2023-12-01T19:25:18.887Z","dependency_job_id":"2ec458e2-f069-4ae0-ae12-2ab77d159ea4","html_url":"https://github.com/num30/config","commit_stats":null,"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/num30%2Fconfig","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/num30%2Fconfig/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/num30%2Fconfig/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/num30%2Fconfig/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/num30","download_url":"https://codeload.github.com/num30/config/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248076294,"owners_count":21043744,"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","environment-variables","go","golang","hacktoberfest","json","toml","yaml"],"created_at":"2024-07-30T20:01:09.161Z","updated_at":"2025-04-09T17:26:41.390Z","avatar_url":"https://github.com/num30.png","language":"Go","readme":"# Declarative configuration for Go  :rocket:\n[![test-and-lint](https://github.com/num30/config/actions/workflows/test-and-lint.yaml/badge.svg)](https://github.com/num30/config/actions/workflows/test-and-lint.yaml)\n[![codecov](https://codecov.io/gh/num30/config/branch/main/graph/badge.svg?token=YBOM7T2YUK)](https://codecov.io/gh/num30/config)\n[![Go Report Card](https://goreportcard.com/badge/github.com/num30/config)](https://goreportcard.com/report/github.com/num30/config)\n[![Go Reference](https://pkg.go.dev/badge/github.com/num30/config.svg)](https://pkg.go.dev/github.com/num30/config)\n\n## Features\n- declarative way of defining configuration\n- reading configuration from file, environment variables or command line arguments in one lines of code\n- validation \n\n## Example \n`config` is a package that supports reading configuration into a struct from files, environment variable and command line arguments.\nAll you need is to declare a config structure and call `Read` method.\n\n``` go\npackage main\n\nimport (\n\t\"fmt\"\n\n\t\"github.com/num30/config\"\n)\n\ntype Config struct {\n\tDB        Database `default:{}`\n\tDebugMode bool     `flag:\"debug\" usage:\"enable verbose debug logging\"`\n}\n\ntype Database struct {\n\tHost     string `default:\"localhost\" validate:\"required\"`\n\tPassword string `validate:\"required\" envvar:\"DB_PASS\"`\n\tDbName   string `default:\"mydb\"`\n\tUsername string `default:\"root\"`\n\tPort     int    `default:\"5432\"`\n}\n\nfunc main() {\n\tvar conf Config\n\terr := config.NewConfReader(\"myconf\").Read(\u0026conf)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tfmt.Printf(\"Config %+v\", conf)\n}\n\n```\nWhen you want to change, a DB Host of your applications you can do it in 3 ways:\n1. create config `myconf.yaml` file in home directory \n``` \ndb:\n   host: \"localhost\"\n```\n2. set environment variable. Like `DB_HOST=localhost`\n3. set command line argument. Like `--db.host=localhost`\n\n:information_source: Refer to the [example](/examples/main.go) that illustrates how to use `ConfReader`. \n\nExecute  `go run examples/main.go` to run the example. \n\n\n\n### Install :package:\n``` go\ngo get github.com/num30/config  \n```\n\n## Setting Configuration Values :construction_worker:\n\n`ConfReader` merges values from all three sources in the following order:\n1. File\n2. Environment variables\n3. Command line arguments\n\nSetting same key in file will be overridden by environment variable and command line argument has the highest priority. \nHowever, you can set one key in file and other in env vars or command line args. Those will be merged. \n\n### Config File :memo:\n#### Name\n`ConfReader` will use config name property to search for a config file with that name.\n\n#### Location\nBy default, config reader will search for a config file in home or in current directory. \nYou can override this behavior by using `NewConfReader(\"myconf\").WithSearchDirs(\"/etc/conf\")` of config builder\n\n#### Referring fields\nField names are converted from camel case starting with lower case letter. For example if it code you refer to value as `DB.DbName` then it will be converted to \n``` yaml\ndb:\n   dbName: \"mydb\"\n```\n\n#### Format\n\nConfig file type could be any type supported by  [viper](https://github.com/spf13/viper#reading-config-files): JSON, TOML, YAML, HCL, INI, envfile and Java Properties files.\n\n### Environment Variables :package:\n\nTo set a flag via environment variable, make all letters uppercase and replace '.' with '_' in path. For example: app.Server.Port -\u003e APP_SERVER_PORT\n\nYou can set a prefix for environment variables. For example `NewConfReader(\"myconf\").WithPrefix(\"MYAPP\")` will search for environment variables like `MYAPP_DB_HOST`\n\nEnvironment variable names could be set in the struct tag `envvar`. For example \n```\nPassword string `envvar:\"DB_PASS\"`\n``` \nwill use value from environment variable `DB_PASS` to configure `Password` field.\n\n### Command Line Arguments :computer: \n\nTo set a configuration field via command line argument you need to pass and argument prefixes wiht `--` and lowercase field name with path. Like `--db.host=localhost`\nBoolean value could be set by passing only flag name like `--verbose`\n\nYou can override name for a flag by using tag `flag:\"name\"` on a field. For example:\n\n``` go\ntype Config struct {\t\t\n\tDebugMode             bool `flag:\"debug\"`\n}\n```\nYou can set the flag by calling `myapp --debug`\n\n\n## Validations :underage:\nYou can validate fields of you configuration struct by using `validate` tag. For example:\n\n``` go\ntype Config struct {\t\t\n    Host           string `validate:\"required\"`\n}\n```\n\nFor full list of validation tag refer to [validator](https://github.com/go-playground/validator#baked-in-validations) documentation.\n\n## FAQ\n\n- How to set values for slice? \n    If we have struct like\n    ```\n    type SliceConf struct {\n\t    Slice []string\n    }\n    ```\n    then we can set values for slice in the following ways:\n    - environment variable\n        `export SLICE=\"a,b\"`\n    - command line argument\n        `myapp --slice\", \"a\", \"--slice\", \"b\"`\n    - config file\n        `slice: [ \"a\", \"b\"]`\n\n    \n\n##  Contributing :clap:\nWe love help! Contribute by forking the repo and opening a pull requests or by creating an issue.\n\n## Credits :star:\nThis package is based [Viper](https://github.com/spf13/viper)\nSpecial thanks:\n- [enviper](https://github.com/iamolegga/enviper) for making environment variables work with viper\n- [defaults](https://github.com/creasty/defaults) for making default values work with structures\n","funding_links":[],"categories":["Configuration","配置","Go"],"sub_categories":["Standard CLI","标准CLI","Advanced Console UIs"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnum30%2Fconfig","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnum30%2Fconfig","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnum30%2Fconfig/lists"}