mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-29 22:29:56 +02:00
all: Refactor to nonglobal Viper, i18n etc.
This is a final rewrite that removes all the global state in Hugo, which also enables the use if `t.Parallel` in tests. Updates #2701 Fixes #3016
This commit is contained in:
25
hugofs/fs.go
25
hugofs/fs.go
@@ -16,7 +16,7 @@ package hugofs
|
||||
|
||||
import (
|
||||
"github.com/spf13/afero"
|
||||
"github.com/spf13/viper"
|
||||
"github.com/spf13/hugo/config"
|
||||
)
|
||||
|
||||
// Os points to an Os Afero file system.
|
||||
@@ -39,30 +39,37 @@ type Fs struct {
|
||||
|
||||
// NewDefault creates a new Fs with the OS file system
|
||||
// as source and destination file systems.
|
||||
func NewDefault() *Fs {
|
||||
func NewDefault(cfg config.Provider) *Fs {
|
||||
fs := &afero.OsFs{}
|
||||
return newFs(fs)
|
||||
return newFs(fs, cfg)
|
||||
}
|
||||
|
||||
// NewDefault creates a new Fs with the MemMapFs
|
||||
// as source and destination file systems.
|
||||
// Useful for testing.
|
||||
func NewMem() *Fs {
|
||||
func NewMem(cfg config.Provider) *Fs {
|
||||
fs := &afero.MemMapFs{}
|
||||
return newFs(fs)
|
||||
return newFs(fs, cfg)
|
||||
}
|
||||
|
||||
func newFs(base afero.Fs) *Fs {
|
||||
// NewFrom creates a new Fs based on the provided Afero Fs
|
||||
// as source and destination file systems.
|
||||
// Useful for testing.
|
||||
func NewFrom(fs afero.Fs, cfg config.Provider) *Fs {
|
||||
return newFs(fs, cfg)
|
||||
}
|
||||
|
||||
func newFs(base afero.Fs, cfg config.Provider) *Fs {
|
||||
return &Fs{
|
||||
Source: base,
|
||||
Destination: base,
|
||||
Os: &afero.OsFs{},
|
||||
WorkingDir: getWorkingDirFs(base),
|
||||
WorkingDir: getWorkingDirFs(base, cfg),
|
||||
}
|
||||
}
|
||||
|
||||
func getWorkingDirFs(base afero.Fs) *afero.BasePathFs {
|
||||
workingDir := viper.GetString("workingDir")
|
||||
func getWorkingDirFs(base afero.Fs, cfg config.Provider) *afero.BasePathFs {
|
||||
workingDir := cfg.GetString("workingDir")
|
||||
|
||||
if workingDir != "" {
|
||||
return afero.NewBasePathFs(afero.NewReadOnlyFs(base), workingDir).(*afero.BasePathFs)
|
||||
|
Reference in New Issue
Block a user