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:
Bjørn Erik Pedersen 2018-04-07 11:27:22 +02:00
parent 094ec17142
commit 080302eb87
No known key found for this signature in database
GPG key ID: 330E6E2BD4859D8F
4 changed files with 89 additions and 56 deletions

View file

@ -348,7 +348,7 @@ func createLogger(cfg config.Provider) (*jww.Notepad, error) {
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",
@ -370,10 +370,10 @@ func (c *commandeer) initializeFlags(cmd *cobra.Command) {
}
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)
}
}
@ -385,7 +385,7 @@ var deprecatedFlags = map[string]bool{
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.
@ -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)
}
f := flags.Lookup(key)
c.Set(key, f.Value.String())
cfg.Set(key, f.Value.String())
}
}