Skip to content

Commit

Permalink
commands: Correctly handle destination and i18n-warnings
Browse files Browse the repository at this point in the history
And add some more CLI tests.

See #4607
  • Loading branch information
bep committed Apr 14, 2018
1 parent 2aab6de commit bede93d
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 6 deletions.
45 changes: 44 additions & 1 deletion commands/commands_test.go
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"testing" "testing"


"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper"


"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
Expand Down Expand Up @@ -53,15 +54,57 @@ func TestCommandsPersistentFlags(t *testing.T) {
tests := []struct { tests := []struct {
args []string args []string
check func(command []cmder) check func(command []cmder)
}{{[]string{"server", "--config=myconfig.toml", "-b=https://example.com/b/", "--source=mysource"}, func(commands []cmder) { }{{[]string{"server",
"--config=myconfig.toml",
"--contentDir=mycontent",
"--layoutDir=mylayouts",
"--theme=mytheme",
"--themesDir=mythemes",
"--cleanDestinationDir",
"--navigateToChanged",
"--disableLiveReload",
"--noHTTPCache",
"--i18n-warnings",
"--destination=/tmp/mydestination",
"-b=https://example.com/b/",
"--port=1366",
"--renderToDisk",
"--source=mysource",
"--uglyURLs"}, func(commands []cmder) {
var sc *serverCmd
for _, command := range commands { for _, command := range commands {
if b, ok := command.(commandsBuilderGetter); ok { if b, ok := command.(commandsBuilderGetter); ok {
v := b.getCmmandsBuilder().hugoBuilderCommon v := b.getCmmandsBuilder().hugoBuilderCommon
assert.Equal("myconfig.toml", v.cfgFile) assert.Equal("myconfig.toml", v.cfgFile)
assert.Equal("mysource", v.source) assert.Equal("mysource", v.source)
assert.Equal("https://example.com/b/", v.baseURL) assert.Equal("https://example.com/b/", v.baseURL)
} }

if srvCmd, ok := command.(*serverCmd); ok {
sc = srvCmd
}
} }

assert.NotNil(sc)
assert.True(sc.navigateToChanged)
assert.True(sc.disableLiveReload)
assert.True(sc.noHTTPCache)
assert.True(sc.renderToDisk)
assert.Equal(1366, sc.serverPort)

cfg := viper.New()
sc.flagsToConfig(cfg)
assert.Equal("/tmp/mydestination", cfg.GetString("publishDir"))
assert.Equal("mycontent", cfg.GetString("contentDir"))
assert.Equal("mylayouts", cfg.GetString("layoutDir"))
assert.Equal("mytheme", cfg.GetString("theme"))
assert.Equal("mythemes", cfg.GetString("themesDir"))

assert.True(cfg.GetBool("uglyURLs"))

// The flag is named i18n-warnings
assert.True(cfg.GetBool("logI18nWarnings"))

}}} }}}


for _, test := range tests { for _, test := range tests {
Expand Down
18 changes: 13 additions & 5 deletions commands/hugo.go
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ func initializeFlags(cmd *cobra.Command, cfg config.Provider) {
"gc", "gc",
"layoutDir", "layoutDir",
"logFile", "logFile",
"logI18nWarnings", "i18n-warnings",
"quiet", "quiet",
"renderToMemory", "renderToMemory",
"source", "source",
Expand All @@ -211,12 +211,16 @@ func initializeFlags(cmd *cobra.Command, cfg config.Provider) {
} }


for _, key := range persFlagKeys { for _, key := range persFlagKeys {
setValueFromFlag(cmd.PersistentFlags(), key, cfg) setValueFromFlag(cmd.PersistentFlags(), key, cfg, "")
} }
for _, key := range flagKeys { for _, key := range flagKeys {
setValueFromFlag(cmd.Flags(), key, cfg) setValueFromFlag(cmd.Flags(), key, cfg, "")
} }


// Set some "config aliases"
setValueFromFlag(cmd.Flags(), "destination", cfg, "publishDir")
setValueFromFlag(cmd.Flags(), "i18n-warnings", cfg, "logI18nWarnings")

} }


var deprecatedFlags = map[string]bool{ var deprecatedFlags = map[string]bool{
Expand All @@ -226,7 +230,7 @@ var deprecatedFlags = map[string]bool{
strings.ToLower("canonifyURLs"): true, strings.ToLower("canonifyURLs"): true,
} }


func setValueFromFlag(flags *flag.FlagSet, key string, cfg config.Provider) { func setValueFromFlag(flags *flag.FlagSet, key string, cfg config.Provider, targetKey string) {
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.
Expand All @@ -235,7 +239,11 @@ 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)
cfg.Set(key, f.Value.String()) configKey := key
if targetKey != "" {
configKey = targetKey
}
cfg.Set(configKey, f.Value.String())
} }
} }


Expand Down

0 comments on commit bede93d

Please sign in to comment.