add definition config
This commit is contained in:
@@ -7,23 +7,27 @@ import (
|
||||
"strings"
|
||||
|
||||
"gitoa.ru/go-4devs/config"
|
||||
"gitoa.ru/go-4devs/config/key"
|
||||
"gitoa.ru/go-4devs/config/value"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
const Name = "arg"
|
||||
|
||||
var _ config.Provider = (*Provider)(nil)
|
||||
|
||||
type Option func(*Provider)
|
||||
|
||||
func WithKeyFactory(factory config.KeyFactory) Option {
|
||||
func WithKeyFactory(factory func(s ...string) string) Option {
|
||||
return func(p *Provider) { p.key = factory }
|
||||
}
|
||||
|
||||
func New(opts ...Option) *Provider {
|
||||
prov := Provider{
|
||||
key: key.Name,
|
||||
key: func(s ...string) string {
|
||||
return strings.Join(s, "-")
|
||||
},
|
||||
args: make(map[string][]string, len(os.Args[1:])),
|
||||
name: Name,
|
||||
}
|
||||
|
||||
for _, opt := range opts {
|
||||
@@ -35,7 +39,8 @@ func New(opts ...Option) *Provider {
|
||||
|
||||
type Provider struct {
|
||||
args map[string][]string
|
||||
key config.KeyFactory
|
||||
key func(...string) string
|
||||
name string
|
||||
}
|
||||
|
||||
// nolint: cyclop
|
||||
@@ -98,45 +103,29 @@ func (p *Provider) parse() error {
|
||||
}
|
||||
|
||||
func (p *Provider) Name() string {
|
||||
return "arg"
|
||||
return p.name
|
||||
}
|
||||
|
||||
func (p *Provider) IsSupport(ctx context.Context, key config.Key) bool {
|
||||
return p.key(ctx, key) != ""
|
||||
}
|
||||
|
||||
func (p *Provider) Read(ctx context.Context, key config.Key) (config.Variable, error) {
|
||||
func (p *Provider) Value(ctx context.Context, path ...string) (config.Value, error) {
|
||||
if err := p.parse(); err != nil {
|
||||
return config.Variable{
|
||||
Name: "",
|
||||
Value: nil,
|
||||
Provider: p.Name(),
|
||||
}, err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
name := p.key(ctx, key)
|
||||
name := p.key(path...)
|
||||
if val, ok := p.args[name]; ok {
|
||||
switch {
|
||||
case len(val) == 1:
|
||||
return config.Variable{
|
||||
Name: name,
|
||||
Provider: p.Name(),
|
||||
Value: value.JString(val[0]),
|
||||
}, nil
|
||||
return value.JString(val[0]), nil
|
||||
default:
|
||||
var yNode yaml.Node
|
||||
|
||||
if err := yaml.Unmarshal([]byte("["+strings.Join(val, ",")+"]"), &yNode); err != nil {
|
||||
return config.Variable{}, fmt.Errorf("arg: failed unmarshal yaml:%w", err)
|
||||
return nil, fmt.Errorf("arg: failed unmarshal yaml:%w", err)
|
||||
}
|
||||
|
||||
return config.Variable{
|
||||
Name: name,
|
||||
Provider: p.Name(),
|
||||
Value: value.Decode(yNode.Decode),
|
||||
}, nil
|
||||
return value.Decode(yNode.Decode), nil
|
||||
}
|
||||
}
|
||||
|
||||
return config.Variable{}, fmt.Errorf("%w: %s", config.ErrVariableNotFound, name)
|
||||
return nil, fmt.Errorf("%s:%w", p.Name(), config.ErrValueNotFound)
|
||||
}
|
||||
|
||||
@@ -31,15 +31,15 @@ func TestProvider(t *testing.T) {
|
||||
"--end-after=2008-01-02T15:04:05+03:00",
|
||||
}
|
||||
read := []test.Read{
|
||||
test.NewRead("listen", 8080),
|
||||
test.NewRead("config", "config.hcl"),
|
||||
test.NewRead("start-at", test.Time("2010-01-02T15:04:05Z")),
|
||||
test.NewReadUnmarshal("url", &[]string{"http://4devs.io", "https://4devs.io"}, &[]string{}),
|
||||
test.NewReadUnmarshal("timeout", &[]time.Duration{time.Minute, time.Hour}, &[]time.Duration{}),
|
||||
test.NewReadUnmarshal("end-after", &[]time.Time{
|
||||
test.NewRead(8080, "listen"),
|
||||
test.NewRead("config.hcl", "config"),
|
||||
test.NewRead(test.Time("2010-01-02T15:04:05Z"), "start-at"),
|
||||
test.NewReadUnmarshal(&[]string{"http://4devs.io", "https://4devs.io"}, &[]string{}, "url"),
|
||||
test.NewReadUnmarshal(&[]time.Duration{time.Minute, time.Hour}, &[]time.Duration{}, "timeout"),
|
||||
test.NewReadUnmarshal(&[]time.Time{
|
||||
test.Time("2009-01-02T15:04:05Z"),
|
||||
test.Time("2008-01-02T15:04:05+03:00"),
|
||||
}, &[]time.Time{}),
|
||||
}, &[]time.Time{}, "end-after"),
|
||||
}
|
||||
|
||||
prov := arg.New()
|
||||
|
||||
35
provider/env/provider.go
vendored
35
provider/env/provider.go
vendored
@@ -2,27 +2,30 @@ package env
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"gitoa.ru/go-4devs/config"
|
||||
"gitoa.ru/go-4devs/config/key"
|
||||
"gitoa.ru/go-4devs/config/value"
|
||||
)
|
||||
|
||||
const Name = "env"
|
||||
|
||||
var _ config.Provider = (*Provider)(nil)
|
||||
|
||||
type Option func(*Provider)
|
||||
|
||||
func WithKeyFactory(factory config.KeyFactory) Option {
|
||||
func WithKeyFactory(factory func(...string) string) Option {
|
||||
return func(p *Provider) { p.key = factory }
|
||||
}
|
||||
|
||||
func New(opts ...Option) *Provider {
|
||||
func New(namespace, appName string, opts ...Option) *Provider {
|
||||
provider := Provider{
|
||||
key: func(ctx context.Context, k config.Key) string {
|
||||
return strings.ToUpper(key.NsAppName("_")(ctx, k))
|
||||
key: func(path ...string) string {
|
||||
return strings.ToUpper(strings.Join(path, "_"))
|
||||
},
|
||||
prefix: strings.ToUpper(namespace + "_" + appName + "_"),
|
||||
}
|
||||
|
||||
for _, opt := range opts {
|
||||
@@ -33,26 +36,20 @@ func New(opts ...Option) *Provider {
|
||||
}
|
||||
|
||||
type Provider struct {
|
||||
key config.KeyFactory
|
||||
key func(...string) string
|
||||
name string
|
||||
prefix string
|
||||
}
|
||||
|
||||
func (p *Provider) Name() string {
|
||||
return "env"
|
||||
return p.name
|
||||
}
|
||||
|
||||
func (p *Provider) IsSupport(ctx context.Context, key config.Key) bool {
|
||||
return p.key(ctx, key) != ""
|
||||
}
|
||||
|
||||
func (p *Provider) Read(ctx context.Context, key config.Key) (config.Variable, error) {
|
||||
name := p.key(ctx, key)
|
||||
func (p *Provider) Value(ctx context.Context, path ...string) (config.Value, error) {
|
||||
name := p.prefix + p.key(path...)
|
||||
if val, ok := os.LookupEnv(name); ok {
|
||||
return config.Variable{
|
||||
Name: name,
|
||||
Provider: p.Name(),
|
||||
Value: value.JString(val),
|
||||
}, nil
|
||||
return value.JString(val), nil
|
||||
}
|
||||
|
||||
return config.Variable{}, config.ErrVariableNotFound
|
||||
return nil, fmt.Errorf("%v:%w", p.Name(), config.ErrValueNotFound)
|
||||
}
|
||||
|
||||
6
provider/env/provider_test.go
vendored
6
provider/env/provider_test.go
vendored
@@ -14,11 +14,11 @@ func TestProvider(t *testing.T) {
|
||||
os.Setenv("FDEVS_CONFIG_DSN", test.DSN)
|
||||
os.Setenv("FDEVS_CONFIG_PORT", "8080")
|
||||
|
||||
provider := env.New()
|
||||
provider := env.New("fdevs", "config")
|
||||
|
||||
read := []test.Read{
|
||||
test.NewRead("dsn", test.DSN),
|
||||
test.NewRead("port", 8080),
|
||||
test.NewRead(test.DSN, "dsn"),
|
||||
test.NewRead(8080, "port"),
|
||||
}
|
||||
test.Run(t, provider, read)
|
||||
}
|
||||
|
||||
@@ -3,14 +3,19 @@ package etcd
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"gitoa.ru/go-4devs/config"
|
||||
"gitoa.ru/go-4devs/config/key"
|
||||
"gitoa.ru/go-4devs/config/value"
|
||||
pb "go.etcd.io/etcd/api/v3/mvccpb"
|
||||
client "go.etcd.io/etcd/client/v3"
|
||||
)
|
||||
|
||||
const (
|
||||
Name = "etcd"
|
||||
Separator = "/"
|
||||
)
|
||||
|
||||
var (
|
||||
_ config.Provider = (*Provider)(nil)
|
||||
_ config.WatchProvider = (*Provider)(nil)
|
||||
@@ -21,10 +26,14 @@ type Client interface {
|
||||
client.Watcher
|
||||
}
|
||||
|
||||
func NewProvider(client Client) *Provider {
|
||||
func NewProvider(namespace, appName string, client Client) *Provider {
|
||||
p := Provider{
|
||||
client: client,
|
||||
key: key.NsAppName("/"),
|
||||
key: func(s ...string) string {
|
||||
return strings.Join(s, Separator)
|
||||
},
|
||||
name: Name,
|
||||
prefix: namespace + Separator + appName,
|
||||
}
|
||||
|
||||
return &p
|
||||
@@ -32,34 +41,35 @@ func NewProvider(client Client) *Provider {
|
||||
|
||||
type Provider struct {
|
||||
client Client
|
||||
key config.KeyFactory
|
||||
}
|
||||
|
||||
func (p *Provider) IsSupport(ctx context.Context, key config.Key) bool {
|
||||
return p.key(ctx, key) != ""
|
||||
key func(...string) string
|
||||
name string
|
||||
prefix string
|
||||
}
|
||||
|
||||
func (p *Provider) Name() string {
|
||||
return "etcd"
|
||||
return p.name
|
||||
}
|
||||
func (p *Provider) Key(s []string) string {
|
||||
return p.prefix + Separator + p.key(s...)
|
||||
}
|
||||
|
||||
func (p *Provider) Read(ctx context.Context, key config.Key) (config.Variable, error) {
|
||||
name := p.key(ctx, key)
|
||||
func (p *Provider) Value(ctx context.Context, path ...string) (config.Value, error) {
|
||||
name := p.Key(path)
|
||||
|
||||
resp, err := p.client.Get(ctx, name, client.WithPrefix())
|
||||
if err != nil {
|
||||
return config.Variable{}, fmt.Errorf("%w: key:%s, prov:%s", err, name, p.Name())
|
||||
return nil, fmt.Errorf("%w: key:%s, prov:%s", err, name, p.Name())
|
||||
}
|
||||
|
||||
val, err := p.resolve(name, resp.Kvs)
|
||||
if err != nil {
|
||||
return config.Variable{}, fmt.Errorf("%w: key:%s, prov:%s", err, name, p.Name())
|
||||
return nil, fmt.Errorf("%w: key:%s, prov:%s", err, name, p.Name())
|
||||
}
|
||||
|
||||
return val, nil
|
||||
}
|
||||
|
||||
func (p *Provider) Watch(ctx context.Context, key config.Key, callback config.WatchCallback) error {
|
||||
func (p *Provider) Watch(ctx context.Context, callback config.WatchCallback, path ...string) error {
|
||||
go func(ctx context.Context, key string, callback config.WatchCallback) {
|
||||
watch := p.client.Watch(ctx, key, client.WithPrevKV(), client.WithPrefix())
|
||||
for w := range watch {
|
||||
@@ -70,7 +80,7 @@ func (p *Provider) Watch(ctx context.Context, key config.Key, callback config.Wa
|
||||
callback(ctx, oldVar, newVar)
|
||||
}
|
||||
}
|
||||
}(ctx, p.key(ctx, key), callback)
|
||||
}(ctx, p.Key(path), callback)
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -87,23 +97,15 @@ func (p *Provider) getEventKvs(events []*client.Event) ([]*pb.KeyValue, []*pb.Ke
|
||||
return kvs, old
|
||||
}
|
||||
|
||||
func (p *Provider) resolve(key string, kvs []*pb.KeyValue) (config.Variable, error) {
|
||||
func (p *Provider) resolve(key string, kvs []*pb.KeyValue) (config.Value, error) {
|
||||
for _, kv := range kvs {
|
||||
switch {
|
||||
case kv == nil:
|
||||
return config.Variable{
|
||||
Name: key,
|
||||
Provider: p.Name(),
|
||||
Value: nil,
|
||||
}, nil
|
||||
return nil, nil
|
||||
case string(kv.Key) == key:
|
||||
return config.Variable{
|
||||
Value: value.JBytes(kv.Value),
|
||||
Name: key,
|
||||
Provider: p.Name(),
|
||||
}, nil
|
||||
return value.JBytes(kv.Value), nil
|
||||
}
|
||||
}
|
||||
|
||||
return config.Variable{}, fmt.Errorf("%w: name %s", config.ErrVariableNotFound, key)
|
||||
return nil, fmt.Errorf("%w: name %s", config.ErrValueNotFound, key)
|
||||
}
|
||||
|
||||
@@ -23,17 +23,17 @@ func TestProvider(t *testing.T) {
|
||||
et, err := test.NewEtcd(ctx)
|
||||
require.NoError(t, err)
|
||||
|
||||
provider := etcd.NewProvider(et)
|
||||
provider := etcd.NewProvider("fdevs", "config", et)
|
||||
read := []test.Read{
|
||||
test.NewRead("db_dsn", test.DSN),
|
||||
test.NewRead("duration", 12*time.Minute),
|
||||
test.NewRead("port", 8080),
|
||||
test.NewRead("maintain", true),
|
||||
test.NewRead("start_at", test.Time("2020-01-02T15:04:05Z")),
|
||||
test.NewRead("percent", .064),
|
||||
test.NewRead("count", uint(2020)),
|
||||
test.NewRead("int64", int64(2021)),
|
||||
test.NewRead("uint64", int64(2022)),
|
||||
test.NewRead(test.DSN, "db_dsn"),
|
||||
test.NewRead(12*time.Minute, "duration"),
|
||||
test.NewRead(8080, "port"),
|
||||
test.NewRead(true, "maintain"),
|
||||
test.NewRead(test.Time("2020-01-02T15:04:05Z"), "start_at"),
|
||||
test.NewRead(.064, "percent"),
|
||||
test.NewRead(uint(2020), "count"),
|
||||
test.NewRead(int64(2021), "int64"),
|
||||
test.NewRead(int64(2022), "uint64"),
|
||||
test.NewReadConfig("config"),
|
||||
}
|
||||
test.Run(t, provider, read)
|
||||
@@ -49,11 +49,7 @@ func TestWatcher(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
|
||||
key := config.Key{
|
||||
AppName: "config",
|
||||
Namespace: "fdevs",
|
||||
Name: "test_watch",
|
||||
}
|
||||
key := "test_watch"
|
||||
|
||||
et, err := test.NewEtcd(ctx)
|
||||
require.NoError(t, err)
|
||||
@@ -65,24 +61,24 @@ func TestWatcher(t *testing.T) {
|
||||
|
||||
var cnt, cnt2 int32
|
||||
|
||||
prov := etcd.NewProvider(et)
|
||||
prov := etcd.NewProvider("fdevs", "config", et)
|
||||
wg := sync.WaitGroup{}
|
||||
wg.Add(6)
|
||||
|
||||
watch := func(cnt *int32) func(ctx context.Context, oldVar, newVar config.Variable) {
|
||||
return func(ctx context.Context, oldVar, newVar config.Variable) {
|
||||
watch := func(cnt *int32) func(ctx context.Context, oldVar, newVar config.Value) {
|
||||
return func(ctx context.Context, oldVar, newVar config.Value) {
|
||||
switch *cnt {
|
||||
case 0:
|
||||
assert.Equal(t, value(*cnt), newVar.Value.String())
|
||||
assert.Nil(t, oldVar.Value)
|
||||
assert.Equal(t, value(*cnt), newVar.String())
|
||||
assert.Nil(t, oldVar)
|
||||
case 1:
|
||||
assert.Equal(t, value(*cnt), newVar.Value.String())
|
||||
assert.Equal(t, value(*cnt-1), oldVar.Value.String())
|
||||
assert.Equal(t, value(*cnt), newVar.String())
|
||||
assert.Equal(t, value(*cnt-1), oldVar.String())
|
||||
case 2:
|
||||
_, perr := newVar.Value.ParseString()
|
||||
_, perr := newVar.ParseString()
|
||||
assert.NoError(t, perr)
|
||||
assert.Equal(t, "", newVar.Value.String())
|
||||
assert.Equal(t, value(*cnt-1), oldVar.Value.String())
|
||||
assert.Equal(t, "", newVar.String())
|
||||
assert.Equal(t, value(*cnt-1), oldVar.String())
|
||||
default:
|
||||
assert.Fail(t, "unexpected watch")
|
||||
}
|
||||
@@ -92,8 +88,8 @@ func TestWatcher(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
err = prov.Watch(ctx, key, watch(&cnt))
|
||||
err = prov.Watch(ctx, key, watch(&cnt2))
|
||||
err = prov.Watch(ctx, watch(&cnt), key)
|
||||
err = prov.Watch(ctx, watch(&cnt2), key)
|
||||
require.NoError(t, err)
|
||||
|
||||
time.AfterFunc(time.Second, func() {
|
||||
|
||||
@@ -10,55 +10,49 @@ import (
|
||||
"gopkg.in/ini.v1"
|
||||
)
|
||||
|
||||
const (
|
||||
Name = "ini"
|
||||
Separator = "."
|
||||
)
|
||||
|
||||
var _ config.Provider = (*Provider)(nil)
|
||||
|
||||
func New(data *ini.File) *Provider {
|
||||
const nameParts = 2
|
||||
|
||||
return &Provider{
|
||||
data: data,
|
||||
resolve: func(ctx context.Context, key config.Key) (string, string) {
|
||||
keys := strings.SplitN(key.Name, "/", nameParts)
|
||||
if len(keys) == 1 {
|
||||
return "", keys[0]
|
||||
resolve: func(path []string) (string, string) {
|
||||
if len(path) == 1 {
|
||||
return "", path[0]
|
||||
}
|
||||
|
||||
return keys[0], keys[1]
|
||||
return strings.Join(path[:len(path)-1], Separator), strings.ToUpper(path[len(path)-1])
|
||||
},
|
||||
name: Name,
|
||||
}
|
||||
}
|
||||
|
||||
type Provider struct {
|
||||
data *ini.File
|
||||
resolve func(ctx context.Context, key config.Key) (string, string)
|
||||
}
|
||||
|
||||
func (p *Provider) IsSupport(ctx context.Context, key config.Key) bool {
|
||||
section, name := p.resolve(ctx, key)
|
||||
|
||||
return section != "" && name != ""
|
||||
resolve func(path []string) (string, string)
|
||||
name string
|
||||
}
|
||||
|
||||
func (p *Provider) Name() string {
|
||||
return "ini"
|
||||
return p.name
|
||||
}
|
||||
|
||||
func (p *Provider) Read(ctx context.Context, key config.Key) (config.Variable, error) {
|
||||
section, name := p.resolve(ctx, key)
|
||||
func (p *Provider) Value(ctx context.Context, path ...string) (config.Value, error) {
|
||||
section, name := p.resolve(path)
|
||||
|
||||
iniSection, err := p.data.GetSection(section)
|
||||
if err != nil {
|
||||
return config.Variable{}, fmt.Errorf("%w: %s: %w", config.ErrVariableNotFound, p.Name(), err)
|
||||
return nil, fmt.Errorf("%w: %s: %w", config.ErrValueNotFound, p.Name(), err)
|
||||
}
|
||||
|
||||
iniKey, err := iniSection.GetKey(name)
|
||||
if err != nil {
|
||||
return config.Variable{}, fmt.Errorf("%w: %s: %w", config.ErrVariableNotFound, p.Name(), err)
|
||||
return nil, fmt.Errorf("%w: %s: %w", config.ErrValueNotFound, p.Name(), err)
|
||||
}
|
||||
|
||||
return config.Variable{
|
||||
Name: section + ":" + name,
|
||||
Provider: p.Name(),
|
||||
Value: value.JString(iniKey.String()),
|
||||
}, nil
|
||||
return value.JString(iniKey.String()), nil
|
||||
}
|
||||
|
||||
@@ -14,13 +14,13 @@ func TestProvider(t *testing.T) {
|
||||
file := test.NewINI()
|
||||
|
||||
read := []test.Read{
|
||||
test.NewRead("project/PROJECT_BOARD_BASIC_KANBAN_TYPE", "To Do, In Progress, Done"),
|
||||
test.NewRead("repository.editor/PREVIEWABLE_FILE_MODES", "markdown"),
|
||||
test.NewRead("server/LOCAL_ROOT_URL", "http://0.0.0.0:3000/"),
|
||||
test.NewRead("server/LFS_HTTP_AUTH_EXPIRY", 20*time.Minute),
|
||||
test.NewRead("repository.pull-request/DEFAULT_MERGE_MESSAGE_SIZE", 5120),
|
||||
test.NewRead("ui/SHOW_USER_EMAIL", true),
|
||||
test.NewRead("cors/ENABLED", false),
|
||||
test.NewRead("To Do, In Progress, Done", "project", "PROJECT_BOARD_BASIC_KANBAN_TYPE"),
|
||||
test.NewRead("markdown", "repository.editor", "PREVIEWABLE_FILE_MODES"),
|
||||
test.NewRead("http://0.0.0.0:3000/", "server", "LOCAL_ROOT_URL"),
|
||||
test.NewRead(20*time.Minute, "server", "LFS_HTTP_AUTH_EXPIRY"),
|
||||
test.NewRead(5120, "repository.pull-request", "DEFAULT_MERGE_MESSAGE_SIZE"),
|
||||
test.NewRead(true, "ui", "SHOW_USER_EMAIL"),
|
||||
test.NewRead(false, "cors", "enabled"),
|
||||
}
|
||||
|
||||
prov := ini.New(file)
|
||||
|
||||
@@ -3,20 +3,27 @@ package json
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/tidwall/gjson"
|
||||
"gitoa.ru/go-4devs/config"
|
||||
"gitoa.ru/go-4devs/config/key"
|
||||
"gitoa.ru/go-4devs/config/value"
|
||||
)
|
||||
|
||||
const (
|
||||
Name = "json"
|
||||
Separator = "."
|
||||
)
|
||||
|
||||
var _ config.Provider = (*Provider)(nil)
|
||||
|
||||
func New(json []byte, opts ...Option) *Provider {
|
||||
provider := Provider{
|
||||
key: key.Name,
|
||||
key: func(s ...string) string {
|
||||
return strings.Join(s, Separator)
|
||||
},
|
||||
data: json,
|
||||
}
|
||||
|
||||
@@ -28,7 +35,7 @@ func New(json []byte, opts ...Option) *Provider {
|
||||
}
|
||||
|
||||
func NewFile(path string, opts ...Option) (*Provider, error) {
|
||||
file, err := ioutil.ReadFile(filepath.Clean(path))
|
||||
file, err := os.ReadFile(filepath.Clean(path))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%w: unable to read config file %#q: file not found or unreadable", err, path)
|
||||
}
|
||||
@@ -40,27 +47,19 @@ type Option func(*Provider)
|
||||
|
||||
type Provider struct {
|
||||
data []byte
|
||||
key config.KeyFactory
|
||||
}
|
||||
|
||||
func (p *Provider) IsSupport(ctx context.Context, key config.Key) bool {
|
||||
return p.key(ctx, key) != ""
|
||||
key func(...string) string
|
||||
name string
|
||||
}
|
||||
|
||||
func (p *Provider) Name() string {
|
||||
return "json"
|
||||
return p.name
|
||||
}
|
||||
|
||||
func (p *Provider) Read(ctx context.Context, key config.Key) (config.Variable, error) {
|
||||
path := p.key(ctx, key)
|
||||
|
||||
if val := gjson.GetBytes(p.data, path); val.Exists() {
|
||||
return config.Variable{
|
||||
Name: path,
|
||||
Provider: p.Name(),
|
||||
Value: value.JString(val.String()),
|
||||
}, nil
|
||||
func (p *Provider) Value(ctx context.Context, path ...string) (config.Value, error) {
|
||||
key := p.key(path...)
|
||||
if val := gjson.GetBytes(p.data, key); val.Exists() {
|
||||
return value.JString(val.String()), nil
|
||||
}
|
||||
|
||||
return config.Variable{}, config.ErrVariableNotFound
|
||||
return nil, fmt.Errorf("%v:%w", p.Name(), config.ErrValueNotFound)
|
||||
}
|
||||
|
||||
@@ -16,11 +16,11 @@ func TestProvider(t *testing.T) {
|
||||
prov := provider.New(js)
|
||||
sl := []string{}
|
||||
read := []test.Read{
|
||||
test.NewRead("app.name.title", "config title"),
|
||||
test.NewRead("app.name.timeout", time.Minute),
|
||||
test.NewReadUnmarshal("app.name.var", &[]string{"name"}, &sl),
|
||||
test.NewRead("config title", "app.name.title"),
|
||||
test.NewRead(time.Minute, "app.name.timeout"),
|
||||
test.NewReadUnmarshal(&[]string{"name"}, &sl, "app.name.var"),
|
||||
test.NewReadConfig("cfg"),
|
||||
test.NewRead("app.name.success", true),
|
||||
test.NewRead(true, "app", "name", "success"),
|
||||
}
|
||||
|
||||
test.Run(t, prov, read)
|
||||
|
||||
@@ -3,13 +3,18 @@ package toml
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/pelletier/go-toml"
|
||||
"gitoa.ru/go-4devs/config"
|
||||
"gitoa.ru/go-4devs/config/key"
|
||||
"gitoa.ru/go-4devs/config/value"
|
||||
)
|
||||
|
||||
const (
|
||||
Name = "toml"
|
||||
Separator = "."
|
||||
)
|
||||
|
||||
var _ config.Provider = (*Provider)(nil)
|
||||
|
||||
func NewFile(file string, opts ...Option) (*Provider, error) {
|
||||
@@ -26,7 +31,9 @@ type Option func(*Provider)
|
||||
func configure(tree *toml.Tree, opts ...Option) *Provider {
|
||||
prov := &Provider{
|
||||
tree: tree,
|
||||
key: key.Name,
|
||||
key: func(s []string) string {
|
||||
return strings.Join(s, Separator)
|
||||
},
|
||||
}
|
||||
|
||||
for _, opt := range opts {
|
||||
@@ -47,25 +54,18 @@ func New(data []byte, opts ...Option) (*Provider, error) {
|
||||
|
||||
type Provider struct {
|
||||
tree *toml.Tree
|
||||
key config.KeyFactory
|
||||
}
|
||||
|
||||
func (p *Provider) IsSupport(ctx context.Context, key config.Key) bool {
|
||||
return p.key(ctx, key) != ""
|
||||
key func([]string) string
|
||||
name string
|
||||
}
|
||||
|
||||
func (p *Provider) Name() string {
|
||||
return "toml"
|
||||
return p.name
|
||||
}
|
||||
|
||||
func (p *Provider) Read(ctx context.Context, key config.Key) (config.Variable, error) {
|
||||
if k := p.key(ctx, key); p.tree.Has(k) {
|
||||
return config.Variable{
|
||||
Name: k,
|
||||
Provider: p.Name(),
|
||||
Value: Value{Value: value.Value{Val: p.tree.Get(k)}},
|
||||
}, nil
|
||||
func (p *Provider) Value(ctx context.Context, path ...string) (config.Value, error) {
|
||||
if k := p.key(path); p.tree.Has(k) {
|
||||
return Value{Value: value.Value{Val: p.tree.Get(k)}}, nil
|
||||
}
|
||||
|
||||
return config.Variable{}, config.ErrVariableNotFound
|
||||
return nil, config.ErrValueNotFound
|
||||
}
|
||||
|
||||
@@ -17,12 +17,12 @@ func TestProvider(t *testing.T) {
|
||||
m := []int{}
|
||||
|
||||
read := []test.Read{
|
||||
test.NewRead("database.server", "192.168.1.1"),
|
||||
test.NewRead("title", "TOML Example"),
|
||||
test.NewRead("servers.alpha.ip", "10.0.0.1"),
|
||||
test.NewRead("database.enabled", true),
|
||||
test.NewRead("database.connection_max", 5000),
|
||||
test.NewReadUnmarshal("database.ports", &[]int{8001, 8001, 8002}, &m),
|
||||
test.NewRead("192.168.1.1", "database.server"),
|
||||
test.NewRead("TOML Example", "title"),
|
||||
test.NewRead("10.0.0.1", "servers.alpha.ip"),
|
||||
test.NewRead(true, "database.enabled"),
|
||||
test.NewRead(5000, "database.connection_max"),
|
||||
test.NewReadUnmarshal(&[]int{8001, 8001, 8002}, &m, "database", "ports"),
|
||||
}
|
||||
|
||||
test.Run(t, prov, read)
|
||||
|
||||
@@ -4,25 +4,42 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"strings"
|
||||
|
||||
"github.com/hashicorp/vault/api"
|
||||
"gitoa.ru/go-4devs/config"
|
||||
"gitoa.ru/go-4devs/config/key"
|
||||
"gitoa.ru/go-4devs/config/value"
|
||||
)
|
||||
|
||||
const (
|
||||
Name = "vault"
|
||||
Separator = "/"
|
||||
Prefix = "secret/data/"
|
||||
ValueName = "value"
|
||||
)
|
||||
|
||||
var _ config.Provider = (*SecretKV2)(nil)
|
||||
|
||||
type SecretOption func(*SecretKV2)
|
||||
|
||||
func WithSecretResolve(f func(context.Context, config.Key) (string, string)) SecretOption {
|
||||
func WithSecretResolve(f func(key []string) (string, string)) SecretOption {
|
||||
return func(s *SecretKV2) { s.resolve = f }
|
||||
}
|
||||
|
||||
func NewSecretKV2(client *api.Client, opts ...SecretOption) *SecretKV2 {
|
||||
func NewSecretKV2(namespace, appName string, client *api.Client, opts ...SecretOption) *SecretKV2 {
|
||||
prov := SecretKV2{
|
||||
client: client,
|
||||
resolve: key.LastIndexField(":", "value", key.PrefixName("secret/data/", key.NsAppName("/"))),
|
||||
client: client,
|
||||
resolve: func(key []string) (string, string) {
|
||||
keysLen := len(key)
|
||||
if keysLen == 1 {
|
||||
return "", key[0]
|
||||
}
|
||||
|
||||
return strings.Join(key[:keysLen-1], Separator), key[keysLen-1]
|
||||
},
|
||||
name: Name,
|
||||
prefix: Prefix + namespace + Separator + appName,
|
||||
}
|
||||
|
||||
for _, opt := range opts {
|
||||
@@ -34,57 +51,69 @@ func NewSecretKV2(client *api.Client, opts ...SecretOption) *SecretKV2 {
|
||||
|
||||
type SecretKV2 struct {
|
||||
client *api.Client
|
||||
resolve func(ctx context.Context, key config.Key) (string, string)
|
||||
}
|
||||
|
||||
func (p *SecretKV2) IsSupport(ctx context.Context, key config.Key) bool {
|
||||
path, _ := p.resolve(ctx, key)
|
||||
|
||||
return path != ""
|
||||
resolve func(key []string) (string, string)
|
||||
name string
|
||||
prefix string
|
||||
}
|
||||
|
||||
func (p *SecretKV2) Name() string {
|
||||
return "vault"
|
||||
return p.name
|
||||
}
|
||||
func (p *SecretKV2) Key(in []string) (string, string) {
|
||||
path, val := p.resolve(in)
|
||||
if path == "" {
|
||||
return p.prefix, val
|
||||
}
|
||||
|
||||
func (p *SecretKV2) Read(ctx context.Context, key config.Key) (config.Variable, error) {
|
||||
path, field := p.resolve(ctx, key)
|
||||
|
||||
return p.prefix + Separator + path, val
|
||||
}
|
||||
func (p *SecretKV2) read(path, key string) (*api.Secret, error) {
|
||||
secret, err := p.client.Logical().Read(path)
|
||||
if err != nil {
|
||||
return config.Variable{}, fmt.Errorf("%w: path:%s, field:%s, provider:%s", err, path, field, p.Name())
|
||||
return nil, err
|
||||
}
|
||||
if secret == nil && key != ValueName {
|
||||
return p.read(path+Separator+key, ValueName)
|
||||
}
|
||||
|
||||
return secret, err
|
||||
}
|
||||
|
||||
func (p *SecretKV2) Value(ctx context.Context, key ...string) (config.Value, error) {
|
||||
path, field := p.Key(key)
|
||||
|
||||
secret, err := p.read(path, field)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%w: path:%s, field:%s, provider:%s", err, path, field, p.Name())
|
||||
}
|
||||
|
||||
if secret == nil || len(secret.Data) == 0 {
|
||||
return config.Variable{}, fmt.Errorf("%w: path:%s, field:%s, provider:%s", config.ErrVariableNotFound, path, field, p.Name())
|
||||
log.Println(secret == nil)
|
||||
return nil, fmt.Errorf("%w: path:%s, field:%s, provider:%s", config.ErrValueNotFound, path, field, p.Name())
|
||||
}
|
||||
|
||||
if len(secret.Warnings) > 0 {
|
||||
return config.Variable{},
|
||||
fmt.Errorf("%w: warn: %s, path:%s, field:%s, provider:%s", config.ErrVariableNotFound, secret.Warnings, path, field, p.Name())
|
||||
return nil,
|
||||
fmt.Errorf("%w: warn: %s, path:%s, field:%s, provider:%s", config.ErrValueNotFound, secret.Warnings, path, field, p.Name())
|
||||
}
|
||||
|
||||
data, ok := secret.Data["data"].(map[string]interface{})
|
||||
if !ok {
|
||||
return config.Variable{}, fmt.Errorf("%w: path:%s, field:%s, provider:%s", config.ErrVariableNotFound, path, field, p.Name())
|
||||
return nil, fmt.Errorf("%w: path:%s, field:%s, provider:%s", config.ErrValueNotFound, path, field, p.Name())
|
||||
}
|
||||
|
||||
if val, ok := data[field]; ok {
|
||||
return config.Variable{
|
||||
Name: path + field,
|
||||
Provider: p.Name(),
|
||||
Value: value.JString(fmt.Sprint(val)),
|
||||
}, nil
|
||||
return value.JString(fmt.Sprint(val)), nil
|
||||
}
|
||||
|
||||
if val, ok := data[ValueName]; ok {
|
||||
return value.JString(fmt.Sprint(val)), nil
|
||||
}
|
||||
|
||||
md, err := json.Marshal(data)
|
||||
if err != nil {
|
||||
return config.Variable{}, fmt.Errorf("%w: %w", config.ErrInvalidValue, err)
|
||||
return nil, fmt.Errorf("%w: %w", config.ErrInvalidValue, err)
|
||||
}
|
||||
|
||||
return config.Variable{
|
||||
Name: path + field,
|
||||
Provider: p.Name(),
|
||||
Value: value.JBytes(md),
|
||||
}, nil
|
||||
return value.JBytes(md), nil
|
||||
}
|
||||
|
||||
@@ -15,12 +15,12 @@ func TestProvider(t *testing.T) {
|
||||
cl, err := test.NewVault()
|
||||
require.NoError(t, err)
|
||||
|
||||
provider := vault.NewSecretKV2(cl)
|
||||
provider := vault.NewSecretKV2("fdevs", "config", cl)
|
||||
|
||||
read := []test.Read{
|
||||
test.NewReadConfig("database"),
|
||||
test.NewRead("db:dsn", test.DSN),
|
||||
test.NewRead("db:timeout", time.Minute),
|
||||
test.NewRead(test.DSN, "db", "dsn"),
|
||||
test.NewRead(time.Minute, "db", "timeout"),
|
||||
}
|
||||
test.Run(t, provider, read)
|
||||
}
|
||||
|
||||
@@ -14,10 +14,10 @@ var (
|
||||
_ config.WatchProvider = (*Provider)(nil)
|
||||
)
|
||||
|
||||
func New(duration time.Duration, provider config.Provider, opts ...Option) *Provider {
|
||||
func New(duration time.Duration, provider config.NamedProvider, opts ...Option) *Provider {
|
||||
prov := &Provider{
|
||||
Provider: provider,
|
||||
ticker: time.NewTicker(duration),
|
||||
NamedProvider: provider,
|
||||
ticker: time.NewTicker(duration),
|
||||
logger: func(_ context.Context, msg string) {
|
||||
log.Print(msg)
|
||||
},
|
||||
@@ -39,13 +39,13 @@ func WithLogger(l func(context.Context, string)) Option {
|
||||
type Option func(*Provider)
|
||||
|
||||
type Provider struct {
|
||||
config.Provider
|
||||
config.NamedProvider
|
||||
ticker *time.Ticker
|
||||
logger func(context.Context, string)
|
||||
}
|
||||
|
||||
func (p *Provider) Watch(ctx context.Context, key config.Key, callback config.WatchCallback) error {
|
||||
oldVar, err := p.Provider.Read(ctx, key)
|
||||
func (p *Provider) Watch(ctx context.Context, callback config.WatchCallback, key ...string) error {
|
||||
oldVar, err := p.NamedProvider.Value(ctx, key...)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed watch variable: %w", err)
|
||||
}
|
||||
@@ -54,7 +54,7 @@ func (p *Provider) Watch(ctx context.Context, key config.Key, callback config.Wa
|
||||
for {
|
||||
select {
|
||||
case <-p.ticker.C:
|
||||
newVar, err := p.Provider.Read(ctx, key)
|
||||
newVar, err := p.NamedProvider.Value(ctx, key...)
|
||||
if err != nil {
|
||||
p.logger(ctx, err.Error())
|
||||
} else if !newVar.IsEquals(oldVar) {
|
||||
|
||||
@@ -22,14 +22,10 @@ func (p *provider) Name() string {
|
||||
return "test"
|
||||
}
|
||||
|
||||
func (p *provider) Read(context.Context, config.Key) (config.Variable, error) {
|
||||
func (p *provider) Value(context.Context, ...string) (config.Value, error) {
|
||||
p.cnt++
|
||||
|
||||
return config.Variable{
|
||||
Name: "tmpname",
|
||||
Provider: p.Name(),
|
||||
Value: value.JString(fmt.Sprint(p.cnt)),
|
||||
}, nil
|
||||
return value.JString(fmt.Sprint(p.cnt)), nil
|
||||
}
|
||||
|
||||
func TestWatcher(t *testing.T) {
|
||||
@@ -46,11 +42,11 @@ func TestWatcher(t *testing.T) {
|
||||
|
||||
err := w.Watch(
|
||||
ctx,
|
||||
config.Key{Name: "tmpname"},
|
||||
func(ctx context.Context, oldVar, newVar config.Variable) {
|
||||
func(ctx context.Context, oldVar, newVar config.Value) {
|
||||
atomic.AddInt32(&cnt, 1)
|
||||
wg.Done()
|
||||
},
|
||||
"tmpname",
|
||||
)
|
||||
require.NoError(t, err)
|
||||
wg.Wait()
|
||||
|
||||
@@ -4,22 +4,21 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"strings"
|
||||
"os"
|
||||
|
||||
"gitoa.ru/go-4devs/config"
|
||||
"gitoa.ru/go-4devs/config/value"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
const (
|
||||
Name = "yaml"
|
||||
)
|
||||
|
||||
var _ config.Provider = (*Provider)(nil)
|
||||
|
||||
func keyFactory(_ context.Context, key config.Key) []string {
|
||||
return strings.Split(key.Name, "/")
|
||||
}
|
||||
|
||||
func NewFile(name string, opts ...Option) (*Provider, error) {
|
||||
in, err := ioutil.ReadFile(name)
|
||||
in, err := os.ReadFile(name)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("yaml_file: read error: %w", err)
|
||||
}
|
||||
@@ -38,7 +37,7 @@ func New(yml []byte, opts ...Option) (*Provider, error) {
|
||||
|
||||
func create(opts ...Option) *Provider {
|
||||
prov := Provider{
|
||||
key: keyFactory,
|
||||
name: Name,
|
||||
}
|
||||
|
||||
for _, opt := range opts {
|
||||
@@ -52,22 +51,20 @@ type Option func(*Provider)
|
||||
|
||||
type Provider struct {
|
||||
data node
|
||||
key func(context.Context, config.Key) []string
|
||||
name string
|
||||
}
|
||||
|
||||
func (p *Provider) Name() string {
|
||||
return "yaml"
|
||||
return p.name
|
||||
}
|
||||
|
||||
func (p *Provider) Read(ctx context.Context, key config.Key) (config.Variable, error) {
|
||||
k := p.key(ctx, key)
|
||||
func (p *Provider) Value(_ context.Context, path ...string) (config.Value, error) {
|
||||
|
||||
return p.data.read(p.Name(), k)
|
||||
return p.data.read(p.Name(), path)
|
||||
}
|
||||
|
||||
func (p *Provider) With(data *yaml.Node) *Provider {
|
||||
return &Provider{
|
||||
key: p.key,
|
||||
data: node{Node: data},
|
||||
}
|
||||
}
|
||||
@@ -76,21 +73,17 @@ type node struct {
|
||||
*yaml.Node
|
||||
}
|
||||
|
||||
func (n *node) read(name string, keys []string) (config.Variable, error) {
|
||||
func (n *node) read(name string, keys []string) (config.Value, error) {
|
||||
val, err := getData(n.Node.Content[0].Content, keys)
|
||||
if err != nil {
|
||||
if errors.Is(err, config.ErrVariableNotFound) {
|
||||
return config.Variable{}, fmt.Errorf("%w: %s", config.ErrVariableNotFound, name)
|
||||
if errors.Is(err, config.ErrValueNotFound) {
|
||||
return nil, fmt.Errorf("%w: %s", config.ErrValueNotFound, name)
|
||||
}
|
||||
|
||||
return config.Variable{}, fmt.Errorf("%w: %s", err, name)
|
||||
return nil, fmt.Errorf("%w: %s", err, name)
|
||||
}
|
||||
|
||||
return config.Variable{
|
||||
Name: strings.Join(keys, "."),
|
||||
Provider: name,
|
||||
Value: value.Decode(val),
|
||||
}, nil
|
||||
return value.Decode(val), nil
|
||||
}
|
||||
|
||||
func getData(node []*yaml.Node, keys []string) (func(interface{}) error, error) {
|
||||
@@ -104,5 +97,5 @@ func getData(node []*yaml.Node, keys []string) (func(interface{}) error, error)
|
||||
}
|
||||
}
|
||||
|
||||
return nil, config.ErrVariableNotFound
|
||||
return nil, config.ErrValueNotFound
|
||||
}
|
||||
|
||||
@@ -16,9 +16,9 @@ func TestProvider(t *testing.T) {
|
||||
require.Nil(t, err)
|
||||
|
||||
read := []test.Read{
|
||||
test.NewRead("duration_var", 21*time.Minute),
|
||||
test.NewRead("app/name/bool_var", true),
|
||||
test.NewRead("time_var", test.Time("2020-01-02T15:04:05Z")),
|
||||
test.NewRead(21*time.Minute, "duration_var"),
|
||||
test.NewRead(true, "app", "name", "bool_var"),
|
||||
test.NewRead(test.Time("2020-01-02T15:04:05Z"), "time_var"),
|
||||
test.NewReadConfig("cfg"),
|
||||
}
|
||||
|
||||
|
||||
@@ -3,16 +3,19 @@ package yaml
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
|
||||
"gitoa.ru/go-4devs/config"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
const NameWatch = "yaml_watch"
|
||||
|
||||
func NewWatch(name string, opts ...Option) *Watch {
|
||||
f := Watch{
|
||||
file: name,
|
||||
prov: create(opts...),
|
||||
name: NameWatch,
|
||||
}
|
||||
|
||||
return &f
|
||||
@@ -21,22 +24,23 @@ func NewWatch(name string, opts ...Option) *Watch {
|
||||
type Watch struct {
|
||||
file string
|
||||
prov *Provider
|
||||
name string
|
||||
}
|
||||
|
||||
func (p *Watch) Name() string {
|
||||
return "yaml_watch"
|
||||
return p.name
|
||||
}
|
||||
|
||||
func (p *Watch) Read(ctx context.Context, key config.Key) (config.Variable, error) {
|
||||
in, err := ioutil.ReadFile(p.file)
|
||||
func (p *Watch) Value(ctx context.Context, path ...string) (config.Value, error) {
|
||||
in, err := os.ReadFile(p.file)
|
||||
if err != nil {
|
||||
return config.Variable{}, fmt.Errorf("yaml_file: read error: %w", err)
|
||||
return nil, fmt.Errorf("yaml_file: read error: %w", err)
|
||||
}
|
||||
|
||||
var yNode yaml.Node
|
||||
if err = yaml.Unmarshal(in, &yNode); err != nil {
|
||||
return config.Variable{}, fmt.Errorf("yaml_file: unmarshal error: %w", err)
|
||||
return nil, fmt.Errorf("yaml_file: unmarshal error: %w", err)
|
||||
}
|
||||
|
||||
return p.prov.With(&yNode).Read(ctx, key)
|
||||
return p.prov.With(&yNode).Value(ctx, path...)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user