File: README.md

package info (click to toggle)
golang-go.pedge-env 0.0~git20171203.5f5a7de-4
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 92 kB
  • sloc: makefile: 44
file content (38 lines) | stat: -rw-r--r-- 1,454 bytes parent folder | download
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
[![CircleCI](https://circleci.com/gh/peter-edge/env-go/tree/master.png)](https://circleci.com/gh/peter-edge/env-go/tree/master)
[![Go Report Card](http://goreportcard.com/badge/peter-edge/env-go)](http://goreportcard.com/report/peter-edge/env-go)
[![GoDoc](http://img.shields.io/badge/GoDoc-Reference-blue.svg)](https://godoc.org/go.pedge.io/env)
[![MIT License](http://img.shields.io/badge/License-MIT-blue.svg)](https://github.com/peter-edge/env-go/blob/master/LICENSE)

Package env handles environment variables in a structured manner. It uses reflection
to set fields on a struct pointer passed to the main public method, `Populate`.

Example (from the test):

```go
type subEnv struct {
	SubEnvRequiredString        string `env:"SUB_ENV_REQUIRED_STRING,required"`
	SubEnvOptionalString        string `env:"SUB_ENV_OPTIONAL_STRING"`
	SubEnvOptionalUint16Default uint16 `env:"SUB_ENV_OPTIONAL_UINT16_DEFAULT,default=1024"`
}

type testEnv struct {
	RequiredString string `env:"REQUIRED_STRING,required"`
	OptionalString string `env:"OPTIONAL_STRING"`
	OptionalInt    int    `env:"OPTIONAL_INT"`
	OptionalBool   bool   `env:"OPTIONAL_BOOL"`
	SubEnv         subEnv
	Struct         struct {
		StructOptionalInt int `env:"STRUCT_OPTIONAL_INT"`
	}
	OptionalStringDefault string `env:"OPTIONAL_STRING_DEFAULT,default=foo"`
}
```

To populate this, one would call:

```go
testEnv := &testEnv{}
if err := env.Populate(testEnv); err != nil {
	return err
}
```