mirror of
https://github.com/gohugoio/hugo.git
synced 2024-11-21 20:46:30 -05:00
Simplify some config loading code
This commit is contained in:
parent
57d8d208ed
commit
df8bb8812f
1 changed files with 43 additions and 42 deletions
|
@ -167,61 +167,62 @@ func LoadConfig(d ConfigSourceDescriptor, doWithConfig ...func(cfg config.Provid
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// This is invoked both after we load the main config and at the end
|
||||||
|
// to support OS env override of config options used in the module collector.
|
||||||
applyOsEnvOverrides := func() error {
|
applyOsEnvOverrides := func() error {
|
||||||
|
if d.Environ == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
const delim = "__env__delim"
|
const delim = "__env__delim"
|
||||||
|
|
||||||
// Apply environment overrides
|
// Extract all that start with the HUGO prefix.
|
||||||
if len(d.Environ) > 0 {
|
// The delimiter is the following rune, usually "_".
|
||||||
// Extract all that start with the HUGO prefix.
|
const hugoEnvPrefix = "HUGO"
|
||||||
// The delimiter is the following rune, usually "_".
|
var hugoEnv []types.KeyValueStr
|
||||||
const hugoEnvPrefix = "HUGO"
|
for _, v := range d.Environ {
|
||||||
var hugoEnv []types.KeyValueStr
|
key, val := config.SplitEnvVar(v)
|
||||||
for _, v := range d.Environ {
|
if strings.HasPrefix(key, hugoEnvPrefix) {
|
||||||
key, val := config.SplitEnvVar(v)
|
delimiterAndKey := strings.TrimPrefix(key, hugoEnvPrefix)
|
||||||
if strings.HasPrefix(key, hugoEnvPrefix) {
|
if len(delimiterAndKey) < 2 {
|
||||||
delimiterAndKey := strings.TrimPrefix(key, hugoEnvPrefix)
|
continue
|
||||||
if len(delimiterAndKey) < 2 {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
// Allow delimiters to be case sensitive.
|
|
||||||
// It turns out there isn't that many allowed special
|
|
||||||
// chars in environment variables when used in Bash and similar,
|
|
||||||
// so variables on the form HUGOxPARAMSxFOO=bar is one option.
|
|
||||||
key := strings.ReplaceAll(delimiterAndKey[1:], delimiterAndKey[:1], delim)
|
|
||||||
key = strings.ToLower(key)
|
|
||||||
hugoEnv = append(hugoEnv, types.KeyValueStr{
|
|
||||||
Key: key,
|
|
||||||
Value: val,
|
|
||||||
})
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
// Allow delimiters to be case sensitive.
|
||||||
|
// It turns out there isn't that many allowed special
|
||||||
|
// chars in environment variables when used in Bash and similar,
|
||||||
|
// so variables on the form HUGOxPARAMSxFOO=bar is one option.
|
||||||
|
key := strings.ReplaceAll(delimiterAndKey[1:], delimiterAndKey[:1], delim)
|
||||||
|
key = strings.ToLower(key)
|
||||||
|
hugoEnv = append(hugoEnv, types.KeyValueStr{
|
||||||
|
Key: key,
|
||||||
|
Value: val,
|
||||||
|
})
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, env := range hugoEnv {
|
||||||
|
existing, nestedKey, owner, err := maps.GetNestedParamFn(env.Key, delim, v.Get)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, env := range hugoEnv {
|
if existing != nil {
|
||||||
existing, nestedKey, owner, err := maps.GetNestedParamFn(env.Key, delim, v.Get)
|
val, err := metadecoders.Default.UnmarshalStringTo(env.Value, existing)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if existing != nil {
|
if owner != nil {
|
||||||
val, err := metadecoders.Default.UnmarshalStringTo(env.Value, existing)
|
owner[nestedKey] = val
|
||||||
if err != nil {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
if owner != nil {
|
|
||||||
owner[nestedKey] = val
|
|
||||||
} else {
|
|
||||||
v.Set(env.Key, val)
|
|
||||||
}
|
|
||||||
} else if nestedKey != "" {
|
|
||||||
owner[nestedKey] = env.Value
|
|
||||||
} else {
|
} else {
|
||||||
v.Set(env.Key, env.Value)
|
v.Set(env.Key, val)
|
||||||
}
|
}
|
||||||
|
} else if nestedKey != "" {
|
||||||
|
owner[nestedKey] = env.Value
|
||||||
|
} else {
|
||||||
|
v.Set(env.Key, env.Value)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|
Loading…
Reference in a new issue