first commit
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
andrey1s
2021-04-27 14:58:19 +03:00
commit 913ca9672d
55 changed files with 4355 additions and 0 deletions

136
provider/arg/provider.go Normal file
View File

@@ -0,0 +1,136 @@
package arg
import (
"context"
"fmt"
"os"
"strings"
"gitoa.ru/go-4devs/config"
"gitoa.ru/go-4devs/config/key"
"gitoa.ru/go-4devs/config/value"
"gopkg.in/yaml.v3"
)
var _ config.Provider = (*Provider)(nil)
type Option func(*Provider)
func WithKeyFactory(factory config.KeyFactory) Option {
return func(p *Provider) { p.key = factory }
}
func New(opts ...Option) *Provider {
p := Provider{
key: key.Name,
}
for _, opt := range opts {
opt(&p)
}
return &p
}
type Provider struct {
args map[string][]string
key config.KeyFactory
}
// nolint: cyclop
func (p *Provider) parseOne(arg string) (name, val string, err error) {
if arg[0] != '-' {
return
}
numMinuses := 1
if arg[1] == '-' {
numMinuses++
}
name = strings.TrimSpace(arg[numMinuses:])
if len(name) == 0 {
return
}
if name[0] == '-' || name[0] == '=' {
return "", "", fmt.Errorf("%w: bad flag syntax: %s", config.ErrInvalidValue, arg)
}
for i := 1; i < len(name); i++ {
if name[i] == '=' || name[i] == ' ' {
val = strings.TrimSpace(name[i+1:])
name = name[0:i]
break
}
}
if val == "" && numMinuses == 1 && len(arg) > 2 {
name, val = name[:1], name[1:]
}
return name, val, nil
}
func (p *Provider) parse() error {
if len(p.args) > 0 {
return nil
}
p.args = make(map[string][]string, len(os.Args[1:]))
for _, arg := range os.Args[1:] {
name, value, err := p.parseOne(arg)
if err != nil {
return err
}
if name != "" {
p.args[name] = append(p.args[name], value)
}
}
return nil
}
func (p *Provider) Name() string {
return "arg"
}
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) {
if err := p.parse(); err != nil {
return config.Variable{Provider: p.Name()}, err
}
k := p.key(ctx, key)
if val, ok := p.args[k]; ok {
switch {
case len(val) == 1:
return config.Variable{
Name: k,
Provider: p.Name(),
Value: value.JString(val[0]),
}, nil
default:
var n yaml.Node
if err := yaml.Unmarshal([]byte("["+strings.Join(val, ",")+"]"), &n); err != nil {
return config.Variable{}, fmt.Errorf("arg: failed unmarshal yaml:%w", err)
}
return config.Variable{
Name: k,
Provider: p.Name(),
Value: value.Decode(n.Decode),
}, nil
}
}
return config.Variable{Name: k, Provider: p.Name()}, config.ErrVariableNotFound
}

View File

@@ -0,0 +1,48 @@
package arg_test
import (
"os"
"testing"
"time"
"gitoa.ru/go-4devs/config/provider/arg"
"gitoa.ru/go-4devs/config/test"
)
func TestProvider(t *testing.T) {
t.Parallel()
args := os.Args
defer func() {
os.Args = args
}()
os.Args = []string{
"main.go",
"--listen=8080",
"--config=config.hcl",
"--url=http://4devs.io",
"--url=https://4devs.io",
"--timeout=1m",
"--timeout=1h",
"--start-at=2010-01-02T15:04:05Z",
"--end-after=2009-01-02T15:04:05Z",
"--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.Time("2009-01-02T15:04:05Z"),
test.Time("2008-01-02T15:04:05+03:00"),
}, &[]time.Time{}),
}
prov := arg.New()
test.Run(t, prov, read)
}

61
provider/env/provider.go vendored Normal file
View File

