Skip to content

Commit 0f9f73c

Browse files
jgielstrabep
authored andcommitted
Add support for multiple config files via --config a.toml,b.toml,c.toml
1 parent c8257f8 commit 0f9f73c

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

‎hugolib/config.go‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import (
1919
"github.com/gohugoio/hugo/helpers"
2020
"github.com/spf13/afero"
2121
"github.com/spf13/viper"
22+
"io"
23+
"strings"
2224
)
2325

2426
// LoadConfig loads Hugo configuration into a new Viper and then adds
@@ -29,10 +31,10 @@ func LoadConfig(fs afero.Fs, relativeSourcePath, configFilename string) (*viper.
2931
if relativeSourcePath == "" {
3032
relativeSourcePath = "."
3133
}
32-
34+
configFilenames := strings.Split(configFilename, ",")
3335
v.AutomaticEnv()
3436
v.SetEnvPrefix("hugo")
35-
v.SetConfigFile(configFilename)
37+
v.SetConfigFile(configFilenames[0])
3638
// See https://github.com/spf13/viper/issues/73#issuecomment-126970794
3739
if relativeSourcePath == "" {
3840
v.AddConfigPath(".")
@@ -46,6 +48,16 @@ func LoadConfig(fs afero.Fs, relativeSourcePath, configFilename string) (*viper.
4648
}
4749
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)
4850
}
51+
for _, configFile := range configFilenames[1:] {
52+
var r io.Reader
53+
var err error
54+
if r, err = fs.Open(configFile); err != nil {
55+
return nil, fmt.Errorf("Unable to open Config file.\n (%s)\n", err)
56+
}
57+
if err = v.MergeConfig(r); err != nil {
58+
return nil, fmt.Errorf("Unable to parse/merge Config file (%s).\n (%s)\n", configFile, err)
59+
}
60+
}
4961

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

‎hugolib/config_test.go‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,27 @@ func TestLoadConfig(t *testing.T) {
4141
// default
4242
assert.Equal(t, "layouts", cfg.GetString("layoutDir"))
4343
}
44+
func TestLoadMultiConfig(t *testing.T) {
45+
t.Parallel()
46+
47+
// Add a random config variable for testing.
48+
// side = page in Norwegian.
49+
configContentBase := `
50+
DontChange = "same"
51+
PaginatePath = "side"
52+
`
53+
configContentSub := `
54+
PaginatePath = "top"
55+
`
56+
mm := afero.NewMemMapFs()
57+
58+
writeToFs(t, mm, "base.toml", configContentBase)
59+
60+
writeToFs(t, mm, "override.toml", configContentSub)
61+
62+
cfg, err := LoadConfig(mm, "", "base.toml,override.toml")
63+
require.NoError(t, err)
64+
65+
assert.Equal(t, "top", cfg.GetString("paginatePath"))
66+
assert.Equal(t, "same", cfg.GetString("DontChange"))
67+
}

0 commit comments

Comments
 (0)