mirror of
https://github.com/go-micro/go-micro.git
synced 2025-05-13 21:16:43 +02:00
feat(config): add withFs option to file source (#2557)
Co-authored-by: Mohamed MHAMDI <mmhamdi@hubside.com>
This commit is contained in:
parent
34b6434791
commit
39e00f11a8
@ -3,12 +3,14 @@ package file
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"io"
|
"io"
|
||||||
|
"io/fs"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"go-micro.dev/v4/config/source"
|
"go-micro.dev/v4/config/source"
|
||||||
)
|
)
|
||||||
|
|
||||||
type file struct {
|
type file struct {
|
||||||
|
fs fs.FS
|
||||||
path string
|
path string
|
||||||
opts source.Options
|
opts source.Options
|
||||||
}
|
}
|
||||||
@ -18,7 +20,15 @@ var (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func (f *file) Read() (*source.ChangeSet, error) {
|
func (f *file) Read() (*source.ChangeSet, error) {
|
||||||
fh, err := os.Open(f.path)
|
var fh fs.File
|
||||||
|
var err error
|
||||||
|
|
||||||
|
if f.fs != nil {
|
||||||
|
fh, err = f.fs.Open(f.path)
|
||||||
|
} else {
|
||||||
|
fh, err = os.Open(f.path)
|
||||||
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -48,6 +58,11 @@ func (f *file) String() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (f *file) Watch() (source.Watcher, error) {
|
func (f *file) Watch() (source.Watcher, error) {
|
||||||
|
// do not watch if fs.FS instance is provided
|
||||||
|
if f.fs != nil {
|
||||||
|
return source.NewNoopWatcher()
|
||||||
|
}
|
||||||
|
|
||||||
if _, err := os.Stat(f.path); err != nil {
|
if _, err := os.Stat(f.path); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -60,10 +75,13 @@ func (f *file) Write(cs *source.ChangeSet) error {
|
|||||||
|
|
||||||
func NewSource(opts ...source.Option) source.Source {
|
func NewSource(opts ...source.Option) source.Source {
|
||||||
options := source.NewOptions(opts...)
|
options := source.NewOptions(opts...)
|
||||||
|
|
||||||
|
fs, _ := options.Context.Value(fsKey{}).(fs.FS)
|
||||||
|
|
||||||
path := DefaultPath
|
path := DefaultPath
|
||||||
f, ok := options.Context.Value(filePathKey{}).(string)
|
f, ok := options.Context.Value(filePathKey{}).(string)
|
||||||
if ok {
|
if ok {
|
||||||
path = f
|
path = f
|
||||||
}
|
}
|
||||||
return &file{opts: options, path: path}
|
return &file{opts: options, fs: fs, path: path}
|
||||||
}
|
}
|
||||||
|
@ -5,6 +5,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"testing"
|
"testing"
|
||||||
|
"testing/fstest"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"go-micro.dev/v4/config"
|
"go-micro.dev/v4/config"
|
||||||
@ -64,3 +65,25 @@ func TestFile(t *testing.T) {
|
|||||||
t.Error("data from file does not match")
|
t.Error("data from file does not match")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestWithFS(t *testing.T) {
|
||||||
|
data := []byte(`{"foo": "bar"}`)
|
||||||
|
path := fmt.Sprintf("file.%d", time.Now().UnixNano())
|
||||||
|
|
||||||
|
fsMock := fstest.MapFS{
|
||||||
|
path: &fstest.MapFile{
|
||||||
|
Data: data,
|
||||||
|
Mode: 0666,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
f := file.NewSource(file.WithFS(fsMock), file.WithPath(path))
|
||||||
|
c, err := f.Read()
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
if string(c.Data) != string(data) {
|
||||||
|
t.Logf("%+v", c)
|
||||||
|
t.Error("data from file does not match")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -2,11 +2,13 @@ package file
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"io/fs"
|
||||||
|
|
||||||
"go-micro.dev/v4/config/source"
|
"go-micro.dev/v4/config/source"
|
||||||
)
|
)
|
||||||
|
|
||||||
type filePathKey struct{}
|
type filePathKey struct{}
|
||||||
|
type fsKey struct{}
|
||||||
|
|
||||||
// WithPath sets the path to file
|
// WithPath sets the path to file
|
||||||
func WithPath(p string) source.Option {
|
func WithPath(p string) source.Option {
|
||||||
@ -17,3 +19,13 @@ func WithPath(p string) source.Option {
|
|||||||
o.Context = context.WithValue(o.Context, filePathKey{}, p)
|
o.Context = context.WithValue(o.Context, filePathKey{}, p)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WithFS sets the underlying filesystem to lookup file from (default os.FS)
|
||||||
|
func WithFS(fs fs.FS) source.Option {
|
||||||
|
return func(o *source.Options) {
|
||||||
|
if o.Context == nil {
|
||||||
|
o.Context = context.Background()
|
||||||
|
}
|
||||||
|
o.Context = context.WithValue(o.Context, fsKey{}, fs)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user