You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

43 lines
738 B

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