https://github.com/mikayelgr/hydra
Hydra is a hybrid configuration management library for Go, with focus on simplicity and testability.
https://github.com/mikayelgr/hydra
configuration configuration-management environment environment-variables go golang yaml
Last synced: over 1 year ago
JSON representation
Hydra is a hybrid configuration management library for Go, with focus on simplicity and testability.
- Host: GitHub
- URL: https://github.com/mikayelgr/hydra
- Owner: mikayelgr
- License: bsd-3-clause
- Created: 2022-04-09T20:13:55.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2022-05-14T05:57:23.000Z (about 4 years ago)
- Last Synced: 2025-03-19T23:35:00.286Z (over 1 year ago)
- Topics: configuration, configuration-management, environment, environment-variables, go, golang, yaml
- Language: Go
- Homepage:
- Size: 27.3 KB
- Stars: 2
- Watchers: 0
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# hydra
[](https://github.com/getpolygon/hydra/actions/workflows/go.yml)
[](https://pkg.go.dev/github.com/getpolygon/hydra)
Hydra is a hybrid configuration management library for Go, created for simplicity and testability. It supports using YAML and optionally paired with environment variables.
## Why?
In testing environments, you might not want to have separate configuration files for each one of your tests. Moreover, when using more mature configuration libraries such as [Viper](https://github.com/spf13/viper), it is virtually impossible to write tests which use configuration files, since they have to be in a directory different than your project root, which honestly, makes stuff messy.
For this reason, we created Hydra, which will read, load, and fill in the blanks of the incomplete YAML configuration using environment variables. Think of it this way, if a YAML file does not exist, then Hydra will attempt to load the configuration using environment variables, optionally defined in your schema. However, if a configuration file was found, but has missing fields, Hydra will optionally fill in those fields with the values loaded from the environment.
## Installation
Get started by installing the latest version of hydra:
```bash
go get -u github.com/getpolygon/hydra
```
Define a configuration schema with validators and names for fallback environment variables:
```go
type Settings struct {
Port int16 `yaml:"port" env:"PORT"`
Address string `yaml:"address" env:"ADDRESS"`
PostgreSQL string `yaml:"postgres" validate:"uri" env:"DSN"`
}
```
create the configuration file:
`conf.yml`
```yml
port: 1234
address : "127.0.0.1"
postgres: "postgres://:@:/?sslmode=disable"
```
and load the configuration by creating a `hydra.Hydra` instance:
```go
h := hydra.Hydra{
Config: hydra.Config{
Paths: []string{
"~/somepath/config.yaml",
},
}
}
_, err := h.Load(new(Settings))
// ...
```
Initially, Hydra will attempt to find an existing configuration file provided from the paths in Hydra configuration, and then it will move on to filling in the missing values from environment variables.
## License
This software is licensed under the permissive [BSD-3-Clause license](./LICENSE).