add definition config
This commit is contained in:
@@ -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