config

package module
v0.0.0-...-6ba8252 Latest Latest
Warning

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

Go to latest
Published: Jul 16, 2018 License: MIT Imports: 5 Imported by: 0

README

go-config

⚠️ This package is not mature and has not been extensively tested or used. This package was developed quickly for use in my projects. Bugs are likely.

Introduction

This is a go package intended to support hierarchical configurations for application deployments. It is modeled after lorenwest/node-config.

Quickstart

  1. Create your configuration files.
# config/default.yaml
plumber: mario
tool: wrench

# config/development.yaml
tool: sledgehammer

# config/production.yaml
plumber: luigi
  1. Create a struct for your configuration.
type AppConfig struct {
    Plumber string
    Tool string
}
  1. Load the configs from your app
import (
    "os"
    "github.com/alex-whitney/go-config"
)

// Example
func getConfig() (*AppConfig, error) {
    env := os.Getenv("ENV")
    if env == "" {
        env = "development"
    }

    appConfig := &AppConfig{}
    err := config.Load(&config.Options{
        Environment: env,
        Directory: "config",
    }, appConfig)
    
    return appConfig, err
}

Environment Variables

Overrides can be set using Environment Variables with the custom-environment-variables file. These overrides are applied last.

# app/config/custom-environment-variables.yaml
foo: ENV_FOO
bar: BAR
// app/main.go

package main

import (
    "fmt"
    "github.com/alex-whitney/go-config"
)

type AppConfig struct {
    Foo string
    Bar string
}

func main() {
    appConfig := &AppConfig{}
    config.Load(&config.Options{
        Directory: "config",
    }, appConfig)

    fmt.Println(appConfig.Foo)
    fmt.Println(appConfig.Bar)
}
$ go install
$ ENV_FOO=abc BAR=def app
abc
def

Load order

Configurations are applied in the following order.

default.yaml
{environment}.yaml
local.yaml
local-{environment}.yaml
custom-environment-variables.yaml

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Load

func Load(opts *Options, out interface{}) error

Types

type Options

type Options struct {
	Environment string
	Directory   string
}

Jump to

Keyboard shortcuts

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