diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2018-03-18 10:32:29 +0100 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2018-03-20 21:30:43 +0100 |
commit | 3d1a6e109ce9b25fc2e9731098a82fb4c0abff68 (patch) | |
tree | b66741ed1463a3ee2a96bd74d36914de4dccecde /hugolib/config.go | |
parent | b6798ee8676c48f86b0bd8581ea244f4be4ef3fa (diff) |
hugolib: Add ConfigSourceDescriptor
To prepare for config in themes
See #4490
Diffstat (limited to 'hugolib/config.go')
-rw-r--r-- | hugolib/config.go | 43 |
1 files changed, 31 insertions, 12 deletions
diff --git a/hugolib/config.go b/hugolib/config.go index 678ebf669..e47e65435 100644 --- a/hugolib/config.go +++ b/hugolib/config.go @@ -26,24 +26,43 @@ import ( "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 // 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.SetFs(fs) - if relativeSourcePath == "" { - relativeSourcePath = "." + + if d.Name == "" { + d.Name = "config.toml" + } + + if d.Src == "" { + d.Src = "." } - configFilenames := strings.Split(configFilename, ",") + + configFilenames := d.configFilenames() v.AutomaticEnv() v.SetEnvPrefix("hugo") v.SetConfigFile(configFilenames[0]) - // See https://github.com/spf13/viper/issues/73#issuecomment-126970794 - if relativeSourcePath == "" { - v.AddConfigPath(".") - } else { - v.AddConfigPath(relativeSourcePath) - } + v.AddConfigPath(d.Src) + err := v.ReadInConfig() if err != nil { 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 { return v, err } @@ -191,6 +208,8 @@ func loadDefaultSettingsFor(v *viper.Viper) error { return err } + v.RegisterAlias("indexes", "taxonomies") + v.SetDefault("cleanDestinationDir", false) v.SetDefault("watch", false) v.SetDefault("metaDataFormat", "toml") |