@@ -0,0 +1,61 @@
package env
import (
"context"
"os"
"strings"
"gitoa.ru/go-4devs/config"
"gitoa.ru/go-4devs/config/key"
"gitoa.ru/go-4devs/config/value"
)
var _ config.Provider = (*Provider)(nil)
type Option func(*Provider)
func WithKeyFactory(factory config.KeyFactory) Option {
return func(p *Provider) { p.key = factory }
}
func New(opts ...Option) *Provider {
p := Provider{
key: func(ctx context.Context, k config.Key) string {
return strings.ToUpper(key.NsAppName("_")(ctx, k))
},
}
for _, opt := range opts {
opt(&p)
}
return &p
}
type Provider struct {
key config.KeyFactory
}
func (p *Provider) Name() string {
return "env"
}
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) {
k := p.key(ctx, key)
if val, ok := os.LookupEnv(k); ok {
return config.Variable{
Name: k,
Provider: p.Name(),
Value: value.JString(val),
}, nil
}
return config.Variable{
Name: k,
Provider: p.Name(),
}, config.ErrVariableNotFound
}

24
provider/env/provider_test.go vendored Normal file
View File

@@ -0,0 +1,24 @@
package env_test
import (
"os"
"testing"
"gitoa.ru/go-4devs/config/provider/env"
"gitoa.ru/go-4devs/config/test"
)
func TestProvider(t *testing.T) {
t.Parallel()
os.Setenv("FDEVS_CONFIG_DSN", test.DSN)
os.Setenv("FDEVS_CONFIG_PORT", "8080")
provider := env.New()
read := []test.Read{
test.NewRead("dsn", test.DSN),
test.NewRead("port", 8080),
}
test.Run(t, provider, read)
}

113
provider/etcd/provider.go Normal file
View File

@@ -0,0 +1,113 @@
package etcd
import (
"context"
"fmt"
"log"
"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"
)
var (
_ config.Provider = (*Provider)(nil)
_ config.WatchProvider = (*Provider)(nil)
)
type Client interface {
client.KV
client.Watcher
}
func NewProvider(client Client) *Provider {
p := Provider{
client: client,
key: key.NsAppName("/"),
}
return &p
}
type Provider struct {
client Client
key config.KeyFactory
}
func (p *Provider) IsSupport(ctx context.Context, key config.Key) bool {
return p.key(ctx, key) != ""
}
func (p *Provider) Name() string {
return "etcd"
}
func (p *Provider) Read(ctx context.Context, key config.Key) (config.Variable, error) {
k := p.key(ctx, key)
resp, err := p.client.Get(ctx, k, client.WithPrefix())
if err != nil {
return config.Variable{}, fmt.Errorf("%w: key:%s, prov:%s", err, k, p.Name())
}
val, err := p.resolve(k, resp.Kvs)
if err != nil {
return config.Variable{}, fmt.Errorf("%w: key:%s, prov:%s", err, k, p.Name())
}
return val, nil
}
func (p *Provider) Watch(ctx context.Context, key config.Key, callback config.WatchCallback) 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 {
kvs, olds := p.getEventKvs(w.Events)
if len(kvs) > 0 {
newVar, _ := p.resolve(key, kvs)
oldVar, _ := p.resolve(key, olds)
callback(ctx, oldVar, newVar)
}
}
}(ctx, p.key(ctx, key), callback)
return nil
}
func (p *Provider) getEventKvs(events []*client.Event) ([]*pb.KeyValue, []*pb.KeyValue) {
kvs := make([]*pb.KeyValue, 0, len(events))
old := make([]*pb.KeyValue, 0, len(events))
for i := range events {
kvs = append(kvs, events[i].Kv)
old = append(old, events[i].PrevKv)
log.Println(events[i].Type)
}
return kvs, old
}
func (p *Provider) resolve(key string, kvs []*pb.KeyValue) (config.Variable, error) {
for _, kv := range kvs {
switch {
case kv == nil:
return config.Variable{
Name: key,
Provider: p.Name(),
}, nil
case string(kv.Key) == key:
return config.Variable{
Value: value.JBytes(kv.Value),
Name: key,
Provider: p.Name(),
}, nil
}
}
return config.Variable{
Name: key,
Provider: p.Name(),
}, config.ErrVariableNotFound
}

View File

