Skip to content

Commit 09f0b64

Browse files
committed
Initial commit
0 parents  commit 09f0b64

File tree

5 files changed

+130
-0
lines changed

5 files changed

+130
-0
lines changed

README.md

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# What is it?
2+
GO package to deal with complex configs provided through environment variables.
3+
4+
# Why we need it?
5+
In dockerized applications and by https://12factor.net/ recommendation
6+
we have to pass configuration for our GO program through environment variables.
7+
8+
It is easy if configuration consists of simple values (strings, numbers, boolean, etc), something like this:
9+
10+
```json
11+
{
12+
"login":"user",
13+
"password":"somepass",
14+
"mfa_enabled": true
15+
}
16+
```
17+
18+
In this case we could use several environment variables like `APP_LOGIN="user" APP_PASSWORD="somepass"`.
19+
20+
But how do we work for complex configuration
21+
that consists of arrays of objects? For example:
22+
```json
23+
{
24+
"nodes":[
25+
{
26+
"url":"http://1.localhost/",
27+
"priority":1,
28+
"enabled":true
29+
},
30+
{
31+
"url":"http://2.localhost/",
32+
"priority":2,
33+
"enabled":false
34+
}
35+
]
36+
}
37+
```
38+
39+
It is difficult to split this configuration to several environment variables.
40+
41+
This package is aimed to deal with such configs.
42+
43+
Just put the config into single environment variable.
44+
It could be multiline, but work with single line is simplier.
45+
46+
Running program example:
47+
48+
```bash
49+
50+
CONFIG={"nodes":[{"url":"http:\/\/1.localhost\/","priority":1,"enabled":true},{"url":"http:\/\/2.localhost\/","priority":2,"enabled":false}]} ./app
51+
```
52+
53+
Program code example:
54+
```GO
55+
import jsonEnvGo "github.com/antelman107/json-env-go"
56+
57+
type Config struct {
58+
Nodes []struct {
59+
URL string `json:"url"`
60+
Priority int `json:"priority"`
61+
Enabled bool `json:"enabled"`
62+
} `json:"nodes"`
63+
}
64+
65+
func main() {
66+
var cfg Config
67+
if err := jsonEnvGo.DecodeConfigFromEnv(envName, &cfg); err != nil {
68+
logger.Error(err)
69+
return
70+
}
71+
72+
// cfg now filled, do something with it
73+
// ...
74+
}
75+
```
76+
77+

decode.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package decode
2+
3+
import (
4+
"encoding/json"
5+
"os"
6+
)
7+
8+
func DecodeConfigFromEnv(envName string, target interface{}) error {
9+
return json.Unmarshal([]byte(os.Getenv(envName)), &target)
10+
}

decode_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package decode
2+
3+
import (
4+
"os"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func TestDecode(t *testing.T) {
11+
envName := "CONFIG"
12+
env := `{"a":"1","b":"2"}`
13+
14+
type config struct {
15+
A string `json:"a"`
16+
B string `json:"b"`
17+
}
18+
19+
os.Setenv(envName, env)
20+
21+
var a config
22+
if err := DecodeConfigFromEnv(envName, &a); err != nil {
23+
panic(err)
24+
}
25+
26+
assert.Equal(t, config{ A: "1", B:"2"}, a)
27+
}

go.mod

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module github.com/antelman107/json-env-go
2+
3+
go 1.14
4+
5+
require github.com/stretchr/testify v1.6.1

go.sum

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
2+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
4+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
5+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
6+
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
7+
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
8+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
9+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
10+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
11+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 commit comments

Comments
 (0)