Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
antelman107 committed Jul 1, 2020
0 parents commit 09f0b64
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 0 deletions.
77 changes: 77 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# What is it?
GO package to deal with complex configs provided through environment variables.

# Why we need it?
In dockerized applications and by https://12factor.net/ recommendation
we have to pass configuration for our GO program through environment variables.

It is easy if configuration consists of simple values (strings, numbers, boolean, etc), something like this:

```json
{
"login":"user",
"password":"somepass",
"mfa_enabled": true
}
```

In this case we could use several environment variables like `APP_LOGIN="user" APP_PASSWORD="somepass"`.

But how do we work for complex configuration
that consists of arrays of objects? For example:
```json
{
"nodes":[
{
"url":"http://1.localhost/",
"priority":1,
"enabled":true
},
{
"url":"http://2.localhost/",
"priority":2,
"enabled":false
}
]
}
```

It is difficult to split this configuration to several environment variables.

This package is aimed to deal with such configs.

Just put the config into single environment variable.
It could be multiline, but work with single line is simplier.

Running program example:

```bash

CONFIG={"nodes":[{"url":"http:\/\/1.localhost\/","priority":1,"enabled":true},{"url":"http:\/\/2.localhost\/","priority":2,"enabled":false}]} ./app
```

Program code example:
```GO
import jsonEnvGo "github.com/antelman107/json-env-go"

type Config struct {
Nodes []struct {
URL string `json:"url"`
Priority int `json:"priority"`
Enabled bool `json:"enabled"`
} `json:"nodes"`
}

func main() {
var cfg Config
if err := jsonEnvGo.DecodeConfigFromEnv(envName, &cfg); err != nil {
logger.Error(err)
return
}

// cfg now filled, do something with it
// ...
}
```


10 changes: 10 additions & 0 deletions decode.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package decode

import (
"encoding/json"
"os"
)

func DecodeConfigFromEnv(envName string, target interface{}) error {
return json.Unmarshal([]byte(os.Getenv(envName)), &target)
}
27 changes: 27 additions & 0 deletions decode_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package decode

import (
"os"
"testing"

"github.com/stretchr/testify/assert"
)

func TestDecode(t *testing.T) {
envName := "CONFIG"
env := `{"a":"1","b":"2"}`

type config struct {
A string `json:"a"`
B string `json:"b"`
}

os.Setenv(envName, env)

var a config
if err := DecodeConfigFromEnv(envName, &a); err != nil {
panic(err)
}

assert.Equal(t, config{ A: "1", B:"2"}, a)
}
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module github.com/antelman107/json-env-go

go 1.14

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

0 comments on commit 09f0b64

Please sign in to comment.