mirror of
https://github.com/gohugoio/hugo.git
synced 2025-09-01 22:42:45 +02:00
90
hugofs/fs.go
90
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
|
||||
)
|
||||
// Os points to an Os Afero file system.
|
||||
var Os = &afero.OsFs{}
|
||||
|
||||
// Source returns Hugo's source file system.
|
||||
func Source() afero.Fs {
|
||||
return sourceFs
|
||||
type Fs struct {
|
||||
// Source is Hugo's source file system.
|
||||
Source afero.Fs
|
||||
|
||||
// Destination is Hugo's destionation file system.
|
||||
Destination afero.Fs
|
||||
|
||||
// Os is an OS file system.
|
||||
Os afero.Fs
|
||||
|
||||
// WorkingDir is a read-only file system
|
||||
// restricted to the project working dir.
|
||||
WorkingDir *afero.BasePathFs
|
||||
}
|
||||
|
||||
// SetSource sets Hugo's source file system
|
||||
// and re-initializes dependent file systems.
|
||||
func SetSource(fs afero.Fs) {
|
||||
sourceFs = fs
|
||||
initSourceDependencies()
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
// Os returns an OS file system.
|
||||
func Os() afero.Fs {
|
||||
return osFs
|
||||
}
|
||||
|
||||
// WorkingDir returns a read-only file system
|
||||
// restricted to the project working dir.
|
||||
func WorkingDir() *afero.BasePathFs {
|
||||
return workingDirFs
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
|
@@ -21,51 +21,35 @@ import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestInitDefault(t *testing.T) {
|
||||
func TestNewDefault(t *testing.T) {
|
||||
viper.Reset()
|
||||
defer viper.Reset()
|
||||
|
||||
InitDefaultFs()
|
||||
f := NewDefault()
|
||||
|
||||
assert.NotNil(t, Source())
|
||||
assert.IsType(t, new(afero.OsFs), Source())
|
||||
assert.NotNil(t, Destination())
|
||||
assert.IsType(t, new(afero.OsFs), Destination())
|
||||
assert.NotNil(t, Os())
|
||||
assert.IsType(t, new(afero.OsFs), Os())
|
||||
assert.Nil(t, WorkingDir())
|
||||
assert.NotNil(t, f.Source)
|
||||
assert.IsType(t, new(afero.OsFs), f.Source)
|
||||
assert.NotNil(t, f.Destination)
|
||||
assert.IsType(t, new(afero.OsFs), f.Destination)
|
||||
assert.NotNil(t, f.Os)
|
||||
assert.IsType(t, new(afero.OsFs), f.Os)
|
||||
assert.Nil(t, f.WorkingDir)
|
||||
|
||||
assert.IsType(t, new(afero.OsFs), Os)
|
||||
}
|
||||
|
||||
func TestInitMemFs(t *testing.T) {
|
||||
func TestNewMem(t *testing.T) {
|
||||
viper.Reset()
|
||||
defer viper.Reset()
|
||||
|
||||
InitMemFs()
|
||||
f := NewMem()
|
||||
|
||||
assert.NotNil(t, Source())
|
||||
assert.IsType(t, new(afero.MemMapFs), Source())
|
||||
assert.NotNil(t, Destination())
|
||||
assert.IsType(t, new(afero.MemMapFs), Destination())
|
||||
assert.IsType(t, new(afero.OsFs), Os())
|
||||
assert.Nil(t, WorkingDir())
|
||||
}
|
||||
|
||||
func TestSetSource(t *testing.T) {
|
||||
|
||||
InitMemFs()
|
||||
|
||||
SetSource(new(afero.OsFs))
|
||||
assert.NotNil(t, Source())
|
||||
assert.IsType(t, new(afero.OsFs), Source())
|
||||
}
|
||||
|
||||
func TestSetDestination(t *testing.T) {
|
||||
|
||||
InitMemFs()
|
||||
|
||||
SetDestination(new(afero.OsFs))
|
||||
assert.NotNil(t, Destination())
|
||||
assert.IsType(t, new(afero.OsFs), Destination())
|
||||
assert.NotNil(t, f.Source)
|
||||
assert.IsType(t, new(afero.MemMapFs), f.Source)
|
||||
assert.NotNil(t, f.Destination)
|
||||
assert.IsType(t, new(afero.MemMapFs), f.Destination)
|
||||
assert.IsType(t, new(afero.OsFs), f.Os)
|
||||
assert.Nil(t, f.WorkingDir)
|
||||
}
|
||||
|
||||
func TestWorkingDir(t *testing.T) {
|
||||
@@ -74,8 +58,8 @@ func TestWorkingDir(t *testing.T) {
|
||||
|
||||
viper.Set("workingDir", "/a/b/")
|
||||
|
||||
InitMemFs()
|
||||
f := NewMem()
|
||||
|
||||
assert.NotNil(t, WorkingDir())
|
||||
assert.IsType(t, new(afero.BasePathFs), WorkingDir())
|
||||
assert.NotNil(t, f.WorkingDir)
|
||||
assert.IsType(t, new(afero.BasePathFs), f.WorkingDir)
|
||||
}
|
||||
|
Reference in New Issue
Block a user