Skip to content

Commit

Permalink
Merge pull request #9 from luizvnasc/feature/env_variables
Browse files Browse the repository at this point in the history
Feature/env variables
  • Loading branch information
luizvnasc authored Dec 18, 2019
2 parents a6233da + 895f3c6 commit da105fd
Show file tree
Hide file tree
Showing 10 changed files with 110 additions and 37 deletions.
49 changes: 39 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,26 +41,55 @@ func main() {
gonfig.Load("config.toml", &config)
fmt.Printf("%v", config)
}
```

If you want to use environment variables:

```go
package main

import (
"fmt"

"github.com/luizvnasc/gonfig"
)

//Config struct to store the app configuration
type Config struct {
Version string `env:"VERSION"`
Description string `env:"DESCRIPTION"`
Redis struct {
Host string `env:"REDIS_HOST"`
Port uint `env:"REDIS_PORT"`
}
}

func main() {
var config Config
gonfig.Load(&config)
fmt.Printf("%v", config)
}
```

You can see more examples [here](https://github.com/luizvnasc/gonfig/tree/master/examples).

## Files supported
## Supported formats

| Extension | is supported? |
| :--------: | :----------------: |
| .json | :heavy_check_mark: |
| .xml | :heavy_check_mark: |
| .yaml/.yml | :heavy_check_mark: |
| .toml | :heavy_check_mark: |
| Format | is supported? |
| :-------------------: | :----------------: |
| json | :heavy_check_mark: |
| xml | :heavy_check_mark: |
| yaml | :heavy_check_mark: |
| toml | :heavy_check_mark: |
| environment variables | :heavy_check_mark: |

## Dependencies

| Dependency | Repository |
| :--------: | :----------------: |
| Dependency | Repository |
| :--------------: | :----------------------------------------------------------------: |
| gopkg.in/yaml.v3 | [https://github.com/go-yaml/yaml](https://github.com/go-yaml/yaml) |
| go-toml | [github.com/pelletier/go-toml](github.com/pelletier/go-toml) |
| go-toml | [github.com/pelletier/go-toml](github.com/pelletier/go-toml) |
| go-env | [github.com/luizvnasc/goenv](github.com/luizvnasc/goenv) |

## Authors
* Luiz Augusto Volpi Nascimento - Initial work - [@luizvnasc](https://github.com/luizvnasc)
Expand Down
23 changes: 23 additions & 0 deletions examples/env/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package main

import (
"fmt"

"github.com/luizvnasc/gonfig"
)

//Config struct to store the app configuration
type Config struct {
Version string `env:"VERSION"`
Description string `env:"DESCRIPTION"`
Redis struct {
Host string `env:"REDIS_HOST"`
Port uint `env:"REDIS_PORT"`
}
}

func main() {
var config Config
gonfig.Load(&config)
fmt.Printf("%v", config)
}
2 changes: 1 addition & 1 deletion examples/json/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ type Config struct {

func main() {
var config Config
gonfig.Load("config.json", &config)
gonfig.Load(&config, "config.json")
fmt.Printf("%v", config)
}
2 changes: 1 addition & 1 deletion examples/toml/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ type Config struct {

func main() {
var config Config
gonfig.Load("config.toml", &config)
gonfig.Load(&config, "config.toml")
fmt.Printf("%v", config)
}
2 changes: 1 addition & 1 deletion examples/xml/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ type Config struct {

func main() {
var config Config
gonfig.Load("config.xml", &config)
gonfig.Load(&config, "config.xml")
fmt.Printf("%v", config)
}
2 changes: 1 addition & 1 deletion examples/yaml/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ type Config struct {

func main() {
var config Config
gonfig.Load("config.yaml", &config)
gonfig.Load(&config, "config.yaml")
fmt.Printf("%v", config)
}
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ module github.com/luizvnasc/gonfig
go 1.12

require (
github.com/luizvnasc/goenv v0.0.0-20191218175301-c19dd8aa8023
github.com/pelletier/go-toml v1.6.0
gopkg.in/yaml.v2 v2.2.7
gopkg.in/yaml.v2 v2.2.7 // indirect
gopkg.in/yaml.v3 v3.0.0-20191120175047-4206685974f2
)
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/luizvnasc/goenv v0.0.0-20191218175301-c19dd8aa8023 h1:ObI7S/YvYRFTNWeqWvn9TPsfqu64uo9DjDc4q+7hLVc=
github.com/luizvnasc/goenv v0.0.0-20191218175301-c19dd8aa8023/go.mod h1:G9RRDiWXzqfsaLdNrhTWG2ceSs3dXQ2AAJuDQ3bR26M=
github.com/pelletier/go-toml v1.6.0 h1:aetoXYr0Tv7xRU/V4B4IZJ2QcbtMUFoNb3ORp7TzIK4=
github.com/pelletier/go-toml v1.6.0/go.mod h1:5N711Q9dKgbdkxHL+MEfF31hpT7l0S0s/t2kKREewys=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
Expand Down
20 changes: 10 additions & 10 deletions gonfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,33 @@ import (
"encoding/json"
"encoding/xml"
"io/ioutil"
"log"
"os"
"path/filepath"

"github.com/luizvnasc/goenv"
"github.com/pelletier/go-toml"
"gopkg.in/yaml.v3"
)

type unmarshalerFunc func(data []byte, v interface{}) error

// Load a struct with the configuration from a config file.
func Load(path string, config interface{}) error {
content, err := getFileContent(path)
if err != nil {
log.Printf("%v\n", err)
return LoadError
func Load(config interface{}, args ...string) error {
if len(args) == 0 {
err := goenv.Unmarshal(config)
return err
}
path := args[0]
unmarshaler, err := getUnmarshaler(path)
if err != nil {
log.Printf("%v\n", err)
config = nil
return err
}
content, err := getFileContent(path)
if err != nil {
return LoadError
}
err = unmarshaler(content, config)
if err != nil {
log.Printf("%v\n", err)
config = nil
return LoadError
}
return nil
Expand Down
40 changes: 28 additions & 12 deletions gonfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ const (
)

type SomeConfiguration struct {
Version string `json:"version" xml:"version" yaml:"version" toml:"version"`
ProjectName string `json:"project_name" xml:"project-name" yaml:"project_name" toml:"project_name"`
Version string `json:"version" xml:"version" yaml:"version" toml:"version" env:"VERSION"`
ProjectName string `json:"project_name" xml:"project-name" yaml:"project_name" toml:"project_name" env:"PROJECT_NAME"`
}

var configValid SomeConfiguration
Expand All @@ -45,7 +45,7 @@ func TestGonfig(t *testing.T) {
t.Run("JSON tests", func(t *testing.T) {
t.Run("Load a configuration from a valid json file", func(t *testing.T) {
config := SomeConfiguration{}
err := gonfig.Load(validJsonFile, &config)
err := gonfig.Load(&config, validJsonFile)
if err != nil {
t.Errorf("Error loading the configuration: %v", err)
}
Expand All @@ -56,7 +56,7 @@ func TestGonfig(t *testing.T) {

t.Run("Load a configuration from an invalid json file", func(t *testing.T) {
config := SomeConfiguration{}
err := gonfig.Load(invalidJsonFile, &config)
err := gonfig.Load(&config, invalidJsonFile)
if err == nil {
t.Errorf("It was expected to get an error. Got nil")
}
Expand All @@ -67,7 +67,7 @@ func TestGonfig(t *testing.T) {

t.Run("Load a configuration from an invalid json body", func(t *testing.T) {
config := SomeConfiguration{}
err := gonfig.Load(invalidJsonBodyFile, &config)
err := gonfig.Load(&config, invalidJsonBodyFile)
if err == nil {
t.Errorf("It was expected to get an error. Got nil")
}
Expand All @@ -79,7 +79,7 @@ func TestGonfig(t *testing.T) {
t.Run("XML tests", func(t *testing.T) {
t.Run("Load a configuration from a valid xml file", func(t *testing.T) {
config := SomeConfiguration{}
err := gonfig.Load(validXMLFile, &config)
err := gonfig.Load(&config, validXMLFile)
if err != nil {
t.Errorf("Error loading the configuration: %v", err)
}
Expand All @@ -89,7 +89,7 @@ func TestGonfig(t *testing.T) {
})
t.Run("Load a configuration from an invalid xml body", func(t *testing.T) {
config := SomeConfiguration{}
err := gonfig.Load(invalidXMLBodyFile, &config)
err := gonfig.Load(&config, invalidXMLBodyFile)
if err == nil {
t.Errorf("It was expected to get an error. Got nil")
}
Expand All @@ -101,7 +101,7 @@ func TestGonfig(t *testing.T) {
t.Run("YAML tests", func(t *testing.T) {
t.Run("Load a configuration from a valid yaml file", func(t *testing.T) {
config := SomeConfiguration{}
err := gonfig.Load(validYamlFile, &config)
err := gonfig.Load(&config, validYamlFile)
if err != nil {
t.Errorf("Error loading the configuration: %v", err)
}
Expand All @@ -112,7 +112,7 @@ func TestGonfig(t *testing.T) {

t.Run("Load a configuration from an invalid yaml body", func(t *testing.T) {
config := SomeConfiguration{}
err := gonfig.Load(invalidYamlBodyFile, &config)
err := gonfig.Load(&config, invalidYamlBodyFile)
if err == nil {
t.Errorf("It was expected to get an error. Got nil")
}
Expand All @@ -125,7 +125,7 @@ func TestGonfig(t *testing.T) {
t.Run("TOML tests", func(t *testing.T) {
t.Run("Load a configuration from a valid toml file", func(t *testing.T) {
config := SomeConfiguration{}
err := gonfig.Load(validTomlFile, &config)
err := gonfig.Load(&config, validTomlFile)
if err != nil {
t.Errorf("Error loading the configuration: %v", err)
}
Expand All @@ -136,7 +136,7 @@ func TestGonfig(t *testing.T) {

t.Run("Load a configuration from an invalid toml body", func(t *testing.T) {
config := SomeConfiguration{}
err := gonfig.Load(invalidTomlBodyFile, &config)
err := gonfig.Load(&config, invalidTomlBodyFile)
if err == nil {
t.Errorf("It was expected to get an error. Got nil")
}
Expand All @@ -146,9 +146,25 @@ func TestGonfig(t *testing.T) {
})
})

t.Run("ENV tests", func(t *testing.T) {
t.Run("Load a configuration from ENV", func(t *testing.T) {
config := SomeConfiguration{}
os.Setenv("VERSION", configValid.Version)
os.Setenv("PROJECT_NAME", configValid.ProjectName)
err := gonfig.Load(&config)
if err != nil {
t.Errorf("Error loading the configuration: %v", err)
}
if !reflect.DeepEqual(config, configValid) {
t.Errorf("Error loading the configuration: expected %v, got %v", configValid, config)
}
})

})

t.Run("Unsupported file", func(t *testing.T) {
config := SomeConfiguration{}
err := gonfig.Load(unsupportedFile, &config)
err := gonfig.Load(&config, unsupportedFile)
if err == nil {
t.Errorf("It was expected to get an error. Got nil")
}
Expand Down

0 comments on commit da105fd

Please sign in to comment.