diff options
author | Pacien TRAN-GIRARD | 2015-02-14 11:40:09 +0100 |
---|---|---|
committer | Pacien TRAN-GIRARD | 2015-02-14 11:44:31 +0100 |
commit | 96e522c8ed36b8888fbd33b7be98dca98ae9dc4a (patch) | |
tree | ad97fb9c9e2013d67105bbeaf5ed49f8e7c97477 /README.md | |
parent | 451997aa1fd9f4150ea9f1dbc2d9e017e0866938 (diff) | |
download | go-envcfg-96e522c8ed36b8888fbd33b7be98dca98ae9dc4a.tar.gz |
Diffstat (limited to 'README.md')
-rw-r--r-- | README.md | 94 |
1 files changed, 94 insertions, 0 deletions
diff --git a/README.md b/README.md new file mode 100644 index 0000000..b61fe1c --- /dev/null +++ b/README.md | |||
@@ -0,0 +1,94 @@ | |||
1 | envcfg [![Build Status](https://travis-ci.org/Pacien/envcfg.svg)](//travis-ci.org/Pacien/envcfg) | ||
2 | ====== | ||
3 | |||
4 | Package [envcfg](//github.com/Pacien/envcfg) provides environment variable mapping to structs. | ||
5 | |||
6 | It can be used to read configuration parameters from the environment. | ||
7 | |||
8 | Fields for which environment variables can be found are overwritten, otherwise they are left to their previous value. | ||
9 | |||
10 | This package can be used, for example, after [gcfg](//code.google.com/p/gcfg/) to override settings provided in a | ||
11 | configuration file. | ||
12 | |||
13 | |||
14 | ```Go | ||
15 | import "github.com/Pacien/envcfg" | ||
16 | ``` | ||
17 | |||
18 | |||
19 | Documentation | ||
20 | ------------- | ||
21 | |||
22 | Package documentation can be found on [GoDoc](//godoc.org/github.com/Pacien/envcfg) | ||
23 | |||
24 | |||
25 | Usage example | ||
26 | ------------- | ||
27 | |||
28 | Set environment variables: | ||
29 | |||
30 | ```Bash | ||
31 | export PORT=8080 | ||
32 | export USER_PASSWORD="S3cUrE" | ||
33 | ``` | ||
34 | |||
35 | |||
36 | Create a struct optionally with tagged fields and/or already set values, then call the `ReadInto` function to read | ||
37 | the values set in the environment variables. | ||
38 | |||
39 | ```Go | ||
40 | package main | ||
41 | |||
42 | import ( | ||
43 | "fmt" | ||
44 | "github.com/Pacien/envcfg" | ||
45 | ) | ||
46 | |||
47 | type Config struct { | ||
48 | Server struct { | ||
49 | Port int `env:"PORT" absenv:"true"` | ||
50 | } | ||
51 | User struct { | ||
52 | Username string | ||
53 | Password string | ||
54 | } | ||
55 | } | ||
56 | |||
57 | var cnf Config | ||
58 | |||
59 | func (c *Config) setDefaults() *Config { | ||
60 | c.User.Username = "root" | ||
61 | c.User.Password = "password" | ||
62 | return c | ||
63 | } | ||
64 | |||
65 | func init() { | ||
66 | cnf.setDefaults() | ||
67 | |||
68 | _, errs := envcfg.ReadInto(&cnf) | ||
69 | if len(errs) != 0 { | ||
70 | fmt.Println(errs) | ||
71 | } | ||
72 | } | ||
73 | |||
74 | func main() { | ||
75 | fmt.Println(cnf) | ||
76 | } | ||
77 | ``` | ||
78 | |||
79 | |||
80 | Output of the previous program: | ||
81 | |||
82 | ```Bash | ||
83 | {{8080} {root S3cUrE}} | ||
84 | ``` | ||
85 | |||
86 | |||
87 | See tests for other examples. | ||
88 | |||
89 | |||
90 | License | ||
91 | ------- | ||
92 | |||
93 | This program is published under the MIT License. | ||
94 | See the LICENSE.txt file. | ||