https://github.com/vkd/gowalker
Golang struct walker. Loads ENV variables into Golang struct fields.
https://github.com/vkd/gowalker
configuration env environment golang golang-library struct walker
Last synced: 5 months ago
JSON representation
Golang struct walker. Loads ENV variables into Golang struct fields.
- Host: GitHub
- URL: https://github.com/vkd/gowalker
- Owner: vkd
- License: mit
- Created: 2019-01-25T18:05:28.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2024-12-17T23:07:37.000Z (over 1 year ago)
- Last Synced: 2024-12-18T00:19:21.813Z (over 1 year ago)
- Topics: configuration, env, environment, golang, golang-library, struct, walker
- Language: Go
- Homepage:
- Size: 101 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Golang struct walker
[](https://travis-ci.org/vkd/gowalker)
[](https://codecov.io/gh/vkd/gowalker)
[](https://goreportcard.com/report/github.com/vkd/gowalker)
[](https://godoc.org/github.com/vkd/gowalker)
[](https://sourcegraph.com/github.com/vkd/gowalker?badge)
[](https://github.com/vkd/gowalker/releases)
Walking throught golang struct to fullfil its fields from ENV variables.
## Install
```sh
$ go get github.com/vkd/gowalker
```
## Example of the config parsing
```go
import (
"log"
"os"
"github.com/vkd/gowalker"
"github.com/vkd/gowalker/config"
)
type Config struct {
LogLevel string `flag:"loglevel" env:"LOGLEVEL" required:"true"`
Timeout time.Duration `default:"3s"`
DB struct {
Port int `default:"5432" flag:"db-port" env:"DB_PORT"`
}
}
func ParseConfig() {
var cfg Config
err := config.Walk(&cfg, log.New(os.Stdout, "", 0),
gowalker.Flags(gowalker.FieldKey("flag", gowalker.Fullname("-", strings.ToLower)), os.Args),
gowalker.Envs(gowalker.FieldKey("env", gowalker.Fullname("_", strings.ToUpper)), os.LookupEnv),
gowalker.Tag("default"),
gowalker.Required("required"),
)
if err != nil {
if errors.Is(err, gowalker.ErrPrintHelp) {
return nil
}
...
}
}
```