config

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

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

Go to latest
Published: Apr 20, 2022 License: MIT Imports: 11 Imported by: 0

README

go-config

unit-tests codecov

This repository provides a library that automatically detects the file extension and parses the data into the desired struct. If these steps were successful, the library will overload the parsed configuration with the desired environment variables.

Example

type Config struct {
    IsAlive bool
    Server struct {
        Address string
        Port int
    }
}

func main() {
    os.Setenv("CFG_ISALIVE", "true")
    os.Setenv("CFG_SERVER_ADDRESS", "0.0.0.0")
    os.Setenv("CFG_SERVER_PORT", "8491")

    cfg := Config{}
    AutoloadAndEnrichConfig("config.yml", &cfg)

    // custom env prefix
    os.Setenv("MYPREFIX_ISALIVE", "true")
    os.Setenv("MYPREFIX_SERVER_ADDRESS", "0.0.0.0")
    os.Setenv("MYPREFIX_SERVER_PORT", "8491")
    cfg2 := Config{}
    AutoloadAndEnrichConfigWithEnvPrefix("config.yml", "myprefix", &cfg2)
}

Documentation

Index

Constants

View Source
const (
	YAML format = "yaml"
	JSON format = "json"
	TOML format = "toml"
	HCL  format = "hcl"
)

Variables

View Source
var (
	EnvDelimeter      = "_"
	EnvSliceDelimeter = ";"
)

Functions

func AutoloadAndEnrichConfig

func AutoloadAndEnrichConfig(filePath string, receiver interface{}) error

AutoloadAndEnrichConfig takes a config file and a receiver and enriches the config with the value from env variables. @filePath: The path to the config file. @receiver: The receiver to parse the config file into.

By default the prefix is set to CFG.

func AutoloadAndEnrichConfigWithEnvPrefix

func AutoloadAndEnrichConfigWithEnvPrefix(filePath string, prefix string, receiver interface{}) error

AutoloadAndEnrichConfig takes a config file and a receiver and enriches the config with the value from env variables. @filePath: The path to the config file. @receiver: The receiver to parse the config file into. @prefix: The prefix to use for the env variables.

Types

This section is empty.

Jump to

Keyboard shortcuts

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