2014-12-05 02:55:51 +00:00
|
|
|
// Copyright © 2014 Steve Francia <spf@spf13.com>.
|
|
|
|
//
|
|
|
|
// Use of this source code is governed by an MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
// Viper is a application configuration system.
|
|
|
|
// It believes that applications can be configured a variety of ways
|
|
|
|
// via flags, ENVIRONMENT variables, configuration files retrieved
|
|
|
|
// from the file system, or a remote key/value store.
|
|
|
|
|
|
|
|
package viper
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
|
|
|
"strings"
|
2015-02-28 21:03:22 +00:00
|
|
|
"unicode"
|
2014-12-05 02:55:51 +00:00
|
|
|
|
2023-09-11 22:03:41 +00:00
|
|
|
slog "github.com/sagikazarmark/slog-shim"
|
2015-02-28 21:03:22 +00:00
|
|
|
"github.com/spf13/cast"
|
2014-12-05 02:55:51 +00:00
|
|
|
)
|
|
|
|
|
2016-09-20 08:17:41 +00:00
|
|
|
// ConfigParseError denotes failing to parse configuration file.
|
2015-08-02 01:32:35 +00:00
|
|
|
type ConfigParseError struct {
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
2016-09-20 08:17:41 +00:00
|
|
|
// Error returns the formatted configuration error.
|
2015-08-02 01:32:35 +00:00
|
|
|
func (pe ConfigParseError) Error() string {
|
|
|
|
return fmt.Sprintf("While parsing config: %s", pe.err.Error())
|
|
|
|
}
|
|
|
|
|
2023-05-30 10:07:27 +00:00
|
|
|
// Unwrap returns the wrapped error.
|
|
|
|
func (pe ConfigParseError) Unwrap() error {
|
|
|
|
return pe.err
|
|
|
|
}
|
|
|
|
|
Add CaseSensitiveKeys option
When reading configuration from sources with case-sensitive keys,
such as YAML, TOML, and JSON, a user may wish to preserve the case
of keys that appear in maps. For example, consider when the value
of a setting is a map with string keys that are case-sensitive.
Ideally, if the value is not going to be indexed by a Viper lookup
key, then the map value should be treated as an opaque value by
Viper, and its keys should not be modified. See #1014
Viper's default behaviour is that keys are case-sensitive, and this
behavior is implemented by converting all keys to lower-case. For
users that wish to preserve the case of keys, this commit introduces
an Option `CaseSensitiveKeys()` that can be used to configure Viper
to use case-sensitive keys. When CaseSensitiveKeys is enabled, all
keys retain the original case, and lookups become case-sensitive
(except for lookups of values bound to environment variables).
The behavior of Viper could become hard to understand if a user
could change the CaseSensitiveKeys setting after values have been
stored. For this reason, the setting may only be set when creating
a Viper instance, and it cannot be set on the "global" Viper.
2023-10-20 22:09:25 +00:00
|
|
|
// CopyMap returns a deep copy of a map[any]any or map[string]any. If value is
|
|
|
|
// not one of those map types, then it is returned as-is. If preserveCase is
|
|
|
|
// false, then all keys will be converted to lower-case in the copy that is
|
|
|
|
// returned.
|
|
|
|
func CopyMap(value any, preserveCase bool) any {
|
|
|
|
var copyMap func(map[string]any, bool) map[string]any
|
|
|
|
copyMap = func(m map[string]any, preserveCase bool) map[string]any {
|
|
|
|
nm := make(map[string]any)
|
|
|
|
|
|
|
|
for key, val := range m {
|
|
|
|
if !preserveCase {
|
|
|
|
key = strings.ToLower(key)
|
|
|
|
}
|
|
|
|
switch v := val.(type) {
|
|
|
|
case map[any]any:
|
|
|
|
nm[key] = copyMap(cast.ToStringMap(v), preserveCase)
|
|
|
|
case map[string]any:
|
|
|
|
nm[key] = copyMap(v, preserveCase)
|
|
|
|
default:
|
|
|
|
nm[key] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nm
|
|
|
|
}
|
|
|
|
|
2016-10-24 15:49:00 +00:00
|
|
|
switch v := value.(type) {
|
2023-09-26 14:59:38 +00:00
|
|
|
case map[any]any:
|
Add CaseSensitiveKeys option
When reading configuration from sources with case-sensitive keys,
such as YAML, TOML, and JSON, a user may wish to preserve the case
of keys that appear in maps. For example, consider when the value
of a setting is a map with string keys that are case-sensitive.
Ideally, if the value is not going to be indexed by a Viper lookup
key, then the map value should be treated as an opaque value by
Viper, and its keys should not be modified. See #1014
Viper's default behaviour is that keys are case-sensitive, and this
behavior is implemented by converting all keys to lower-case. For
users that wish to preserve the case of keys, this commit introduces
an Option `CaseSensitiveKeys()` that can be used to configure Viper
to use case-sensitive keys. When CaseSensitiveKeys is enabled, all
keys retain the original case, and lookups become case-sensitive
(except for lookups of values bound to environment variables).
The behavior of Viper could become hard to understand if a user
could change the CaseSensitiveKeys setting after values have been
stored. For this reason, the setting may only be set when creating
a Viper instance, and it cannot be set on the "global" Viper.
2023-10-20 22:09:25 +00:00
|
|
|
value = copyMap(cast.ToStringMap(v), preserveCase)
|
2023-09-26 14:59:38 +00:00
|
|
|
case map[string]any:
|
Add CaseSensitiveKeys option
When reading configuration from sources with case-sensitive keys,
such as YAML, TOML, and JSON, a user may wish to preserve the case
of keys that appear in maps. For example, consider when the value
of a setting is a map with string keys that are case-sensitive.
Ideally, if the value is not going to be indexed by a Viper lookup
key, then the map value should be treated as an opaque value by
Viper, and its keys should not be modified. See #1014
Viper's default behaviour is that keys are case-sensitive, and this
behavior is implemented by converting all keys to lower-case. For
users that wish to preserve the case of keys, this commit introduces
an Option `CaseSensitiveKeys()` that can be used to configure Viper
to use case-sensitive keys. When CaseSensitiveKeys is enabled, all
keys retain the original case, and lookups become case-sensitive
(except for lookups of values bound to environment variables).
The behavior of Viper could become hard to understand if a user
could change the CaseSensitiveKeys setting after values have been
stored. For this reason, the setting may only be set when creating
a Viper instance, and it cannot be set on the "global" Viper.
2023-10-20 22:09:25 +00:00
|
|
|
value = copyMap(v, preserveCase)
|
2016-10-24 15:49:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
|
2023-09-26 14:59:38 +00:00
|
|
|
func insensitiviseVal(val any) any {
|
2023-07-27 18:56:32 +00:00
|
|
|
switch v := val.(type) {
|
2023-09-26 14:59:38 +00:00
|
|
|
case map[any]any:
|
2022-06-23 21:34:55 +00:00
|
|
|
// nested map: cast and recursively insensitivise
|
|
|
|
val = cast.ToStringMap(val)
|
2023-09-26 14:59:38 +00:00
|
|
|
insensitiviseMap(val.(map[string]any))
|
|
|
|
case map[string]any:
|
2022-06-23 21:34:55 +00:00
|
|
|
// nested map: recursively insensitivise
|
2023-07-27 18:56:32 +00:00
|
|
|
insensitiviseMap(v)
|
2023-09-26 14:59:38 +00:00
|
|
|
case []any:
|
2022-06-23 21:34:55 +00:00
|
|
|
// nested array: recursively insensitivise
|
2023-07-27 18:56:32 +00:00
|
|
|
insensitiveArray(v)
|
2022-06-23 21:34:55 +00:00
|
|
|
}
|
|
|
|
return val
|
|
|
|
}
|
|
|
|
|
2023-09-26 14:59:38 +00:00
|
|
|
func insensitiviseMap(m map[string]any) {
|
2014-12-05 02:55:51 +00:00
|
|
|
for key, val := range m {
|
2022-06-23 21:34:55 +00:00
|
|
|
val = insensitiviseVal(val)
|
2014-12-05 02:55:51 +00:00
|
|
|
lower := strings.ToLower(key)
|
|
|
|
if key != lower {
|
2016-10-14 09:24:45 +00:00
|
|
|
// remove old key (not lower-cased)
|
2014-12-05 02:55:51 +00:00
|
|
|
delete(m, key)
|
|
|
|
}
|
2016-10-14 09:24:45 +00:00
|
|
|
// update map
|
|
|
|
m[lower] = val
|
2014-12-05 02:55:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-26 14:59:38 +00:00
|
|
|
func insensitiveArray(a []any) {
|
2022-06-23 21:34:55 +00:00
|
|
|
for i, val := range a {
|
|
|
|
a[i] = insensitiviseVal(val)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-11 22:03:41 +00:00
|
|
|
func absPathify(logger *slog.Logger, inPath string) string {
|
2021-12-09 17:36:49 +00:00
|
|
|
logger.Info("trying to resolve absolute path", "path", inPath)
|
2014-12-05 02:55:51 +00:00
|
|
|
|
2020-07-31 19:57:33 +00:00
|
|
|
if inPath == "$HOME" || strings.HasPrefix(inPath, "$HOME"+string(os.PathSeparator)) {
|
2014-12-05 02:55:51 +00:00
|
|
|
inPath = userHomeDir() + inPath[5:]
|
|
|
|
}
|
|
|
|
|
2021-01-15 08:29:58 +00:00
|
|
|
inPath = os.ExpandEnv(inPath)
|
2014-12-05 02:55:51 +00:00
|
|
|
|
|
|
|
if filepath.IsAbs(inPath) {
|
|
|
|
return filepath.Clean(inPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
p, err := filepath.Abs(inPath)
|
|
|
|
if err == nil {
|
|
|
|
return filepath.Clean(p)
|
|
|
|
}
|
2016-09-20 08:17:41 +00:00
|
|
|
|
2021-12-09 17:36:49 +00:00
|
|
|
logger.Error(fmt.Errorf("could not discover absolute path: %w", err).Error())
|
|
|
|
|
2014-12-05 02:55:51 +00:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func stringInSlice(a string, list []string) bool {
|
|
|
|
for _, b := range list {
|
|
|
|
if b == a {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func userHomeDir() string {
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
home := os.Getenv("HOMEDRIVE") + os.Getenv("HOMEPATH")
|
|
|
|
if home == "" {
|
|
|
|
home = os.Getenv("USERPROFILE")
|
|
|
|
}
|
|
|
|
return home
|
|
|
|
}
|
|
|
|
return os.Getenv("HOME")
|
|
|
|
}
|
|
|
|
|
2015-02-28 21:03:22 +00:00
|
|
|
func safeMul(a, b uint) uint {
|
|
|
|
c := a * b
|
|
|
|
if a > 1 && b > 1 && c/b != a {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
// parseSizeInBytes converts strings like 1GB or 12 mb into an unsigned integer number of bytes
|
|
|
|
func parseSizeInBytes(sizeStr string) uint {
|
|
|
|
sizeStr = strings.TrimSpace(sizeStr)
|
|
|
|
lastChar := len(sizeStr) - 1
|
|
|
|
multiplier := uint(1)
|
|
|
|
|
|
|
|
if lastChar > 0 {
|
|
|
|
if sizeStr[lastChar] == 'b' || sizeStr[lastChar] == 'B' {
|
|
|
|
if lastChar > 1 {
|
|
|
|
switch unicode.ToLower(rune(sizeStr[lastChar-1])) {
|
|
|
|
case 'k':
|
|
|
|
multiplier = 1 << 10
|
|
|
|
sizeStr = strings.TrimSpace(sizeStr[:lastChar-1])
|
|
|
|
case 'm':
|
|
|
|
multiplier = 1 << 20
|
|
|
|
sizeStr = strings.TrimSpace(sizeStr[:lastChar-1])
|
|
|
|
case 'g':
|
|
|
|
multiplier = 1 << 30
|
|
|
|
sizeStr = strings.TrimSpace(sizeStr[:lastChar-1])
|
|
|
|
default:
|
|
|
|
multiplier = 1
|
|
|
|
sizeStr = strings.TrimSpace(sizeStr[:lastChar])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
size := cast.ToInt(sizeStr)
|
|
|
|
if size < 0 {
|
|
|
|
size = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
return safeMul(uint(size), multiplier)
|
|
|
|
}
|
2016-10-08 08:00:18 +00:00
|
|
|
|
|
|
|
// deepSearch scans deep maps, following the key indexes listed in the
|
|
|
|
// sequence "path".
|
|
|
|
// The last value is expected to be another map, and is returned.
|
|
|
|
//
|
|
|
|
// In case intermediate keys do not exist, or map to a non-map value,
|
|
|
|
// a new map is created and inserted, and the search continues from there:
|
|
|
|
// the initial map "m" may be modified!
|
2023-09-26 14:59:38 +00:00
|
|
|
func deepSearch(m map[string]any, path []string) map[string]any {
|
2016-10-08 08:00:18 +00:00
|
|
|
for _, k := range path {
|
|
|
|
m2, ok := m[k]
|
|
|
|
if !ok {
|
|
|
|
// intermediate key does not exist
|
|
|
|
// => create it and continue from there
|
2023-09-26 14:59:38 +00:00
|
|
|
m3 := make(map[string]any)
|
2016-10-08 08:00:18 +00:00
|
|
|
m[k] = m3
|
|
|
|
m = m3
|
|
|
|
continue
|
|
|
|
}
|
2023-09-26 14:59:38 +00:00
|
|
|
m3, ok := m2.(map[string]any)
|
2016-10-08 08:00:18 +00:00
|
|
|
if !ok {
|
|
|
|
// intermediate key is a value
|
|
|
|
// => replace with a new map
|
2023-09-26 14:59:38 +00:00
|
|
|
m3 = make(map[string]any)
|
2016-10-08 08:00:18 +00:00
|
|
|
m[k] = m3
|
|
|
|
}
|
|
|
|
// continue search from here
|
|
|
|
m = m3
|
|
|
|
}
|
|
|
|
return m
|
|
|
|
}
|