Skip to content

Commit cc80206

Browse files
authored
Merge branch 'master' into master
2 parents 2133fb5 + 01f68ce commit cc80206

8 files changed

+45
-52
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Configor
22

3-
Golang Configuration tool that support YAML, JSON, TOML, Shell Environment
3+
Golang Configuration tool that support YAML, JSON, TOML, Shell Environment (Supports Go 1.10+)
44

55
[![wercker status](https://app.wercker.com/status/9ebd3684ff8998501af5aac38a79a380/s/master "wercker status")](https://app.wercker.com/project/byKey/9ebd3684ff8998501af5aac38a79a380)
66

configor.go

+5
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ type Config struct {
1515
ENVPrefix string
1616
Debug bool
1717
Verbose bool
18+
Silent bool
1819

1920
// In case of json files, this field will be used only when compiled with
2021
// go 1.10 or later.
@@ -36,6 +37,10 @@ func New(config *Config) *Configor {
3637
config.Verbose = true
3738
}
3839

40+
if os.Getenv("CONFIGOR_SILENT_MODE") != "" {
41+
config.Silent = true
42+
}
43+
3944
return &Configor{Config: config}
4045
}
4146

go.mod

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module github.com/jinzhu/configor
2+
3+
go 1.12
4+
5+
require (
6+
github.com/BurntSushi/toml v0.3.1
7+
gopkg.in/yaml.v2 v2.2.2
8+
)

go.sum

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
2+
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
3+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
4+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
5+
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
6+
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

unmarshal_json_1_10.go

-28
This file was deleted.

unmarshal_json_1_9.go

-18
This file was deleted.

unmarshal_json_1_10_test.go unmarshal_json_test.go

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// +build go1.10
2-
31
package configor_test
42

53
import (

utils.go

+25-3
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
package configor
22

33
import (
4+
"encoding/json"
45
"errors"
56
"fmt"
7+
"io"
68
"io/ioutil"
79
"os"
810
"path"
911
"reflect"
1012
"strings"
1113

1214
"github.com/BurntSushi/toml"
13-
yaml "gopkg.in/yaml.v2"
15+
"gopkg.in/yaml.v2"
1416
)
1517

1618
// UnmatchedTomlKeysError errors are returned by the Load function when
@@ -79,9 +81,11 @@ func (configor *Configor) getConfigurationFiles(files ...string) []string {
7981
// check example configuration
8082
if !foundFile {
8183
if example, err := getConfigurationFileWithENVPrefix(file, "example"); err == nil {
82-
fmt.Printf("Failed to find configuration %v, using example file %v\n", file, example)
84+
if !configor.Silent {
85+
fmt.Printf("Failed to find configuration %v, using example file %v\n", file, example)
86+
}
8387
results = append(results, example)
84-
} else {
88+
} else if !configor.Silent {
8589
fmt.Printf("Failed to find configuration %v\n", file)
8690
}
8791
}
@@ -154,6 +158,24 @@ func unmarshalToml(data []byte, config interface{}, errorOnUnmatchedKeys bool) e
154158
return err
155159
}
156160

161+
// unmarshalJSON unmarshals the given data into the config interface.
162+
// If the errorOnUnmatchedKeys boolean is true, an error will be returned if there
163+
// are keys in the data that do not match fields in the config interface.
164+
func unmarshalJSON(data []byte, config interface{}, errorOnUnmatchedKeys bool) error {
165+
reader := strings.NewReader(string(data))
166+
decoder := json.NewDecoder(reader)
167+
168+
if errorOnUnmatchedKeys {
169+
decoder.DisallowUnknownFields()
170+
}
171+
172+
err := decoder.Decode(config)
173+
if err != nil && err != io.EOF {
174+
return err
175+
}
176+
return nil
177+
}
178+
157179
func getPrefixForStruct(prefixes []string, fieldStruct *reflect.StructField) []string {
158180
if fieldStruct.Anonymous && fieldStruct.Tag.Get("anonymous") == "true" {
159181
return prefixes

0 commit comments

Comments
 (0)