hydra

package module
v0.0.0-...-7616500 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 14, 2022 License: BSD-3-Clause Imports: 7 Imported by: 0

README

hydra

Go CI Go Reference

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, 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:

go get -u github.com/getpolygon/hydra

Define a configuration schema with validators and names for fallback environment variables:

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

port: 1234
address : "127.0.0.1"
postgres: "postgres://<postgres>:<password>@<host>:<port>/<database>?sslmode=disable"

and load the configuration by creating a hydra.Hydra instance:

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.

Documentation

Overview

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	Paths []string
}

hydra.Config is the configuration struct which is going to be used by Hydra for initialization.

type Hydra

type Hydra struct {
	Config Config
}

This struct will initialize a new Hydra instance which will take care of handling your configuration.

func (*Hydra) Load

func (h *Hydra) Load(dst any) (any, error)

This function will attempt to load all configuration variables both, from the environment and the YAML configuration file, which must be specified when initializing a `Hydra` struct.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL