Skip to content

Commit

Permalink
Merge pull request #2 from wgarunap/return_configs
Browse files Browse the repository at this point in the history
[IMPR] ⚡ return errors
  • Loading branch information
Lucif3rMorningStar authored Dec 15, 2020
2 parents cca6d35 + 5432d9a commit 09a31d0
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 24 deletions.
28 changes: 19 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,44 @@ Library to load env configuration
### How to use it

```go
package main
import (

"errors"
"github.com/caarlos0/env/v6"
"github.com/wgarunap/goconf"
"log"
)

type Conf struct {
Name string `env:"MY_NAME"`
}

var Config Conf

func (Conf) Register() {
err := env.Parse(&Config)
if err != nil {
log.Fatal("error loading stream goconf, ", err)
}
func (Conf) Register()error {
return env.Parse(&Config)
}

func (Conf) Validate() {
func (Conf) Validate() error{
if Config.Name == "" {
log.Fatal(`MY_NAME environmental variable cannot be empty`)
return errors.New(`MY_NAME environmental variable cannot be empty`)
}
return nil
}

func (Conf) Print() interface{} {
return Config
}

func main() {
config.Load(
err := goconf.Load(
new(Conf),
)
log.Info(`configuration loaded, `,Config.Name)
if err != nil{
log.Fatal(err)
}
log.Print(`configuration loaded, `,Config.Name)
}

```
21 changes: 11 additions & 10 deletions example/main.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package main

import (
"errors"
"github.com/caarlos0/env/v6"
"github.com/tryfix/log"
"github.com/wgarunap/config"
"github.com/wgarunap/goconf"
"os"
)

Expand All @@ -13,17 +14,15 @@ type Conf struct {

var Config Conf

func (Conf) Register() {
err := env.Parse(&Config)
if err != nil {
log.Fatal("error loading stream goconf, ", err)
}
func (Conf) Register() error {
return env.Parse(&Config)
}

func (Conf) Validate() {
func (Conf) Validate() error {
if Config.Name == "" {
log.Fatal(`MY_NAME environmental variable cannot be empty`)
return errors.New(`MY_NAME environmental variable cannot be empty`)
}
return nil
}

func (Conf) Print() interface{} {
Expand All @@ -33,10 +32,12 @@ func (Conf) Print() interface{} {
func main() {
_ = os.Setenv("MY_NAME", "My First Configuration")

goconf.Load(
err := goconf.Load(
new(Conf),
)

if err != nil {
log.Fatal(err)
}
if Config.Name != `My First Configuration` {
log.Fatal(`error while comparing config`)
}
Expand Down
17 changes: 12 additions & 5 deletions register.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,36 @@ import (
)

type Configer interface {
Register()
Register() error
}
type Validater interface {
Validate()
Validate() error
}
type Printer interface {
Print() interface{}
}

func Load(configs ...Configer) {
func Load(configs ...Configer) error {
for _, c := range configs {
c.Register()
err := c.Register()
if err != nil {
return err
}

v, ok := c.(Validater)
if ok {
v.Validate()
err = v.Validate()
if err != nil {
return err
}
}

p, ok := c.(Printer)
if ok {
printTable(p)
}
}
return nil
}

func printTable(p Printer) {
Expand Down

0 comments on commit 09a31d0

Please sign in to comment.