Skip to content

Commit f6e6887

Browse files
committed
minor cleanup and lint
1 parent 37ac614 commit f6e6887

10 files changed

Lines changed: 42 additions & 53 deletions

File tree

cnfgfile/file.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,27 @@ import (
1313
"strings"
1414

1515
toml "github.com/BurntSushi/toml"
16-
yaml "gopkg.in/yaml.v2"
16+
yaml "gopkg.in/yaml.v3"
1717
)
1818

19+
var ErrNoFile = fmt.Errorf("must provide at least 1 file to unmarshal")
20+
1921
// Unmarshal parses a configuration file (of any format) into a config struct.
2022
// This is a shorthand method for calling Unmarshal against the json, xml, yaml
2123
// or toml packages. If the file name contains an appropriate suffix it is
2224
// unmarshaled with the corresponding package. If the suffix is missing, TOML
2325
// is assumed. Works with multiple files, so you can have stacked configurations.
2426
func Unmarshal(c interface{}, configFile ...string) error {
2527
if len(configFile) < 1 {
26-
return fmt.Errorf("must provide at least 1 file to unmarshal")
28+
return ErrNoFile
2729
}
2830

2931
for _, f := range configFile {
3032
buf, err := ioutil.ReadFile(f)
3133

3234
switch {
3335
case err != nil:
34-
return err
36+
return fmt.Errorf("reading file %s: %w", configFile, err)
3537
case strings.Contains(f, ".json"):
3638
err = json.Unmarshal(buf, c)
3739
case strings.Contains(f, ".xml"):
@@ -43,7 +45,7 @@ func Unmarshal(c interface{}, configFile ...string) error {
4345
}
4446

4547
if err != nil {
46-
return err
48+
return fmt.Errorf("unmarshaling file %s: %w", configFile, err)
4749
}
4850
}
4951

cnfgfile/file_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ func ExampleUnmarshal() {
146146
yaml := []byte("---\ninterval: 5m\nlocation: Earth\nprovided: true")
147147
path := "/tmp/path_to_config.yaml"
148148

149-
err := ioutil.WriteFile(path, yaml, 0644)
149+
err := ioutil.WriteFile(path, yaml, 0600)
150150
if err != nil {
151151
panic(err)
152152
}

config.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package cnfg
33
import (
44
"encoding"
55
"encoding/json"
6+
"fmt"
67
"time"
78
)
89

@@ -33,7 +34,7 @@ type parser struct {
3334
Vals Pairs // pairs of env variables (saved at start)
3435
}
3536

36-
// satify goconst
37+
// Satify goconst.
3738
const (
3839
typeINT = "int"
3940
typeINT8 = "int8"
@@ -64,15 +65,20 @@ type Duration struct{ time.Duration }
6465
// env variables in the same struct. You won't generally call this directly.
6566
func (d *Duration) UnmarshalText(b []byte) (err error) {
6667
d.Duration, err = time.ParseDuration(string(b))
67-
return
68+
69+
if err != nil {
70+
return fmt.Errorf("parsing duration '%s': %w", b, err)
71+
}
72+
73+
return nil
6874
}
6975

70-
// MarshalText returns the string representation of a Duration. ie. 1m32s
76+
// MarshalText returns the string representation of a Duration. ie. 1m32s.
7177
func (d *Duration) MarshalText() ([]byte, error) {
7278
return []byte(d.Duration.String()), nil
7379
}
7480

75-
// MarshalJSON returns the string representation of a Duration for JSON. ie. "1m32s"
81+
// MarshalJSON returns the string representation of a Duration for JSON. ie. "1m32s".
7682
func (d *Duration) MarshalJSON() ([]byte, error) {
7783
return []byte(`"` + d.Duration.String() + `"`), nil
7884
}

config_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@ func (t *TimeX) UnmarshalENV(tag, val string) error {
3333

3434
multiplier, err := strconv.Atoi(xString)
3535
if err != nil {
36-
return fmt.Errorf("multiplier invalid %s: %v", xTag, err)
36+
return fmt.Errorf("multiplier invalid %s: %w", xTag, err)
3737
}
3838

3939
t.Duration, err = time.ParseDuration(val)
4040
if err != nil {
41-
return fmt.Errorf("duration invalid %s: %v", tag, err)
41+
return fmt.Errorf("duration invalid %s: %w", tag, err)
4242
}
4343

4444
t.Duration *= time.Duration(multiplier)

doc_test.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"time"
77
)
88

9-
// Complete working example for ENV.Unmarshal()
9+
// Complete working example for ENV.Unmarshal().
1010
func ExampleENV_Unmarshal_simple() {
1111
// Systems is used to show an example of how to access nested slices.
1212
type System struct {
@@ -26,8 +26,10 @@ func ExampleENV_Unmarshal_simple() {
2626
// Make a pointer to your struct with some default data.
2727
// Maybe this data came from a config file? Using ParseFile()!
2828
c := &Config{
29-
Debug: true,
30-
Users: []string{"me", "you", "them"},
29+
Debug: true,
30+
Users: []string{"me", "you", "them"},
31+
Interval: nil,
32+
Systems: nil,
3133
}
3234

3335
// Okay set some ENV variables. Pretend you did this in bash.

env.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66
"reflect"
77
)
88

9+
var ErrInvalidInterface = fmt.Errorf("can only unmarshal ENV into pointer to struct")
10+
911
// UnmarshalENV copies environment variables into configuration values.
1012
// This is useful for Docker users that find it easier to pass ENV variables
1113
// than a specific configuration file. Uses reflection to find struct tags.
@@ -18,7 +20,7 @@ func UnmarshalENV(i interface{}, prefix string) (bool, error) {
1820
func (e *ENV) Unmarshal(i interface{}) (bool, error) {
1921
value := reflect.ValueOf(i)
2022
if value.Kind() != reflect.Ptr || value.Elem().Kind() != reflect.Struct {
21-
return false, fmt.Errorf("can only unmarshal ENV into pointer to struct")
23+
return false, ErrInvalidInterface
2224
}
2325

2426
if e.Tag == "" {

go.mod

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,6 @@ go 1.15
44

55
require (
66
github.com/BurntSushi/toml v0.3.1
7-
github.com/creack/pty v1.1.11 // indirect
8-
github.com/kr/pty v1.1.8 // indirect
9-
github.com/kr/text v0.2.0 // indirect
10-
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
117
github.com/stretchr/testify v1.6.1
12-
golang.org/x/sys v0.0.0-20200926100807-9d91bd62050c // indirect
13-
gopkg.in/check.v1 v1.0.0-20200902074654-038fdea0a05b // indirect
14-
gopkg.in/yaml.v2 v2.3.0
8+
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776
159
)

go.sum

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,13 @@
11
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
22
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
3-
github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7DoTY=
4-
github.com/creack/pty v1.1.9 h1:uDmaGzcdjhF4i/plgjmEsriH11Y0o7RKapEf/LDaM3w=
5-
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
6-
github.com/creack/pty v1.1.11 h1:07n33Z8lZxZ2qwegKbObQohDhXDQxiMMz1NOUGYlesw=
7-
github.com/creack/pty v1.1.11/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
83
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
94
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
10-
github.com/kr/pty v1.1.1 h1:VkoXIwSboBpnk99O/KFauAEILuNHv5DVFKZMBN/gUgw=
11-
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
12-
github.com/kr/pty v1.1.8 h1:AkaSdXYQOWeaO3neb8EM634ahkXXe3jYbVh/F9lq+GI=
13-
github.com/kr/pty v1.1.8/go.mod h1:O1sed60cT9XZ5uDucP5qwvh+TE3NnUj51EiZO/lmSfw=
14-
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
15-
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
16-
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
17-
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
18-
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs=
19-
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
205
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
216
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
227
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
238
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
249
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
25-
golang.org/x/sys v0.0.0-20200926100807-9d91bd62050c h1:38q6VNPWR010vN82/SB121GujZNIfAUb4YttE2rhGuc=
26-
golang.org/x/sys v0.0.0-20200926100807-9d91bd62050c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
27-
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
2810
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
29-
gopkg.in/check.v1 v1.0.0-20200902074654-038fdea0a05b h1:QRR6H1YWRnHb4Y/HeNFCTJLFVxaq6wH4YuVdsUOr75U=
30-
gopkg.in/check.v1 v1.0.0-20200902074654-038fdea0a05b/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
31-
gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
32-
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
33-
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
3411
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
12+
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776 h1:tQIYjPdBoyREyB9XMu+nnTclpTYkz2zFM+lzLJFO4gQ=
13+
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

map.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package cnfg
22

33
import (
4-
"fmt"
54
"reflect"
65
"strings"
76
)
@@ -38,7 +37,7 @@ func UnmarshalMap(pairs map[string]string, i interface{}) (bool, error) {
3837
func (e *ENV) UnmarshalMap(pairs map[string]string, i interface{}) (bool, error) {
3938
value := reflect.ValueOf(i)
4039
if value.Kind() != reflect.Ptr || value.Elem().Kind() != reflect.Struct {
41-
return false, fmt.Errorf("can only unmarshal into pointer to struct")
40+
return false, ErrInvalidInterface
4241
}
4342

4443
if e.Tag == "" {

parse.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ import (
1212
/* This file contains all the logic to parse a data structure
1313
using reflection tags from a map of keys and values. */
1414

15+
var (
16+
ErrUnsupported = fmt.Errorf("unsupported type, please report this if this type should be supported")
17+
ErrInvalidByte = fmt.Errorf("invalid byte")
18+
)
19+
1520
// Struct does most of the heavy lifting. Called every time a struct is encountered.
1621
// The entire process begins here. It's very recursive.
1722
func (p *parser) Struct(field reflect.Value, prefix string) (bool, error) {
@@ -154,18 +159,17 @@ func (p *parser) Member(field reflect.Value, tag, envval string) (bool, error) {
154159
val, err = strconv.ParseBool(envval)
155160
field.SetBool(val)
156161
case typeError: // lul
157-
field.Set(reflect.ValueOf(fmt.Errorf(envval)))
162+
field.Set(reflect.ValueOf(fmt.Errorf(envval))) // nolint: goerr113
158163
default:
159164
var ok bool
160165

161166
if ok, err = p.Interface(field, tag, envval); err == nil && !ok {
162-
err = fmt.Errorf("unsupported type: %v (val: %s) - please report this if "+
163-
"this type should be supported", field.Type(), envval)
167+
err = fmt.Errorf("%w: %v (val: %s)", ErrUnsupported, field.Type(), envval)
164168
}
165169
}
166170

167171
if err != nil {
168-
return false, fmt.Errorf("%s: %v", tag, err)
172+
return false, fmt.Errorf("%s: %w", tag, err)
169173
}
170174

171175
return true, nil
@@ -295,7 +299,7 @@ func parseUint(field reflect.Value, intType, envval string) error {
295299
field.Set(reflect.ValueOf(envval[0]))
296300
return nil
297301
default:
298-
return fmt.Errorf("invalid byte: %s", envval)
302+
return fmt.Errorf("%w: %s", ErrInvalidByte, envval)
299303
}
300304
case typeUINT16:
301305
val, err = strconv.ParseUint(envval, 10, 16)
@@ -307,9 +311,10 @@ func parseUint(field reflect.Value, intType, envval string) error {
307311

308312
if err == nil {
309313
field.SetUint(val)
314+
return nil
310315
}
311316

312-
return err
317+
return fmt.Errorf("integer parse error: %w", err)
313318
}
314319

315320
func parseInt(intType, envval string) (int64, error) {

0 commit comments

Comments
 (0)