add configure provider by preview providers
This commit is contained in:
@@ -1,57 +0,0 @@
|
||||
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)
|
||||
}
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"strings"
|
||||
|
||||
"gitoa.ru/go-4devs/config"
|
||||
@@ -17,22 +18,34 @@ func keyFactory(ctx context.Context, key config.Key) []string {
|
||||
return strings.Split(key.Name, "/")
|
||||
}
|
||||
|
||||
func NewFile(name string, opts ...Option) (*Provider, error) {
|
||||
in, err := ioutil.ReadFile(name)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("yaml_file: read error: %w", err)
|
||||
}
|
||||
|
||||
return New(in, opts...)
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
return create(opts...).With(&data), nil
|
||||
}
|
||||
|
||||
func create(opts ...Option) *Provider {
|
||||
p := Provider{
|
||||
key: keyFactory,
|
||||
data: node{Node: &data},
|
||||
key: keyFactory,
|
||||
}
|
||||
|
||||
for _, opt := range opts {
|
||||
opt(&p)
|
||||
}
|
||||
|
||||
return &p, nil
|
||||
return &p
|
||||
}
|
||||
|
||||
type Option func(*Provider)
|
||||
@@ -52,6 +65,13 @@ func (p *Provider) Read(ctx context.Context, key config.Key) (config.Variable, e
|
||||
return p.data.read(p.Name(), k)
|
||||
}
|
||||
|
||||
func (p *Provider) With(data *yaml.Node) *Provider {
|
||||
return &Provider{
|
||||
key: p.key,
|
||||
data: node{Node: data},
|
||||
}
|
||||
}
|
||||
|
||||
type node struct {
|
||||
*yaml.Node
|
||||
}
|
||||
|
||||
42
provider/yaml/watch.go
Normal file
42
provider/yaml/watch.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package yaml
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
|
||||
"gitoa.ru/go-4devs/config"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
func NewWatch(name string, opts ...Option) *Watch {
|
||||
f := Watch{
|
||||
file: name,
|
||||
prov: create(opts...),
|
||||
}
|
||||
|
||||
return &f
|
||||
}
|
||||
|
||||
type Watch struct {
|
||||
file string
|
||||
prov *Provider
|
||||
}
|
||||
|
||||
func (p *Watch) Name() string {
|
||||
return "yaml_watch"
|
||||
}
|
||||
|
||||
func (p *Watch) 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)
|
||||
}
|
||||
|
||||
return p.prov.With(&n).Read(ctx, key)
|
||||
}
|
||||
Reference in New Issue
Block a user