"sync"
"time"
+ "github.com/gohugoio/hugo/config"
+
"github.com/spf13/cobra"
"github.com/gohugoio/hugo/utils"
// Used in cases where we get flooded with events in server mode.
debounce func(f func())
- serverPorts []int
- languages helpers.Languages
+ serverPorts []int
+ languagesConfigured bool
+ languages helpers.Languages
configured bool
}
sourceFs = c.DepsCfg.Fs.Source
}
- config, configFiles, err := hugolib.LoadConfig(hugolib.ConfigSourceDescriptor{Fs: sourceFs, Path: source, WorkingDir: dir, Filename: cfgFile})
- if err != nil {
- return err
- }
+ doWithConfig := func(cfg config.Provider) error {
+ for _, cmdV := range c.subCmdVs {
+ initializeFlags(cmdV, cfg)
+ }
- c.Cfg = config
- c.configFiles = configFiles
+ if baseURL != "" {
+ cfg.Set("baseURL", baseURL)
+ }
- for _, cmdV := range c.subCmdVs {
- c.initializeFlags(cmdV)
- }
+ if len(disableKinds) > 0 {
+ cfg.Set("disableKinds", disableKinds)
+ }
- if l, ok := c.Cfg.Get("languagesSorted").(helpers.Languages); ok {
- c.languages = l
- }
+ cfg.Set("logI18nWarnings", logI18nWarnings)
- if baseURL != "" {
- config.Set("baseURL", baseURL)
- }
+ if theme != "" {
+ cfg.Set("theme", theme)
+ }
- if c.doWithCommandeer != nil {
- err = c.doWithCommandeer(c)
- }
+ if themesDir != "" {
+ cfg.Set("themesDir", themesDir)
+ }
- if err != nil {
- return err
- }
+ if destination != "" {
+ cfg.Set("publishDir", destination)
+ }
- if len(disableKinds) > 0 {
- c.Set("disableKinds", disableKinds)
- }
+ cfg.Set("workingDir", dir)
- logger, err := createLogger(cfg.Cfg)
- if err != nil {
- return err
- }
+ if contentDir != "" {
+ cfg.Set("contentDir", contentDir)
+ }
- cfg.Logger = logger
+ if layoutDir != "" {
+ cfg.Set("layoutDir", layoutDir)
+ }
- config.Set("logI18nWarnings", logI18nWarnings)
+ if cacheDir != "" {
+ cfg.Set("cacheDir", cacheDir)
+ }
- if theme != "" {
- config.Set("theme", theme)
+ return nil
}
- if themesDir != "" {
- config.Set("themesDir", themesDir)
+ doWithCommandeer := func(cfg config.Provider) error {
+ c.Cfg = cfg
+ if c.doWithCommandeer == nil {
+ return nil
+ }
+ err := c.doWithCommandeer(c)
+ return err
}
- if destination != "" {
- config.Set("publishDir", destination)
+ config, configFiles, err := hugolib.LoadConfig(
+ hugolib.ConfigSourceDescriptor{Fs: sourceFs, Path: source, WorkingDir: dir, Filename: cfgFile},
+ doWithCommandeer,
+ doWithConfig)
+
+ if err != nil {
+ return err
}
- config.Set("workingDir", dir)
+ c.configFiles = configFiles
- if contentDir != "" {
- config.Set("contentDir", contentDir)
+ if l, ok := c.Cfg.Get("languagesSorted").(helpers.Languages); ok {
+ c.languagesConfigured = true
+ c.languages = l
}
- if layoutDir != "" {
- config.Set("layoutDir", layoutDir)
+ // This is potentially double work, but we need to do this one more time now
+ // that all the languages have been configured.
+ if c.doWithCommandeer != nil {
+ if err := c.doWithCommandeer(c); err != nil {
+ return err
+ }
}
- if cacheDir != "" {
- config.Set("cacheDir", cacheDir)
+ logger, err := createLogger(config)
+ if err != nil {
+ return err
}
+ cfg.Logger = logger
+
createMemFs := config.GetBool("renderToMemory")
if createMemFs {
return jww.NewNotepad(stdoutThreshold, logThreshold, outHandle, logHandle, "", log.Ldate|log.Ltime), nil
}
-func (c *commandeer) initializeFlags(cmd *cobra.Command) {
+func initializeFlags(cmd *cobra.Command, cfg config.Provider) {
persFlagKeys := []string{"debug", "verbose", "logFile"}
flagKeys := []string{
"cleanDestinationDir",
}
for _, key := range persFlagKeys {
- c.setValueFromFlag(cmd.PersistentFlags(), key)
+ setValueFromFlag(cmd.PersistentFlags(), key, cfg)
}
for _, key := range flagKeys {
- c.setValueFromFlag(cmd.Flags(), key)
+ setValueFromFlag(cmd.Flags(), key, cfg)
}
}
strings.ToLower("canonifyURLs"): true,
}
-func (c *commandeer) setValueFromFlag(flags *flag.FlagSet, key string) {
+func setValueFromFlag(flags *flag.FlagSet, key string, cfg config.Provider) {
if flags.Changed(key) {
if _, deprecated := deprecatedFlags[strings.ToLower(key)]; deprecated {
msg := fmt.Sprintf(`Set "%s = true" in your config.toml.
helpers.Deprecated("hugo", "--"+key+" flag", msg, true)
}
f := flags.Lookup(key)
- c.Set(key, f.Value.String())
+ cfg.Set(key, f.Value.String())
}
}
// LoadConfig loads Hugo configuration into a new Viper and then adds
// a set of defaults.
-func LoadConfig(d ConfigSourceDescriptor) (*viper.Viper, []string, error) {
+func LoadConfig(d ConfigSourceDescriptor, doWithConfig ...func(cfg config.Provider) error) (*viper.Viper, []string, error) {
var configFiles []string
fs := d.Fs
configFiles = append(configFiles, themeConfigFile)
}
+ // We create languages based on the settings, so we need to make sure that
+ // all configuration is loaded/set before doing that.
+ for _, d := range doWithConfig {
+ if err := d(v); err != nil {
+ return v, configFiles, err
+ }
+ }
+
if err := loadLanguageSettings(v, nil); err != nil {
return v, configFiles, err
}