@@ -0,0 +1,119 @@
package etcd_test
import (
"context"
"fmt"
"sync"
"sync/atomic"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gitoa.ru/go-4devs/config"
"gitoa.ru/go-4devs/config/provider/etcd"
"gitoa.ru/go-4devs/config/test"
)
func TestProvider(t *testing.T) {
t.Parallel()
ctx := context.Background()
et, err := test.NewEtcd(ctx)
require.NoError(t, err)
provider := etcd.NewProvider(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.NewReadConfig("config"),
}
test.Run(t, provider, read)
}
func value(cnt int32) string {
return fmt.Sprintf("test data: %d", cnt)
}
func TestWatcher(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
key := config.Key{
AppName: "config",
Namespace: "fdevs",
Name: "test_watch",
}
et, err := test.NewEtcd(ctx)
require.NoError(t, err)
defer func() {
_, err = et.KV.Delete(context.Background(), "fdevs/config/test_watch")
require.NoError(t, err)
}()
var cnt, cnt2 int32
prov := etcd.NewProvider(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) {
switch *cnt {
case 0:
assert.Equal(t, value(*cnt), newVar.Value.String())
assert.Nil(t, oldVar.Value)
case 1:
assert.Equal(t, value(*cnt), newVar.Value.String())
assert.Equal(t, value(*cnt-1), oldVar.Value.String())
case 2:
_, perr := newVar.Value.ParseString()
assert.NoError(t, perr)
assert.Equal(t, "", newVar.Value.String())
assert.Equal(t, value(*cnt-1), oldVar.Value.String())
default:
assert.Fail(t, "unexpected watch")
}
wg.Done()
atomic.AddInt32(cnt, 1)
}
}
err = prov.Watch(ctx, key, watch(&cnt))
err = prov.Watch(ctx, key, watch(&cnt2))
require.NoError(t, err)
time.AfterFunc(time.Second, func() {
_, err = et.KV.Put(ctx, "fdevs/config/test_watch", value(0))
require.NoError(t, err)
_, err = et.KV.Put(ctx, "fdevs/config/test_watch", value(1))
require.NoError(t, err)
_, err = et.KV.Delete(ctx, "fdevs/config/test_watch")
require.NoError(t, err)
})
time.AfterFunc(time.Second*10, func() {
assert.Fail(t, "failed watch after 5 sec")
cancel()
})
go func() {
wg.Wait()
cancel()
}()
<-ctx.Done()
}

62
provider/ini/provider.go Normal file
View File

@@ -0,0 +1,62 @@
package ini
import (
"context"
"fmt"
"strings"
"gitoa.ru/go-4devs/config"
"gitoa.ru/go-4devs/config/value"
"gopkg.in/ini.v1"
)
var _ config.Provider = (*Provider)(nil)
func New(data *ini.File) *Provider {
return &Provider{
data: data,
resolve: func(ctx context.Context, key config.Key) (string, string) {
keys := strings.SplitN(key.Name, "/", 2)
if len(keys) == 1 {
return "", keys[0]
}
return keys[0], keys[1]
},
}
}
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 != ""
}
func (p *Provider) Name() string {
return "ini"
}
func (p *Provider) Read(ctx context.Context, key config.Key) (config.Variable, error) {
section, name := p.resolve(ctx, key)
iniSection, err := p.data.GetSection(section)
if err != nil {
return config.Variable{}, fmt.Errorf("%w: %s: %v", config.ErrVariableNotFound, p.Name(), err)
}
iniKey, err := iniSection.GetKey(name)
if err != nil {
return config.Variable{}, fmt.Errorf("%w: %s: %v", config.ErrVariableNotFound, p.Name(), err)
}
return config.Variable{
Name: section + ":" + name,
Provider: p.Name(),
Value: value.JString(iniKey.String()),
}, nil
}

View File

@@ -0,0 +1,28 @@
package ini_test
import (
"testing"
"time"
"gitoa.ru/go-4devs/config/provider/ini"
"gitoa.ru/go-4devs/config/test"
)
func TestProvider(t *testing.T) {
t.Parallel()
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),
}
prov := ini.New(file)
test.Run(t, prov, read)
}

66
provider/json/provider.go Normal file
View File

@@ -0,0 +1,66 @@
package json
import (
"context"
"fmt"
"io/ioutil"
"path/filepath"
"github.com/tidwall/gjson"
"gitoa.ru/go-4devs/config"
"gitoa.ru/go-4devs/config/key"
"gitoa.ru/go-4devs/config/value"
)
var _ config.Provider = (*Provider)(nil)
func New(json []byte, opts ...Option) *Provider {
provider := Provider{
key: key.Name,
data: json,
}
for _, opt := range opts {
opt(&provider)
}
return &provider
}
func NewFile(path string, opts ...Option) (*Provider, error) {
file, err := ioutil.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)
}
return New(file), nil
}
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) != ""
}
func (p *Provider) Name() string {
return "json"
}
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
}
return config.Variable{}, config.ErrVariableNotFound
}

View File

@@ -0,0 +1,27 @@
package json_test
import (
"testing"
"time"
provider "gitoa.ru/go-4devs/config/provider/json"
"gitoa.ru/go-4devs/config/test"
)
func TestProvider(t *testing.T) {
t.Parallel()
js := test.ReadFile("config.json")
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.NewReadConfig("cfg"),
test.NewRead("app.name.success", true),
}
test.Run(t, prov, read)
}

71
provider/toml/provider.go Normal file
View File

@@ -0,0 +1,71 @@
package toml
import (
"context"
"fmt"
"github.com/pelletier/go-toml"
"gitoa.ru/go-4devs/config"
"gitoa.ru/go-4devs/config/key"
"gitoa.ru/go-4devs/config/value"
)
var _ config.Provider = (*Provider)(nil)
func NewFile(file string, opts ...Option) (*Provider, error) {
tree, err := toml.LoadFile(file)
if err != nil {
return nil, fmt.Errorf("toml: failed load file: %w", err)
}
return configure(tree, opts...), nil
}
type Option func(*Provider)
func configure(tree *toml.Tree, opts ...Option) *Provider {
p := &Provider{
tree: tree,
key: key.Name,
}
for _, opt := range opts {
opt(p)
}
return p
}
func New(data []byte, opts ...Option) (*Provider, error) {
tree, err := toml.LoadBytes(data)
if err != nil {
return nil, fmt.Errorf("toml failed load data: %w", err)
}
return configure(tree, opts...), nil
}
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) != ""
}
func (p *Provider) Name() string {
return "toml"
}
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
}
return config.Variable{}, config.ErrVariableNotFound
}

View File

@@ -0,0 +1,29 @@
package toml_test
import (
"testing"
"github.com/stretchr/testify/require"
"gitoa.ru/go-4devs/config/provider/toml"
"gitoa.ru/go-4devs/config/test"
)
func TestProvider(t *testing.T) {
t.Parallel()
prov, err := toml.NewFile(test.FixturePath("config.toml"))
require.NoError(t, err)
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.Run(t, prov, read)
}

41
provider/toml/value.go Normal file
View File

@@ -0,0 +1,41 @@
package toml
import (
"encoding/json"
"fmt"
"gitoa.ru/go-4devs/config"
"gitoa.ru/go-4devs/config/value"
)
type Value struct {
value.Value
}
func (s Value) Int() int {
v, _ := s.ParseInt()
return v
}
func (s Value) ParseInt() (int, error) {
v, err := s.ParseInt64()
if err != nil {
return 0, fmt.Errorf("toml failed parce int: %w", err)
}
return int(v), nil
}
func (s Value) Unmarshal(target interface{}) error {
b, err := json.Marshal(s.Raw())
if err != nil {
return fmt.Errorf("%w: %s", config.ErrInvalidValue, err)
}
if err := json.Unmarshal(b, target); err != nil {
return fmt.Errorf("%w: %s", config.ErrInvalidValue, err)
}
return nil
}

90
provider/vault/secret.go Normal file
View File

