summaryrefslogtreecommitdiffstats
path: root/modules
diff options
context:
space:
mode:
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2021-06-16 19:11:01 +0200
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2021-06-18 12:54:30 +0200
commitbb2aa08709c812a5be29922a1a7f4d814e200cab (patch)
treea709117fe1a882b0179e41db0d72b103f53a4f6a /modules
parent9096842b0494166e401cc08a70b93ae2ee19a198 (diff)
Implement configuration in a directory for modules
Fixes #8654
Diffstat (limited to 'modules')
-rw-r--r--modules/client.go3
-rw-r--r--modules/collect.go35
-rw-r--r--modules/module.go14
3 files changed, 36 insertions, 16 deletions
diff --git a/modules/client.go b/modules/client.go
index 571ece15e..73c3242a8 100644
--- a/modules/client.go
+++ b/modules/client.go
@@ -653,6 +653,9 @@ type ClientConfig struct {
// Absolute path to the project's themes dir.
ThemesDir string
+ // Eg. "production"
+ Environment string
+
CacheDir string // Module cache
ModuleConfig Config
}
diff --git a/modules/collect.go b/modules/collect.go
index 163eda74a..52d75af59 100644
--- a/modules/collect.go
+++ b/modules/collect.go
@@ -396,17 +396,16 @@ func (c *collector) applyMounts(moduleImport Import, mod *moduleAdapter) error {
func (c *collector) applyThemeConfig(tc *moduleAdapter) error {
var (
configFilename string
- cfg config.Provider
themeCfg map[string]interface{}
- hasConfig bool
+ hasConfigFile bool
err error
)
// Viper supports more, but this is the sub-set supported by Hugo.
for _, configFormats := range config.ValidConfigFileExtensions {
configFilename = filepath.Join(tc.Dir(), "config."+configFormats)
- hasConfig, _ = afero.Exists(c.fs, configFilename)
- if hasConfig {
+ hasConfigFile, _ = afero.Exists(c.fs, configFilename)
+ if hasConfigFile {
break
}
}
@@ -428,20 +427,38 @@ func (c *collector) applyThemeConfig(tc *moduleAdapter) error {
}
}
- if hasConfig {
+ if hasConfigFile {
if configFilename != "" {
var err error
- cfg, err = config.FromFile(c.fs, configFilename)
+ tc.cfg, err = config.FromFile(c.fs, configFilename)
if err != nil {
return errors.Wrapf(err, "failed to read module config for %q in %q", tc.Path(), configFilename)
}
}
- tc.configFilename = configFilename
- tc.cfg = cfg
+ tc.configFilenames = append(tc.configFilenames, configFilename)
+
+ }
+
+ // Also check for a config dir, which we overlay on top of the file configuration.
+ configDir := filepath.Join(tc.Dir(), "config")
+ dcfg, dirnames, err := config.LoadConfigFromDir(c.fs, configDir, c.ccfg.Environment)
+ if err != nil {
+ return err
+ }
+
+ if len(dirnames) > 0 {
+ tc.configFilenames = append(tc.configFilenames, dirnames...)
+
+ if hasConfigFile {
+ // Set will overwrite existing keys.
+ tc.cfg.Set("", dcfg.Get(""))
+ } else {
+ tc.cfg = dcfg
+ }
}
- config, err := decodeConfig(cfg, c.moduleConfig.replacementsMap)
+ config, err := decodeConfig(tc.cfg, c.moduleConfig.replacementsMap)
if err != nil {
return err
}
diff --git a/modules/module.go b/modules/module.go
index a5f707635..c3343c820 100644
--- a/modules/module.go
+++ b/modules/module.go
@@ -30,10 +30,10 @@ type Module interface {
// The decoded module config and mounts.
Config() Config
- // Optional configuration filename (e.g. "/themes/mytheme/config.json").
+ // Optional configuration filenames (e.g. "/themes/mytheme/config.json").
// This will be added to the special configuration watch list when in
// server mode.
- ConfigFilename() string
+ ConfigFilenames() []string
// Directory holding files for this module.
Dir() string
@@ -82,9 +82,9 @@ type moduleAdapter struct {
mounts []Mount
- configFilename string
- cfg config.Provider
- config Config
+ configFilenames []string
+ cfg config.Provider
+ config Config
// Set if a Go module.
gomod *goModule
@@ -98,8 +98,8 @@ func (m *moduleAdapter) Config() Config {
return m.config
}
-func (m *moduleAdapter) ConfigFilename() string {
- return m.configFilename
+func (m *moduleAdapter) ConfigFilenames() []string {
+ return m.configFilenames
}
func (m *moduleAdapter) Dir() string {