From bb2aa08709c812a5be29922a1a7f4d814e200cab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Wed, 16 Jun 2021 19:11:01 +0200 Subject: Implement configuration in a directory for modules Fixes #8654 --- modules/collect.go | 35 ++++++++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 9 deletions(-) (limited to 'modules/collect.go') 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 } -- cgit v1.2.3