commands: Load config before creating the filesystem

To allow publishDir to be set in config file.
This commit is contained in:
Bjørn Erik Pedersen 2023-05-18 13:29:33 +02:00
parent 834b3d7e41
commit 3f00f47535
6 changed files with 48 additions and 24 deletions

View file

@ -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)
}
}
cfg.Set("publishDirStatic", cfg.Get("publishDir"))
cfg.Set("publishDirDynamic", cfg.Get("publishDir"))
// 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
}
base := configs.Base
cfg.Set("publishDir", base.PublishDir)
cfg.Set("publishDirStatic", base.PublishDir)
cfg.Set("publishDirDynamic", base.PublishDir)
renderStaticToDisk := cfg.GetBool("renderStaticToDisk")
@ -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)
@ -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)
})

View file

@ -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 {

View file

@ -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})
}

View file

@ -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)
}
}

View file

@ -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 .
@ -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 }}

View file

@ -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.