Skip to content

Commit

Permalink
Merge pull request #10 from luizvnasc/v2
Browse files Browse the repository at this point in the history
correção do major v2
  • Loading branch information
luizvnasc authored Sep 11, 2020
2 parents dd05c97 + e1af797 commit bc606ad
Show file tree
Hide file tree
Showing 23 changed files with 446 additions and 0 deletions.
15 changes: 15 additions & 0 deletions v2/error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package gonfig

const (
// LoadError = "Error loading the configuration"
LoadError = GonfigError("Error loading the configuration")
// UnsupportedFileError = "Unsupported file extension"
UnsupportedFileError = GonfigError("Unsupported file extension")
)

// GonfigError is an error in the module.
type GonfigError string

func (g GonfigError) Error() string {
return string(g)
}
23 changes: 23 additions & 0 deletions v2/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/v2"
)

//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)
}
8 changes: 8 additions & 0 deletions v2/examples/json/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"version": "1.0.0",
"desc": "An example using json",
"redis": {
"host": "127.0.0.1",
"port": 6379
}
}
23 changes: 23 additions & 0 deletions v2/examples/json/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package main

import (
"fmt"

"github.com/luizvnasc/gonfig/v2"
)

//Config struct to store the app configuration
type Config struct {
Version string `json:"version"`
Description string `json:"desc"`
Redis struct {
Host string `json:"host"`
Port uint `json:"port"`
} `json:"redis"`
}

func main() {
var config Config
gonfig.Load(&config, "config.json")
fmt.Printf("%v", config)
}
5 changes: 5 additions & 0 deletions v2/examples/toml/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
version = "1.0.0"
desc = "An example using toml"
[redis]
host = "127.0.0.1"
port = 6379
23 changes: 23 additions & 0 deletions v2/examples/toml/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package main

import (
"fmt"

"github.com/luizvnasc/gonfig/v2"
)

//Config struct to store the app configuration
type Config struct {
Version string `toml:"version"`
Description string `toml:"desc"`
Redis struct {
Host string `toml:"host"`
Port uint `toml:"port"`
} `toml:"redis"`
}

func main() {
var config Config
gonfig.Load(&config, "config.toml")
fmt.Printf("%v", config)
}
5 changes: 5 additions & 0 deletions v2/examples/xml/config.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<config>
<version>1.0.0</version>
<description>A xml exemple</description>
<redis host="127.0.0.1" port="6379"/>
</config>
23 changes: 23 additions & 0 deletions v2/examples/xml/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package main

import (
"fmt"

"github.com/luizvnasc/gonfig/v2"
)

//Config struct to store the app configuration
type Config struct {
Version string `xml:"version"`
Description string `xml:"description"`
Redis struct {
Host string `xml:"host,attr"`
Port uint `xml:"port,attr"`
} `xml:"redis"`
}

func main() {
var config Config
gonfig.Load(&config, "config.xml")
fmt.Printf("%v", config)
}
5 changes: 5 additions & 0 deletions v2/examples/yaml/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
version: 1.0.0
desc: An example using yaml
redis:
host: 127.0.0.1
port: 6379
23 changes: 23 additions & 0 deletions v2/examples/yaml/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package main

import (
"fmt"

"github.com/luizvnasc/gonfig/v2"
)

//Config struct to store the app configuration
type Config struct {
Version string `yaml:"version"`
Description string `yaml:"desc"`
Redis struct {
Host string `yaml:"host"`
Port uint `yaml:"port"`
} `yaml:"redis"`
}

func main() {
var config Config
gonfig.Load(&config, "config.yaml")
fmt.Printf("%v", config)
}
10 changes: 10 additions & 0 deletions v2/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module github.com/luizvnasc/gonfig/v2

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 // indirect
gopkg.in/yaml.v3 v3.0.0-20191120175047-4206685974f2
)
15 changes: 15 additions & 0 deletions v2/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
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=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.7 h1:VUgggvou5XRW9mHwD/yXxIYSMtY0zoKQf/v226p2nyo=
gopkg.in/yaml.v2 v2.2.7/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20191120175047-4206685974f2 h1:XZx7nhd5GMaZpmDaEHFVafUZC7ya0fuo7cSJ3UCKYmM=
gopkg.in/yaml.v3 v3.0.0-20191120175047-4206685974f2/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
64 changes: 64 additions & 0 deletions v2/gonfig.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package gonfig

import (
"encoding/json"
"encoding/xml"
"io/ioutil"
"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(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 {
return err
}
content, err := getFileContent(path)
if err != nil {
return LoadError
}
err = unmarshaler(content, config)
if err != nil {
return LoadError
}
return nil
}

func getUnmarshaler(path string) (unmarshalerFunc, error) {
ext := filepath.Ext(path)

switch {
case ext == ".json":
return json.Unmarshal, nil
case ext == ".xml":
return xml.Unmarshal, nil
case ext == ".yaml" || ext == ".yml":
return yaml.Unmarshal, nil
case ext == ".toml":
return toml.Unmarshal, nil
default:
return nil, UnsupportedFileError
}
}

func getFileContent(path string) (bytes []byte, err error) {
configFile, err := os.Open(path)
if err != nil {
return
}
defer configFile.Close()
bytes, err = ioutil.ReadAll(configFile)
return
}
Loading

0 comments on commit bc606ad

Please sign in to comment.