Skip to content

Commit

Permalink
commands: Add --lang to hugo config
Browse files Browse the repository at this point in the history
Fixes #11057
  • Loading branch information
bep committed Jun 1, 2023
1 parent e3ae8f0 commit 0ef2952
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 5 deletions.
14 changes: 13 additions & 1 deletion commands/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"time"

"github.com/bep/simplecobra"
"github.com/gohugoio/hugo/config/allconfig"
"github.com/gohugoio/hugo/modules"
"github.com/gohugoio/hugo/parser"
"github.com/gohugoio/hugo/parser/metadecoders"
Expand All @@ -42,6 +43,7 @@ type configCommand struct {
r *rootCommand

format string
lang string

commands []simplecobra.Commander
}
Expand All @@ -59,7 +61,16 @@ func (c *configCommand) Run(ctx context.Context, cd *simplecobra.Commandeer, arg
if err != nil {
return err
}
config := conf.configs.Base
var config *allconfig.Config
if c.lang != "" {
var found bool
config, found = conf.configs.LanguageConfigMap[c.lang]
if !found {
return fmt.Errorf("language %q not found", c.lang)
}
} else {
config = conf.configs.LanguageConfigSlice[0]
}

var buf bytes.Buffer
dec := json.NewEncoder(&buf)
Expand Down Expand Up @@ -100,6 +111,7 @@ func (c *configCommand) Init(cd *simplecobra.Commandeer) error {
cmd.Short = "Print the site configuration"
cmd.Long = `Print the site configuration, both default and custom settings.`
cmd.Flags().StringVar(&c.format, "format", "toml", "preferred file format (toml, yaml or json)")
cmd.Flags().StringVar(&c.lang, "lang", "", "the language to display config for. Defaults to the first language defined.")
applyLocalFlagsBuildConfig(cmd, c.r)

return nil
Expand Down
5 changes: 5 additions & 0 deletions config/allconfig/allconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,11 @@ func (c *Configs) Init() error {
}
}

// Transfer the changed mounts to the language versions (all share the same mount set, but can be displayed in different languages).
for _, l := range c.LanguageConfigSlice {
l.Module.Mounts = c.Base.Module.Mounts
}

return nil
}

Expand Down
2 changes: 1 addition & 1 deletion config/allconfig/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ Title: {{ .Title }}

b.Assert(enConcp.BaseURL().String(), qt.Equals, "https://example.com")
modConf := enConf.Module
b.Assert(modConf.Mounts, qt.HasLen, 2)
b.Assert(modConf.Mounts, qt.HasLen, 8)
b.Assert(modConf.Mounts[0].Source, qt.Equals, filepath.FromSlash("content/en"))
b.Assert(modConf.Mounts[0].Target, qt.Equals, "content")
b.Assert(modConf.Mounts[0].Lang, qt.Equals, "en")
Expand Down
5 changes: 2 additions & 3 deletions hugolib/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1172,8 +1172,7 @@ Home.
},
).Build()

modConf := b.H.Configs.Base.Module

b.Assert(modConf.Mounts, qt.HasLen, 7)
b.Assert(b.H.Configs.Base.Module.Mounts, qt.HasLen, 7)
b.Assert(b.H.Configs.LanguageConfigSlice[0].Module.Mounts, qt.HasLen, 7)

}

0 comments on commit 0ef2952

Please sign in to comment.