Skip to content

Commit

Permalink
commands: Load config before creating the filesystem
Browse files Browse the repository at this point in the history
To allow publishDir to be set in config file.
  • Loading branch information
bep committed May 18, 2023
1 parent 834b3d7 commit 3f00f47
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 24 deletions.
46 changes: 26 additions & 20 deletions commands/commandeer.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,17 +201,37 @@ func (r *rootCommand) ConfigFromProvider(key int32, cfg config.Provider) (*commo
if cfg == nil {
cfg = config.New()
}
if !cfg.IsSet("publishDir") {
cfg.Set("publishDir", "public")
}

if !cfg.IsSet("renderToDisk") {
cfg.Set("renderToDisk", true)
}
if !cfg.IsSet("workingDir") {
cfg.Set("workingDir", dir)
} else {
if err := os.MkdirAll(cfg.GetString("workingDir"), 0777); err != nil {
return nil, fmt.Errorf("failed to create workingDir: %w", err)
}
}

// Load the config first to allow publishDir to be configured in config file.
configs, err := allconfig.LoadConfig(
allconfig.ConfigSourceDescriptor{
Flags: cfg,
Fs: hugofs.Os,
Filename: r.cfgFile,
ConfigDir: r.cfgDir,
Environment: r.environment,
},
)
if err != nil {
return nil, err
}
cfg.Set("publishDirStatic", cfg.Get("publishDir"))
cfg.Set("publishDirDynamic", cfg.Get("publishDir"))

base := configs.Base

cfg.Set("publishDir", base.PublishDir)
cfg.Set("publishDirStatic", base.PublishDir)
cfg.Set("publishDirDynamic", base.PublishDir)

renderStaticToDisk := cfg.GetBool("renderStaticToDisk")

Expand Down Expand Up @@ -257,21 +277,6 @@ func (r *rootCommand) ConfigFromProvider(key int32, cfg config.Provider) (*commo

}

configs, err := allconfig.LoadConfig(
allconfig.ConfigSourceDescriptor{
Flags: cfg,
Fs: fs.Source,
Filename: r.cfgFile,
ConfigDir: r.cfgDir,
Environment: r.environment,
},
)
if err != nil {
return nil, err
}

base := configs.Base

if !base.C.Clock.IsZero() {
// TODO(bep) find a better place for this.
htime.Clock = clock.Start(configs.Base.C.Clock)
Expand Down Expand Up @@ -300,6 +305,7 @@ func (r *rootCommand) HugFromConfig(conf *commonConfig) (*hugolib.HugoSites, err
h, _, err := r.hugoSites.GetOrCreate(r.configVersionID.Load(), func(key int32) (*hugolib.HugoSites, error) {
conf.mu.Lock()
defer conf.mu.Unlock()

depsCfg := deps.DepsCfg{Configs: conf.configs, Fs: conf.fs, Logger: r.logger}
return hugolib.NewHugoSites(depsCfg)
})
Expand Down
1 change: 1 addition & 0 deletions commands/hugobuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -954,6 +954,7 @@ func (c *hugoBuilder) loadConfig(cd *simplecobra.Commandeer, running bool) error
if err != nil {
return err
}

c.setConf(conf)
if c.onConfigLoaded != nil {
if err := c.onConfigLoaded(false); err != nil {
Expand Down
5 changes: 3 additions & 2 deletions config/allconfig/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,9 @@ func (l configLoader) applyDefaultConfig() error {
}

func (l configLoader) normalizeCfg(cfg config.Provider) error {
minify := cfg.Get("minify")
if b, ok := minify.(bool); ok && b {
if b, ok := cfg.Get("minifyOutput").(bool); ok && b {
cfg.Set("minify.minifyOutput", true)
} else if b, ok := cfg.Get("minify").(bool); ok && b {
cfg.Set("minify", maps.Params{"minifyOutput": true})
}

Expand Down
2 changes: 1 addition & 1 deletion modules/collect.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ func (c *collector) addAndRecurse(owner *moduleAdapter, disabled bool) error {
moduleConfig := owner.Config()
if owner.projectMod {
if err := c.applyMounts(Import{}, owner); err != nil {
return fmt.Errorf("failed to apply mounts for project module: %w", err)
return fmt.Errorf("failed to apply mounts for project: %w", err)
}
}

Expand Down
6 changes: 5 additions & 1 deletion testscripts/commands/hugo__flags.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
# Test the hugo command.

hugo --baseURL http://example.com/ --destination ${WORK}/newpublic --clock 2021-11-06T22:30:00.00+09:00 -e staging --config ${WORK}/myconfig --configDir ${WORK}/myconfigdir -s mysource
hugo --baseURL http://example.com/ --minify --destination ${WORK}/newpublic --clock 2021-11-06T22:30:00.00+09:00 -e staging --config ${WORK}/myconfig --configDir ${WORK}/myconfigdir -s mysource
stdout 'Pages.*|1'
stdout 'Total in'
grep 'Home: http://example.com/, Time: 2021-11-06' newpublic/index.html
grep 'Environment: staging, foo: bar, bar: baz' newpublic/index.html
# Verify that it's minified.
grep '<body>Home' newpublic/index.html

hugo --quiet
! stdout .
Expand All @@ -17,7 +19,9 @@ foo = "bar"
-- myconfigdir/_default/params.toml --
bar = "baz"
-- mysource/layouts/index.html --
<body>
Home: {{ .Permalink }}, Time: {{ now }}
</body>
Environment: {{ hugo.Environment }}, foo: {{ .Site.Params.foo }}, bar: {{ .Site.Params.bar }}
-- mysource/layouts/_default/single.html --
Title: {{ .Title }}
Expand Down
12 changes: 12 additions & 0 deletions testscripts/commands/hugo__publishdir_in_config.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Test the hugo command.

hugo

grep 'Home' newpublic/index.html

-- hugo.toml --
baseURL = "http://example.org/"
disableKinds = ["RSS", "sitemap", "robotsTXT", "404", "taxonomy", "term"]
publishDir = "newpublic"
-- layouts/index.html --
Home.

0 comments on commit 3f00f47

Please sign in to comment.