1 // Package env provides a convenient way to convert environment
2 // variables into Go data. It is similar in design to package
15 var funcs []func() bool
17 // Int returns a new int pointer.
18 // When Parse is called,
19 // env var name will be parsed
20 // and the resulting value
21 // will be assigned to the returned location.
22 func Int(name string, value int) *int {
24 IntVar(p, name, value)
28 // IntVar defines an int var with the specified
29 // name and default value. The argument p points
30 // to an int variable in which to store the
31 // value of the environment var.
32 func IntVar(p *int, name string, value int) {
34 funcs = append(funcs, func() bool {
35 if s := os.Getenv(name); s != "" {
36 v, err := strconv.Atoi(s)
38 log.Println(name, err)
47 // Bool returns a new bool pointer.
48 // When Parse is called,
49 // env var name will be parsed
50 // and the resulting value
51 // will be assigned to the returned location.
52 // Parsing uses strconv.ParseBool.
53 func Bool(name string, value bool) *bool {
55 BoolVar(p, name, value)
59 // BoolVar defines a bool var with the specified
60 // name and default value. The argument p points
61 // to a bool variable in which to store the value
62 // of the environment variable.
63 func BoolVar(p *bool, name string, value bool) {
65 funcs = append(funcs, func() bool {
66 if s := os.Getenv(name); s != "" {
67 v, err := strconv.ParseBool(s)
69 log.Println(name, err)
78 // Duration returns a new time.Duration pointer.
79 // When Parse is called,
80 // env var name will be parsed
81 // and the resulting value
82 // will be assigned to the returned location.
83 func Duration(name string, value time.Duration) *time.Duration {
84 p := new(time.Duration)
85 DurationVar(p, name, value)
89 // DurationVar defines a time.Duration var with
90 // the specified name and default value. The
91 // argument p points to a time.Duration variable
92 // in which to store the value of the environment
94 func DurationVar(p *time.Duration, name string, value time.Duration) {
96 funcs = append(funcs, func() bool {
97 if s := os.Getenv(name); s != "" {
98 v, err := time.ParseDuration(s)
100 log.Println(name, err)
109 // URL returns a new url.URL pointer.
110 // When Parse is called,
111 // env var name will be parsed
112 // and the resulting value
113 // will be assigned to the returned location.
114 // URL panics if there is an error parsing
115 // the given default value.
116 func URL(name string, value string) *url.URL {
118 URLVar(p, name, value)
122 // URLVar defines a url.URL variable with
123 // the specified name ande default value.
124 // The argument p points to a url.URL variable
125 // in which to store the value of the environment
127 func URLVar(p *url.URL, name string, value string) {
128 v, err := url.Parse(value)
133 funcs = append(funcs, func() bool {
134 if s := os.Getenv(name); s != "" {
135 v, err := url.Parse(s)
137 log.Println(name, err)
146 // String returns a new string pointer.
147 // When Parse is called,
148 // env var name will be assigned
149 // to the returned location.
150 func String(name string, value string) *string {
152 StringVar(p, name, value)
156 // StringVar defines a string with the
157 // specified name and default value. The
158 // argument p points to a string variable in
159 // which to store the value of the environment
161 func StringVar(p *string, name string, value string) {
163 funcs = append(funcs, func() bool {
164 if s := os.Getenv(name); s != "" {
171 // StringSlice returns a pointer to a slice
172 // of strings. It expects env var name to
173 // be a list of items delimited by commas.
174 // If env var name is missing, StringSlice
175 // returns a pointer to a slice of the value
177 func StringSlice(name string, value ...string) *[]string {
179 StringSliceVar(p, name, value...)
183 // StringSliceVar defines a new string slice
184 // with the specified name. The argument p
185 // points to a string slice variable in which
186 // to store the value of the environment var.
187 func StringSliceVar(p *[]string, name string, value ...string) {
189 funcs = append(funcs, func() bool {
190 if s := os.Getenv(name); s != "" {
191 a := strings.Split(s, ",")
198 // Parse parses known env vars
199 // and assigns the values to the variables
200 // that were previously registered.
201 // If any values cannot be parsed,
202 // Parse prints an error message for each one
203 // and exits the process with status 1.
206 for _, f := range funcs {