gonfig

package module
v0.0.0-...-76deb94 Latest Latest
Warning

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

Go to latest
Published: May 16, 2019 License: MIT Imports: 8 Imported by: 0

README

Build Status Go Report Card code-coverage go-doc

gonfig

gonfig is a lightweight Golang package for intergrating both JSON configs and enviornment variables into one config object.

Usage

First define a configuration structure:

type Configuration struct {
	Port              int
	Connection_String string
}

Then fill in our JSON file:

{
	"Port": 8080
}

We do not define Connection_String in the JSON as we would prefer to define that through an enviornment variable.

Best practices of configuration file

using Docker:

$ docker run [...] -e Connection_String="..." [...]

To make this simple for developers we can use gonfig to easily fill in our struct.

$ go get github.com/B4dT0bi/gonfig
import "github.com/B4dT0bi/gonfig"

configuration := Configuration{}
err := gonfig.GetConf("pathtomyjonfile.json", &configuration)
if err != nil {
	panic(err)
}

Now we can use the configuration as if it was coming from one source.

// pseudo code
if configuration.Port == 8080 {
	return true
}
if configuration.Connection_String != nil {
	return true
}
using different environment variables name

If your env variable has a different name than the json one, you can just define an env attribute

type Configuration struct {
	Port              int  `env:"MYAPP_PORT"`
	Connection_String string
}

When should gonfig be used?

If you have a limited number of enviornment configuration variables, it's probably better to set the struct values yourself.

configuration.Connection_String = os.Getenv("Connection_String")

gonfig makes it easier to combine JSON and enviornment variables into one struct automatically.

Sample

You can find a sample of the use of Gonfig project HERE

Documentation

Overview

Package gonfig implements simple configuration reading from both YAML files and environment variables.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetConf

func GetConf(configuration interface{}) (err error)

GetConf aggregates all the YAML and environment variable values and puts them into the passed interface.

func GetConfByFilename

func GetConfByFilename(filename string, configuration interface{}) (err error)

GetConfByFilename aggregates all the YAML and environment variable values and puts them into the passed interface.

Types

This section is empty.

Jump to

Keyboard shortcuts

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