aboutsummaryrefslogtreecommitdiff
path: root/README.md
blob: b61fe1ce5a51a1d11cb0a92299459bd59931329e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
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.