gonfig

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: May 20, 2020 License: MIT Imports: 5 Imported by: 0

README

gonfig

Usage:

YAML

Take an example config file:

api-url: "https://my-fucking-awesome-api.mydomain.com"
credentials:
    user: "awesome-user"
    password: "1703dc79cce842638fba56fcb611c471"

Load config into your Go program:

package main

import (
    "gitlab.com/felipemocruha/gonfig"
)

type credentials struct {
    User     string `yaml:"user"`
    Password string `yaml:"password"`
}

type Config struct {
    ApiURL      string      `yaml:"api-url"`
    Credentials credentials `yaml:"credentials"`
}

func main() {
    config := &Config{}

    // Loading from path
    if err := gonfig.LoadFromPath("config.yaml", config); err != nil {
        // Treat errors
    }
    
    // Loading path from Environment Variable
    if err := gonfig.LoadFromEnv("CONFIG_PATH", config); err != nil {
        // Treat errors
    }
    
    // Loading path from Environment Variable with fallback options
    if err := gonfig.LoadFromEnv("CONFIG_PATH", config, "/home/user/config.yaml"); err != nil {
        // Treat errors
    }
    
    ...
}

EDN

Take an example config file:

{:api-url "https://my-fucking-awesome-api.mydomain.com"
 :credentials {:user     "awesome-user"
               :password "1703dc79cce842638fba56fcb611c471"}}

Load config into your Go program:

package main

import (
    "gitlab.com/felipemocruha/gonfig"
)

type credentials struct {
    User     string `edn:"user"`
    Password string `edn:"password"`
}

type Config struct {
    ApiURL      string      `edn:"api-url"`
    Credentials credentials `edn:"credentials"`
}

func main() {
    config := &Config{}

    // Loading from path
    if err := gonfig.LoadEdnFromPath("config.edn", config); err != nil {
        // Treat errors
    }
    
    // Loading path from Environment Variable
    if err := gonfig.LoadEdnFromEnv("CONFIG_PATH", config); err != nil {
        // Treat errors
    }
    
    // Loading path from Environment Variable with fallback options
    if err := gonfig.LoadEdnFromEnv("CONFIG_PATH", config, "/home/user/config.edn"); err != nil {
        // Treat errors
    }
    
    ...
}

Now go on with your life ;)

Merge Requests are welcome!

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func LoadEdnFromEnv added in v0.2.0

func LoadEdnFromEnv(envVar string, config interface{}, fallback ...string) error

LoadEdnFromEnv in case of fallback options, returns the first one it can find. Case none of the fallback paths is valid, it'll return a 'ConfigNotFound' error.

func LoadEdnFromPath added in v0.2.0

func LoadEdnFromPath(path string, config interface{}) error

LoadEdnFromPath takes a path, loads and decodes an EDN file into config struct with expected fields.

func LoadFromEnv

func LoadFromEnv(envVar string, config interface{}, fallback ...string) error

LoadFromEnv in case of fallback options, returns the first one it can find. Case none of the fallback paths is valid, it'll return a 'ConfigNotFound' error.

func LoadFromPath

func LoadFromPath(path string, config interface{}) error

LoadFromPath takes a path, loads and decodes a YAML into config struct with expected fields.

Types

type ConfigNotFound

type ConfigNotFound struct {
	// contains filtered or unexported fields
}

func (*ConfigNotFound) Error

func (e *ConfigNotFound) Error() string

type DecodeError

type DecodeError struct {
	// contains filtered or unexported fields
}

func (*DecodeError) Error

func (e *DecodeError) Error() string

type InvalidPathProvided

type InvalidPathProvided struct {
	// contains filtered or unexported fields
}

func (*InvalidPathProvided) Error

func (e *InvalidPathProvided) Error() string

Jump to

Keyboard shortcuts

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