From 96e522c8ed36b8888fbd33b7be98dca98ae9dc4a Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Sat, 14 Feb 2015 11:40:09 +0100 Subject: Add readme --- README.md | 94 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..b61fe1c --- /dev/null +++ b/README.md @@ -0,0 +1,94 @@ +envcfg [![Build Status](https://travis-ci.org/Pacien/envcfg.svg)](//travis-ci.org/Pacien/envcfg) +====== + +Package [envcfg](//github.com/Pacien/envcfg) provides environment variable mapping to structs. + +It can be used to read configuration parameters from the environment. + +Fields for which environment variables can be found are overwritten, otherwise they are left to their previous value. + +This package can be used, for example, after [gcfg](//code.google.com/p/gcfg/) to override settings provided in a +configuration file. + + +```Go +import "github.com/Pacien/envcfg" +``` + + +Documentation +------------- + +Package documentation can be found on [GoDoc](//godoc.org/github.com/Pacien/envcfg) + + +Usage example +------------- + +Set environment variables: + +```Bash +export PORT=8080 +export USER_PASSWORD="S3cUrE" +``` + + +Create a struct optionally with tagged fields and/or already set values, then call the `ReadInto` function to read +the values set in the environment variables. + +```Go +package main + +import ( + "fmt" + "github.com/Pacien/envcfg" +) + +type Config struct { + Server struct { + Port int `env:"PORT" absenv:"true"` + } + User struct { + Username string + Password string + } +} + +var cnf Config + +func (c *Config) setDefaults() *Config { + c.User.Username = "root" + c.User.Password = "password" + return c +} + +func init() { + cnf.setDefaults() + + _, errs := envcfg.ReadInto(&cnf) + if len(errs) != 0 { + fmt.Println(errs) + } +} + +func main() { + fmt.Println(cnf) +} +``` + + +Output of the previous program: + +```Bash +{{8080} {root S3cUrE}} +``` + + +See tests for other examples. + + +License +------- + +This program is published under the MIT License. +See the LICENSE.txt file. -- cgit v1.2.3