diff options
Diffstat (limited to 'hugofs/fs.go')
-rw-r--r-- | hugofs/fs.go | 84 |
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 } |