summaryrefslogtreecommitdiffstats
path: root/create
diff options
context:
space:
mode:
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2016-03-22 00:28:42 +0100
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2016-03-31 21:24:18 +0200
commit4f66f790b168005efb835b2499c4a502e492b747 (patch)
tree2e5f808bf10ac18ff1735dd743aa63e5513737d5 /create
parenta89035bdaaa8bb1525a74d82e068ef80bfa28aed (diff)
Add readFile template func
This also includes a refactor of the hugofs package and its usage. The motivation for that is: The Afero filesystems are brilliant. Hugo's way of adding a dozen of global variables for the different filesystems was a mistake. In readFile (and also in some other places in Hugo today) we need a way to restrict the access inside the working dir. We could use ioutil.ReadFile and implement the path checking, checking the base path and the dots ("..") etc. But it is obviously better to use an Afero BasePathFs combined witha ReadOnlyFs. We could create a use-once-filesystem and handle the initialization ourselves, but since this is also useful to others and the initialization depends on some other global state (which would mean to create a new file system on every invocation), we might as well do it properly and encapsulate the predefined set of filesystems. This change also leads the way, if needed, to encapsulate the file systems in a struct, making it possible to have several file system sets in action at once (parallel multilanguage site building? With Moore's law and all...) Fixes #1551
Diffstat (limited to 'create')
-rw-r--r--create/content_test.go12
1 files changed, 6 insertions, 6 deletions
diff --git a/create/content_test.go b/create/content_test.go
index f0b3de1a0..0ccbd03ea 100644
--- a/create/content_test.go
+++ b/create/content_test.go
@@ -44,19 +44,19 @@ func TestNewContent(t *testing.T) {
}
for i, c := range cases {
- err = create.NewContent(hugofs.SourceFs, c.kind, c.path)
+ err = create.NewContent(hugofs.Source(), c.kind, c.path)
if err != nil {
t.Errorf("[%d] NewContent: %s", i, err)
}
fname := filepath.Join(os.TempDir(), "content", filepath.FromSlash(c.path))
- _, err = hugofs.SourceFs.Stat(fname)
+ _, err = hugofs.Source().Stat(fname)
if err != nil {
t.Errorf("[%d] Stat: %s", i, err)
}
for _, v := range c.resultStrings {
- found, err := afero.FileContainsBytes(hugofs.SourceFs, fname, []byte(v))
+ found, err := afero.FileContainsBytes(hugofs.Source(), fname, []byte(v))
if err != nil {
t.Errorf("[%d] FileContainsBytes: %s", i, err)
}
@@ -77,7 +77,7 @@ func initViper() {
}
func initFs() error {
- hugofs.SourceFs = new(afero.MemMapFs)
+ hugofs.SetSource(new(afero.MemMapFs))
perm := os.FileMode(0755)
var err error
@@ -89,7 +89,7 @@ func initFs() error {
}
for _, dir := range dirs {
dir = filepath.Join(os.TempDir(), dir)
- err = hugofs.SourceFs.Mkdir(dir, perm)
+ err = hugofs.Source().Mkdir(dir, perm)
if err != nil {
return err
}
@@ -109,7 +109,7 @@ func initFs() error {
content: "+++\n+++\n",
},
} {
- f, err := hugofs.SourceFs.Create(v.path)
+ f, err := hugofs.Source().Create(v.path)
if err != nil {
return err
}