zrok/controller/metrics/fileSource.go

43 lines
841 B
Go
Raw Normal View History

2023-03-03 19:31:57 +01:00
package metrics
2023-03-03 19:51:10 +01:00
import (
"github.com/michaelquigley/cf"
"github.com/pkg/errors"
"os"
)
2023-03-03 19:31:57 +01:00
type FileSourceConfig struct {
Path string
}
func loadFileSourceConfig(v interface{}, opts *cf.Options) (interface{}, error) {
2023-03-03 19:51:10 +01:00
if submap, ok := v.(map[string]interface{}); ok {
cfg := &FileSourceConfig{}
if err := cf.Bind(cfg, submap, cf.DefaultOptions()); err != nil {
return nil, err
}
return &fileSource{cfg}, nil
}
return nil, errors.New("invalid config structure for 'file' source")
2023-03-03 19:31:57 +01:00
}
2023-03-03 19:51:10 +01:00
type fileSource struct {
cfg *FileSourceConfig
}
2023-03-03 19:31:57 +01:00
func (s *fileSource) Start() (chan struct{}, error) {
2023-03-03 19:51:10 +01:00
f, err := os.Open(s.cfg.Path)
if err != nil {
return nil, errors.Wrapf(err, "error opening '%v'", s.cfg.Path)
}
ch := make(chan struct{})
go func() {
f.Close()
close(ch)
}()
return ch, nil
}
func (s *fileSource) Stop() {
2023-03-03 19:31:57 +01:00
}