github.com/DominikUrban/viper@v0.0.0-20220730150717-aaf74638bd32/viper.go (about)

     1  // Copyright © 2014 Steve Francia <spf@spf13.com>.
     2  //
     3  // Use of this source code is governed by an MIT-style
     4  // license that can be found in the LICENSE file.
     5  
     6  // Viper is an application configuration system.
     7  // It believes that applications can be configured a variety of ways
     8  // via flags, ENVIRONMENT variables, configuration files retrieved
     9  // from the file system, or a remote key/value store.
    10  
    11  // Each item takes precedence over the item below it:
    12  
    13  // overrides
    14  // flag
    15  // env
    16  // config
    17  // key/value store
    18  // default
    19  
    20  package viper
    21  
    22  import (
    23  	"bytes"
    24  	"encoding/csv"
    25  	"errors"
    26  	"fmt"
    27  	"io"
    28  	"log"
    29  	"os"
    30  	"path/filepath"
    31  	"reflect"
    32  	"strconv"
    33  	"strings"
    34  	"sync"
    35  	"time"
    36  
    37  	"github.com/fsnotify/fsnotify"
    38  	"github.com/mitchellh/mapstructure"
    39  	"github.com/spf13/afero"
    40  	"github.com/spf13/cast"
    41  	"github.com/spf13/pflag"
    42  
    43  	"github.com/DominikUrban/viper/internal/encoding"
    44  	"github.com/DominikUrban/viper/internal/encoding/dotenv"
    45  	"github.com/DominikUrban/viper/internal/encoding/hcl"
    46  	"github.com/DominikUrban/viper/internal/encoding/ini"
    47  	"github.com/DominikUrban/viper/internal/encoding/javaproperties"
    48  	"github.com/DominikUrban/viper/internal/encoding/json"
    49  	"github.com/DominikUrban/viper/internal/encoding/toml"
    50  	"github.com/DominikUrban/viper/internal/encoding/yaml"
    51  )
    52  
    53  // ConfigMarshalError happens when failing to marshal the configuration.
    54  type ConfigMarshalError struct {
    55  	err error
    56  }
    57  
    58  // Error returns the formatted configuration error.
    59  func (e ConfigMarshalError) Error() string {
    60  	return fmt.Sprintf("While marshaling config: %s", e.err.Error())
    61  }
    62  
    63  var v *Viper
    64  
    65  type RemoteResponse struct {
    66  	Value []byte
    67  	Error error
    68  }
    69  
    70  func init() {
    71  	v = New()
    72  }
    73  
    74  type remoteConfigFactory interface {
    75  	Get(rp RemoteProvider) (io.Reader, error)
    76  	Watch(rp RemoteProvider) (io.Reader, error)
    77  	WatchChannel(rp RemoteProvider) (<-chan *RemoteResponse, chan bool)
    78  }
    79  
    80  // RemoteConfig is optional, see the remote package
    81  var RemoteConfig remoteConfigFactory
    82  
    83  // UnsupportedConfigError denotes encountering an unsupported
    84  // configuration filetype.
    85  type UnsupportedConfigError string
    86  
    87  // Error returns the formatted configuration error.
    88  func (str UnsupportedConfigError) Error() string {
    89  	return fmt.Sprintf("Unsupported Config Type %q", string(str))
    90  }
    91  
    92  // UnsupportedRemoteProviderError denotes encountering an unsupported remote
    93  // provider. Currently only etcd and Consul are supported.
    94  type UnsupportedRemoteProviderError string
    95  
    96  // Error returns the formatted remote provider error.
    97  func (str UnsupportedRemoteProviderError) Error() string {
    98  	return fmt.Sprintf("Unsupported Remote Provider Type %q", string(str))
    99  }
   100  
   101  // RemoteConfigError denotes encountering an error while trying to
   102  // pull the configuration from the remote provider.
   103  type RemoteConfigError string
   104  
   105  // Error returns the formatted remote provider error
   106  func (rce RemoteConfigError) Error() string {
   107  	return fmt.Sprintf("Remote Configurations Error: %s", string(rce))
   108  }
   109  
   110  // ConfigFileNotFoundError denotes failing to find configuration file.
   111  type ConfigFileNotFoundError struct {
   112  	name, locations string
   113  }
   114  
   115  // Error returns the formatted configuration error.
   116  func (fnfe ConfigFileNotFoundError) Error() string {
   117  	return fmt.Sprintf("Config File %q Not Found in %q", fnfe.name, fnfe.locations)
   118  }
   119  
   120  // ConfigFileAlreadyExistsError denotes failure to write new configuration file.
   121  type ConfigFileAlreadyExistsError string
   122  
   123  // Error returns the formatted error when configuration already exists.
   124  func (faee ConfigFileAlreadyExistsError) Error() string {
   125  	return fmt.Sprintf("Config File %q Already Exists", string(faee))
   126  }
   127  
   128  // A DecoderConfigOption can be passed to viper.Unmarshal to configure
   129  // mapstructure.DecoderConfig options
   130  type DecoderConfigOption func(*mapstructure.DecoderConfig)
   131  
   132  // DecodeHook returns a DecoderConfigOption which overrides the default
   133  // DecoderConfig.DecodeHook value, the default is:
   134  //
   135  //  mapstructure.ComposeDecodeHookFunc(
   136  //		mapstructure.StringToTimeDurationHookFunc(),
   137  //		mapstructure.StringToSliceHookFunc(","),
   138  //	)
   139  func DecodeHook(hook mapstructure.DecodeHookFunc) DecoderConfigOption {
   140  	return func(c *mapstructure.DecoderConfig) {
   141  		c.DecodeHook = hook
   142  	}
   143  }
   144  
   145  // Viper is a prioritized configuration registry. It
   146  // maintains a set of configuration sources, fetches
   147  // values to populate those, and provides them according
   148  // to the source's priority.
   149  // The priority of the sources is the following:
   150  // 1. overrides
   151  // 2. flags
   152  // 3. env. variables
   153  // 4. config file
   154  // 5. key/value store
   155  // 6. defaults
   156  //
   157  // For example, if values from the following sources were loaded:
   158  //
   159  //  Defaults : {
   160  //  	"secret": "",
   161  //  	"user": "default",
   162  //  	"endpoint": "https://localhost"
   163  //  }
   164  //  Config : {
   165  //  	"user": "root"
   166  //  	"secret": "defaultsecret"
   167  //  }
   168  //  Env : {
   169  //  	"secret": "somesecretkey"
   170  //  }
   171  //
   172  // The resulting config will have the following values:
   173  //
   174  //	{
   175  //		"secret": "somesecretkey",
   176  //		"user": "root",
   177  //		"endpoint": "https://localhost"
   178  //	}
   179  //
   180  // Note: Vipers are not safe for concurrent Get() and Set() operations.
   181  type Viper struct {
   182  	// Delimiter that separates a list of keys
   183  	// used to access a nested value in one go
   184  	keyDelim string
   185  
   186  	// A set of paths to look for the config file in
   187  	configPaths []string
   188  
   189  	// The filesystem to read config from.
   190  	fs afero.Fs
   191  
   192  	// A set of remote providers to search for the configuration
   193  	remoteProviders []*defaultRemoteProvider
   194  
   195  	// Name of file to look for inside the path
   196  	configName        string
   197  	configFile        string
   198  	configType        string
   199  	configPermissions os.FileMode
   200  	envPrefix         string
   201  
   202  	// Specific commands for ini parsing
   203  	iniLoadOptions ini.LoadOptions
   204  
   205  	automaticEnvApplied bool
   206  	envKeyReplacer      StringReplacer
   207  	allowEmptyEnv       bool
   208  
   209  	config         map[string]interface{}
   210  	override       map[string]interface{}
   211  	defaults       map[string]interface{}
   212  	kvstore        map[string]interface{}
   213  	pflags         map[string]FlagValue
   214  	env            map[string][]string
   215  	aliases        map[string]string
   216  	typeByDefValue bool
   217  
   218  	onConfigChange func(fsnotify.Event)
   219  
   220  	logger Logger
   221  
   222  	// TODO: should probably be protected with a mutex
   223  	encoderRegistry *encoding.EncoderRegistry
   224  	decoderRegistry *encoding.DecoderRegistry
   225  }
   226  
   227  // New returns an initialized Viper instance.
   228  func New() *Viper {
   229  	v := new(Viper)
   230  	v.keyDelim = "."
   231  	v.configName = "config"
   232  	v.configPermissions = os.FileMode(0o644)
   233  	v.fs = afero.NewOsFs()
   234  	v.config = make(map[string]interface{})
   235  	v.override = make(map[string]interface{})
   236  	v.defaults = make(map[string]interface{})
   237  	v.kvstore = make(map[string]interface{})
   238  	v.pflags = make(map[string]FlagValue)
   239  	v.env = make(map[string][]string)
   240  	v.aliases = make(map[string]string)
   241  	v.typeByDefValue = false
   242  	v.logger = jwwLogger{}
   243  
   244  	v.resetEncoding()
   245  
   246  	return v
   247  }
   248  
   249  // Option configures Viper using the functional options paradigm popularized by Rob Pike and Dave Cheney.
   250  // If you're unfamiliar with this style,
   251  // see https://commandcenter.blogspot.com/2014/01/self-referential-functions-and-design.html and
   252  // https://dave.cheney.net/2014/10/17/functional-options-for-friendly-apis.
   253  type Option interface {
   254  	apply(v *Viper)
   255  }
   256  
   257  type optionFunc func(v *Viper)
   258  
   259  func (fn optionFunc) apply(v *Viper) {
   260  	fn(v)
   261  }
   262  
   263  // KeyDelimiter sets the delimiter used for determining key parts.
   264  // By default it's value is ".".
   265  func KeyDelimiter(d string) Option {
   266  	return optionFunc(func(v *Viper) {
   267  		v.keyDelim = d
   268  	})
   269  }
   270  
   271  // StringReplacer applies a set of replacements to a string.
   272  type StringReplacer interface {
   273  	// Replace returns a copy of s with all replacements performed.
   274  	Replace(s string) string
   275  }
   276  
   277  // EnvKeyReplacer sets a replacer used for mapping environment variables to internal keys.
   278  func EnvKeyReplacer(r StringReplacer) Option {
   279  	return optionFunc(func(v *Viper) {
   280  		v.envKeyReplacer = r
   281  	})
   282  }
   283  
   284  // NewWithOptions creates a new Viper instance.
   285  func NewWithOptions(opts ...Option) *Viper {
   286  	v := New()
   287  
   288  	for _, opt := range opts {
   289  		opt.apply(v)
   290  	}
   291  
   292  	v.resetEncoding()
   293  
   294  	return v
   295  }
   296  
   297  // Reset is intended for testing, will reset all to default settings.
   298  // In the public interface for the viper package so applications
   299  // can use it in their testing as well.
   300  func Reset() {
   301  	v = New()
   302  	SupportedExts = []string{"json", "toml", "yaml", "yml", "properties", "props", "prop", "hcl", "tfvars", "dotenv", "env", "ini"}
   303  	SupportedRemoteProviders = []string{"etcd", "etcd3", "consul", "firestore"}
   304  }
   305  
   306  // TODO: make this lazy initialization instead
   307  func (v *Viper) resetEncoding() {
   308  	encoderRegistry := encoding.NewEncoderRegistry()
   309  	decoderRegistry := encoding.NewDecoderRegistry()
   310  
   311  	{
   312  		codec := yaml.Codec{}
   313  
   314  		encoderRegistry.RegisterEncoder("yaml", codec)
   315  		decoderRegistry.RegisterDecoder("yaml", codec)
   316  
   317  		encoderRegistry.RegisterEncoder("yml", codec)
   318  		decoderRegistry.RegisterDecoder("yml", codec)
   319  	}
   320  
   321  	{
   322  		codec := json.Codec{}
   323  
   324  		encoderRegistry.RegisterEncoder("json", codec)
   325  		decoderRegistry.RegisterDecoder("json", codec)
   326  	}
   327  
   328  	{
   329  		codec := toml.Codec{}
   330  
   331  		encoderRegistry.RegisterEncoder("toml", codec)
   332  		decoderRegistry.RegisterDecoder("toml", codec)
   333  	}
   334  
   335  	{
   336  		codec := hcl.Codec{}
   337  
   338  		encoderRegistry.RegisterEncoder("hcl", codec)
   339  		decoderRegistry.RegisterDecoder("hcl", codec)
   340  
   341  		encoderRegistry.RegisterEncoder("tfvars", codec)
   342  		decoderRegistry.RegisterDecoder("tfvars", codec)
   343  	}
   344  
   345  	{
   346  		codec := ini.Codec{
   347  			KeyDelimiter: v.keyDelim,
   348  			LoadOptions:  v.iniLoadOptions,
   349  		}
   350  
   351  		encoderRegistry.RegisterEncoder("ini", codec)
   352  		decoderRegistry.RegisterDecoder("ini", codec)
   353  	}
   354  
   355  	{
   356  		codec := &javaproperties.Codec{
   357  			KeyDelimiter: v.keyDelim,
   358  		}
   359  
   360  		encoderRegistry.RegisterEncoder("properties", codec)
   361  		decoderRegistry.RegisterDecoder("properties", codec)
   362  
   363  		encoderRegistry.RegisterEncoder("props", codec)
   364  		decoderRegistry.RegisterDecoder("props", codec)
   365  
   366  		encoderRegistry.RegisterEncoder("prop", codec)
   367  		decoderRegistry.RegisterDecoder("prop", codec)
   368  	}
   369  
   370  	{
   371  		codec := &dotenv.Codec{}
   372  
   373  		encoderRegistry.RegisterEncoder("dotenv", codec)
   374  		decoderRegistry.RegisterDecoder("dotenv", codec)
   375  
   376  		encoderRegistry.RegisterEncoder("env", codec)
   377  		decoderRegistry.RegisterDecoder("env", codec)
   378  	}
   379  
   380  	v.encoderRegistry = encoderRegistry
   381  	v.decoderRegistry = decoderRegistry
   382  }
   383  
   384  type defaultRemoteProvider struct {
   385  	provider      string
   386  	endpoint      string
   387  	path          string
   388  	secretKeyring string
   389  }
   390  
   391  func (rp defaultRemoteProvider) Provider() string {
   392  	return rp.provider
   393  }
   394  
   395  func (rp defaultRemoteProvider) Endpoint() string {
   396  	return rp.endpoint
   397  }
   398  
   399  func (rp defaultRemoteProvider) Path() string {
   400  	return rp.path
   401  }
   402  
   403  func (rp defaultRemoteProvider) SecretKeyring() string {
   404  	return rp.secretKeyring
   405  }
   406  
   407  // RemoteProvider stores the configuration necessary
   408  // to connect to a remote key/value store.
   409  // Optional secretKeyring to unencrypt encrypted values
   410  // can be provided.
   411  type RemoteProvider interface {
   412  	Provider() string
   413  	Endpoint() string
   414  	Path() string
   415  	SecretKeyring() string
   416  }
   417  
   418  // SupportedExts are universally supported extensions.
   419  var SupportedExts = []string{"json", "toml", "yaml", "yml", "properties", "props", "prop", "hcl", "tfvars", "dotenv", "env", "ini"}
   420  
   421  // SupportedRemoteProviders are universally supported remote providers.
   422  var SupportedRemoteProviders = []string{"etcd", "etcd3", "consul", "firestore"}
   423  
   424  func OnConfigChange(run func(in fsnotify.Event)) { v.OnConfigChange(run) }
   425  func (v *Viper) OnConfigChange(run func(in fsnotify.Event)) {
   426  	v.onConfigChange = run
   427  }
   428  
   429  func WatchConfig() { v.WatchConfig() }
   430  
   431  func (v *Viper) WatchConfig() {
   432  	initWG := sync.WaitGroup{}
   433  	initWG.Add(1)
   434  	go func() {
   435  		watcher, err := newWatcher()
   436  		if err != nil {
   437  			log.Fatal(err)
   438  		}
   439  		defer watcher.Close()
   440  		// we have to watch the entire directory to pick up renames/atomic saves in a cross-platform way
   441  		filename, err := v.getConfigFile()
   442  		if err != nil {
   443  			log.Printf("error: %v\n", err)
   444  			initWG.Done()
   445  			return
   446  		}
   447  
   448  		configFile := filepath.Clean(filename)
   449  		configDir, _ := filepath.Split(configFile)
   450  		realConfigFile, _ := filepath.EvalSymlinks(filename)
   451  
   452  		eventsWG := sync.WaitGroup{}
   453  		eventsWG.Add(1)
   454  		go func() {
   455  			for {
   456  				select {
   457  				case event, ok := <-watcher.Events:
   458  					if !ok { // 'Events' channel is closed
   459  						eventsWG.Done()
   460  						return
   461  					}
   462  					currentConfigFile, _ := filepath.EvalSymlinks(filename)
   463  					// we only care about the config file with the following cases:
   464  					// 1 - if the config file was modified or created
   465  					// 2 - if the real path to the config file changed (eg: k8s ConfigMap replacement)
   466  					const writeOrCreateMask = fsnotify.Write | fsnotify.Create
   467  					if (filepath.Clean(event.Name) == configFile &&
   468  						event.Op&writeOrCreateMask != 0) ||
   469  						(currentConfigFile != "" && currentConfigFile != realConfigFile) {
   470  						realConfigFile = currentConfigFile
   471  						err := v.ReadInConfig()
   472  						if err != nil {
   473  							log.Printf("error reading config file: %v\n", err)
   474  						}
   475  						if v.onConfigChange != nil {
   476  							v.onConfigChange(event)
   477  						}
   478  					} else if filepath.Clean(event.Name) == configFile &&
   479  						event.Op&fsnotify.Remove != 0 {
   480  						eventsWG.Done()
   481  						return
   482  					}
   483  
   484  				case err, ok := <-watcher.Errors:
   485  					if ok { // 'Errors' channel is not closed
   486  						log.Printf("watcher error: %v\n", err)
   487  					}
   488  					eventsWG.Done()
   489  					return
   490  				}
   491  			}
   492  		}()
   493  		watcher.Add(configDir)
   494  		initWG.Done()   // done initializing the watch in this go routine, so the parent routine can move on...
   495  		eventsWG.Wait() // now, wait for event loop to end in this go-routine...
   496  	}()
   497  	initWG.Wait() // make sure that the go routine above fully ended before returning
   498  }
   499  
   500  // SetConfigFile explicitly defines the path, name and extension of the config file.
   501  // Viper will use this and not check any of the config paths.
   502  func SetConfigFile(in string) { v.SetConfigFile(in) }
   503  
   504  func (v *Viper) SetConfigFile(in string) {
   505  	if in != "" {
   506  		v.configFile = in
   507  	}
   508  }
   509  
   510  // SetEnvPrefix defines a prefix that ENVIRONMENT variables will use.
   511  // E.g. if your prefix is "spf", the env registry will look for env
   512  // variables that start with "SPF_".
   513  func SetEnvPrefix(in string) { v.SetEnvPrefix(in) }
   514  
   515  func (v *Viper) SetEnvPrefix(in string) {
   516  	if in != "" {
   517  		v.envPrefix = in
   518  	}
   519  }
   520  
   521  func (v *Viper) mergeWithEnvPrefix(in string) string {
   522  	if v.envPrefix != "" {
   523  		return strings.ToUpper(v.envPrefix + "_" + in)
   524  	}
   525  
   526  	return strings.ToUpper(in)
   527  }
   528  
   529  // AllowEmptyEnv tells Viper to consider set,
   530  // but empty environment variables as valid values instead of falling back.
   531  // For backward compatibility reasons this is false by default.
   532  func AllowEmptyEnv(allowEmptyEnv bool) { v.AllowEmptyEnv(allowEmptyEnv) }
   533  
   534  func (v *Viper) AllowEmptyEnv(allowEmptyEnv bool) {
   535  	v.allowEmptyEnv = allowEmptyEnv
   536  }
   537  
   538  // TODO: should getEnv logic be moved into find(). Can generalize the use of
   539  // rewriting keys many things, Ex: Get('someKey') -> some_key
   540  // (camel case to snake case for JSON keys perhaps)
   541  
   542  // getEnv is a wrapper around os.Getenv which replaces characters in the original
   543  // key. This allows env vars which have different keys than the config object
   544  // keys.
   545  func (v *Viper) getEnv(key string) (string, bool) {
   546  	if v.envKeyReplacer != nil {
   547  		key = v.envKeyReplacer.Replace(key)
   548  	}
   549  
   550  	val, ok := os.LookupEnv(key)
   551  
   552  	return val, ok && (v.allowEmptyEnv || val != "")
   553  }
   554  
   555  // ConfigFileUsed returns the file used to populate the config registry.
   556  func ConfigFileUsed() string            { return v.ConfigFileUsed() }
   557  func (v *Viper) ConfigFileUsed() string { return v.configFile }
   558  
   559  // AddConfigPath adds a path for Viper to search for the config file in.
   560  // Can be called multiple times to define multiple search paths.
   561  func AddConfigPath(in string) { v.AddConfigPath(in) }
   562  
   563  func (v *Viper) AddConfigPath(in string) {
   564  	if in != "" {
   565  		absin := absPathify(v.logger, in)
   566  
   567  		v.logger.Info("adding path to search paths", "path", absin)
   568  		if !stringInSlice(absin, v.configPaths) {
   569  			v.configPaths = append(v.configPaths, absin)
   570  		}
   571  	}
   572  }
   573  
   574  // AddRemoteProvider adds a remote configuration source.
   575  // Remote Providers are searched in the order they are added.
   576  // provider is a string value: "etcd", "etcd3", "consul" or "firestore" are currently supported.
   577  // endpoint is the url.  etcd requires http://ip:port  consul requires ip:port
   578  // path is the path in the k/v store to retrieve configuration
   579  // To retrieve a config file called myapp.json from /configs/myapp.json
   580  // you should set path to /configs and set config name (SetConfigName()) to
   581  // "myapp"
   582  func AddRemoteProvider(provider, endpoint, path string) error {
   583  	return v.AddRemoteProvider(provider, endpoint, path)
   584  }
   585  
   586  func (v *Viper) AddRemoteProvider(provider, endpoint, path string) error {
   587  	if !stringInSlice(provider, SupportedRemoteProviders) {
   588  		return UnsupportedRemoteProviderError(provider)
   589  	}
   590  	if provider != "" && endpoint != "" {
   591  		v.logger.Info("adding remote provider", "provider", provider, "endpoint", endpoint)
   592  
   593  		rp := &defaultRemoteProvider{
   594  			endpoint: endpoint,
   595  			provider: provider,
   596  			path:     path,
   597  		}
   598  		if !v.providerPathExists(rp) {
   599  			v.remoteProviders = append(v.remoteProviders, rp)
   600  		}
   601  	}
   602  	return nil
   603  }
   604  
   605  // AddSecureRemoteProvider adds a remote configuration source.
   606  // Secure Remote Providers are searched in the order they are added.
   607  // provider is a string value: "etcd", "etcd3", "consul" or "firestore" are currently supported.
   608  // endpoint is the url.  etcd requires http://ip:port  consul requires ip:port
   609  // secretkeyring is the filepath to your openpgp secret keyring.  e.g. /etc/secrets/myring.gpg
   610  // path is the path in the k/v store to retrieve configuration
   611  // To retrieve a config file called myapp.json from /configs/myapp.json
   612  // you should set path to /configs and set config name (SetConfigName()) to
   613  // "myapp"
   614  // Secure Remote Providers are implemented with github.com/bketelsen/crypt
   615  func AddSecureRemoteProvider(provider, endpoint, path, secretkeyring string) error {
   616  	return v.AddSecureRemoteProvider(provider, endpoint, path, secretkeyring)
   617  }
   618  
   619  func (v *Viper) AddSecureRemoteProvider(provider, endpoint, path, secretkeyring string) error {
   620  	if !stringInSlice(provider, SupportedRemoteProviders) {
   621  		return UnsupportedRemoteProviderError(provider)
   622  	}
   623  	if provider != "" && endpoint != "" {
   624  		v.logger.Info("adding remote provider", "provider", provider, "endpoint", endpoint)
   625  
   626  		rp := &defaultRemoteProvider{
   627  			endpoint:      endpoint,
   628  			provider:      provider,
   629  			path:          path,
   630  			secretKeyring: secretkeyring,
   631  		}
   632  		if !v.providerPathExists(rp) {
   633  			v.remoteProviders = append(v.remoteProviders, rp)
   634  		}
   635  	}
   636  	return nil
   637  }
   638  
   639  func (v *Viper) providerPathExists(p *defaultRemoteProvider) bool {
   640  	for _, y := range v.remoteProviders {
   641  		if reflect.DeepEqual(y, p) {
   642  			return true
   643  		}
   644  	}
   645  	return false
   646  }
   647  
   648  // searchMap recursively searches for a value for path in source map.
   649  // Returns nil if not found.
   650  // Note: This assumes that the path entries and map keys are lower cased.
   651  func (v *Viper) searchMap(source map[string]interface{}, path []string) interface{} {
   652  	if len(path) == 0 {
   653  		return source
   654  	}
   655  
   656  	next, ok := source[path[0]]
   657  	if ok {
   658  		// Fast path
   659  		if len(path) == 1 {
   660  			return next
   661  		}
   662  
   663  		// Nested case
   664  		switch next.(type) {
   665  		case map[interface{}]interface{}:
   666  			return v.searchMap(cast.ToStringMap(next), path[1:])
   667  		case map[string]interface{}:
   668  			// Type assertion is safe here since it is only reached
   669  			// if the type of `next` is the same as the type being asserted
   670  			return v.searchMap(next.(map[string]interface{}), path[1:])
   671  		default:
   672  			// got a value but nested key expected, return "nil" for not found
   673  			return nil
   674  		}
   675  	}
   676  	return nil
   677  }
   678  
   679  // searchIndexableWithPathPrefixes recursively searches for a value for path in source map/slice.
   680  //
   681  // While searchMap() considers each path element as a single map key or slice index, this
   682  // function searches for, and prioritizes, merged path elements.
   683  // e.g., if in the source, "foo" is defined with a sub-key "bar", and "foo.bar"
   684  // is also defined, this latter value is returned for path ["foo", "bar"].
   685  //
   686  // This should be useful only at config level (other maps may not contain dots
   687  // in their keys).
   688  //
   689  // Note: This assumes that the path entries and map keys are lower cased.
   690  func (v *Viper) searchIndexableWithPathPrefixes(source interface{}, path []string) interface{} {
   691  	if len(path) == 0 {
   692  		return source
   693  	}
   694  
   695  	// search for path prefixes, starting from the longest one
   696  	for i := len(path); i > 0; i-- {
   697  		prefixKey := strings.ToLower(strings.Join(path[0:i], v.keyDelim))
   698  
   699  		var val interface{}
   700  		switch sourceIndexable := source.(type) {
   701  		case []interface{}:
   702  			val = v.searchSliceWithPathPrefixes(sourceIndexable, prefixKey, i, path)
   703  		case map[string]interface{}:
   704  			val = v.searchMapWithPathPrefixes(sourceIndexable, prefixKey, i, path)
   705  		}
   706  		if val != nil {
   707  			return val
   708  		}
   709  	}
   710  
   711  	// not found
   712  	return nil
   713  }
   714  
   715  // searchSliceWithPathPrefixes searches for a value for path in sourceSlice
   716  //
   717  // This function is part of the searchIndexableWithPathPrefixes recurring search and
   718  // should not be called directly from functions other than searchIndexableWithPathPrefixes.
   719  func (v *Viper) searchSliceWithPathPrefixes(
   720  	sourceSlice []interface{},
   721  	prefixKey string,
   722  	pathIndex int,
   723  	path []string,
   724  ) interface{} {
   725  	// if the prefixKey is not a number or it is out of bounds of the slice
   726  	index, err := strconv.Atoi(prefixKey)
   727  	if err != nil || len(sourceSlice) <= index {
   728  		return nil
   729  	}
   730  
   731  	next := sourceSlice[index]
   732  
   733  	// Fast path
   734  	if pathIndex == len(path) {
   735  		return next
   736  	}
   737  
   738  	switch n := next.(type) {
   739  	case map[interface{}]interface{}:
   740  		return v.searchIndexableWithPathPrefixes(cast.ToStringMap(n), path[pathIndex:])
   741  	case map[string]interface{}, []interface{}:
   742  		return v.searchIndexableWithPathPrefixes(n, path[pathIndex:])
   743  	default:
   744  		// got a value but nested key expected, do nothing and look for next prefix
   745  	}
   746  
   747  	// not found
   748  	return nil
   749  }
   750  
   751  // searchMapWithPathPrefixes searches for a value for path in sourceMap
   752  //
   753  // This function is part of the searchIndexableWithPathPrefixes recurring search and
   754  // should not be called directly from functions other than searchIndexableWithPathPrefixes.
   755  func (v *Viper) searchMapWithPathPrefixes(
   756  	sourceMap map[string]interface{},
   757  	prefixKey string,
   758  	pathIndex int,
   759  	path []string,
   760  ) interface{} {
   761  	next, ok := sourceMap[prefixKey]
   762  	if !ok {
   763  		return nil
   764  	}
   765  
   766  	// Fast path
   767  	if pathIndex == len(path) {
   768  		return next
   769  	}
   770  
   771  	// Nested case
   772  	switch n := next.(type) {
   773  	case map[interface{}]interface{}:
   774  		return v.searchIndexableWithPathPrefixes(cast.ToStringMap(n), path[pathIndex:])
   775  	case map[string]interface{}, []interface{}:
   776  		return v.searchIndexableWithPathPrefixes(n, path[pathIndex:])
   777  	default:
   778  		// got a value but nested key expected, do nothing and look for next prefix
   779  	}
   780  
   781  	// not found
   782  	return nil
   783  }
   784  
   785  // isPathShadowedInDeepMap makes sure the given path is not shadowed somewhere
   786  // on its path in the map.
   787  // e.g., if "foo.bar" has a value in the given map, it “shadows”
   788  //       "foo.bar.baz" in a lower-priority map
   789  func (v *Viper) isPathShadowedInDeepMap(path []string, m map[string]interface{}) string {
   790  	var parentVal interface{}
   791  	for i := 1; i < len(path); i++ {
   792  		parentVal = v.searchMap(m, path[0:i])
   793  		if parentVal == nil {
   794  			// not found, no need to add more path elements
   795  			return ""
   796  		}
   797  		switch parentVal.(type) {
   798  		case map[interface{}]interface{}:
   799  			continue
   800  		case map[string]interface{}:
   801  			continue
   802  		default:
   803  			// parentVal is a regular value which shadows "path"
   804  			return strings.Join(path[0:i], v.keyDelim)
   805  		}
   806  	}
   807  	return ""
   808  }
   809  
   810  // isPathShadowedInFlatMap makes sure the given path is not shadowed somewhere
   811  // in a sub-path of the map.
   812  // e.g., if "foo.bar" has a value in the given map, it “shadows”
   813  //       "foo.bar.baz" in a lower-priority map
   814  func (v *Viper) isPathShadowedInFlatMap(path []string, mi interface{}) string {
   815  	// unify input map
   816  	var m map[string]interface{}
   817  	switch mi.(type) {
   818  	case map[string]string, map[string]FlagValue:
   819  		m = cast.ToStringMap(mi)
   820  	default:
   821  		return ""
   822  	}
   823  
   824  	// scan paths
   825  	var parentKey string
   826  	for i := 1; i < len(path); i++ {
   827  		parentKey = strings.Join(path[0:i], v.keyDelim)
   828  		if _, ok := m[parentKey]; ok {
   829  			return parentKey
   830  		}
   831  	}
   832  	return ""
   833  }
   834  
   835  // isPathShadowedInAutoEnv makes sure the given path is not shadowed somewhere
   836  // in the environment, when automatic env is on.
   837  // e.g., if "foo.bar" has a value in the environment, it “shadows”
   838  //       "foo.bar.baz" in a lower-priority map
   839  func (v *Viper) isPathShadowedInAutoEnv(path []string) string {
   840  	var parentKey string
   841  	for i := 1; i < len(path); i++ {
   842  		parentKey = strings.Join(path[0:i], v.keyDelim)
   843  		if _, ok := v.getEnv(v.mergeWithEnvPrefix(parentKey)); ok {
   844  			return parentKey
   845  		}
   846  	}
   847  	return ""
   848  }
   849  
   850  // SetTypeByDefaultValue enables or disables the inference of a key value's
   851  // type when the Get function is used based upon a key's default value as
   852  // opposed to the value returned based on the normal fetch logic.
   853  //
   854  // For example, if a key has a default value of []string{} and the same key
   855  // is set via an environment variable to "a b c", a call to the Get function
   856  // would return a string slice for the key if the key's type is inferred by
   857  // the default value and the Get function would return:
   858  //
   859  //   []string {"a", "b", "c"}
   860  //
   861  // Otherwise the Get function would return:
   862  //
   863  //   "a b c"
   864  func SetTypeByDefaultValue(enable bool) { v.SetTypeByDefaultValue(enable) }
   865  
   866  func (v *Viper) SetTypeByDefaultValue(enable bool) {
   867  	v.typeByDefValue = enable
   868  }
   869  
   870  // GetViper gets the global Viper instance.
   871  func GetViper() *Viper {
   872  	return v
   873  }
   874  
   875  // Get can retrieve any value given the key to use.
   876  // Get is case-insensitive for a key.
   877  // Get has the behavior of returning the value associated with the first
   878  // place from where it is set. Viper will check in the following order:
   879  // override, flag, env, config file, key/value store, default
   880  //
   881  // Get returns an interface. For a specific value use one of the Get____ methods.
   882  func Get(key string) interface{} { return v.Get(key) }
   883  
   884  func (v *Viper) Get(key string) interface{} {
   885  	lcaseKey := strings.ToLower(key)
   886  	val := v.find(lcaseKey, true)
   887  	if val == nil {
   888  		return nil
   889  	}
   890  
   891  	if v.typeByDefValue {
   892  		// TODO(bep) this branch isn't covered by a single test.
   893  		valType := val
   894  		path := strings.Split(lcaseKey, v.keyDelim)
   895  		defVal := v.searchMap(v.defaults, path)
   896  		if defVal != nil {
   897  			valType = defVal
   898  		}
   899  
   900  		switch valType.(type) {
   901  		case bool:
   902  			return cast.ToBool(val)
   903  		case string:
   904  			return cast.ToString(val)
   905  		case int32, int16, int8, int:
   906  			return cast.ToInt(val)
   907  		case uint:
   908  			return cast.ToUint(val)
   909  		case uint32:
   910  			return cast.ToUint32(val)
   911  		case uint64:
   912  			return cast.ToUint64(val)
   913  		case int64:
   914  			return cast.ToInt64(val)
   915  		case float64, float32:
   916  			return cast.ToFloat64(val)
   917  		case time.Time:
   918  			return cast.ToTime(val)
   919  		case time.Duration:
   920  			return cast.ToDuration(val)
   921  		case []string:
   922  			return cast.ToStringSlice(val)
   923  		case []int:
   924  			return cast.ToIntSlice(val)
   925  		}
   926  	}
   927  
   928  	return val
   929  }
   930  
   931  // Sub returns new Viper instance representing a sub tree of this instance.
   932  // Sub is case-insensitive for a key.
   933  func Sub(key string) *Viper { return v.Sub(key) }
   934  
   935  func (v *Viper) Sub(key string) *Viper {
   936  	subv := New()
   937  	data := v.Get(key)
   938  	if data == nil {
   939  		return nil
   940  	}
   941  
   942  	if reflect.TypeOf(data).Kind() == reflect.Map {
   943  		subv.config = cast.ToStringMap(data)
   944  		return subv
   945  	}
   946  	return nil
   947  }
   948  
   949  // GetString returns the value associated with the key as a string.
   950  func GetString(key string) string { return v.GetString(key) }
   951  
   952  func (v *Viper) GetString(key string) string {
   953  	return cast.ToString(v.Get(key))
   954  }
   955  
   956  // GetBool returns the value associated with the key as a boolean.
   957  func GetBool(key string) bool { return v.GetBool(key) }
   958  
   959  func (v *Viper) GetBool(key string) bool {
   960  	return cast.ToBool(v.Get(key))
   961  }
   962  
   963  // GetInt returns the value associated with the key as an integer.
   964  func GetInt(key string) int { return v.GetInt(key) }
   965  
   966  func (v *Viper) GetInt(key string) int {
   967  	return cast.ToInt(v.Get(key))
   968  }
   969  
   970  // GetInt32 returns the value associated with the key as an integer.
   971  func GetInt32(key string) int32 { return v.GetInt32(key) }
   972  
   973  func (v *Viper) GetInt32(key string) int32 {
   974  	return cast.ToInt32(v.Get(key))
   975  }
   976  
   977  // GetInt64 returns the value associated with the key as an integer.
   978  func GetInt64(key string) int64 { return v.GetInt64(key) }
   979  
   980  func (v *Viper) GetInt64(key string) int64 {
   981  	return cast.ToInt64(v.Get(key))
   982  }
   983  
   984  // GetUint returns the value associated with the key as an unsigned integer.
   985  func GetUint(key string) uint { return v.GetUint(key) }
   986  
   987  func (v *Viper) GetUint(key string) uint {
   988  	return cast.ToUint(v.Get(key))
   989  }
   990  
   991  // GetUint32 returns the value associated with the key as an unsigned integer.
   992  func GetUint32(key string) uint32 { return v.GetUint32(key) }
   993  
   994  func (v *Viper) GetUint32(key string) uint32 {
   995  	return cast.ToUint32(v.Get(key))
   996  }
   997  
   998  // GetUint64 returns the value associated with the key as an unsigned integer.
   999  func GetUint64(key string) uint64 { return v.GetUint64(key) }
  1000  
  1001  func (v *Viper) GetUint64(key string) uint64 {
  1002  	return cast.ToUint64(v.Get(key))
  1003  }
  1004  
  1005  // GetFloat64 returns the value associated with the key as a float64.
  1006  func GetFloat64(key string) float64 { return v.GetFloat64(key) }
  1007  
  1008  func (v *Viper) GetFloat64(key string) float64 {
  1009  	return cast.ToFloat64(v.Get(key))
  1010  }
  1011  
  1012  // GetTime returns the value associated with the key as time.
  1013  func GetTime(key string) time.Time { return v.GetTime(key) }
  1014  
  1015  func (v *Viper) GetTime(key string) time.Time {
  1016  	return cast.ToTime(v.Get(key))
  1017  }
  1018  
  1019  // GetDuration returns the value associated with the key as a duration.
  1020  func GetDuration(key string) time.Duration { return v.GetDuration(key) }
  1021  
  1022  func (v *Viper) GetDuration(key string) time.Duration {
  1023  	return cast.ToDuration(v.Get(key))
  1024  }
  1025  
  1026  // GetIntSlice returns the value associated with the key as a slice of int values.
  1027  func GetIntSlice(key string) []int { return v.GetIntSlice(key) }
  1028  
  1029  func (v *Viper) GetIntSlice(key string) []int {
  1030  	return cast.ToIntSlice(v.Get(key))
  1031  }
  1032  
  1033  // GetStringSlice returns the value associated with the key as a slice of strings.
  1034  func GetStringSlice(key string) []string { return v.GetStringSlice(key) }
  1035  
  1036  func (v *Viper) GetStringSlice(key string) []string {
  1037  	return cast.ToStringSlice(v.Get(key))
  1038  }
  1039  
  1040  // GetStringMap returns the value associated with the key as a map of interfaces.
  1041  func GetStringMap(key string) map[string]interface{} { return v.GetStringMap(key) }
  1042  
  1043  func (v *Viper) GetStringMap(key string) map[string]interface{} {
  1044  	return cast.ToStringMap(v.Get(key))
  1045  }
  1046  
  1047  // GetStringMapString returns the value associated with the key as a map of strings.
  1048  func GetStringMapString(key string) map[string]string { return v.GetStringMapString(key) }
  1049  
  1050  func (v *Viper) GetStringMapString(key string) map[string]string {
  1051  	return cast.ToStringMapString(v.Get(key))
  1052  }
  1053  
  1054  // GetStringMapStringSlice returns the value associated with the key as a map to a slice of strings.
  1055  func GetStringMapStringSlice(key string) map[string][]string { return v.GetStringMapStringSlice(key) }
  1056  
  1057  func (v *Viper) GetStringMapStringSlice(key string) map[string][]string {
  1058  	return cast.ToStringMapStringSlice(v.Get(key))
  1059  }
  1060  
  1061  // GetSizeInBytes returns the size of the value associated with the given key
  1062  // in bytes.
  1063  func GetSizeInBytes(key string) uint { return v.GetSizeInBytes(key) }
  1064  
  1065  func (v *Viper) GetSizeInBytes(key string) uint {
  1066  	sizeStr := cast.ToString(v.Get(key))
  1067  	return parseSizeInBytes(sizeStr)
  1068  }
  1069  
  1070  // UnmarshalKey takes a single key and unmarshals it into a Struct.
  1071  func UnmarshalKey(key string, rawVal interface{}, opts ...DecoderConfigOption) error {
  1072  	return v.UnmarshalKey(key, rawVal, opts...)
  1073  }
  1074  
  1075  func (v *Viper) UnmarshalKey(key string, rawVal interface{}, opts ...DecoderConfigOption) error {
  1076  	return decode(v.Get(key), defaultDecoderConfig(rawVal, opts...))
  1077  }
  1078  
  1079  // Unmarshal unmarshals the config into a Struct. Make sure that the tags
  1080  // on the fields of the structure are properly set.
  1081  func Unmarshal(rawVal interface{}, opts ...DecoderConfigOption) error {
  1082  	return v.Unmarshal(rawVal, opts...)
  1083  }
  1084  
  1085  func (v *Viper) Unmarshal(rawVal interface{}, opts ...DecoderConfigOption) error {
  1086  	return decode(v.AllSettings(), defaultDecoderConfig(rawVal, opts...))
  1087  }
  1088  
  1089  // defaultDecoderConfig returns default mapsstructure.DecoderConfig with suppot
  1090  // of time.Duration values & string slices
  1091  func defaultDecoderConfig(output interface{}, opts ...DecoderConfigOption) *mapstructure.DecoderConfig {
  1092  	c := &mapstructure.DecoderConfig{
  1093  		Metadata:         nil,
  1094  		Result:           output,
  1095  		WeaklyTypedInput: true,
  1096  		DecodeHook: mapstructure.ComposeDecodeHookFunc(
  1097  			mapstructure.StringToTimeDurationHookFunc(),
  1098  			mapstructure.StringToSliceHookFunc(","),
  1099  		),
  1100  	}
  1101  	for _, opt := range opts {
  1102  		opt(c)
  1103  	}
  1104  	return c
  1105  }
  1106  
  1107  // A wrapper around mapstructure.Decode that mimics the WeakDecode functionality
  1108  func decode(input interface{}, config *mapstructure.DecoderConfig) error {
  1109  	decoder, err := mapstructure.NewDecoder(config)
  1110  	if err != nil {
  1111  		return err
  1112  	}
  1113  	return decoder.Decode(input)
  1114  }
  1115  
  1116  // UnmarshalExact unmarshals the config into a Struct, erroring if a field is nonexistent
  1117  // in the destination struct.
  1118  func UnmarshalExact(rawVal interface{}, opts ...DecoderConfigOption) error {
  1119  	return v.UnmarshalExact(rawVal, opts...)
  1120  }
  1121  
  1122  func (v *Viper) UnmarshalExact(rawVal interface{}, opts ...DecoderConfigOption) error {
  1123  	config := defaultDecoderConfig(rawVal, opts...)
  1124  	config.ErrorUnused = true
  1125  
  1126  	return decode(v.AllSettings(), config)
  1127  }
  1128  
  1129  // BindPFlags binds a full flag set to the configuration, using each flag's long
  1130  // name as the config key.
  1131  func BindPFlags(flags *pflag.FlagSet) error { return v.BindPFlags(flags) }
  1132  
  1133  func (v *Viper) BindPFlags(flags *pflag.FlagSet) error {
  1134  	return v.BindFlagValues(pflagValueSet{flags})
  1135  }
  1136  
  1137  // BindPFlag binds a specific key to a pflag (as used by cobra).
  1138  // Example (where serverCmd is a Cobra instance):
  1139  //
  1140  //	 serverCmd.Flags().Int("port", 1138, "Port to run Application server on")
  1141  //	 Viper.BindPFlag("port", serverCmd.Flags().Lookup("port"))
  1142  //
  1143  func BindPFlag(key string, flag *pflag.Flag) error { return v.BindPFlag(key, flag) }
  1144  
  1145  func (v *Viper) BindPFlag(key string, flag *pflag.Flag) error {
  1146  	if flag == nil {
  1147  		return fmt.Errorf("flag for %q is nil", key)
  1148  	}
  1149  	return v.BindFlagValue(key, pflagValue{flag})
  1150  }
  1151  
  1152  // BindFlagValues binds a full FlagValue set to the configuration, using each flag's long
  1153  // name as the config key.
  1154  func BindFlagValues(flags FlagValueSet) error { return v.BindFlagValues(flags) }
  1155  
  1156  func (v *Viper) BindFlagValues(flags FlagValueSet) (err error) {
  1157  	flags.VisitAll(func(flag FlagValue) {
  1158  		if err = v.BindFlagValue(flag.Name(), flag); err != nil {
  1159  			return
  1160  		}
  1161  	})
  1162  	return nil
  1163  }
  1164  
  1165  // BindFlagValue binds a specific key to a FlagValue.
  1166  func BindFlagValue(key string, flag FlagValue) error { return v.BindFlagValue(key, flag) }
  1167  
  1168  func (v *Viper) BindFlagValue(key string, flag FlagValue) error {
  1169  	if flag == nil {
  1170  		return fmt.Errorf("flag for %q is nil", key)
  1171  	}
  1172  	v.pflags[strings.ToLower(key)] = flag
  1173  	return nil
  1174  }
  1175  
  1176  // BindEnv binds a Viper key to a ENV variable.
  1177  // ENV variables are case sensitive.
  1178  // If only a key is provided, it will use the env key matching the key, uppercased.
  1179  // If more arguments are provided, they will represent the env variable names that
  1180  // should bind to this key and will be taken in the specified order.
  1181  // EnvPrefix will be used when set when env name is not provided.
  1182  func BindEnv(input ...string) error { return v.BindEnv(input...) }
  1183  
  1184  func (v *Viper) BindEnv(input ...string) error {
  1185  	if len(input) == 0 {
  1186  		return fmt.Errorf("missing key to bind to")
  1187  	}
  1188  
  1189  	key := strings.ToLower(input[0])
  1190  
  1191  	if len(input) == 1 {
  1192  		v.env[key] = append(v.env[key], v.mergeWithEnvPrefix(key))
  1193  	} else {
  1194  		v.env[key] = append(v.env[key], input[1:]...)
  1195  	}
  1196  
  1197  	return nil
  1198  }
  1199  
  1200  // MustBindEnv wraps BindEnv in a panic.
  1201  // If there is an error binding an environment variable, MustBindEnv will
  1202  // panic.
  1203  func MustBindEnv(input ...string) { v.MustBindEnv(input...) }
  1204  
  1205  func (v *Viper) MustBindEnv(input ...string) {
  1206  	if err := v.BindEnv(input...); err != nil {
  1207  		panic(fmt.Sprintf("error while binding environment variable: %v", err))
  1208  	}
  1209  }
  1210  
  1211  // Given a key, find the value.
  1212  //
  1213  // Viper will check to see if an alias exists first.
  1214  // Viper will then check in the following order:
  1215  // flag, env, config file, key/value store.
  1216  // Lastly, if no value was found and flagDefault is true, and if the key
  1217  // corresponds to a flag, the flag's default value is returned.
  1218  //
  1219  // Note: this assumes a lower-cased key given.
  1220  func (v *Viper) find(lcaseKey string, flagDefault bool) interface{} {
  1221  	var (
  1222  		val    interface{}
  1223  		exists bool
  1224  		path   = strings.Split(lcaseKey, v.keyDelim)
  1225  		nested = len(path) > 1
  1226  	)
  1227  
  1228  	// compute the path through the nested maps to the nested value
  1229  	if nested && v.isPathShadowedInDeepMap(path, castMapStringToMapInterface(v.aliases)) != "" {
  1230  		return nil
  1231  	}
  1232  
  1233  	// if the requested key is an alias, then return the proper key
  1234  	lcaseKey = v.realKey(lcaseKey)
  1235  	path = strings.Split(lcaseKey, v.keyDelim)
  1236  	nested = len(path) > 1
  1237  
  1238  	// Set() override first
  1239  	val = v.searchMap(v.override, path)
  1240  	if val != nil {
  1241  		return val
  1242  	}
  1243  	if nested && v.isPathShadowedInDeepMap(path, v.override) != "" {
  1244  		return nil
  1245  	}
  1246  
  1247  	// PFlag override next
  1248  	flag, exists := v.pflags[lcaseKey]
  1249  	if exists && flag.HasChanged() {
  1250  		switch flag.ValueType() {
  1251  		case "int", "int8", "int16", "int32", "int64":
  1252  			return cast.ToInt(flag.ValueString())
  1253  		case "bool":
  1254  			return cast.ToBool(flag.ValueString())
  1255  		case "stringSlice", "stringArray":
  1256  			s := strings.TrimPrefix(flag.ValueString(), "[")
  1257  			s = strings.TrimSuffix(s, "]")
  1258  			res, _ := readAsCSV(s)
  1259  			return res
  1260  		case "intSlice":
  1261  			s := strings.TrimPrefix(flag.ValueString(), "[")
  1262  			s = strings.TrimSuffix(s, "]")
  1263  			res, _ := readAsCSV(s)
  1264  			return cast.ToIntSlice(res)
  1265  		case "stringToString":
  1266  			return stringToStringConv(flag.ValueString())
  1267  		default:
  1268  			return flag.ValueString()
  1269  		}
  1270  	}
  1271  	if nested && v.isPathShadowedInFlatMap(path, v.pflags) != "" {
  1272  		return nil
  1273  	}
  1274  
  1275  	// Env override next
  1276  	if v.automaticEnvApplied {
  1277  		// even if it hasn't been registered, if automaticEnv is used,
  1278  		// check any Get request
  1279  		if val, ok := v.getEnv(v.mergeWithEnvPrefix(lcaseKey)); ok {
  1280  			return val
  1281  		}
  1282  		if nested && v.isPathShadowedInAutoEnv(path) != "" {
  1283  			return nil
  1284  		}
  1285  	}
  1286  	envkeys, exists := v.env[lcaseKey]
  1287  	if exists {
  1288  		for _, envkey := range envkeys {
  1289  			if val, ok := v.getEnv(envkey); ok {
  1290  				return val
  1291  			}
  1292  		}
  1293  	}
  1294  	if nested && v.isPathShadowedInFlatMap(path, v.env) != "" {
  1295  		return nil
  1296  	}
  1297  
  1298  	// Config file next
  1299  	val = v.searchIndexableWithPathPrefixes(v.config, path)
  1300  	if val != nil {
  1301  		return val
  1302  	}
  1303  	if nested && v.isPathShadowedInDeepMap(path, v.config) != "" {
  1304  		return nil
  1305  	}
  1306  
  1307  	// K/V store next
  1308  	val = v.searchMap(v.kvstore, path)
  1309  	if val != nil {
  1310  		return val
  1311  	}
  1312  	if nested && v.isPathShadowedInDeepMap(path, v.kvstore) != "" {
  1313  		return nil
  1314  	}
  1315  
  1316  	// Default next
  1317  	val = v.searchMap(v.defaults, path)
  1318  	if val != nil {
  1319  		return val
  1320  	}
  1321  	if nested && v.isPathShadowedInDeepMap(path, v.defaults) != "" {
  1322  		return nil
  1323  	}
  1324  
  1325  	if flagDefault {
  1326  		// last chance: if no value is found and a flag does exist for the key,
  1327  		// get the flag's default value even if the flag's value has not been set.
  1328  		if flag, exists := v.pflags[lcaseKey]; exists {
  1329  			switch flag.ValueType() {
  1330  			case "int", "int8", "int16", "int32", "int64":
  1331  				return cast.ToInt(flag.ValueString())
  1332  			case "bool":
  1333  				return cast.ToBool(flag.ValueString())
  1334  			case "stringSlice", "stringArray":
  1335  				s := strings.TrimPrefix(flag.ValueString(), "[")
  1336  				s = strings.TrimSuffix(s, "]")
  1337  				res, _ := readAsCSV(s)
  1338  				return res
  1339  			case "intSlice":
  1340  				s := strings.TrimPrefix(flag.ValueString(), "[")
  1341  				s = strings.TrimSuffix(s, "]")
  1342  				res, _ := readAsCSV(s)
  1343  				return cast.ToIntSlice(res)
  1344  			case "stringToString":
  1345  				return stringToStringConv(flag.ValueString())
  1346  			default:
  1347  				return flag.ValueString()
  1348  			}
  1349  		}
  1350  		// last item, no need to check shadowing
  1351  	}
  1352  
  1353  	return nil
  1354  }
  1355  
  1356  func readAsCSV(val string) ([]string, error) {
  1357  	if val == "" {
  1358  		return []string{}, nil
  1359  	}
  1360  	stringReader := strings.NewReader(val)
  1361  	csvReader := csv.NewReader(stringReader)
  1362  	return csvReader.Read()
  1363  }
  1364  
  1365  // mostly copied from pflag's implementation of this operation here https://github.com/spf13/pflag/blob/master/string_to_string.go#L79
  1366  // alterations are: errors are swallowed, map[string]interface{} is returned in order to enable cast.ToStringMap
  1367  func stringToStringConv(val string) interface{} {
  1368  	val = strings.Trim(val, "[]")
  1369  	// An empty string would cause an empty map
  1370  	if len(val) == 0 {
  1371  		return map[string]interface{}{}
  1372  	}
  1373  	r := csv.NewReader(strings.NewReader(val))
  1374  	ss, err := r.Read()
  1375  	if err != nil {
  1376  		return nil
  1377  	}
  1378  	out := make(map[string]interface{}, len(ss))
  1379  	for _, pair := range ss {
  1380  		kv := strings.SplitN(pair, "=", 2)
  1381  		if len(kv) != 2 {
  1382  			return nil
  1383  		}
  1384  		out[kv[0]] = kv[1]
  1385  	}
  1386  	return out
  1387  }
  1388  
  1389  // IsSet checks to see if the key has been set in any of the data locations.
  1390  // IsSet is case-insensitive for a key.
  1391  func IsSet(key string) bool { return v.IsSet(key) }
  1392  
  1393  func (v *Viper) IsSet(key string) bool {
  1394  	lcaseKey := strings.ToLower(key)
  1395  	val := v.find(lcaseKey, false)
  1396  	return val != nil
  1397  }
  1398  
  1399  // AutomaticEnv makes Viper check if environment variables match any of the existing keys
  1400  // (config, default or flags). If matching env vars are found, they are loaded into Viper.
  1401  func AutomaticEnv() { v.AutomaticEnv() }
  1402  
  1403  func (v *Viper) AutomaticEnv() {
  1404  	v.automaticEnvApplied = true
  1405  }
  1406  
  1407  // SetEnvKeyReplacer sets the strings.Replacer on the viper object
  1408  // Useful for mapping an environmental variable to a key that does
  1409  // not match it.
  1410  func SetEnvKeyReplacer(r *strings.Replacer) { v.SetEnvKeyReplacer(r) }
  1411  
  1412  func (v *Viper) SetEnvKeyReplacer(r *strings.Replacer) {
  1413  	v.envKeyReplacer = r
  1414  }
  1415  
  1416  // RegisterAlias creates an alias that provides another accessor for the same key.
  1417  // This enables one to change a name without breaking the application.
  1418  func RegisterAlias(alias string, key string) { v.RegisterAlias(alias, key) }
  1419  
  1420  func (v *Viper) RegisterAlias(alias string, key string) {
  1421  	v.registerAlias(alias, strings.ToLower(key))
  1422  }
  1423  
  1424  func (v *Viper) registerAlias(alias string, key string) {
  1425  	alias = strings.ToLower(alias)
  1426  	if alias != key && alias != v.realKey(key) {
  1427  		_, exists := v.aliases[alias]
  1428  
  1429  		if !exists {
  1430  			// if we alias something that exists in one of the maps to another
  1431  			// name, we'll never be able to get that value using the original
  1432  			// name, so move the config value to the new realkey.
  1433  			if val, ok := v.config[alias]; ok {
  1434  				delete(v.config, alias)
  1435  				v.config[key] = val
  1436  			}
  1437  			if val, ok := v.kvstore[alias]; ok {
  1438  				delete(v.kvstore, alias)
  1439  				v.kvstore[key] = val
  1440  			}
  1441  			if val, ok := v.defaults[alias]; ok {
  1442  				delete(v.defaults, alias)
  1443  				v.defaults[key] = val
  1444  			}
  1445  			if val, ok := v.override[alias]; ok {
  1446  				delete(v.override, alias)
  1447  				v.override[key] = val
  1448  			}
  1449  			v.aliases[alias] = key
  1450  		}
  1451  	} else {
  1452  		v.logger.Warn("creating circular reference alias", "alias", alias, "key", key, "real_key", v.realKey(key))
  1453  	}
  1454  }
  1455  
  1456  func (v *Viper) realKey(key string) string {
  1457  	newkey, exists := v.aliases[key]
  1458  	if exists {
  1459  		v.logger.Debug("key is an alias", "alias", key, "to", newkey)
  1460  
  1461  		return v.realKey(newkey)
  1462  	}
  1463  	return key
  1464  }
  1465  
  1466  // InConfig checks to see if the given key (or an alias) is in the config file.
  1467  func InConfig(key string) bool { return v.InConfig(key) }
  1468  
  1469  func (v *Viper) InConfig(key string) bool {
  1470  	lcaseKey := strings.ToLower(key)
  1471  
  1472  	// if the requested key is an alias, then return the proper key
  1473  	lcaseKey = v.realKey(lcaseKey)
  1474  	path := strings.Split(lcaseKey, v.keyDelim)
  1475  
  1476  	return v.searchIndexableWithPathPrefixes(v.config, path) != nil
  1477  }
  1478  
  1479  // SetDefault sets the default value for this key.
  1480  // SetDefault is case-insensitive for a key.
  1481  // Default only used when no value is provided by the user via flag, config or ENV.
  1482  func SetDefault(key string, value interface{}) { v.SetDefault(key, value) }
  1483  
  1484  func (v *Viper) SetDefault(key string, value interface{}) {
  1485  	// If alias passed in, then set the proper default
  1486  	key = v.realKey(strings.ToLower(key))
  1487  	value = toCaseInsensitiveValue(value)
  1488  
  1489  	path := strings.Split(key, v.keyDelim)
  1490  	lastKey := strings.ToLower(path[len(path)-1])
  1491  	deepestMap := deepSearch(v.defaults, path[0:len(path)-1])
  1492  
  1493  	// set innermost value
  1494  	deepestMap[lastKey] = value
  1495  }
  1496  
  1497  // Set sets the value for the key in the override register.
  1498  // Set is case-insensitive for a key.
  1499  // Will be used instead of values obtained via
  1500  // flags, config file, ENV, default, or key/value store.
  1501  func Set(key string, value interface{}) { v.Set(key, value) }
  1502  
  1503  func (v *Viper) Set(key string, value interface{}) {
  1504  	// If alias passed in, then set the proper override
  1505  	key = v.realKey(strings.ToLower(key))
  1506  	value = toCaseInsensitiveValue(value)
  1507  
  1508  	path := strings.Split(key, v.keyDelim)
  1509  	lastKey := strings.ToLower(path[len(path)-1])
  1510  	deepestMap := deepSearch(v.override, path[0:len(path)-1])
  1511  
  1512  	// set innermost value
  1513  	deepestMap[lastKey] = value
  1514  }
  1515  
  1516  // ReadInConfig will discover and load the configuration file from disk
  1517  // and key/value stores, searching in one of the defined paths.
  1518  func ReadInConfig() error { return v.ReadInConfig() }
  1519  
  1520  func (v *Viper) ReadInConfig() error {
  1521  	v.logger.Info("attempting to read in config file")
  1522  	filename, err := v.getConfigFile()
  1523  	if err != nil {
  1524  		return err
  1525  	}
  1526  
  1527  	if !stringInSlice(v.getConfigType(), SupportedExts) {
  1528  		return UnsupportedConfigError(v.getConfigType())
  1529  	}
  1530  
  1531  	v.logger.Debug("reading file", "file", filename)
  1532  	file, err := afero.ReadFile(v.fs, filename)
  1533  	if err != nil {
  1534  		return err
  1535  	}
  1536  
  1537  	config := make(map[string]interface{})
  1538  
  1539  	err = v.unmarshalReader(bytes.NewReader(file), config)
  1540  	if err != nil {
  1541  		return err
  1542  	}
  1543  
  1544  	v.config = config
  1545  	return nil
  1546  }
  1547  
  1548  // MergeInConfig merges a new configuration with an existing config.
  1549  func MergeInConfig() error { return v.MergeInConfig() }
  1550  
  1551  func (v *Viper) MergeInConfig() error {
  1552  	v.logger.Info("attempting to merge in config file")
  1553  	filename, err := v.getConfigFile()
  1554  	if err != nil {
  1555  		return err
  1556  	}
  1557  
  1558  	if !stringInSlice(v.getConfigType(), SupportedExts) {
  1559  		return UnsupportedConfigError(v.getConfigType())
  1560  	}
  1561  
  1562  	file, err := afero.ReadFile(v.fs, filename)
  1563  	if err != nil {
  1564  		return err
  1565  	}
  1566  
  1567  	return v.MergeConfig(bytes.NewReader(file))
  1568  }
  1569  
  1570  // ReadConfig will read a configuration file, setting existing keys to nil if the
  1571  // key does not exist in the file.
  1572  func ReadConfig(in io.Reader) error { return v.ReadConfig(in) }
  1573  
  1574  func (v *Viper) ReadConfig(in io.Reader) error {
  1575  	v.config = make(map[string]interface{})
  1576  	return v.unmarshalReader(in, v.config)
  1577  }
  1578  
  1579  // MergeConfig merges a new configuration with an existing config.
  1580  func MergeConfig(in io.Reader) error { return v.MergeConfig(in) }
  1581  
  1582  func (v *Viper) MergeConfig(in io.Reader) error {
  1583  	cfg := make(map[string]interface{})
  1584  	if err := v.unmarshalReader(in, cfg); err != nil {
  1585  		return err
  1586  	}
  1587  	return v.MergeConfigMap(cfg)
  1588  }
  1589  
  1590  // MergeConfigMap merges the configuration from the map given with an existing config.
  1591  // Note that the map given may be modified.
  1592  func MergeConfigMap(cfg map[string]interface{}) error { return v.MergeConfigMap(cfg) }
  1593  
  1594  func (v *Viper) MergeConfigMap(cfg map[string]interface{}) error {
  1595  	if v.config == nil {
  1596  		v.config = make(map[string]interface{})
  1597  	}
  1598  	insensitiviseMap(cfg)
  1599  	mergeMaps(cfg, v.config, nil)
  1600  	return nil
  1601  }
  1602  
  1603  // WriteConfig writes the current configuration to a file.
  1604  func WriteConfig() error { return v.WriteConfig() }
  1605  
  1606  func (v *Viper) WriteConfig() error {
  1607  	filename, err := v.getConfigFile()
  1608  	if err != nil {
  1609  		return err
  1610  	}
  1611  	return v.writeConfig(filename, true)
  1612  }
  1613  
  1614  // SafeWriteConfig writes current configuration to file only if the file does not exist.
  1615  func SafeWriteConfig() error { return v.SafeWriteConfig() }
  1616  
  1617  func (v *Viper) SafeWriteConfig() error {
  1618  	if len(v.configPaths) < 1 {
  1619  		return errors.New("missing configuration for 'configPath'")
  1620  	}
  1621  	return v.SafeWriteConfigAs(filepath.Join(v.configPaths[0], v.configName+"."+v.configType))
  1622  }
  1623  
  1624  // WriteConfigAs writes current configuration to a given filename.
  1625  func WriteConfigAs(filename string) error { return v.WriteConfigAs(filename) }
  1626  
  1627  func (v *Viper) WriteConfigAs(filename string) error {
  1628  	return v.writeConfig(filename, true)
  1629  }
  1630  
  1631  // SafeWriteConfigAs writes current configuration to a given filename if it does not exist.
  1632  func SafeWriteConfigAs(filename string) error { return v.SafeWriteConfigAs(filename) }
  1633  
  1634  func (v *Viper) SafeWriteConfigAs(filename string) error {
  1635  	alreadyExists, err := afero.Exists(v.fs, filename)
  1636  	if alreadyExists && err == nil {
  1637  		return ConfigFileAlreadyExistsError(filename)
  1638  	}
  1639  	return v.writeConfig(filename, false)
  1640  }
  1641  
  1642  func (v *Viper) writeConfig(filename string, force bool) error {
  1643  	v.logger.Info("attempting to write configuration to file")
  1644  
  1645  	var configType string
  1646  
  1647  	ext := filepath.Ext(filename)
  1648  	if ext != "" && ext != filepath.Base(filename) {
  1649  		configType = ext[1:]
  1650  	} else {
  1651  		configType = v.configType
  1652  	}
  1653  	if configType == "" {
  1654  		return fmt.Errorf("config type could not be determined for %s", filename)
  1655  	}
  1656  
  1657  	if !stringInSlice(configType, SupportedExts) {
  1658  		return UnsupportedConfigError(configType)
  1659  	}
  1660  	if v.config == nil {
  1661  		v.config = make(map[string]interface{})
  1662  	}
  1663  	flags := os.O_CREATE | os.O_TRUNC | os.O_WRONLY
  1664  	if !force {
  1665  		flags |= os.O_EXCL
  1666  	}
  1667  	f, err := v.fs.OpenFile(filename, flags, v.configPermissions)
  1668  	if err != nil {
  1669  		return err
  1670  	}
  1671  	defer f.Close()
  1672  
  1673  	if err := v.marshalWriter(f, configType); err != nil {
  1674  		return err
  1675  	}
  1676  
  1677  	return f.Sync()
  1678  }
  1679  
  1680  // Unmarshal a Reader into a map.
  1681  // Should probably be an unexported function.
  1682  func unmarshalReader(in io.Reader, c map[string]interface{}) error {
  1683  	return v.unmarshalReader(in, c)
  1684  }
  1685  
  1686  func (v *Viper) unmarshalReader(in io.Reader, c map[string]interface{}) error {
  1687  	buf := new(bytes.Buffer)
  1688  	buf.ReadFrom(in)
  1689  
  1690  	switch format := strings.ToLower(v.getConfigType()); format {
  1691  	case "yaml", "yml", "json", "toml", "hcl", "tfvars", "ini", "properties", "props", "prop", "dotenv", "env":
  1692  		err := v.decoderRegistry.Decode(format, buf.Bytes(), c)
  1693  		if err != nil {
  1694  			return ConfigParseError{err}
  1695  		}
  1696  	}
  1697  
  1698  	insensitiviseMap(c)
  1699  	return nil
  1700  }
  1701  
  1702  // Marshal a map into Writer.
  1703  func (v *Viper) marshalWriter(f afero.File, configType string) error {
  1704  	c := v.AllSettings()
  1705  	switch configType {
  1706  	case "yaml", "yml", "json", "toml", "hcl", "tfvars", "ini", "prop", "props", "properties", "dotenv", "env":
  1707  		b, err := v.encoderRegistry.Encode(configType, c)
  1708  		if err != nil {
  1709  			return ConfigMarshalError{err}
  1710  		}
  1711  
  1712  		_, err = f.WriteString(string(b))
  1713  		if err != nil {
  1714  			return ConfigMarshalError{err}
  1715  		}
  1716  	}
  1717  	return nil
  1718  }
  1719  
  1720  func keyExists(k string, m map[string]interface{}) string {
  1721  	lk := strings.ToLower(k)
  1722  	for mk := range m {
  1723  		lmk := strings.ToLower(mk)
  1724  		if lmk == lk {
  1725  			return mk
  1726  		}
  1727  	}
  1728  	return ""
  1729  }
  1730  
  1731  func castToMapStringInterface(
  1732  	src map[interface{}]interface{},
  1733  ) map[string]interface{} {
  1734  	tgt := map[string]interface{}{}
  1735  	for k, v := range src {
  1736  		tgt[fmt.Sprintf("%v", k)] = v
  1737  	}
  1738  	return tgt
  1739  }
  1740  
  1741  func castMapStringSliceToMapInterface(src map[string][]string) map[string]interface{} {
  1742  	tgt := map[string]interface{}{}
  1743  	for k, v := range src {
  1744  		tgt[k] = v
  1745  	}
  1746  	return tgt
  1747  }
  1748  
  1749  func castMapStringToMapInterface(src map[string]string) map[string]interface{} {
  1750  	tgt := map[string]interface{}{}
  1751  	for k, v := range src {
  1752  		tgt[k] = v
  1753  	}
  1754  	return tgt
  1755  }
  1756  
  1757  func castMapFlagToMapInterface(src map[string]FlagValue) map[string]interface{} {
  1758  	tgt := map[string]interface{}{}
  1759  	for k, v := range src {
  1760  		tgt[k] = v
  1761  	}
  1762  	return tgt
  1763  }
  1764  
  1765  // mergeMaps merges two maps. The `itgt` parameter is for handling go-yaml's
  1766  // insistence on parsing nested structures as `map[interface{}]interface{}`
  1767  // instead of using a `string` as the key for nest structures beyond one level
  1768  // deep. Both map types are supported as there is a go-yaml fork that uses
  1769  // `map[string]interface{}` instead.
  1770  func mergeMaps(
  1771  	src, tgt map[string]interface{}, itgt map[interface{}]interface{},
  1772  ) {
  1773  	for sk, sv := range src {
  1774  		tk := keyExists(sk, tgt)
  1775  		if tk == "" {
  1776  			v.logger.Trace("", "tk", "\"\"", fmt.Sprintf("tgt[%s]", sk), sv)
  1777  			tgt[sk] = sv
  1778  			if itgt != nil {
  1779  				itgt[sk] = sv
  1780  			}
  1781  			continue
  1782  		}
  1783  
  1784  		tv, ok := tgt[tk]
  1785  		if !ok {
  1786  			v.logger.Trace("", fmt.Sprintf("ok[%s]", tk), false, fmt.Sprintf("tgt[%s]", sk), sv)
  1787  			tgt[sk] = sv
  1788  			if itgt != nil {
  1789  				itgt[sk] = sv
  1790  			}
  1791  			continue
  1792  		}
  1793  
  1794  		svType := reflect.TypeOf(sv)
  1795  		tvType := reflect.TypeOf(tv)
  1796  
  1797  		v.logger.Trace(
  1798  			"processing",
  1799  			"key", sk,
  1800  			"st", svType,
  1801  			"tt", tvType,
  1802  			"sv", sv,
  1803  			"tv", tv,
  1804  		)
  1805  
  1806  		switch ttv := tv.(type) {
  1807  		case map[interface{}]interface{}:
  1808  			v.logger.Trace("merging maps (must convert)")
  1809  			tsv, ok := sv.(map[interface{}]interface{})
  1810  			if !ok {
  1811  				v.logger.Error(
  1812  					"Could not cast sv to map[interface{}]interface{}",
  1813  					"key", sk,
  1814  					"st", svType,
  1815  					"tt", tvType,
  1816  					"sv", sv,
  1817  					"tv", tv,
  1818  				)
  1819  				continue
  1820  			}
  1821  
  1822  			ssv := castToMapStringInterface(tsv)
  1823  			stv := castToMapStringInterface(ttv)
  1824  			mergeMaps(ssv, stv, ttv)
  1825  		case map[string]interface{}:
  1826  			v.logger.Trace("merging maps")
  1827  			tsv, ok := sv.(map[string]interface{})
  1828  			if !ok {
  1829  				v.logger.Error(
  1830  					"Could not cast sv to map[string]interface{}",
  1831  					"key", sk,
  1832  					"st", svType,
  1833  					"tt", tvType,
  1834  					"sv", sv,
  1835  					"tv", tv,
  1836  				)
  1837  				continue
  1838  			}
  1839  			mergeMaps(tsv, ttv, nil)
  1840  		default:
  1841  			v.logger.Trace("setting value")
  1842  			tgt[tk] = sv
  1843  			if itgt != nil {
  1844  				itgt[tk] = sv
  1845  			}
  1846  		}
  1847  	}
  1848  }
  1849  
  1850  // ReadRemoteConfig attempts to get configuration from a remote source
  1851  // and read it in the remote configuration registry.
  1852  func ReadRemoteConfig() error { return v.ReadRemoteConfig() }
  1853  
  1854  func (v *Viper) ReadRemoteConfig() error {
  1855  	return v.getKeyValueConfig()
  1856  }
  1857  
  1858  func WatchRemoteConfig() error { return v.WatchRemoteConfig() }
  1859  func (v *Viper) WatchRemoteConfig() error {
  1860  	return v.watchKeyValueConfig()
  1861  }
  1862  
  1863  func (v *Viper) WatchRemoteConfigOnChannel() error {
  1864  	return v.watchKeyValueConfigOnChannel()
  1865  }
  1866  
  1867  // Retrieve the first found remote configuration.
  1868  func (v *Viper) getKeyValueConfig() error {
  1869  	if RemoteConfig == nil {
  1870  		return RemoteConfigError("Enable the remote features by doing a blank import of the viper/remote package: '_ github.com/DominikUrban/viper/remote'")
  1871  	}
  1872  
  1873  	if len(v.remoteProviders) == 0 {
  1874  		return RemoteConfigError("No Remote Providers")
  1875  	}
  1876  
  1877  	for _, rp := range v.remoteProviders {
  1878  		val, err := v.getRemoteConfig(rp)
  1879  		if err != nil {
  1880  			v.logger.Error(fmt.Errorf("get remote config: %w", err).Error())
  1881  
  1882  			continue
  1883  		}
  1884  
  1885  		v.kvstore = val
  1886  
  1887  		return nil
  1888  	}
  1889  	return RemoteConfigError("No Files Found")
  1890  }
  1891  
  1892  func (v *Viper) getRemoteConfig(provider RemoteProvider) (map[string]interface{}, error) {
  1893  	reader, err := RemoteConfig.Get(provider)
  1894  	if err != nil {
  1895  		return nil, err
  1896  	}
  1897  	err = v.unmarshalReader(reader, v.kvstore)
  1898  	return v.kvstore, err
  1899  }
  1900  
  1901  // Retrieve the first found remote configuration.
  1902  func (v *Viper) watchKeyValueConfigOnChannel() error {
  1903  	if len(v.remoteProviders) == 0 {
  1904  		return RemoteConfigError("No Remote Providers")
  1905  	}
  1906  
  1907  	for _, rp := range v.remoteProviders {
  1908  		respc, _ := RemoteConfig.WatchChannel(rp)
  1909  		// Todo: Add quit channel
  1910  		go func(rc <-chan *RemoteResponse) {
  1911  			for {
  1912  				b := <-rc
  1913  				reader := bytes.NewReader(b.Value)
  1914  				v.unmarshalReader(reader, v.kvstore)
  1915  			}
  1916  		}(respc)
  1917  		return nil
  1918  	}
  1919  	return RemoteConfigError("No Files Found")
  1920  }
  1921  
  1922  // Retrieve the first found remote configuration.
  1923  func (v *Viper) watchKeyValueConfig() error {
  1924  	if len(v.remoteProviders) == 0 {
  1925  		return RemoteConfigError("No Remote Providers")
  1926  	}
  1927  
  1928  	for _, rp := range v.remoteProviders {
  1929  		val, err := v.watchRemoteConfig(rp)
  1930  		if err != nil {
  1931  			v.logger.Error(fmt.Errorf("watch remote config: %w", err).Error())
  1932  
  1933  			continue
  1934  		}
  1935  		v.kvstore = val
  1936  		return nil
  1937  	}
  1938  	return RemoteConfigError("No Files Found")
  1939  }
  1940  
  1941  func (v *Viper) watchRemoteConfig(provider RemoteProvider) (map[string]interface{}, error) {
  1942  	reader, err := RemoteConfig.Watch(provider)
  1943  	if err != nil {
  1944  		return nil, err
  1945  	}
  1946  	err = v.unmarshalReader(reader, v.kvstore)
  1947  	return v.kvstore, err
  1948  }
  1949  
  1950  // AllKeys returns all keys holding a value, regardless of where they are set.
  1951  // Nested keys are returned with a v.keyDelim separator
  1952  func AllKeys() []string { return v.AllKeys() }
  1953  
  1954  func (v *Viper) AllKeys() []string {
  1955  	m := map[string]bool{}
  1956  	// add all paths, by order of descending priority to ensure correct shadowing
  1957  	m = v.flattenAndMergeMap(m, castMapStringToMapInterface(v.aliases), "")
  1958  	m = v.flattenAndMergeMap(m, v.override, "")
  1959  	m = v.mergeFlatMap(m, castMapFlagToMapInterface(v.pflags))
  1960  	m = v.mergeFlatMap(m, castMapStringSliceToMapInterface(v.env))
  1961  	m = v.flattenAndMergeMap(m, v.config, "")
  1962  	m = v.flattenAndMergeMap(m, v.kvstore, "")
  1963  	m = v.flattenAndMergeMap(m, v.defaults, "")
  1964  
  1965  	// convert set of paths to list
  1966  	a := make([]string, 0, len(m))
  1967  	for x := range m {
  1968  		a = append(a, x)
  1969  	}
  1970  	return a
  1971  }
  1972  
  1973  // flattenAndMergeMap recursively flattens the given map into a map[string]bool
  1974  // of key paths (used as a set, easier to manipulate than a []string):
  1975  // - each path is merged into a single key string, delimited with v.keyDelim
  1976  // - if a path is shadowed by an earlier value in the initial shadow map,
  1977  //   it is skipped.
  1978  // The resulting set of paths is merged to the given shadow set at the same time.
  1979  func (v *Viper) flattenAndMergeMap(shadow map[string]bool, m map[string]interface{}, prefix string) map[string]bool {
  1980  	if shadow != nil && prefix != "" && shadow[prefix] {
  1981  		// prefix is shadowed => nothing more to flatten
  1982  		return shadow
  1983  	}
  1984  	if shadow == nil {
  1985  		shadow = make(map[string]bool)
  1986  	}
  1987  
  1988  	var m2 map[string]interface{}
  1989  	if prefix != "" {
  1990  		prefix += v.keyDelim
  1991  	}
  1992  	for k, val := range m {
  1993  		fullKey := prefix + k
  1994  		switch val.(type) {
  1995  		case map[string]interface{}:
  1996  			m2 = val.(map[string]interface{})
  1997  		case map[interface{}]interface{}:
  1998  			m2 = cast.ToStringMap(val)
  1999  		default:
  2000  			// immediate value
  2001  			shadow[strings.ToLower(fullKey)] = true
  2002  			continue
  2003  		}
  2004  		// recursively merge to shadow map
  2005  		shadow = v.flattenAndMergeMap(shadow, m2, fullKey)
  2006  	}
  2007  	return shadow
  2008  }
  2009  
  2010  // mergeFlatMap merges the given maps, excluding values of the second map
  2011  // shadowed by values from the first map.
  2012  func (v *Viper) mergeFlatMap(shadow map[string]bool, m map[string]interface{}) map[string]bool {
  2013  	// scan keys
  2014  outer:
  2015  	for k := range m {
  2016  		path := strings.Split(k, v.keyDelim)
  2017  		// scan intermediate paths
  2018  		var parentKey string
  2019  		for i := 1; i < len(path); i++ {
  2020  			parentKey = strings.Join(path[0:i], v.keyDelim)
  2021  			if shadow[parentKey] {
  2022  				// path is shadowed, continue
  2023  				continue outer
  2024  			}
  2025  		}
  2026  		// add key
  2027  		shadow[strings.ToLower(k)] = true
  2028  	}
  2029  	return shadow
  2030  }
  2031  
  2032  // AllSettings merges all settings and returns them as a map[string]interface{}.
  2033  func AllSettings() map[string]interface{} { return v.AllSettings() }
  2034  
  2035  func (v *Viper) AllSettings() map[string]interface{} {
  2036  	m := map[string]interface{}{}
  2037  	// start from the list of keys, and construct the map one value at a time
  2038  	for _, k := range v.AllKeys() {
  2039  		value := v.Get(k)
  2040  		if value == nil {
  2041  			// should not happen, since AllKeys() returns only keys holding a value,
  2042  			// check just in case anything changes
  2043  			continue
  2044  		}
  2045  		path := strings.Split(k, v.keyDelim)
  2046  		lastKey := strings.ToLower(path[len(path)-1])
  2047  		deepestMap := deepSearch(m, path[0:len(path)-1])
  2048  		// set innermost value
  2049  		deepestMap[lastKey] = value
  2050  	}
  2051  	return m
  2052  }
  2053  
  2054  // SetFs sets the filesystem to use to read configuration.
  2055  func SetFs(fs afero.Fs) { v.SetFs(fs) }
  2056  
  2057  func (v *Viper) SetFs(fs afero.Fs) {
  2058  	v.fs = fs
  2059  }
  2060  
  2061  // SetConfigName sets name for the config file.
  2062  // Does not include extension.
  2063  func SetConfigName(in string) { v.SetConfigName(in) }
  2064  
  2065  func (v *Viper) SetConfigName(in string) {
  2066  	if in != "" {
  2067  		v.configName = in
  2068  		v.configFile = ""
  2069  	}
  2070  }
  2071  
  2072  // SetConfigType sets the type of the configuration returned by the
  2073  // remote source, e.g. "json".
  2074  func SetConfigType(in string) { v.SetConfigType(in) }
  2075  
  2076  func (v *Viper) SetConfigType(in string) {
  2077  	if in != "" {
  2078  		v.configType = in
  2079  	}
  2080  }
  2081  
  2082  // SetConfigPermissions sets the permissions for the config file.
  2083  func SetConfigPermissions(perm os.FileMode) { v.SetConfigPermissions(perm) }
  2084  
  2085  func (v *Viper) SetConfigPermissions(perm os.FileMode) {
  2086  	v.configPermissions = perm.Perm()
  2087  }
  2088  
  2089  // IniLoadOptions sets the load options for ini parsing.
  2090  func IniLoadOptions(in ini.LoadOptions) Option {
  2091  	return optionFunc(func(v *Viper) {
  2092  		v.iniLoadOptions = in
  2093  	})
  2094  }
  2095  
  2096  func (v *Viper) getConfigType() string {
  2097  	if v.configType != "" {
  2098  		return v.configType
  2099  	}
  2100  
  2101  	cf, err := v.getConfigFile()
  2102  	if err != nil {
  2103  		return ""
  2104  	}
  2105  
  2106  	ext := filepath.Ext(cf)
  2107  
  2108  	if len(ext) > 1 {
  2109  		return ext[1:]
  2110  	}
  2111  
  2112  	return ""
  2113  }
  2114  
  2115  func (v *Viper) getConfigFile() (string, error) {
  2116  	if v.configFile == "" {
  2117  		cf, err := v.findConfigFile()
  2118  		if err != nil {
  2119  			return "", err
  2120  		}
  2121  		v.configFile = cf
  2122  	}
  2123  	return v.configFile, nil
  2124  }
  2125  
  2126  // Debug prints all configuration registries for debugging
  2127  // purposes.
  2128  func Debug() { v.Debug() }
  2129  
  2130  func (v *Viper) Debug() {
  2131  	fmt.Printf("Aliases:\n%#v\n", v.aliases)
  2132  	fmt.Printf("Override:\n%#v\n", v.override)
  2133  	fmt.Printf("PFlags:\n%#v\n", v.pflags)
  2134  	fmt.Printf("Env:\n%#v\n", v.env)
  2135  	fmt.Printf("Key/Value Store:\n%#v\n", v.kvstore)
  2136  	fmt.Printf("Config:\n%#v\n", v.config)
  2137  	fmt.Printf("Defaults:\n%#v\n", v.defaults)
  2138  }