mirror of
https://github.com/gohugoio/hugo.git
synced 2024-11-07 20:30:36 -05:00
Fix handling of --contentDir etc. flag
We need to revisit the commands package re globals and tests, but this should fix the init order of flags and languages. Fixes #4589
This commit is contained in:
parent
094ec17142
commit
080302eb87
4 changed files with 89 additions and 56 deletions
|
@ -19,6 +19,8 @@ import (
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/gohugoio/hugo/config"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
|
||||||
"github.com/gohugoio/hugo/utils"
|
"github.com/gohugoio/hugo/utils"
|
||||||
|
@ -56,8 +58,9 @@ type commandeer struct {
|
||||||
// Used in cases where we get flooded with events in server mode.
|
// Used in cases where we get flooded with events in server mode.
|
||||||
debounce func(f func())
|
debounce func(f func())
|
||||||
|
|
||||||
serverPorts []int
|
serverPorts []int
|
||||||
languages helpers.Languages
|
languagesConfigured bool
|
||||||
|
languages helpers.Languages
|
||||||
|
|
||||||
configured bool
|
configured bool
|
||||||
}
|
}
|
||||||
|
@ -135,73 +138,90 @@ func (c *commandeer) loadConfig(running bool) error {
|
||||||
sourceFs = c.DepsCfg.Fs.Source
|
sourceFs = c.DepsCfg.Fs.Source
|
||||||
}
|
}
|
||||||
|
|
||||||
config, configFiles, err := hugolib.LoadConfig(hugolib.ConfigSourceDescriptor{Fs: sourceFs, Path: source, WorkingDir: dir, Filename: cfgFile})
|
doWithConfig := func(cfg config.Provider) error {
|
||||||
|
for _, cmdV := range c.subCmdVs {
|
||||||
|
initializeFlags(cmdV, cfg)
|
||||||
|
}
|
||||||
|
|
||||||
|
if baseURL != "" {
|
||||||
|
cfg.Set("baseURL", baseURL)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(disableKinds) > 0 {
|
||||||
|
cfg.Set("disableKinds", disableKinds)
|
||||||
|
}
|
||||||
|
|
||||||
|
cfg.Set("logI18nWarnings", logI18nWarnings)
|
||||||
|
|
||||||
|
if theme != "" {
|
||||||
|
cfg.Set("theme", theme)
|
||||||
|
}
|
||||||
|
|
||||||
|
if themesDir != "" {
|
||||||
|
cfg.Set("themesDir", themesDir)
|
||||||
|
}
|
||||||
|
|
||||||
|
if destination != "" {
|
||||||
|
cfg.Set("publishDir", destination)
|
||||||
|
}
|
||||||
|
|
||||||
|
cfg.Set("workingDir", dir)
|
||||||
|
|
||||||
|
if contentDir != "" {
|
||||||
|
cfg.Set("contentDir", contentDir)
|
||||||
|
}
|
||||||
|
|
||||||
|
if layoutDir != "" {
|
||||||
|
cfg.Set("layoutDir", layoutDir)
|
||||||
|
}
|
||||||
|
|
||||||
|
if cacheDir != "" {
|
||||||
|
cfg.Set("cacheDir", cacheDir)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
doWithCommandeer := func(cfg config.Provider) error {
|
||||||
|
c.Cfg = cfg
|
||||||
|
if c.doWithCommandeer == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
err := c.doWithCommandeer(c)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
config, configFiles, err := hugolib.LoadConfig(
|
||||||
|
hugolib.ConfigSourceDescriptor{Fs: sourceFs, Path: source, WorkingDir: dir, Filename: cfgFile},
|
||||||
|
doWithCommandeer,
|
||||||
|
doWithConfig)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
c.Cfg = config
|
|
||||||
c.configFiles = configFiles
|
c.configFiles = configFiles
|
||||||
|
|
||||||
for _, cmdV := range c.subCmdVs {
|
|
||||||
c.initializeFlags(cmdV)
|
|
||||||
}
|
|
||||||
|
|
||||||
if l, ok := c.Cfg.Get("languagesSorted").(helpers.Languages); ok {
|
if l, ok := c.Cfg.Get("languagesSorted").(helpers.Languages); ok {
|
||||||
|
c.languagesConfigured = true
|
||||||
c.languages = l
|
c.languages = l
|
||||||
}
|
}
|
||||||
|
|
||||||
if baseURL != "" {
|
// This is potentially double work, but we need to do this one more time now
|
||||||
config.Set("baseURL", baseURL)
|
// that all the languages have been configured.
|
||||||
}
|
|
||||||
|
|
||||||
if c.doWithCommandeer != nil {
|
if c.doWithCommandeer != nil {
|
||||||
err = c.doWithCommandeer(c)
|
if err := c.doWithCommandeer(c); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if err != nil {
|
logger, err := createLogger(config)
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(disableKinds) > 0 {
|
|
||||||
c.Set("disableKinds", disableKinds)
|
|
||||||
}
|
|
||||||
|
|
||||||
logger, err := createLogger(cfg.Cfg)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
cfg.Logger = logger
|
cfg.Logger = logger
|
||||||
|
|
||||||
config.Set("logI18nWarnings", logI18nWarnings)
|
|
||||||
|
|
||||||
if theme != "" {
|
|
||||||
config.Set("theme", theme)
|
|
||||||
}
|
|
||||||
|
|
||||||
if themesDir != "" {
|
|
||||||
config.Set("themesDir", themesDir)
|
|
||||||
}
|
|
||||||
|
|
||||||
if destination != "" {
|
|
||||||
config.Set("publishDir", destination)
|
|
||||||
}
|
|
||||||
|
|
||||||
config.Set("workingDir", dir)
|
|
||||||
|
|
||||||
if contentDir != "" {
|
|
||||||
config.Set("contentDir", contentDir)
|
|
||||||
}
|
|
||||||
|
|
||||||
if layoutDir != "" {
|
|
||||||
config.Set("layoutDir", layoutDir)
|
|
||||||
}
|
|
||||||
|
|
||||||
if cacheDir != "" {
|
|
||||||
config.Set("cacheDir", cacheDir)
|
|
||||||
}
|
|
||||||
|
|
||||||
createMemFs := config.GetBool("renderToMemory")
|
createMemFs := config.GetBool("renderToMemory")
|
||||||
|
|
||||||
if createMemFs {
|
if createMemFs {
|
||||||
|
|
|
@ -348,7 +348,7 @@ func createLogger(cfg config.Provider) (*jww.Notepad, error) {
|
||||||
return jww.NewNotepad(stdoutThreshold, logThreshold, outHandle, logHandle, "", log.Ldate|log.Ltime), nil
|
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"}
|
persFlagKeys := []string{"debug", "verbose", "logFile"}
|
||||||
flagKeys := []string{
|
flagKeys := []string{
|
||||||
"cleanDestinationDir",
|
"cleanDestinationDir",
|
||||||
|
@ -370,10 +370,10 @@ func (c *commandeer) initializeFlags(cmd *cobra.Command) {
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, key := range persFlagKeys {
|
for _, key := range persFlagKeys {
|
||||||
c.setValueFromFlag(cmd.PersistentFlags(), key)
|
setValueFromFlag(cmd.PersistentFlags(), key, cfg)
|
||||||
}
|
}
|
||||||
for _, key := range flagKeys {
|
for _, key := range flagKeys {
|
||||||
c.setValueFromFlag(cmd.Flags(), key)
|
setValueFromFlag(cmd.Flags(), key, cfg)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -385,7 +385,7 @@ var deprecatedFlags = map[string]bool{
|
||||||
strings.ToLower("canonifyURLs"): true,
|
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 flags.Changed(key) {
|
||||||
if _, deprecated := deprecatedFlags[strings.ToLower(key)]; deprecated {
|
if _, deprecated := deprecatedFlags[strings.ToLower(key)]; deprecated {
|
||||||
msg := fmt.Sprintf(`Set "%s = true" in your config.toml.
|
msg := fmt.Sprintf(`Set "%s = true" in your config.toml.
|
||||||
|
@ -394,7 +394,7 @@ If you need to set this configuration value from the command line, set it via an
|
||||||
helpers.Deprecated("hugo", "--"+key+" flag", msg, true)
|
helpers.Deprecated("hugo", "--"+key+" flag", msg, true)
|
||||||
}
|
}
|
||||||
f := flags.Lookup(key)
|
f := flags.Lookup(key)
|
||||||
c.Set(key, f.Value.String())
|
cfg.Set(key, f.Value.String())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -137,6 +137,11 @@ func server(cmd *cobra.Command, args []string) error {
|
||||||
c.Set("watch", true)
|
c.Set("watch", true)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO(bep) yes, we should fix.
|
||||||
|
if !c.languagesConfigured {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
// We can only do this once.
|
// We can only do this once.
|
||||||
|
|
|
@ -54,7 +54,7 @@ func LoadConfigDefault(fs afero.Fs) (*viper.Viper, error) {
|
||||||
|
|
||||||
// LoadConfig loads Hugo configuration into a new Viper and then adds
|
// LoadConfig loads Hugo configuration into a new Viper and then adds
|
||||||
// a set of defaults.
|
// 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
|
var configFiles []string
|
||||||
|
|
||||||
fs := d.Fs
|
fs := d.Fs
|
||||||
|
@ -108,6 +108,14 @@ func LoadConfig(d ConfigSourceDescriptor) (*viper.Viper, []string, error) {
|
||||||
configFiles = append(configFiles, themeConfigFile)
|
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 {
|
if err := loadLanguageSettings(v, nil); err != nil {
|
||||||
return v, configFiles, err
|
return v, configFiles, err
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue