summaryrefslogtreecommitdiffstats
path: root/hugofs/fs.go
diff options
context:
space:
mode:
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2017-01-10 10:55:03 +0100
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2017-02-04 11:37:25 +0700
commitc71e1b106e6011d148cac899f83c4685dee33a22 (patch)
treec5c7090f0c2398c7771e4908ebcc97aa7714ffd2 /hugofs/fs.go
parent0ada40591216572b0e4c6a8ab986b0aa4fb13c13 (diff)
all: Refactor to nonglobal file systems
Updates #2701 Fixes #2951
Diffstat (limited to 'hugofs/fs.go')
-rw-r--r--hugofs/fs.go84
1 files changed, 31 insertions, 53 deletions
diff --git a/hugofs/fs.go b/hugofs/fs.go
index 7f8abd337..3afa17956 100644
--- a/hugofs/fs.go
+++ b/hugofs/fs.go
@@ -19,76 +19,54 @@ import (
"github.com/spf13/viper"
)
-var (
- sourceFs afero.Fs
- destinationFs afero.Fs
- osFs afero.Fs = &afero.OsFs{}
- workingDirFs *afero.BasePathFs
-)
-
-// Source returns Hugo's source file system.
-func Source() afero.Fs {
- return sourceFs
-}
+// Os points to an Os Afero file system.
+var Os = &afero.OsFs{}
-// SetSource sets Hugo's source file system
-// and re-initializes dependent file systems.
-func SetSource(fs afero.Fs) {
- sourceFs = fs
- initSourceDependencies()
-}
+type Fs struct {
+ // Source is Hugo's source file system.
+ Source afero.Fs
-// Destination returns Hugo's destionation file system.
-func Destination() afero.Fs {
- return destinationFs
-}
-
-// SetDestination sets Hugo's destionation file system
-func SetDestination(fs afero.Fs) {
- destinationFs = fs
-}
+ // Destination is Hugo's destionation file system.
+ Destination afero.Fs
-// Os returns an OS file system.
-func Os() afero.Fs {
- return osFs
-}
+ // Os is an OS file system.
+ Os afero.Fs
-// WorkingDir returns a read-only file system
-// restricted to the project working dir.
-func WorkingDir() *afero.BasePathFs {
- return workingDirFs
+ // WorkingDir is a read-only file system
+ // restricted to the project working dir.
+ WorkingDir *afero.BasePathFs
}
-// InitDefaultFs initializes with the OS file system
+// NewDefault creates a new Fs with the OS file system
// as source and destination file systems.
-func InitDefaultFs() {
- InitFs(&afero.OsFs{})
+func NewDefault() *Fs {
+ fs := &afero.OsFs{}
+ return newFs(fs)
}
-// InitMemFs initializes with a MemMapFs as source and destination file systems.
+// NewDefault creates a new Fs with the MemMapFs
+// as source and destination file systems.
// Useful for testing.
-func InitMemFs() {
- InitFs(&afero.MemMapFs{})
+func NewMem() *Fs {
+ fs := &afero.MemMapFs{}
+ return newFs(fs)
}
-// InitFs initializes with the given file system
-// as source and destination file systems.
-func InitFs(fs afero.Fs) {
- sourceFs = fs
- destinationFs = fs
-
- initSourceDependencies()
+func newFs(base afero.Fs) *Fs {
+ return &Fs{
+ Source: base,
+ Destination: base,
+ Os: &afero.OsFs{},
+ WorkingDir: getWorkingDirFs(base),
+ }
}
-func initSourceDependencies() {
+func getWorkingDirFs(base afero.Fs) *afero.BasePathFs {
workingDir := viper.GetString("workingDir")
if workingDir != "" {
- workingDirFs = afero.NewBasePathFs(afero.NewReadOnlyFs(sourceFs), workingDir).(*afero.BasePathFs)
+ return afero.NewBasePathFs(afero.NewReadOnlyFs(base), workingDir).(*afero.BasePathFs)
}
-}
-
-func init() {
- InitDefaultFs()
+ return nil
}