mirror of
https://github.com/gohugoio/hugo.git
synced 2024-11-21 20:46:30 -05:00
hugolib: Add ConfigSourceDescriptor
To prepare for config in themes See #4490
This commit is contained in:
parent
b6798ee867
commit
3d1a6e109c
6 changed files with 39 additions and 20 deletions
|
@ -306,7 +306,7 @@ func InitializeConfig(running bool, doWithCommandeer func(c *commandeer) error,
|
||||||
// Init file systems. This may be changed at a later point.
|
// Init file systems. This may be changed at a later point.
|
||||||
osFs := hugofs.Os
|
osFs := hugofs.Os
|
||||||
|
|
||||||
config, err := hugolib.LoadConfig(osFs, source, cfgFile)
|
config, err := hugolib.LoadConfig(hugolib.ConfigSourceDescriptor{Fs: osFs, Src: source, Name: cfgFile})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -149,7 +149,7 @@ func TestCaseInsensitiveConfigurationVariations(t *testing.T) {
|
||||||
|
|
||||||
caseMixingTestsWriteCommonSources(t, mm)
|
caseMixingTestsWriteCommonSources(t, mm)
|
||||||
|
|
||||||
cfg, err := LoadConfig(mm, "", "config.toml")
|
cfg, err := LoadConfig(ConfigSourceDescriptor{Fs: mm})
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
fs := hugofs.NewFrom(mm, cfg)
|
fs := hugofs.NewFrom(mm, cfg)
|
||||||
|
@ -260,7 +260,7 @@ func doTestCaseInsensitiveConfigurationForTemplateEngine(t *testing.T, suffix st
|
||||||
|
|
||||||
caseMixingTestsWriteCommonSources(t, mm)
|
caseMixingTestsWriteCommonSources(t, mm)
|
||||||
|
|
||||||
cfg, err := LoadConfig(mm, "", "config.toml")
|
cfg, err := LoadConfigDefault(mm)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
fs := hugofs.NewFrom(mm, cfg)
|
fs := hugofs.NewFrom(mm, cfg)
|
||||||
|
|
|
@ -26,24 +26,43 @@ import (
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// ConfigSourceDescriptor describes where to find the config (e.g. config.toml etc.).
|
||||||
|
type ConfigSourceDescriptor struct {
|
||||||
|
Fs afero.Fs
|
||||||
|
Src string
|
||||||
|
Name string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d ConfigSourceDescriptor) configFilenames() []string {
|
||||||
|
return strings.Split(d.Name, ",")
|
||||||
|
}
|
||||||
|
|
||||||
|
// LoadConfigDefault is a convenience method to load the default "config.toml" config.
|
||||||
|
func LoadConfigDefault(fs afero.Fs) (*viper.Viper, error) {
|
||||||
|
return LoadConfig(ConfigSourceDescriptor{Fs: fs, Name: "config.toml"})
|
||||||
|
}
|
||||||
|
|
||||||
// LoadConfig loads Hugo configuration into a new Viper and then adds
|
// LoadConfig loads Hugo configuration into a new Viper and then adds
|
||||||
// a set of defaults.
|
// a set of defaults.
|
||||||
func LoadConfig(fs afero.Fs, relativeSourcePath, configFilename string) (*viper.Viper, error) {
|
func LoadConfig(d ConfigSourceDescriptor) (*viper.Viper, error) {
|
||||||
|
fs := d.Fs
|
||||||
v := viper.New()
|
v := viper.New()
|
||||||
v.SetFs(fs)
|
v.SetFs(fs)
|
||||||
if relativeSourcePath == "" {
|
|
||||||
relativeSourcePath = "."
|
if d.Name == "" {
|
||||||
|
d.Name = "config.toml"
|
||||||
}
|
}
|
||||||
configFilenames := strings.Split(configFilename, ",")
|
|
||||||
|
if d.Src == "" {
|
||||||
|
d.Src = "."
|
||||||
|
}
|
||||||
|
|
||||||
|
configFilenames := d.configFilenames()
|
||||||
v.AutomaticEnv()
|
v.AutomaticEnv()
|
||||||
v.SetEnvPrefix("hugo")
|
v.SetEnvPrefix("hugo")
|
||||||
v.SetConfigFile(configFilenames[0])
|
v.SetConfigFile(configFilenames[0])
|
||||||
// See https://github.com/spf13/viper/issues/73#issuecomment-126970794
|
v.AddConfigPath(d.Src)
|
||||||
if relativeSourcePath == "" {
|
|
||||||
v.AddConfigPath(".")
|
|
||||||
} else {
|
|
||||||
v.AddConfigPath(relativeSourcePath)
|
|
||||||
}
|
|
||||||
err := v.ReadInConfig()
|
err := v.ReadInConfig()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if _, ok := err.(viper.ConfigParseError); ok {
|
if _, ok := err.(viper.ConfigParseError); ok {
|
||||||
|
@ -62,8 +81,6 @@ func LoadConfig(fs afero.Fs, relativeSourcePath, configFilename string) (*viper.
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
v.RegisterAlias("indexes", "taxonomies")
|
|
||||||
|
|
||||||
if err := loadDefaultSettingsFor(v); err != nil {
|
if err := loadDefaultSettingsFor(v); err != nil {
|
||||||
return v, err
|
return v, err
|
||||||
}
|
}
|
||||||
|
@ -191,6 +208,8 @@ func loadDefaultSettingsFor(v *viper.Viper) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
v.RegisterAlias("indexes", "taxonomies")
|
||||||
|
|
||||||
v.SetDefault("cleanDestinationDir", false)
|
v.SetDefault("cleanDestinationDir", false)
|
||||||
v.SetDefault("watch", false)
|
v.SetDefault("watch", false)
|
||||||
v.SetDefault("metaDataFormat", "toml")
|
v.SetDefault("metaDataFormat", "toml")
|
||||||
|
|
|
@ -34,7 +34,7 @@ func TestLoadConfig(t *testing.T) {
|
||||||
|
|
||||||
writeToFs(t, mm, "hugo.toml", configContent)
|
writeToFs(t, mm, "hugo.toml", configContent)
|
||||||
|
|
||||||
cfg, err := LoadConfig(mm, "", "hugo.toml")
|
cfg, err := LoadConfig(ConfigSourceDescriptor{Fs: mm, Name: "hugo.toml"})
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
assert.Equal(t, "side", cfg.GetString("paginatePath"))
|
assert.Equal(t, "side", cfg.GetString("paginatePath"))
|
||||||
|
@ -59,7 +59,7 @@ func TestLoadMultiConfig(t *testing.T) {
|
||||||
|
|
||||||
writeToFs(t, mm, "override.toml", configContentSub)
|
writeToFs(t, mm, "override.toml", configContentSub)
|
||||||
|
|
||||||
cfg, err := LoadConfig(mm, "", "base.toml,override.toml")
|
cfg, err := LoadConfig(ConfigSourceDescriptor{Fs: mm, Name: "base.toml,override.toml"})
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
assert.Equal(t, "top", cfg.GetString("paginatePath"))
|
assert.Equal(t, "top", cfg.GetString("paginatePath"))
|
||||||
|
|
|
@ -90,7 +90,7 @@ categories:
|
||||||
siteConfig := fmt.Sprintf(siteConfigTemplate, disabledStr)
|
siteConfig := fmt.Sprintf(siteConfigTemplate, disabledStr)
|
||||||
writeToFs(t, mf, "config.toml", siteConfig)
|
writeToFs(t, mf, "config.toml", siteConfig)
|
||||||
|
|
||||||
cfg, err := LoadConfig(mf, "", "config.toml")
|
cfg, err := LoadConfigDefault(mf)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
fs := hugofs.NewFrom(mf, cfg)
|
fs := hugofs.NewFrom(mf, cfg)
|
||||||
|
|
|
@ -229,7 +229,7 @@ func (s *sitesBuilder) CreateSites() *sitesBuilder {
|
||||||
s.writeFilePairs("i18n", s.i18nFilePairsAdded)
|
s.writeFilePairs("i18n", s.i18nFilePairsAdded)
|
||||||
|
|
||||||
if s.Cfg == nil {
|
if s.Cfg == nil {
|
||||||
cfg, err := LoadConfig(s.Fs.Source, "", "config."+s.configFormat)
|
cfg, err := LoadConfig(ConfigSourceDescriptor{Fs: s.Fs.Source, Name: "config." + s.configFormat})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
s.Fatalf("Failed to load config: %s", err)
|
s.Fatalf("Failed to load config: %s", err)
|
||||||
}
|
}
|
||||||
|
@ -460,7 +460,7 @@ func newTestSitesFromConfig(t testing.TB, afs afero.Fs, tomlConfig string, layou
|
||||||
|
|
||||||
writeToFs(t, afs, "config.toml", tomlConfig)
|
writeToFs(t, afs, "config.toml", tomlConfig)
|
||||||
|
|
||||||
cfg, err := LoadConfig(afs, "", "config.toml")
|
cfg, err := LoadConfigDefault(afs)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
fs := hugofs.NewFrom(afs, cfg)
|
fs := hugofs.NewFrom(afs, cfg)
|
||||||
|
|
Loading…
Reference in a new issue