1
0
mirror of https://github.com/go-micro/go-micro.git synced 2025-07-06 22:35:51 +02:00
Files
go-micro/plugins/config/source/pkger/pkger.go

68 lines
1.1 KiB
Go
Raw Normal View History

2020-12-26 15:32:45 +00:00
package pkger
import (
"io"
2020-12-26 15:32:45 +00:00
"github.com/markbates/pkger"
2021-10-12 12:55:53 +01:00
"go-micro.dev/v4/config/source"
2020-12-26 15:32:45 +00:00
)
type file struct {
path string
opts source.Options
}
var (
DefaultPath = "/config.yaml"
)
func (f *file) Read() (*source.ChangeSet, error) {
fh, err := pkger.Open(f.path)
if err != nil {
return nil, err
}
defer fh.Close()
b, err := io.ReadAll(fh)
2020-12-26 15:32:45 +00:00
if err != nil {
return nil, err
}
info, err := fh.Stat()
if err != nil {
return nil, err
}
cs := &source.ChangeSet{
Format: format(f.path, f.opts.Encoder),
Source: f.String(),
Timestamp: info.ModTime(),
Data: b,
}
cs.Checksum = cs.Sum()
return cs, nil
}
func (f *file) Watch() (source.Watcher, error) {
return source.NewNoopWatcher()
}
// Write is unsupported
func (f *file) Write(cs *source.ChangeSet) error {
return nil
}
func (f *file) String() string {
return "pkger"
}
func NewSource(opts ...source.Option) source.Source {
options := source.NewOptions(opts...)
path := DefaultPath
f, ok := options.Context.Value(pkgerPathKey{}).(string)
if ok {
path = f
}
return &file{opts: options, path: path}
}