Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/zoido/yag-config
yet another Go configuration library
https://github.com/zoido/yag-config
config configuration golang golang-library golang-package
Last synced: 3 months ago
JSON representation
yet another Go configuration library
- Host: GitHub
- URL: https://github.com/zoido/yag-config
- Owner: zoido
- License: mit
- Created: 2020-04-25T19:43:23.000Z (almost 5 years ago)
- Default Branch: main
- Last Pushed: 2024-05-07T11:32:43.000Z (9 months ago)
- Last Synced: 2024-06-21T04:19:44.203Z (8 months ago)
- Topics: config, configuration, golang, golang-library, golang-package
- Language: Go
- Homepage:
- Size: 137 KB
- Stars: 5
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Yet Another Golang Config Library
[![go reference](https://pkg.go.dev/badge/github.com/zoido/yag-config)](https://pkg.go.dev/github.com/zoido/yag-config)
[![licence](https://img.shields.io/github/license/zoido/yag-config?style=flat-square)](https://github.com/zoido/yag-config/blob/master/LICENSE)
![CI](https://img.shields.io/github/actions/workflow/status/zoido/yag-config/go.yaml?style=flat-square&logoColor=white&logo=github)
[![coverage](https://img.shields.io/codecov/c/github/zoido/yag-config?style=flat-square&logoColor=white&logo=codecov)](https://codecov.io/gh/zoido/yag-config)
[![go report](https://goreportcard.com/badge/github.com/zoido/yag-config?style=flat-square)](https://goreportcard.com/report/github.com/zoido/yag-config)## Overview
- obtain configuration values from flags and/or environment variables
(flags always take precedence)
- any variable or struct member can become a destination for the config value
- define defaults in the native type
- option define environment variable prefix
- option to override the environment variable name
- option to mark options as required## Example
```go
type config struct {
Str string
Bool bool
Int int
Duration time.Duration
}y := yag.New(yag.WithEnvPrefix("MY_APP_"))
cfg := &config{
Str: "default str value",
Int: 42,
}y.String(&cfg.Str, "str", "sets Str")
y.Bool(&cfg.Bool, "bool", "sets Bool")
y.Duration(&cfg.Duration, "duration", "sets Duration", yag.FromEnv("MY_DURATION_VALUE"))
y.Int(&cfg.Int, "int", "sets Int")args := []string{"-str=str flag value"}
_ = os.Setenv("MY_APP_STR", "str env value")
_ = os.Setenv("MY_APP_INT", "4")
_ = os.Setenv("MY_DURATION_VALUE", "1h")err := y.Parse(args)
if err != nil {
fmt.Printf("Error: %v\n", err)
return
}fmt.Printf("config.Str: %v, ", cfg.Str)
fmt.Printf("config.Int: %v, ", cfg.Int)
fmt.Printf("config.Bool: %v, ", cfg.Bool)
fmt.Printf("config.Duration: %v", cfg.Duration)// Output:
// config.Str: str flag value, config.Int: 4, config.Bool: false, config.Duration: 1h0m0s
```## Supported types
- `str`
- `int`, `int8`, `int16`, `int32`, `int64`
- `uint`, `uint8`, `uint16`, `uint32`, `uint64`
- `float32`, `float64`
- `bool`
- `time.Duration`
- any `flag.Value` implementation (e.g. [(github.com/sgreben/flagvar](https://github.com/sgreben/flagvar))## Credits
Originally inspired by flags processing in of
[Consul agent](https://github.com/hashicorp/consul).