@@ -0,0 +1,90 @@
package vault
import (
"context"
"encoding/json"
"fmt"
"github.com/hashicorp/vault/api"
"gitoa.ru/go-4devs/config"
"gitoa.ru/go-4devs/config/key"
"gitoa.ru/go-4devs/config/value"
)
var _ config.Provider = (*SecretKV2)(nil)
type SecretOption func(*SecretKV2)
func WithSecretResolve(f func(context.Context, config.Key) (string, string)) SecretOption {
return func(s *SecretKV2) { s.resolve = f }
}
func NewSecretKV2(client *api.Client, opts ...SecretOption) *SecretKV2 {
s := SecretKV2{
client: client,
resolve: key.LastIndexField(":", "value", key.PrefixName("secret/data/", key.NsAppName("/"))),
}
for _, opt := range opts {
opt(&s)
}
return &s
}
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 != ""
}
func (p *SecretKV2) Name() string {
return "vault"
}
func (p *SecretKV2) Read(ctx context.Context, key config.Key) (config.Variable, error) {
path, field := p.resolve(ctx, key)
s, 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())
}
if s == nil || len(s.Data) == 0 {
return config.Variable{}, fmt.Errorf("%w: path:%s, field:%s, provider:%s", config.ErrVariableNotFound, path, field, p.Name())
}
if len(s.Warnings) > 0 {
return config.Variable{},
fmt.Errorf("%w: warn: %s, path:%s, field:%s, provider:%s", config.ErrVariableNotFound, s.Warnings, path, field, p.Name())
}
d, ok := s.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())
}
if val, ok := d[field]; ok {
return config.Variable{
Name: path + field,
Provider: p.Name(),
Value: value.JString(fmt.Sprint(val)),
}, nil
}
md, err := json.Marshal(d)
if err != nil {
return config.Variable{}, fmt.Errorf("%w: %s", config.ErrInvalidValue, err)
}
return config.Variable{
Name: path + field,
Provider: p.Name(),
Value: value.JBytes(md),
}, nil
}

View File

@@ -0,0 +1,26 @@
package vault_test
import (
"testing"
"time"
"github.com/stretchr/testify/require"
"gitoa.ru/go-4devs/config/provider/vault"
"gitoa.ru/go-4devs/config/test"
)
func TestProvider(t *testing.T) {
t.Parallel()
cl, err := test.NewVault()
require.NoError(t, err)
provider := vault.NewSecretKV2(cl)
read := []test.Read{
test.NewReadConfig("database"),
test.NewRead("db:dsn", test.DSN),
test.NewRead("db:timeout", time.Minute),
}
test.Run(t, provider, read)
}

View File

@@ -0,0 +1,71 @@
package watcher
import (
"context"
"fmt"
"log"
"time"
"gitoa.ru/go-4devs/config"
)
var (
_ config.Provider = (*Provider)(nil)
_ config.WatchProvider = (*Provider)(nil)
)
func New(duration time.Duration, provider config.Provider, opts ...Option) *Provider {
p := &Provider{
Provider: provider,
ticker: time.NewTicker(duration),
logger: func(_ context.Context, msg string) {
log.Print(msg)
},
}
for _, opt := range opts {
opt(p)
}
return p
}
func WithLogger(l func(context.Context, string)) Option {
return func(p *Provider) {
p.logger = l
}
}
type Option func(*Provider)
type Provider struct {
config.Provider
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)
if err != nil {
return fmt.Errorf("%s: failed watch variable: %w", p.Provider.Name(), err)
}
go func() {
for {
select {
case <-p.ticker.C:
newVar, err := p.Provider.Read(ctx, key)
if err != nil {
p.logger(ctx, err.Error())
} else if !newVar.IsEquals(oldVar) {
callback(ctx, oldVar, newVar)
oldVar = newVar
}
case <-ctx.Done():
return
}
}
}()
return nil
}

View File

@@ -0,0 +1,58 @@
package watcher_test
import (
"context"
"fmt"
"sync"
"sync/atomic"
"testing"
"time"
"github.com/stretchr/testify/require"
"gitoa.ru/go-4devs/config"
"gitoa.ru/go-4devs/config/provider/watcher"
"gitoa.ru/go-4devs/config/value"
)
type provider struct {
cnt int32
}
func (p *provider) Name() string {
return "test"
}
func (p *provider) Read(ctx context.Context, k config.Key) (config.Variable, error) {
p.cnt++
return config.Variable{
Name: "tmpname",
Value: value.JString(fmt.Sprint(p.cnt)),
}, nil
}
func TestWatcher(t *testing.T) {
t.Parallel()
ctx := context.Background()
prov := &provider{}
w := watcher.New(time.Second, prov)
wg := sync.WaitGroup{}
wg.Add(2)
var cnt int32
err := w.Watch(
ctx,
config.Key{Name: "tmpname"},
func(ctx context.Context, oldVar, newVar config.Variable) {
atomic.AddInt32(&cnt, 1)
wg.Done()
},
)
require.NoError(t, err)
wg.Wait()
require.Equal(t, int32(2), cnt)
}

