2018-11-15 03:28:02 -05:00
|
|
|
// Copyright 2018 The Hugo Authors. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
2022-05-02 10:07:52 -04:00
|
|
|
"fmt"
|
2021-06-16 13:11:01 -04:00
|
|
|
"os"
|
2019-02-01 01:45:35 -05:00
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
2021-08-03 03:57:14 -04:00
|
|
|
"github.com/gohugoio/hugo/common/herrors"
|
|
|
|
|
2021-06-16 13:11:01 -04:00
|
|
|
"github.com/gohugoio/hugo/common/paths"
|
|
|
|
|
2018-11-15 03:28:02 -05:00
|
|
|
"github.com/gohugoio/hugo/common/maps"
|
|
|
|
"github.com/gohugoio/hugo/parser/metadecoders"
|
|
|
|
"github.com/spf13/afero"
|
|
|
|
)
|
|
|
|
|
2019-01-31 07:06:18 -05:00
|
|
|
var (
|
2023-01-15 12:45:51 -05:00
|
|
|
// See issue #8979 for context.
|
|
|
|
// Hugo has always used config.toml etc. as the default config file name.
|
|
|
|
// But hugo.toml is a more descriptive name, but we need to check for both.
|
|
|
|
DefaultConfigNames = []string{"hugo", "config"}
|
|
|
|
|
|
|
|
DefaultConfigNamesSet = make(map[string]bool)
|
|
|
|
|
2019-02-01 01:45:35 -05:00
|
|
|
ValidConfigFileExtensions = []string{"toml", "yaml", "yml", "json"}
|
|
|
|
validConfigFileExtensionsMap map[string]bool = make(map[string]bool)
|
2019-01-31 07:06:18 -05:00
|
|
|
)
|
|
|
|
|
2019-02-01 01:45:35 -05:00
|
|
|
func init() {
|
2023-01-15 12:45:51 -05:00
|
|
|
for _, name := range DefaultConfigNames {
|
|
|
|
DefaultConfigNamesSet[name] = true
|
|
|
|
}
|
|
|
|
|
2019-02-01 01:45:35 -05:00
|
|
|
for _, ext := range ValidConfigFileExtensions {
|
|
|
|
validConfigFileExtensionsMap[ext] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsValidConfigFilename returns whether filename is one of the supported
|
|
|
|
// config formats in Hugo.
|
|
|
|
func IsValidConfigFilename(filename string) bool {
|
|
|
|
ext := strings.ToLower(strings.TrimPrefix(filepath.Ext(filename), "."))
|
|
|
|
return validConfigFileExtensionsMap[ext]
|
|
|
|
}
|
|
|
|
|
2018-11-15 03:28:02 -05:00
|
|
|
// FromConfigString creates a config from the given YAML, JSON or TOML config. This is useful in tests.
|
|
|
|
func FromConfigString(config, configType string) (Provider, error) {
|
|
|
|
m, err := readConfig(metadecoders.FormatFromString(configType), []byte(config))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-06-09 04:58:18 -04:00
|
|
|
return NewFrom(m), nil
|
2018-11-15 03:28:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// FromFile loads the configuration from the given filename.
|
|
|
|
func FromFile(fs afero.Fs, filename string) (Provider, error) {
|
|
|
|
m, err := loadConfigFromFile(fs, filename)
|
|
|
|
if err != nil {
|
2022-05-15 15:01:36 -04:00
|
|
|
fe := herrors.UnwrapFileError(err)
|
|
|
|
if fe != nil {
|
|
|
|
pos := fe.Position()
|
|
|
|
pos.Filename = filename
|
|
|
|
fe.UpdatePosition(pos)
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-05-14 09:51:04 -04:00
|
|
|
return nil, herrors.NewFileErrorFromFile(err, filename, fs, nil)
|
2018-11-15 03:28:02 -05:00
|
|
|
}
|
2021-06-09 04:58:18 -04:00
|
|
|
return NewFrom(m), nil
|
2018-11-15 03:28:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// FromFileToMap is the same as FromFile, but it returns the config values
|
|
|
|
// as a simple map.
|
2022-03-17 17:03:27 -04:00
|
|
|
func FromFileToMap(fs afero.Fs, filename string) (map[string]any, error) {
|
2018-11-15 03:28:02 -05:00
|
|
|
return loadConfigFromFile(fs, filename)
|
|
|
|
}
|
|
|
|
|
2022-03-17 17:03:27 -04:00
|
|
|
func readConfig(format metadecoders.Format, data []byte) (map[string]any, error) {
|
2018-12-23 04:40:32 -05:00
|
|
|
m, err := metadecoders.Default.UnmarshalToMap(data, format)
|
2018-11-15 03:28:02 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
RenameKeys(m)
|
|
|
|
|
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
|
2022-03-17 17:03:27 -04:00
|
|
|
func loadConfigFromFile(fs afero.Fs, filename string) (map[string]any, error) {
|
2018-12-23 04:40:32 -05:00
|
|
|
m, err := metadecoders.Default.UnmarshalFileToMap(fs, filename)
|
2018-11-15 03:28:02 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
RenameKeys(m)
|
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
|
2021-06-16 13:11:01 -04:00
|
|
|
func LoadConfigFromDir(sourceFs afero.Fs, configDir, environment string) (Provider, []string, error) {
|
|
|
|
defaultConfigDir := filepath.Join(configDir, "_default")
|
|
|
|
environmentConfigDir := filepath.Join(configDir, environment)
|
|
|
|
cfg := New()
|
|
|
|
|
|
|
|
var configDirs []string
|
|
|
|
// Merge from least to most specific.
|
|
|
|
for _, dir := range []string{defaultConfigDir, environmentConfigDir} {
|
|
|
|
if _, err := sourceFs.Stat(dir); err == nil {
|
|
|
|
configDirs = append(configDirs, dir)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(configDirs) == 0 {
|
|
|
|
return nil, nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Keep track of these so we can watch them for changes.
|
|
|
|
var dirnames []string
|
|
|
|
|
|
|
|
for _, configDir := range configDirs {
|
|
|
|
err := afero.Walk(sourceFs, configDir, func(path string, fi os.FileInfo, err error) error {
|
|
|
|
if fi == nil || err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if fi.IsDir() {
|
|
|
|
dirnames = append(dirnames, path)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if !IsValidConfigFilename(path) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
name := paths.Filename(filepath.Base(path))
|
|
|
|
|
|
|
|
item, err := metadecoders.Default.UnmarshalFileToMap(sourceFs, path)
|
|
|
|
if err != nil {
|
|
|
|
// This will be used in error reporting, use the most specific value.
|
|
|
|
dirnames = []string{path}
|
2022-05-02 10:07:52 -04:00
|
|
|
return fmt.Errorf("failed to unmarshl config for path %q: %w", path, err)
|
2021-06-16 13:11:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
var keyPath []string
|
2023-01-15 12:45:51 -05:00
|
|
|
if !DefaultConfigNamesSet[name] {
|
2021-06-16 13:11:01 -04:00
|
|
|
// Can be params.jp, menus.en etc.
|
|
|
|
name, lang := paths.FileAndExtNoDelimiter(name)
|
|
|
|
|
|
|
|
keyPath = []string{name}
|
|
|
|
|
|
|
|
if lang != "" {
|
|
|
|
keyPath = []string{"languages", lang}
|
|
|
|
switch name {
|
|
|
|
case "menu", "menus":
|
|
|
|
keyPath = append(keyPath, "menus")
|
|
|
|
case "params":
|
|
|
|
keyPath = append(keyPath, "params")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
root := item
|
|
|
|
if len(keyPath) > 0 {
|
2022-03-17 17:03:27 -04:00
|
|
|
root = make(map[string]any)
|
2021-06-16 13:11:01 -04:00
|
|
|
m := root
|
|
|
|
for i, key := range keyPath {
|
|
|
|
if i >= len(keyPath)-1 {
|
|
|
|
m[key] = item
|
|
|
|
} else {
|
2022-03-17 17:03:27 -04:00
|
|
|
nm := make(map[string]any)
|
2021-06-16 13:11:01 -04:00
|
|
|
m[key] = nm
|
|
|
|
m = nm
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Migrate menu => menus etc.
|
|
|
|
RenameKeys(root)
|
|
|
|
|
|
|
|
// Set will overwrite keys with the same name, recursively.
|
|
|
|
cfg.Set("", root)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, dirnames, err
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return cfg, dirnames, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-11-15 03:28:02 -05:00
|
|
|
var keyAliases maps.KeyRenamer
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
var err error
|
|
|
|
keyAliases, err = maps.NewKeyRenamer(
|
|
|
|
// Before 0.53 we used singular for "menu".
|
|
|
|
"{menu,languages/*/menu}", "menus",
|
|
|
|
)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// RenameKeys renames config keys in m recursively according to a global Hugo
|
|
|
|
// alias definition.
|
2022-03-17 17:03:27 -04:00
|
|
|
func RenameKeys(m map[string]any) {
|
2018-11-15 03:28:02 -05:00
|
|
|
keyAliases.Rename(m)
|
|
|
|
}
|