configProvider.go (2338B)
1 // Copyright 2019 The Hugo Authors. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 package config
15
16 import (
17 "github.com/gohugoio/hugo/common/maps"
18 "github.com/gohugoio/hugo/common/types"
19 )
20
21 // Provider provides the configuration settings for Hugo.
22 type Provider interface {
23 GetString(key string) string
24 GetInt(key string) int
25 GetBool(key string) bool
26 GetParams(key string) maps.Params
27 GetStringMap(key string) map[string]any
28 GetStringMapString(key string) map[string]string
29 GetStringSlice(key string) []string
30 Get(key string) any
31 Set(key string, value any)
32 Merge(key string, value any)
33 SetDefaults(params maps.Params)
34 SetDefaultMergeStrategy()
35 WalkParams(walkFn func(params ...KeyParams) bool)
36 IsSet(key string) bool
37 }
38
39 // GetStringSlicePreserveString returns a string slice from the given config and key.
40 // It differs from the GetStringSlice method in that if the config value is a string,
41 // we do not attempt to split it into fields.
42 func GetStringSlicePreserveString(cfg Provider, key string) []string {
43 sd := cfg.Get(key)
44 return types.ToStringSlicePreserveString(sd)
45 }
46
47 // SetBaseTestDefaults provides some common config defaults used in tests.
48 func SetBaseTestDefaults(cfg Provider) Provider {
49 setIfNotSet(cfg, "baseURL", "https://example.org")
50 setIfNotSet(cfg, "resourceDir", "resources")
51 setIfNotSet(cfg, "contentDir", "content")
52 setIfNotSet(cfg, "dataDir", "data")
53 setIfNotSet(cfg, "i18nDir", "i18n")
54 setIfNotSet(cfg, "layoutDir", "layouts")
55 setIfNotSet(cfg, "assetDir", "assets")
56 setIfNotSet(cfg, "archetypeDir", "archetypes")
57 setIfNotSet(cfg, "publishDir", "public")
58 setIfNotSet(cfg, "workingDir", "")
59 setIfNotSet(cfg, "defaultContentLanguage", "en")
60 return cfg
61 }
62
63 func setIfNotSet(cfg Provider, key string, value any) {
64 if !cfg.IsSet(key) {
65 cfg.Set(key, value)
66 }
67 }