57
provider/yaml/file.go Normal file
View File

@@ -0,0 +1,57 @@
package yaml
import (
"context"
"fmt"
"io/ioutil"
"gitoa.ru/go-4devs/config"
"gopkg.in/yaml.v3"
)
func WithFileKeyFactory(f func(context.Context, config.Key) []string) FileOption {
return func(p *File) {
p.key = f
}
}
type FileOption func(*File)
func NewFile(name string, opts ...FileOption) *File {
f := File{
file: name,
key: keyFactory,
}
for _, opt := range opts {
opt(&f)
}
return &f
}
type File struct {
file string
key func(context.Context, config.Key) []string
}
func (p *File) Name() string {
return "yaml_file"
}
func (p *File) Read(ctx context.Context, key config.Key) (config.Variable, error) {
in, err := ioutil.ReadFile(p.file)
if err != nil {
return config.Variable{}, fmt.Errorf("yaml_file: read error: %w", err)
}
var n yaml.Node
if err = yaml.Unmarshal(in, &n); err != nil {
return config.Variable{}, fmt.Errorf("yaml_file: unmarshal error: %w", err)
}
data := node{Node: &n}
k := p.key(ctx, key)
return data.read(p.Name(), k)
}

88
provider/yaml/provider.go Normal file
View File

@@ -0,0 +1,88 @@
package yaml
import (
"context"
"errors"
"fmt"
"strings"
"gitoa.ru/go-4devs/config"
"gitoa.ru/go-4devs/config/value"
"gopkg.in/yaml.v3"
)
var _ config.Provider = (*Provider)(nil)
func keyFactory(ctx context.Context, key config.Key) []string {
return strings.Split(key.Name, "/")
}
func New(yml []byte, opts ...Option) (*Provider, error) {
var data yaml.Node
if err := yaml.Unmarshal(yml, &data); err != nil {
return nil, fmt.Errorf("yaml: unmarshal err: %w", err)
}
p := Provider{
key: keyFactory,
data: node{Node: &data},
}
for _, opt := range opts {
opt(&p)
}
return &p, nil
}
type Option func(*Provider)
type Provider struct {
data node
key func(context.Context, config.Key) []string
}
func (p *Provider) Name() string {
return "yaml"
}
func (p *Provider) Read(ctx context.Context, key config.Key) (config.Variable, error) {
k := p.key(ctx, key)
return p.data.read(p.Name(), k)
}
type node struct {
*yaml.Node
}
func (n *node) read(name string, k []string) (config.Variable, error) {
val, err := getData(n.Node.Content[0].Content, k)
if err != nil {
if errors.Is(err, config.ErrVariableNotFound) {
return config.Variable{}, fmt.Errorf("%w: %s", config.ErrVariableNotFound, name)
}
return config.Variable{}, fmt.Errorf("%w: %s", err, name)
}
return config.Variable{
Name: strings.Join(k, "."),
Provider: name,
Value: value.Decode(val),
}, nil
}
func getData(node []*yaml.Node, keys []string) (func(interface{}) error, error) {
for i := len(node) - 1; i > 0; i -= 2 {
if node[i-1].Value == keys[0] {
if len(keys) > 1 {
return getData(node[i].Content, keys[1:])
}
return node[i].Decode, nil
}
}
return nil, config.ErrVariableNotFound
}

View File

@@ -0,0 +1,26 @@
package yaml_test
import (
"testing"
"time"
"github.com/stretchr/testify/require"
provider "gitoa.ru/go-4devs/config/provider/yaml"
"gitoa.ru/go-4devs/config/test"
)
func TestProvider(t *testing.T) {
t.Parallel()
prov, err := provider.New(test.ReadFile("config.yaml"))
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.NewReadConfig("cfg"),
}
test.Run(t, prov, read)
}