Skip to content

Commit e95542b

Browse files
authored
Merge pull request #1 from M-Porter/v2-prep-configure-setup
configure v2: update library and usages
2 parents 9d0bc4f + 7b95669 commit e95542b

File tree

11 files changed

+195
-593
lines changed

11 files changed

+195
-593
lines changed

LICENSE.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Copyright 2022 Matthew Porter
1+
Copyright 2024 Matthew Porter
22

33
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
44

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ Dead simple configuration library.
55
# Install
66

77
```
8-
go get -u github.com/m-porter/configure
8+
go get -u github.com/m-porter/configure/v2
99
```
1010

1111
# Usage
1212

13-
See examples in the [examples](./examples) directory.
13+
See [`configure_test.go`](./configure_test.go) for example usage.

configuration.go renamed to configure.go

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ import (
55
"github.com/spf13/viper"
66
)
77

8-
var instance *interface{}
9-
108
type Options struct {
119
ConfigName string
1210
ConfigType string
@@ -15,7 +13,7 @@ type Options struct {
1513
Defaults interface{}
1614
}
1715

18-
func Setup(options Options, conf interface{}) error {
16+
func Setup(options Options, conf any) error {
1917
viper.SetConfigName(options.ConfigName)
2018
viper.SetConfigType(options.ConfigType)
2119
viper.AddConfigPath(options.ConfigAbsPath)
@@ -46,14 +44,5 @@ func Setup(options Options, conf interface{}) error {
4644
return err
4745
}
4846

49-
instance = &conf
50-
5147
return nil
5248
}
53-
54-
func Config() interface{} {
55-
if instance == nil {
56-
return nil
57-
}
58-
return *instance
59-
}

configure_test.go

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
package configure_test
2+
3+
import (
4+
"os"
5+
"testing"
6+
7+
"github.com/m-porter/configure/v2"
8+
)
9+
10+
func TestDefaults(t *testing.T) {
11+
type Config struct {
12+
HostName string `mapstructure:"host_name"`
13+
}
14+
15+
expected := "my.test.host"
16+
17+
options := configure.Options{
18+
Defaults: Config{
19+
HostName: expected,
20+
},
21+
}
22+
23+
conf := &Config{}
24+
25+
if err := configure.Setup(options, conf); err != nil {
26+
t.Fatalf("error setting up config: %v", err)
27+
}
28+
29+
if conf.HostName != expected {
30+
t.Errorf("expected %s, got %s", expected, conf.HostName)
31+
}
32+
}
33+
34+
func TestFromEnv(t *testing.T) {
35+
type Config struct {
36+
Secret string `mapstructure:"app_secret"`
37+
}
38+
39+
key := "APP_SECRET"
40+
_ = os.Unsetenv(key)
41+
expected := "987xyz"
42+
_ = os.Setenv(key, expected)
43+
44+
options := configure.Options{
45+
EnvPrefix: "",
46+
Defaults: Config{
47+
Secret: "abc123",
48+
},
49+
}
50+
51+
conf := &Config{}
52+
53+
if err := configure.Setup(options, conf); err != nil {
54+
t.Fatalf("error setting up config: %v", err)
55+
}
56+
57+
if conf.Secret != expected {
58+
t.Errorf("expected %s, got %s", expected, conf.Secret)
59+
}
60+
61+
_ = os.Unsetenv(key)
62+
}
63+
64+
func TestFromEnvWithNoEnvPrefix(t *testing.T) {
65+
type Config struct {
66+
AnotherSecret string `mapstructure:"another_secret"`
67+
}
68+
69+
key := "APP_ANOTHER_SECRET"
70+
_ = os.Unsetenv(key)
71+
expected := "987xyz"
72+
_ = os.Setenv(key, expected)
73+
74+
options := configure.Options{
75+
EnvPrefix: "APP",
76+
Defaults: Config{
77+
AnotherSecret: "abc123",
78+
},
79+
}
80+
81+
conf := &Config{}
82+
83+
if err := configure.Setup(options, conf); err != nil {
84+
t.Fatalf("error setting up config: %v", err)
85+
}
86+
87+
if conf.AnotherSecret != expected {
88+
t.Errorf("expected %s, got %s", expected, conf.AnotherSecret)
89+
}
90+
91+
_ = os.Unsetenv(key)
92+
}
93+
94+
func TestFromFile(t *testing.T) {
95+
type Config struct {
96+
SomeValue string `mapstructure:"some_value"`
97+
}
98+
99+
dir, err := os.Getwd()
100+
if err != nil {
101+
t.Fatalf("Unable to get working dir: %v", err)
102+
}
103+
104+
options := configure.Options{
105+
ConfigName: "test_config",
106+
ConfigType: "yaml",
107+
ConfigAbsPath: dir,
108+
}
109+
110+
conf := &Config{}
111+
112+
if err := configure.Setup(options, conf); err != nil {
113+
t.Fatalf("error setting up config: %v", err)
114+
}
115+
116+
expected := "foo foo foo"
117+
if conf.SomeValue != expected {
118+
t.Errorf("expected %s, got %s", expected, conf.SomeValue)
119+
}
120+
}

examples/defaults/main.go

Lines changed: 0 additions & 24 deletions
This file was deleted.

examples/from-env/main.go

Lines changed: 0 additions & 46 deletions
This file was deleted.

examples/from-file/config.yaml

Lines changed: 0 additions & 1 deletion
This file was deleted.

examples/from-file/main.go

Lines changed: 0 additions & 39 deletions
This file was deleted.

go.mod

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,28 @@
1-
module github.com/m-porter/configure
1+
module github.com/m-porter/configure/v2
22

3-
go 1.17
3+
go 1.20
44

55
require (
6-
github.com/fsnotify/fsnotify v1.5.1 // indirect
6+
github.com/mitchellh/mapstructure v1.5.0
7+
github.com/spf13/viper v1.18.2
8+
)
9+
10+
require (
11+
github.com/fsnotify/fsnotify v1.7.0 // indirect
712
github.com/hashicorp/hcl v1.0.0 // indirect
8-
github.com/magiconair/properties v1.8.5 // indirect
9-
github.com/mitchellh/mapstructure v1.4.3 // indirect
10-
github.com/pelletier/go-toml v1.9.4 // indirect
11-
github.com/spf13/afero v1.8.0 // indirect
12-
github.com/spf13/cast v1.4.1 // indirect
13-
github.com/spf13/jwalterweatherman v1.1.0 // indirect
13+
github.com/magiconair/properties v1.8.7 // indirect
14+
github.com/pelletier/go-toml/v2 v2.1.1 // indirect
15+
github.com/sagikazarmark/locafero v0.4.0 // indirect
16+
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
17+
github.com/sourcegraph/conc v0.3.0 // indirect
18+
github.com/spf13/afero v1.11.0 // indirect
19+
github.com/spf13/cast v1.6.0 // indirect
1420
github.com/spf13/pflag v1.0.5 // indirect
15-
github.com/spf13/viper v1.10.1 // indirect
16-
github.com/subosito/gotenv v1.2.0 // indirect
17-
golang.org/x/sys v0.0.0-20220114195835-da31bd327af9 // indirect
18-
golang.org/x/text v0.3.7 // indirect
19-
gopkg.in/ini.v1 v1.66.3 // indirect
20-
gopkg.in/yaml.v2 v2.4.0 // indirect
21+
github.com/subosito/gotenv v1.6.0 // indirect
22+
go.uber.org/multierr v1.11.0 // indirect
23+
golang.org/x/exp v0.0.0-20240119083558-1b970713d09a // indirect
24+
golang.org/x/sys v0.16.0 // indirect
25+
golang.org/x/text v0.14.0 // indirect
26+
gopkg.in/ini.v1 v1.67.0 // indirect
27+
gopkg.in/yaml.v3 v3.0.1 // indirect
2128
)

0 commit comments

Comments
 (0)