config

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

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

Go to latest
Published: Oct 26, 2014 License: MIT Imports: 10 Imported by: 0

README

config

GoDoc wercker status

Load environment specific config files

For more information read the documentation at godoc.org.

Usage:

package main

import (
	"fmt"

	"bitbucket.org/utils/config"
)

var C struct {
	Host    string
	ENV     string
	DB      string
	Cache   string
	Websrvr struct {
		ApiURL string `yaml:"api_url"`
		Creds  struct {
			Username string
			Password string
		}
	}
}

func main() {
	//load config
	config.LoadFromFile("./config.yml", &C, nil)
	fmt.Printf("%#v\n", C)
	fmt.Println(C.Websrvr.ApiURL)
	//use it as a normal struct
	//C.Websrvr.ApiURL etc,..
}

sample config.yml file

---
default: &default
  host: localhost
  env: {{.GOENV}}
  db:  "host=/var/run/postgresql dbname=awesome_{{.GOENV}} sslmode=disable"
  cache: {{.HOME}}/cache
  websrvr:
    api_url: https://sandbox.websrvr.in/1/
    creds:
      username: foobar
      password: awesome

development:
  <<: *default

test:
  <<: *default

production:
  <<: *default
  host: minhajuddin.com/
  websrvr:
    api_url: https://api.websrvr.in/1/
    creds:
      username: {{.DROPBOX_USER}}
      password: {{.DROPBOX_PASSWORD}}

Documentation

Overview

Package config allows you to use an environment based configuration file with ability to interpolate environment variables inside the configuration file. e.g. ./config.yml

default: &default
  database: awesome_{{.GOENV}}
  mongo:
    host: localhost:3030
    username: foob
    password: boof
development:
  <<: *default

production:
  <<: *default
  mongo:
    host: {{.MONGO_URL}}
    username: {{.MONGO_USER}}
    password: {{.MONGO_PASSWORD}}

Index

Constants

This section is empty.

Variables

View Source
var (
	//GOENV holds the current value if it is invoked with it.
	//e.g. GOENV=test go run * will set GOENV to test.
	//if the app is invoked without any GOENV it is set to the DEFAULTENV.
	GOENV string
	//DEFAULTENV is set to development by default but you can change this
	//before calling config.Load to change the default GOENV value.
	DEFAULTENV = "development"
)

Functions

func Load

func Load(r io.Reader, config interface{}, logFunc func(args ...interface{})) error

Load allows you to read the configuration from an io.Reader instead of a file. Check the LoadFromFile function for more information on arguments.

func LoadFromFile

func LoadFromFile(path string, config interface{}, logFunc func(args ...interface{})) error

Types

This section is empty.

Jump to

Keyboard shortcuts

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