add definition config
Some checks failed
continuous-integration/drone/push Build is failing
continuous-integration/drone/pr Build is failing

This commit is contained in:
andrey
2024-01-25 18:17:27 +03:00
parent 303433a336
commit 3945d61f17
52 changed files with 1382 additions and 377 deletions

View File

@@ -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...)
}