Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for multiple config files via --config a.toml,b.toml,c.toml #3532

Merged
merged 2 commits into from
Aug 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions hugolib/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (
"github.com/spf13/afero"
"github.com/spf13/hugo/helpers"
"github.com/spf13/viper"
"io"
"strings"
)

// LoadConfig loads Hugo configuration into a new Viper and then adds
Expand All @@ -29,10 +31,10 @@ func LoadConfig(fs afero.Fs, relativeSourcePath, configFilename string) (*viper.
if relativeSourcePath == "" {
relativeSourcePath = "."
}

configFilenames := strings.Split(configFilename, ",")
v.AutomaticEnv()
v.SetEnvPrefix("hugo")
v.SetConfigFile(configFilename)
v.SetConfigFile(configFilenames[0])
// See https://github.com/spf13/viper/issues/73#issuecomment-126970794
if relativeSourcePath == "" {
v.AddConfigPath(".")
Expand All @@ -46,6 +48,16 @@ func LoadConfig(fs afero.Fs, relativeSourcePath, configFilename string) (*viper.
}
return nil, fmt.Errorf("Unable to locate Config file. Perhaps you need to create a new site.\n Run `hugo help new` for details. (%s)\n", err)
}
for _, configFile := range configFilenames[1:] {
var r io.Reader
var err error
if r, err = fs.Open(configFile); err != nil {
return nil, fmt.Errorf("Unable to open Config file.\n (%s)\n", err)
}
if err = v.MergeConfig(r); err != nil {
return nil, fmt.Errorf("Unable to parse/merge Config file (%s).\n (%s)\n", configFile, err)
}
}

v.RegisterAlias("indexes", "taxonomies")

Expand Down
24 changes: 24 additions & 0 deletions hugolib/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,27 @@ func TestLoadConfig(t *testing.T) {
// default
assert.Equal(t, "layouts", cfg.GetString("layoutDir"))
}
func TestLoadMultiConfig(t *testing.T) {
t.Parallel()

// Add a random config variable for testing.
// side = page in Norwegian.
configContentBase := `
DontChange = "same"
PaginatePath = "side"
`
configContentSub := `
PaginatePath = "top"
`
mm := afero.NewMemMapFs()

writeToFs(t, mm, "base.toml", configContentBase)

writeToFs(t, mm, "override.toml", configContentSub)

cfg, err := LoadConfig(mm, "", "base.toml,override.toml")
require.NoError(t, err)

assert.Equal(t, "top", cfg.GetString("paginatePath"))
assert.Equal(t, "same", cfg.GetString("DontChange"))
}