shithub: hugo

Download patch

ref: 0f9f73cce5c3f1f05be20bcf1d23b2332623d7f9
parent: c8257f8b726478ca70dc8984cdcc17b31e4bdc0c
author: Johan Gielstra <[email protected]>
date: Wed Aug 9 09:13:02 EDT 2017

Add support for multiple config files via --config a.toml,b.toml,c.toml



--- a/hugolib/config.go
+++ b/hugolib/config.go
@@ -19,6 +19,8 @@
 	"github.com/gohugoio/hugo/helpers"
 	"github.com/spf13/afero"
 	"github.com/spf13/viper"
+	"io"
+	"strings"
 )
 
 // LoadConfig loads Hugo configuration into a new Viper and then adds
@@ -29,10 +31,10 @@
 	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(".")
@@ -45,6 +47,16 @@
 			return nil, err
 		}
 		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")
--- a/hugolib/config_test.go
+++ b/hugolib/config_test.go
@@ -41,3 +41,27 @@
 	// 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"))
+}