feat: add a global handler for getting items
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
23
config.go
23
config.go
@@ -1,9 +1,9 @@
|
|||||||
package yourconfig
|
package yourconfig
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
|
||||||
"reflect"
|
"reflect"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -11,8 +11,17 @@ import (
|
|||||||
"github.com/ettle/strcase"
|
"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 {
|
func MustLoad[T any]() T {
|
||||||
output, err := Load[T]()
|
output, err := LoadContext[T](context.Background())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(fmt.Sprintf("must load: %s", err.Error()))
|
panic(fmt.Sprintf("must load: %s", err.Error()))
|
||||||
}
|
}
|
||||||
@@ -21,6 +30,10 @@ func MustLoad[T any]() T {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Load[T any]() (T, error) {
|
func Load[T any]() (T, error) {
|
||||||
|
return LoadContext[T](context.Background())
|
||||||
|
}
|
||||||
|
|
||||||
|
func LoadContext[T any](ctx context.Context) (T, error) {
|
||||||
var cfg T
|
var cfg T
|
||||||
|
|
||||||
v := reflect.ValueOf(&cfg).Elem()
|
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 {
|
||||||
|
errs = append(errs, fmt.Errorf("field: %s failed to load: %w", field.Name, err))
|
||||||
|
continue OUTER
|
||||||
|
}
|
||||||
if valueStr == "" && tag.Required {
|
if valueStr == "" && tag.Required {
|
||||||
errs = append(errs, fmt.Errorf("field: %s (env=%s) is not set and is required", field.Name, tag.Env))
|
errs = append(errs, fmt.Errorf("field: %s (env=%s) is not set and is required", field.Name, tag.Env))
|
||||||
continue OUTER
|
continue OUTER
|
||||||
|
56
provider.go
Normal file
56
provider.go
Normal 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{}
|
||||||
|
}
|
Reference in New Issue
Block a user