8 Commits

Author SHA1 Message Date
cuddle-please
4655825d10 chore(release): 0.0.4
All checks were successful
continuous-integration/drone/push Build is passing
2025-11-13 02:55:15 +00:00
df58e4210e fix(deps): update module github.com/stretchr/testify to v1.11.1
Some checks failed
continuous-integration/drone/pr Build is passing
continuous-integration/drone/push Build is failing
2025-11-13 02:54:08 +00:00
207eec0fe3 chore(release): v0.0.3 (#5)
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/tag Build is passing
chore(release): 0.0.3

Co-authored-by: cuddle-please <bot@cuddle.sh>
Reviewed-on: https://git.front.kjuulh.io/kjuulh/yourconfig/pulls/5
2025-07-23 11:15:04 +02:00
b4c3193f01 feat: handle errors via. required (tests)
All checks were successful
continuous-integration/drone/push Build is passing
2025-07-23 11:15:18 +02:00
eaf71fe65b feat: only error on non required values
All checks were successful
continuous-integration/drone/push Build is passing
2025-07-23 11:12:52 +02:00
a525f93093 chore(release): v0.0.2 (#3)
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/tag Build is passing
chore(release): 0.0.2

Co-authored-by: cuddle-please <bot@cuddle.sh>
Reviewed-on: https://git.front.kjuulh.io/kjuulh/yourconfig/pulls/3
2025-07-22 09:14:41 +02:00
ee07910833 chore: Configure Renovate (#2)
All checks were successful
continuous-integration/drone/push Build is passing
Add renovate.json

Reviewed-on: https://git.front.kjuulh.io/kjuulh/yourconfig/pulls/2
2025-07-22 09:14:01 +02:00
e7c3117355 feat: add a global handler for getting items
All checks were successful
continuous-integration/drone/push Build is passing
2025-07-22 09:14:01 +02:00
7 changed files with 130 additions and 4 deletions

View File

@@ -6,11 +6,27 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
## [0.0.4] - 2025-11-13
### Fixed
- *(deps)* update module github.com/stretchr/testify to v1.11.1
## [0.0.3] - 2025-07-23
### Added
- handle errors via. required (tests)
- only error on non required values
## [0.0.2] - 2025-07-22
### Added
- add a global handler for getting items
- rename tag
### Other
- Configure Renovate (#2)
Add renovate.json
## [0.0.1] - 2025-07-21
### Added

View File

@@ -1,9 +1,9 @@
package yourconfig
import (
"context"
"errors"
"fmt"
"os"
"reflect"
"strconv"
"strings"
@@ -11,8 +11,17 @@ import (
"github.com/ettle/strcase"
)
func MustLoadContext[T any](ctx context.Context) T {
output, err := LoadContext[T](ctx)
if err != nil {
panic(fmt.Sprintf("must load: %s", err.Error()))
}
return output
}
func MustLoad[T any]() T {
output, err := Load[T]()
output, err := LoadContext[T](context.Background())
if err != nil {
panic(fmt.Sprintf("must load: %s", err.Error()))
}
@@ -21,6 +30,10 @@ func MustLoad[T any]() T {
}
func Load[T any]() (T, error) {
return LoadContext[T](context.Background())
}
func LoadContext[T any](ctx context.Context) (T, error) {
var cfg T
v := reflect.ValueOf(&cfg).Elem()
@@ -80,7 +93,11 @@ OUTER:
}
}
valueStr := os.Getenv(tag.Env)
valueStr, err := defaultLogger.Load().Get(ctx, tag.Env)
if err != nil && tag.Required {
errs = append(errs, fmt.Errorf("field: %s failed to load: %w", field.Name, err))
continue OUTER
}
if valueStr == "" && tag.Required {
errs = append(errs, fmt.Errorf("field: %s (env=%s) is not set and is required", field.Name, tag.Env))
continue OUTER

View File

@@ -165,4 +165,36 @@ func TestLoad(t *testing.T) {
assert.Equal(t, "some-item", val.SomeItem)
assert.Equal(t, "some-other-item", val.SomeOtherItem)
})
t.Run("required true, error returned", func(t *testing.T) {
type Config struct {
SomeItem string
SomeOtherItem string `cfg:"required:true"`
someBool bool
}
t.Setenv("SOME_OTHER_ITEM", "")
val, err := yourconfig.Load[Config]()
require.Error(t, err)
require.Zero(t, val)
assert.Equal(t, "config failed: field: SomeOtherItem (env=SOME_OTHER_ITEM) is not set and is required", err.Error())
})
t.Run("required false, no error returned", func(t *testing.T) {
type Config struct {
SomeItem string
SomeOtherItem string `cfg:"required:false"`
someBool bool
}
t.Setenv("SOME_OTHER_ITEM", "")
val, err := yourconfig.Load[Config]()
require.NoError(t, err)
assert.Zero(t, val)
})
}

2
go.mod
View File

@@ -4,7 +4,7 @@ go 1.24.5
require (
github.com/ettle/strcase v0.2.0
github.com/stretchr/testify v1.10.0
github.com/stretchr/testify v1.11.1
)
require (

2
go.sum
View File

@@ -6,6 +6,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=

56
provider.go Normal file
View File

@@ -0,0 +1,56 @@
package yourconfig
import (
"context"
"os"
"sync/atomic"
)
var defaultLogger atomic.Pointer[Provider]
func init() {
defaultLogger.Store(newProvider())
}
func SetDefault(provider *Provider) {
defaultLogger.Store(provider)
}
func Default() *Provider {
return defaultLogger.Load()
}
type Handler interface {
Get(ctx context.Context, key string) (string, error)
}
type Provider struct {
handler Handler
}
func (p *Provider) Get(ctx context.Context, key string) (string, error) {
return p.handler.Get(ctx, key)
}
func newProvider() *Provider {
return &Provider{
handler: defaultHandler(),
}
}
func New(handler Handler) *Provider {
return &Provider{
handler: handler,
}
}
type envHandler struct{}
func (e *envHandler) Get(ctx context.Context, key string) (string, error) {
val := os.Getenv(key)
return val, nil
}
func defaultHandler() Handler {
return &envHandler{}
}

3
renovate.json Normal file
View File

@@ -0,0 +1,3 @@
{
"$schema": "https://docs.renovatebot.com/renovate-schema.json"
}