2021-12-07 15:31:39 +03: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"
|
|
|
|
"unicode"
|
|
|
|
|
2024-01-17 17:54:30 +03:00
|
|
|
slog "github.com/sagikazarmark/slog-shim"
|
2021-12-07 15:31:39 +03:00
|
|
|
"github.com/spf13/cast"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ConfigParseError denotes failing to parse configuration file.
|
|
|
|
type ConfigParseError struct {
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
|
|
|
// Error returns the formatted configuration error.
|
|
|
|
func (pe ConfigParseError) Error() string {
|
|
|
|
return fmt.Sprintf("While parsing config: %s", pe.err.Error())
|
|
|
|
}
|
|
|
|
|
2023-06-05 11:15:05 +03:00
|
|
|
// Unwrap returns the wrapped error.
|
|
|
|
func (pe ConfigParseError) Unwrap() error {
|
|
|
|
return pe.err
|
|
|
|
}
|
|
|
|
|
2021-12-07 15:31:39 +03:00
|
|
|
// toCaseInsensitiveValue checks if the value is a map;
|
|
|
|
// if so, create a copy and lower-case the keys recursively.
|
2024-01-17 17:54:30 +03:00
|
|
|
func toCaseInsensitiveValue(value any) any {
|
2021-12-07 15:31:39 +03:00
|
|
|
switch v := value.(type) {
|
2024-01-17 17:54:30 +03:00
|
|
|
case map[any]any:
|
2021-12-07 15:31:39 +03:00
|
|
|
value = copyAndInsensitiviseMap(cast.ToStringMap(v))
|
2024-01-17 17:54:30 +03:00
|
|
|
case map[string]any:
|
2021-12-07 15:31:39 +03:00
|
|
|
value = copyAndInsensitiviseMap(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
|
|
|
|
// copyAndInsensitiviseMap behaves like insensitiviseMap, but creates a copy of
|
|
|
|
// any map it makes case insensitive.
|
2024-01-17 17:54:30 +03:00
|
|
|
func copyAndInsensitiviseMap(m map[string]any) map[string]any {
|
|
|
|
nm := make(map[string]any)
|
2021-12-07 15:31:39 +03:00
|
|
|
|
|
|
|
for key, val := range m {
|
|
|
|
lkey := strings.ToLower(key)
|
|
|
|
switch v := val.(type) {
|
2024-01-17 17:54:30 +03:00
|
|
|
case map[any]any:
|
2021-12-07 15:31:39 +03:00
|
|
|
nm[lkey] = copyAndInsensitiviseMap(cast.ToStringMap(v))
|
2024-01-17 17:54:30 +03:00
|
|
|
case map[string]any:
|
2021-12-07 15:31:39 +03:00
|
|
|
nm[lkey] = copyAndInsensitiviseMap(v)
|
|
|
|
default:
|
|
|
|
nm[lkey] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nm
|
|
|
|
}
|
|
|
|
|
2024-01-17 17:54:30 +03:00
|
|
|
func insensitiviseVal(val any) any {
|
|
|
|
switch v := val.(type) {
|
|
|
|
case map[any]any:
|
2022-09-28 20:30:40 +03:00
|
|
|
// nested map: cast and recursively insensitivise
|
|
|
|
val = cast.ToStringMap(val)
|
2024-01-17 17:54:30 +03:00
|
|
|
insensitiviseMap(val.(map[string]any))
|
|
|
|
case map[string]any:
|
2022-09-28 20:30:40 +03:00
|
|
|
// nested map: recursively insensitivise
|
2024-01-17 17:54:30 +03:00
|
|
|
insensitiviseMap(v)
|
|
|
|
case []any:
|
2022-09-28 20:30:40 +03:00
|
|
|
// nested array: recursively insensitivise
|
2024-01-17 17:54:30 +03:00
|
|
|
insensitiveArray(v)
|
2022-09-28 20:30:40 +03:00
|
|
|
}
|
|
|
|
return val
|
|
|
|
}
|
|
|
|
|
2024-01-17 17:54:30 +03:00
|
|
|
func insensitiviseMap(m map[string]any) {
|
2021-12-07 15:31:39 +03:00
|
|
|
for key, val := range m {
|
2022-09-28 20:30:40 +03:00
|
|
|
val = insensitiviseVal(val)
|
2021-12-07 15:31:39 +03:00
|
|
|
lower := strings.ToLower(key)
|
|
|
|
if key != lower {
|
|
|
|
// remove old key (not lower-cased)
|
|
|
|
delete(m, key)
|
|
|
|
}
|
|
|
|
// update map
|
|
|
|
m[lower] = val
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-17 17:54:30 +03:00
|
|
|
func insensitiveArray(a []any) {
|
2022-09-28 20:30:40 +03:00
|
|
|
for i, val := range a {
|
|
|
|
a[i] = insensitiviseVal(val)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-17 17:54:30 +03:00
|
|
|
func absPathify(logger *slog.Logger, inPath string) string {
|
2021-12-12 17:47:51 +03:00
|
|
|
logger.Info("trying to resolve absolute path", "path", inPath)
|
2021-12-07 15:31:39 +03:00
|
|
|
|
|
|
|
if inPath == "$HOME" || strings.HasPrefix(inPath, "$HOME"+string(os.PathSeparator)) {
|
|
|
|
inPath = userHomeDir() + inPath[5:]
|
|
|
|
}
|
|
|
|
|
|
|
|
inPath = os.ExpandEnv(inPath)
|
|
|
|
|
|
|
|
if filepath.IsAbs(inPath) {
|
|
|
|
return filepath.Clean(inPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
p, err := filepath.Abs(inPath)
|
|
|
|
if err == nil {
|
|
|
|
return filepath.Clean(p)
|
|
|
|
}
|
|
|
|
|
2021-12-12 17:47:51 +03:00
|
|
|
logger.Error(fmt.Errorf("could not discover absolute path: %w", err).Error())
|
2021-12-07 15:31:39 +03:00
|
|
|
|
2021-12-12 17:47:51 +03:00
|
|
|
return ""
|
2021-12-07 15:31:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
|
|
|
func safeMul(a, b uint) uint {
|
|
|
|
c := a * b
|
|
|
|
if a > 1 && b > 1 && c/b != a {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2024-01-17 17:54:30 +03:00
|
|
|
// parseSizeInBytes converts strings like 1GB or 12 mb into an unsigned integer number of bytes.
|
2021-12-07 15:31:39 +03:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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!
|
2024-01-17 17:54:30 +03:00
|
|
|
func deepSearch(m map[string]any, path []string) map[string]any {
|
2021-12-07 15:31:39 +03:00
|
|
|
for _, k := range path {
|
|
|
|
m2, ok := m[k]
|
|
|
|
if !ok {
|
|
|
|
// intermediate key does not exist
|
|
|
|
// => create it and continue from there
|
2024-01-17 17:54:30 +03:00
|
|
|
m3 := make(map[string]any)
|
2021-12-07 15:31:39 +03:00
|
|
|
m[k] = m3
|
|
|
|
m = m3
|
|
|
|
continue
|
|
|
|
}
|
2024-01-17 17:54:30 +03:00
|
|
|
m3, ok := m2.(map[string]any)
|
2021-12-07 15:31:39 +03:00
|
|
|
if !ok {
|
|
|
|
// intermediate key is a value
|
|
|
|
// => replace with a new map
|
2024-01-17 17:54:30 +03:00
|
|
|
m3 = make(map[string]any)
|
2021-12-07 15:31:39 +03:00
|
|
|
m[k] = m3
|
|
|
|
}
|
|
|
|
// continue search from here
|
|
|
|
m = m3
|
|
|
|
}
|
|
|
|
return m
|
|
|
|
}
|