Skip to content

Commit

Permalink
feat(config): add withFs option to file source
Browse files Browse the repository at this point in the history
  • Loading branch information
Mohamed MHAMDI committed Sep 23, 2022
1 parent 34b6434 commit 45fb546
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 2 deletions.
22 changes: 20 additions & 2 deletions config/source/file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ package file

import (
"io"
"io/fs"
"os"

"go-micro.dev/v4/config/source"
)

type file struct {
fs fs.FS
path string
opts source.Options
}
Expand All @@ -18,7 +20,15 @@ var (
)

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 {
return nil, err
}
Expand Down Expand Up @@ -48,6 +58,11 @@ func (f *file) String() string {
}

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 {
return nil, err
}
Expand All @@ -60,10 +75,13 @@ func (f *file) Write(cs *source.ChangeSet) error {

func NewSource(opts ...source.Option) source.Source {
options := source.NewOptions(opts...)

fs := options.Context.Value(fsKey{}).(fs.FS)

path := DefaultPath
f, ok := options.Context.Value(filePathKey{}).(string)
if ok {
path = f
}
return &file{opts: options, path: path}
return &file{opts: options, fs: fs, path: path}
}
23 changes: 23 additions & 0 deletions config/source/file/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"path/filepath"
"testing"
"testing/fstest"
"time"

"go-micro.dev/v4/config"
Expand Down Expand Up @@ -64,3 +65,25 @@ func TestFile(t *testing.T) {
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")
}
}
12 changes: 12 additions & 0 deletions config/source/file/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package file

import (
"context"
"io/fs"

"go-micro.dev/v4/config/source"
)

type filePathKey struct{}
type fsKey struct{}

// WithPath sets the path to file
func WithPath(p string) source.Option {
Expand All @@ -17,3 +19,13 @@ func WithPath(p string) source.Option {
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)
}
}

0 comments on commit 45fb546

Please sign in to comment.