This commit is contained in:
73
processor/env/processor.go
vendored
Normal file
73
processor/env/processor.go
vendored
Normal file
@@ -0,0 +1,73 @@
|
||||
package env
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"gitoa.ru/go-4devs/config"
|
||||
"gitoa.ru/go-4devs/config/value"
|
||||
)
|
||||
|
||||
const (
|
||||
prefix = "%env("
|
||||
suffix = ")%"
|
||||
)
|
||||
|
||||
func New(prov config.Provider) Env {
|
||||
env := Env{
|
||||
Provider: prov,
|
||||
name: "",
|
||||
prefix: prefix,
|
||||
suffix: suffix,
|
||||
}
|
||||
|
||||
return env
|
||||
}
|
||||
|
||||
type Env struct {
|
||||
config.Provider
|
||||
|
||||
name string
|
||||
prefix string
|
||||
suffix string
|
||||
}
|
||||
|
||||
func (e Env) Value(ctx context.Context, key ...string) (config.Value, error) {
|
||||
val, err := e.Provider.Value(ctx, key...)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%w", err)
|
||||
}
|
||||
|
||||
pval, perr := e.Process(ctx, val)
|
||||
if perr != nil {
|
||||
return nil, fmt.Errorf("%w", perr)
|
||||
}
|
||||
|
||||
return pval, nil
|
||||
}
|
||||
|
||||
func (e Env) Name() string {
|
||||
if e.name != "" {
|
||||
return e.name
|
||||
}
|
||||
|
||||
return e.Provider.Name()
|
||||
}
|
||||
|
||||
func (e Env) Process(_ context.Context, in config.Value) (config.Value, error) {
|
||||
data, err := in.ParseString()
|
||||
if err != nil || !strings.HasPrefix(data, e.prefix) || !strings.HasSuffix(data, e.suffix) {
|
||||
return in, nil //nolint:nilerr
|
||||
}
|
||||
|
||||
key := data[len(e.prefix) : len(data)-len(e.suffix)]
|
||||
|
||||
res, ok := os.LookupEnv(key)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("%v:%w", e.Name(), config.ErrNotFound)
|
||||
}
|
||||
|
||||
return value.String(res), nil
|
||||
}
|
||||
34
processor/env/processor_test.go
vendored
Normal file
34
processor/env/processor_test.go
vendored
Normal file
@@ -0,0 +1,34 @@
|
||||
package env_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"gitoa.ru/go-4devs/config"
|
||||
"gitoa.ru/go-4devs/config/processor/env"
|
||||
"gitoa.ru/go-4devs/config/test/require"
|
||||
"gitoa.ru/go-4devs/config/value"
|
||||
)
|
||||
|
||||
type provider struct {
|
||||
value config.Value
|
||||
}
|
||||
|
||||
func (p provider) Value(_ context.Context, _ ...string) (config.Value, error) {
|
||||
return p.value, nil
|
||||
}
|
||||
|
||||
func (p provider) Name() string {
|
||||
return "test"
|
||||
}
|
||||
|
||||
func TestEnvValue(t *testing.T) {
|
||||
const except = "env value"
|
||||
t.Setenv("APP_ENV", except)
|
||||
|
||||
ctx := context.Background()
|
||||
process := env.New(provider{value: value.String("%env(APP_ENV)%")})
|
||||
data, err := process.Value(ctx, "any")
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, except, data.String())
|
||||
}
|
||||
Reference in New Issue
Block a user