-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #252 from MadAppGang/quickfix/admin_host_settings
add config file serve on top of current one
- Loading branch information
Showing
4 changed files
with
109 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package mem | ||
|
||
import ( | ||
"io/fs" | ||
"os" | ||
"path" | ||
|
||
"github.com/spf13/afero" | ||
) | ||
|
||
func NewMapOverlayFS(base fs.FS, files map[string][]byte) fs.FS { | ||
layer := afero.NewMemMapFs() | ||
for filename, data := range files { | ||
afero.WriteFile(layer, path.Clean(filename), data, 0644) | ||
} | ||
|
||
return &mapFS{ | ||
base: base, | ||
memFS: afero.NewIOFS(layer), | ||
} | ||
} | ||
|
||
type mapFS struct { | ||
base fs.FS | ||
memFS fs.FS | ||
} | ||
|
||
func (f *mapFS) Open(name string) (fs.File, error) { | ||
mf, err := f.memFS.Open(name) | ||
if err != nil { | ||
if os.IsNotExist(err) || fs.ErrInvalid == err { | ||
return f.base.Open(name) | ||
} | ||
return nil, err | ||
} | ||
return mf, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package mem_test | ||
|
||
import ( | ||
"io/fs" | ||
"path" | ||
"testing" | ||
|
||
"github.com/madappgang/identifo/storage/mem" | ||
"github.com/spf13/afero" | ||
) | ||
|
||
func TestNewMapOverlayFS(t *testing.T) { | ||
base := afero.NewMemMapFs() | ||
afero.WriteFile(base, "./test1.txt", []byte("file1"), 644) | ||
afero.WriteFile(base, "./path/test2.txt", []byte("file2"), 644) | ||
|
||
files := map[string][]byte{ | ||
"./path/test3.txt": []byte("file3"), | ||
"test4.txt": []byte("file4"), | ||
} | ||
|
||
m := mem.NewMapOverlayFS(afero.NewIOFS(base), files) | ||
|
||
d1, _ := fs.ReadFile(m, "test1.txt") | ||
if string(d1) != "file1" { | ||
t.Fatalf("Error getting data, got %s, expected: %s", d1, "file1") | ||
} | ||
|
||
d2, _ := fs.ReadFile(m, path.Clean("./path/test2.txt")) | ||
if string(d2) != "file2" { | ||
t.Fatalf("Error getting data, got %s, expected: %s", d2, "file2") | ||
} | ||
|
||
d3, _ := fs.ReadFile(m, path.Clean("path/test3.txt")) | ||
if string(d3) != "file3" { | ||
t.Fatalf("Error getting data, got %s, expected: %s", d3, "file3") | ||
} | ||
|
||
d4, _ := fs.ReadFile(m, path.Clean("test4.txt")) | ||
if string(d4) != "file4" { | ||
t.Fatalf("Error getting data, got %s, expected: %s", d4, "file4") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters