Skip to content

Commit fe349d1

Browse files
committed
[config] minor fixes to koanf usage
1 parent 4ac8814 commit fe349d1

File tree

4 files changed

+21
-13
lines changed

4 files changed

+21
-13
lines changed

config/config.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,17 @@ import (
88

99
"github.com/knadh/koanf/parsers/dotenv"
1010
"github.com/knadh/koanf/parsers/yaml"
11-
"github.com/knadh/koanf/providers/env"
11+
"github.com/knadh/koanf/providers/env/v2"
1212
"github.com/knadh/koanf/providers/file"
1313
"github.com/knadh/koanf/providers/s3"
1414
"github.com/knadh/koanf/v2"
1515
)
1616

17+
var errMissingAWSCreds = errors.New("AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_REGION must be set")
18+
1719
// Load reads configuration from various sources and unmarshals it into a given struct.
1820
//
19-
// It looks for configuration in the following order:
21+
// It looks for configuration in the following order (later overrides earlier):
2022
// 1. S3, if `withS3` is provided.
2123
// 2. Local file, if `withYaml` is provided.
2224
// 3. `.env` file in the current working directory.
@@ -28,7 +30,7 @@ import (
2830
//
2931
// The final configuration will be unmarshaled into the given struct. If unmarshaling fails, an error will be returned.
3032
func Load[T any](c *T, opts ...Option) error {
31-
options := &options{}
33+
options := new(options)
3234
options.apply(opts...)
3335

3436
k := koanf.New(".")
@@ -40,7 +42,7 @@ func Load[T any](c *T, opts ...Option) error {
4042
endpoint := os.Getenv("AWS_ENDPOINT")
4143

4244
if accessKey == "" || secretKey == "" || region == "" {
43-
return errors.New("AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_REGION must be set")
45+
return errMissingAWSCreds
4446
}
4547

4648
s3Config := s3.Config{
@@ -70,7 +72,13 @@ func Load[T any](c *T, opts ...Option) error {
7072
return fmt.Errorf("load dotenv: %w", err)
7173
}
7274

73-
if err := k.Load(env.Provider("", "__", strings.ToLower), nil); err != nil {
75+
if err := k.Load(env.Provider("__", env.Opt{
76+
Prefix: "",
77+
TransformFunc: func(k, v string) (string, any) {
78+
return strings.ToLower(k), v
79+
},
80+
EnvironFunc: nil,
81+
}), nil); err != nil {
7482
return fmt.Errorf("load env: %w", err)
7583
}
7684

config/options.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,17 @@ func (o *options) apply(opts ...Option) {
1515
}
1616
}
1717

18-
// WithLocalYaml specifies a path to a local YAML file to load config from.
18+
// WithLocalYAML specifies a path to a local YAML file to load config from.
1919
// If the file does not exist, an error is not returned.
20-
func WithLocalYaml(path string) Option {
20+
func WithLocalYAML(path string) Option {
2121
return func(o *options) {
2222
o.withYaml = path
2323
}
2424
}
2525

26-
// WithS3 specifies a path to a local YAML file to load config from.
27-
// If the file does not exist, an error is not returned.
28-
func WithS3(bucket string, objectKey string) Option {
26+
// WithS3YAML configures loading config from S3 (s3://<bucket>/<objectKey>).
27+
// If the object is missing, it is skipped without returning an error.
28+
func WithS3YAML(bucket string, objectKey string) Option {
2929
return func(o *options) {
3030
o.withS3Bucket = bucket
3131
o.withS3ObjectKey = objectKey

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ require (
88
github.com/gofiber/fiber/v2 v2.52.9
99
github.com/knadh/koanf/parsers/dotenv v1.1.0
1010
github.com/knadh/koanf/parsers/yaml v1.1.0
11-
github.com/knadh/koanf/providers/env v1.1.0
11+
github.com/knadh/koanf/providers/env/v2 v2.0.0
1212
github.com/knadh/koanf/providers/file v1.2.0
1313
github.com/knadh/koanf/providers/s3 v1.0.0
1414
github.com/knadh/koanf/v2 v2.2.2

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ github.com/knadh/koanf/parsers/dotenv v1.1.0 h1:dQaM0Jw54zRsqDcaJ27pciNExuKfOXag
4040
github.com/knadh/koanf/parsers/dotenv v1.1.0/go.mod h1:P3BQjxaIc2+SZ3n9BUceqYl95pz3qaGqYTZX0j0d/DI=
4141
github.com/knadh/koanf/parsers/yaml v1.1.0 h1:3ltfm9ljprAHt4jxgeYLlFPmUaunuCgu1yILuTXRdM4=
4242
github.com/knadh/koanf/parsers/yaml v1.1.0/go.mod h1:HHmcHXUrp9cOPcuC+2wrr44GTUB0EC+PyfN3HZD9tFg=
43-
github.com/knadh/koanf/providers/env v1.1.0 h1:U2VXPY0f+CsNDkvdsG8GcsnK4ah85WwWyJgef9oQMSc=
44-
github.com/knadh/koanf/providers/env v1.1.0/go.mod h1:QhHHHZ87h9JxJAn2czdEl6pdkNnDh/JS1Vtsyt65hTY=
43+
github.com/knadh/koanf/providers/env/v2 v2.0.0 h1:Ad5H3eun722u+FvchiIcEIJZsZ2M6oxCkgZfWN5B5KY=
44+
github.com/knadh/koanf/providers/env/v2 v2.0.0/go.mod h1:1g01PE+Ve1gBfWNNw2wmULRP0tc8RJrjn5p2N/jNCIc=
4545
github.com/knadh/koanf/providers/file v1.2.0 h1:hrUJ6Y9YOA49aNu/RSYzOTFlqzXSCpmYIDXI7OJU6+U=
4646
github.com/knadh/koanf/providers/file v1.2.0/go.mod h1:bp1PM5f83Q+TOUu10J/0ApLBd9uIzg+n9UgthfY+nRA=
4747
github.com/knadh/koanf/providers/s3 v1.0.0 h1:VYzH2B+/pxO2WGGPV0l7ANfYSl0Q6lLkseEiGjGTNz8=

0 commit comments

Comments
 (0)