{"id":13410963,"url":"https://github.com/dsbasko/go-cfg","last_synced_at":"2025-10-07T06:53:19.499Z","repository":{"id":222783900,"uuid":"757937651","full_name":"dsbasko/go-cfg","owner":"dsbasko","description":"The library provides a unified way to read configuration data from different sources, such as environment variables, command line flags, and configuration files.","archived":false,"fork":false,"pushed_at":"2024-08-01T04:42:11.000Z","size":32,"stargazers_count":38,"open_issues_count":1,"forks_count":4,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-08-01T21:20:09.863Z","etag":null,"topics":[],"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/dsbasko.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","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":"2024-02-15T09:48:22.000Z","updated_at":"2024-08-01T04:42:14.000Z","dependencies_parsed_at":"2024-06-04T11:13:53.336Z","dependency_job_id":"c505c6fa-86a4-413f-b664-8b41450165e6","html_url":"https://github.com/dsbasko/go-cfg","commit_stats":null,"previous_names":["dsbasko/go-cfg"],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dsbasko%2Fgo-cfg","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dsbasko%2Fgo-cfg/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dsbasko%2Fgo-cfg/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dsbasko%2Fgo-cfg/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dsbasko","download_url":"https://codeload.github.com/dsbasko/go-cfg/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249760119,"owners_count":21321843,"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:10.494Z","updated_at":"2025-10-07T06:53:14.467Z","avatar_url":"https://github.com/dsbasko.png","language":"Go","funding_links":[],"categories":["Configuration","配置"],"sub_categories":["Standard CLI","标准CLI"],"readme":"# Go Configuration Library\n\n[![Mentioned in Awesome Go](https://awesome.re/mentioned-badge.svg)](https://github.com/avelino/awesome-go) \n[![GoDoc](https://img.shields.io/badge/godoc-reference-blue.svg)](https://godoc.org/github.com/dsbasko/go-cfg)\n\n[![GitHub Workflow](https://github.com/dsbasko/go-cfg/actions/workflows/go.yaml/badge.svg?branch=main)](https://github.com/dsbasko/go-cfg/actions/workflows/go.yaml)\n[![Test Coverage](https://codecov.io/gh/dsbasko/go-cfg/graph/badge.svg?token=142JTUL2X5)](https://codecov.io/gh/dsbasko/go-cfg)\n[![Go Report Card](https://goreportcard.com/badge/github.com/dsbasko/go-cfg)](https://goreportcard.com/report/github.com/dsbasko/go-cfg)\n![GitHub Version](https://img.shields.io/github/go-mod/go-version/dsbasko/go-cfg.svg)\n![GitHub Tag](https://img.shields.io/github/tag/dsbasko/go-cfg.svg)\n\nThis project is a Go library for reading configuration data from various sources such as environment variables, command-line flags, and configuration files. The library provides a unified interface for reading configuration data, making it easier to manage and maintain your application's configuration.  \n\n## Attention\nThe library uses the [env](https://github.com/caarlos0/env), [pflag](https://github.com/spf13/pflag), [yaml](https://github.com/go-yaml/yaml), [toml](https://github.com/BurntSushi/toml) and [godotenv](https://github.com/joho/godotenv) codebase to work with environment variables and flags. This is a temporary solution, maybe I’ll write my own implementation later. Thanks to the authors of these libraries for the work done!\n\n### Installation\nTo install the library, use the go get command:\n```bash\ngo get github.com/dsbasko/go-cfg\n```\n\n## Usage\nThe library provides several functions for reading configuration data: \n\n- `ReadEnv(cfg any) error`: Reads environment variables into the provided `cfg` structure. Each field in the `cfg` structure represents an environment variable.  \n- `MustReadEnv(cfg any)`: Similar to `ReadEnv` but panics if the reading process fails.  \n- `ReadFlag(cfg any) error`: Reads command-line flags into the provided `cfg` structure. Each field in the `cfg` structure represents a command-line flag.  \n- `MustReadFlag(cfg any)`: Similar to `ReadFlag` but panics if the reading process fails.  \n- `ReadFile(path string, cfg any) error`: Reads configuration from a file into the provided `cfg` structure. The path parameter is the path to the configuration file. Each field in the `cfg` structure represents a configuration option. Supported file formats include JSON, YAML, TOML and .env.\n- `MustReadFile(path string, cfg any)`: Similar to `ReadFile` but panics if the reading process fails.\n\nHere is an example of how to use the library:\n\n```go\npackage main\n\nimport (\n\t\"github.com/dsbasko/go-cfg\"\n)\n\ntype Config struct {\n\tMode string `default:\"prod\" json:\"mode\" yaml:\"mode\" s-flag:\"m\" flag:\"mode\" env:\"MODE\" description:\"mode of the application (dev|prod)\"`\n\tHTTP struct {\n\t\tHost         string `default:\"localhost\" json:\"host\" yaml:\"host\" s-flag:\"h\" flag:\"http-host\" env:\"HTTP_HOST\"`\n\t\tPort         int    `default:\"3000\" json:\"port\" yaml:\"port\" s-flag:\"p\" flag:\"http-port\" env:\"HTTP_PORT\"`\n\t\tReadTimeout  int    `json:\"read_timeout\" yaml:\"read-timeout\" flag:\"http-read-timeout\" env:\"HTTP_READ_TIMEOUT\"`\n\t\tWriteTimeout int    `json:\"write_timeout\" yaml:\"write-timeout\" flag:\"http-write-timeout\" env:\"HTTP_WRITE_TIMEOUT\"`\n\t} `json:\"http\" yaml:\"http\"`\n}\n\nfunc main() {\n\tcfg := Config{}\n\n\tgocfg.MustReadFile(\"configs/config.yaml\", \u0026cfg)\n\tgocfg.MustReadEnv(\u0026cfg)\n\tgocfg.MustReadFlag(\u0026cfg)\n}\n```\n\nNote that you can configure the priority of the configuration. For example, you can first read YAML configs, then environment variables, and finally flags, or vice versa.\n\n## Flags\n\nRun a project with flags: `go run ./cmd/main.go -s=\"some short flag\" --flat=f1 --nested n1`\n\n```go\ntype config struct {\n\tWithShort string `s-flag:\"s\" flag:\"with-short\" description:\"With short flag\"`\n\tFlat      string `flag:\"flat\" description:\"Flat flag\"`\n\tParent    struct {\n\t\tNested string `flag:\"nested\" description:\"Nested flag\"`\n\t}\n}\n\nfunc main() {\n\tvar cfg config\n\tif err := gocfg.ReadFlag(\u0026cfg); err != nil {\n\t\tlog.Panicf(\"failed to read flag: %v\", err)\n\t}\n\t// or: gocfg.MustReadFlag(\u0026cfg)\n\n\tlog.Printf(\"WithShort: %v\\n\", cfg.WithShort)\n\tlog.Printf(\"Flat: %v\\n\", cfg.Flat)\n\tlog.Printf(\"Nested: %v\\n\", cfg.Parent.Nested)\n}\n\n// WithShort: some short flag\n// Flat: f1\n// Nested: n1\n```\n\nStruct tags are available for working with flags:\n- `default` default value;\n- `flag` the name of the flag;\n- `s-flag` short name of the flask (1 symbol);\n- `description` description of the flag that is displayed when running the `--help` command.\n\n## Environment variables\n\nThe `env` structure tag is used for environment variables.\nRun a project with environment variables: `FLAT=f1 NESTED=n1 go run ./cmd/main.go`\n\n```go\ntype config struct {\n\tFlat   string `env:\"FLAT\"`\n\tParent struct {\n\t\tNested string `env:\"NESTED\"`\n\t}\n}\n\nfunc main() {\n\tvar cfg config\n\tif err := gocfg.ReadEnv(\u0026cfg); err != nil {\n\t\tlog.Panicf(\"failed to read env: %v\", err)\n\t}\n\t// or: gocfg.MustReadEnv(\u0026cfg)\n\n\tlog.Printf(\"Flat: %v\\n\", cfg.Flat)\n\tlog.Printf(\"Nested: %v\\n\", cfg.Parent.Nested)\n}\n\n// Flat: f1\n// Nested: n1\n```\n\nStruct tags are available for working with environment variables:\n- `default` default value;\n- `env` the name of the environment variable.\n\n## Files\n\nRun a project with environment variables: `go run ./cmd/main.go`\n\n```go\ntype config struct {\n\tFlat   string `yaml:\"flat\"`\n\tParent struct {\n\t\tNested string `yaml:\"nested\"`\n\t}\n}\n\nfunc main() {\n\tvar cfg config\n\tif err := gocfg.ReadFile(path.Join(\"cmd\", \"config.yaml\"), \u0026cfg); err != nil {\n\t\tlog.Panicf(\"failed to read config.yaml file: %v\", err)\n\t}\n\t// or: gocfg.MustReadFile(path.Join(\"cmd\", \"config.yaml\"), \u0026cfg)\n\n\tlog.Printf(\"Flat: %v\\n\", cfg.Flat)\n\tlog.Printf(\"Nested: %v\\n\", cfg.Parent.Nested)\n}\n\n// Flat: f1\n// Nested: n1\n```\n\nStructure of the `yaml` file:\n```yaml\nflat: f1\nfoo:\n  nested: n1\n```\n\nStruct tags are available for working with environment variables:\n- `default` default value;\n- `env` for files of the format `.env`\n- `yaml` for files of the format `.yaml` or `.yml`\n- `toml` for files of the format `.toml`\n- `json` for files of the format `.json`\n\n\u003cbr\u003e\n\n---\n\nIf you enjoyed this project, I would appreciate it if you could give it a star! If you notice any problems or have any suggestions for improvement, please feel free to create a new issue. Your feedback means a lot to me!\n\n❤️ [Dmitriy Basenko](https://github.com/dsbasko)","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdsbasko%2Fgo-cfg","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdsbasko%2Fgo-cfg","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdsbasko%2Fgo-cfg/lists"}