add dump refereence env provider
All checks were successful
Go Action / goaction (pull_request) Successful in 1m17s
All checks were successful
Go Action / goaction (pull_request) Successful in 1m17s
This commit is contained in:
61
provider/env/provider.go
vendored
61
provider/env/provider.go
vendored
@@ -3,10 +3,14 @@ package env
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"gitoa.ru/go-4devs/config"
|
"gitoa.ru/go-4devs/config"
|
||||||
|
"gitoa.ru/go-4devs/config/definition/option"
|
||||||
|
"gitoa.ru/go-4devs/config/key"
|
||||||
|
"gitoa.ru/go-4devs/config/param"
|
||||||
"gitoa.ru/go-4devs/config/value"
|
"gitoa.ru/go-4devs/config/value"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -42,15 +46,68 @@ type Provider struct {
|
|||||||
prefix string
|
prefix string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p *Provider) Key(path ...string) string {
|
||||||
|
return p.prefix + p.key(path...)
|
||||||
|
}
|
||||||
|
|
||||||
func (p *Provider) Name() string {
|
func (p *Provider) Name() string {
|
||||||
return p.name
|
return p.name
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Provider) Value(_ context.Context, path ...string) (config.Value, error) {
|
func (p *Provider) Value(_ context.Context, path ...string) (config.Value, error) {
|
||||||
name := p.prefix + p.key(path...)
|
if val, ok := os.LookupEnv(p.Key(path...)); ok {
|
||||||
if val, ok := os.LookupEnv(name); ok {
|
|
||||||
return value.JString(val), nil
|
return value.JString(val), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil, fmt.Errorf("%v:%w", p.Name(), config.ErrNotFound)
|
return nil, fmt.Errorf("%v:%w", p.Name(), config.ErrNotFound)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p *Provider) DumpReference(_ context.Context, w io.Writer, opt config.Options) error {
|
||||||
|
return p.writeOptions(w, opt)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Provider) writeOptions(w io.Writer, opt config.Options, key ...string) error {
|
||||||
|
for idx, option := range opt.Options() {
|
||||||
|
if err := p.writeOption(w, option, key...); err != nil {
|
||||||
|
return fmt.Errorf("option[%d]:%w", idx, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Provider) writeOption(w io.Writer, opt config.Option, keys ...string) error {
|
||||||
|
if desc := param.Description(opt); desc != "" {
|
||||||
|
if _, derr := fmt.Fprintf(w, "# %v.\n", desc); derr != nil {
|
||||||
|
return fmt.Errorf("write description:%w", derr)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var err error
|
||||||
|
|
||||||
|
switch one := opt.(type) {
|
||||||
|
case config.Group:
|
||||||
|
err = p.writeOptions(w, one, append(keys, one.Name())...)
|
||||||
|
case config.Options:
|
||||||
|
err = p.writeOptions(w, one, keys...)
|
||||||
|
default:
|
||||||
|
def, dok := option.DataDefaut(opt)
|
||||||
|
|
||||||
|
prefix := ""
|
||||||
|
if !dok || key.IsWild(keys...) {
|
||||||
|
prefix = "#"
|
||||||
|
}
|
||||||
|
|
||||||
|
if !dok {
|
||||||
|
def = ""
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = fmt.Fprintf(w, "%s%s=%v\n", prefix, p.Key(append(keys, one.Name())...), def)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("%w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|||||||
39
provider/env/provider_test.go
vendored
39
provider/env/provider_test.go
vendored
@@ -1,10 +1,17 @@
|
|||||||
package env_test
|
package env_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
|
"context"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"gitoa.ru/go-4devs/config"
|
||||||
|
"gitoa.ru/go-4devs/config/definition/group"
|
||||||
|
"gitoa.ru/go-4devs/config/definition/option"
|
||||||
|
"gitoa.ru/go-4devs/config/definition/proto"
|
||||||
"gitoa.ru/go-4devs/config/provider/env"
|
"gitoa.ru/go-4devs/config/provider/env"
|
||||||
"gitoa.ru/go-4devs/config/test"
|
"gitoa.ru/go-4devs/config/test"
|
||||||
|
"gitoa.ru/go-4devs/config/test/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestProvider(t *testing.T) {
|
func TestProvider(t *testing.T) {
|
||||||
@@ -19,3 +26,35 @@ func TestProvider(t *testing.T) {
|
|||||||
}
|
}
|
||||||
test.Run(t, provider, read)
|
test.Run(t, provider, read)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestProvider_DumpReference(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
const expect = `# configure log.
|
||||||
|
# level.
|
||||||
|
FDEVS_CONFIG_LOG_LEVEL=info
|
||||||
|
# configure log service.
|
||||||
|
# level.
|
||||||
|
#FDEVS_CONFIG_LOG_{SERVICE}_LEVEL=
|
||||||
|
`
|
||||||
|
|
||||||
|
ctx := context.Background()
|
||||||
|
prov := env.New("fdevs", "config")
|
||||||
|
buf := bytes.NewBuffer(nil)
|
||||||
|
|
||||||
|
require.NoError(t, prov.DumpReference(ctx, buf, testOptions(t)))
|
||||||
|
require.Equal(t, buf.String(), expect)
|
||||||
|
}
|
||||||
|
|
||||||
|
func testOptions(t *testing.T) config.Options {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
return group.New("test", "test",
|
||||||
|
group.New("log", "configure log",
|
||||||
|
option.String("level", "level", option.Default("info")),
|
||||||
|
proto.New("service", "configure log service",
|
||||||
|
option.String("level", "level"),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user