mirror of
https://github.com/spf13/viper
synced 2025-05-06 20:27:17 +00:00
Merge remote-tracking branch 'origin/master' into Issue-105
This commit is contained in:
commit
54c2235231
6 changed files with 194 additions and 162 deletions
13
.travis.yml
13
.travis.yml
|
@ -1,8 +1,10 @@
|
||||||
|
go_import_path: github.com/spf13/viper
|
||||||
|
|
||||||
language: go
|
language: go
|
||||||
go:
|
go:
|
||||||
- 1.4.3
|
|
||||||
- 1.5.4
|
- 1.5.4
|
||||||
- 1.6.2
|
- 1.6.3
|
||||||
|
- 1.7
|
||||||
- tip
|
- tip
|
||||||
|
|
||||||
os:
|
os:
|
||||||
|
@ -12,7 +14,14 @@ os:
|
||||||
matrix:
|
matrix:
|
||||||
allow_failures:
|
allow_failures:
|
||||||
- go: tip
|
- go: tip
|
||||||
|
fast_finish: true
|
||||||
|
|
||||||
script:
|
script:
|
||||||
|
- go install ./...
|
||||||
- go test -v ./...
|
- go test -v ./...
|
||||||
|
|
||||||
|
after_success:
|
||||||
|
- go get -u -d github.com/spf13/hugo
|
||||||
|
- cd $GOPATH/src/github.com/spf13/hugo && make && ./hugo -s docs && cd -
|
||||||
|
|
||||||
sudo: false
|
sudo: false
|
||||||
|
|
|
@ -10,7 +10,7 @@ Many Go projects are built using Viper including:
|
||||||
* [Nanobox](https://github.com/nanobox-io/nanobox)/[Nanopack](https://github.com/nanopack)
|
* [Nanobox](https://github.com/nanobox-io/nanobox)/[Nanopack](https://github.com/nanopack)
|
||||||
* [Docker Notary](https://github.com/docker/Notary)
|
* [Docker Notary](https://github.com/docker/Notary)
|
||||||
* [BloomApi](https://www.bloomapi.com/)
|
* [BloomApi](https://www.bloomapi.com/)
|
||||||
* [DOIt](https://github.com/bryanl/doit)
|
* [doctl](https://github.com/digitalocean/doctl)
|
||||||
|
|
||||||
[](https://travis-ci.org/spf13/viper) [](https://gitter.im/spf13/viper?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
|
[](https://travis-ci.org/spf13/viper) [](https://gitter.im/spf13/viper?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
|
||||||
|
|
||||||
|
|
|
@ -22,7 +22,7 @@ func TestBindFlagValueSet(t *testing.T) {
|
||||||
"endpoint": "/public",
|
"endpoint": "/public",
|
||||||
}
|
}
|
||||||
|
|
||||||
for name, _ := range testValues {
|
for name := range testValues {
|
||||||
testValues[name] = flagSet.String(name, "", "test")
|
testValues[name] = flagSet.String(name, "", "test")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
44
util.go
44
util.go
|
@ -21,20 +21,20 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
"unicode"
|
"unicode"
|
||||||
|
|
||||||
"github.com/BurntSushi/toml"
|
|
||||||
"github.com/hashicorp/hcl"
|
"github.com/hashicorp/hcl"
|
||||||
"github.com/magiconair/properties"
|
"github.com/magiconair/properties"
|
||||||
|
toml "github.com/pelletier/go-toml"
|
||||||
"github.com/spf13/cast"
|
"github.com/spf13/cast"
|
||||||
jww "github.com/spf13/jwalterweatherman"
|
jww "github.com/spf13/jwalterweatherman"
|
||||||
"gopkg.in/yaml.v2"
|
"gopkg.in/yaml.v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Denotes failing to parse configuration file.
|
// ConfigParseError denotes failing to parse configuration file.
|
||||||
type ConfigParseError struct {
|
type ConfigParseError struct {
|
||||||
err error
|
err error
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns the formatted configuration error.
|
// Error returns the formatted configuration error.
|
||||||
func (pe ConfigParseError) Error() string {
|
func (pe ConfigParseError) Error() string {
|
||||||
return fmt.Sprintf("While parsing config: %s", pe.err.Error())
|
return fmt.Sprintf("While parsing config: %s", pe.err.Error())
|
||||||
}
|
}
|
||||||
|
@ -68,16 +68,16 @@ func absPathify(inPath string) string {
|
||||||
p, err := filepath.Abs(inPath)
|
p, err := filepath.Abs(inPath)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
return filepath.Clean(p)
|
return filepath.Clean(p)
|
||||||
} else {
|
|
||||||
jww.ERROR.Println("Couldn't discover absolute path")
|
|
||||||
jww.ERROR.Println(err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
jww.ERROR.Println("Couldn't discover absolute path")
|
||||||
|
jww.ERROR.Println(err)
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if File / Directory Exists
|
// Check if File / Directory Exists
|
||||||
func exists(path string) (bool, error) {
|
func exists(path string) (bool, error) {
|
||||||
_, err := os.Stat(path)
|
_, err := v.fs.Stat(path)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
return true, nil
|
return true, nil
|
||||||
}
|
}
|
||||||
|
@ -107,29 +107,6 @@ func userHomeDir() string {
|
||||||
return os.Getenv("HOME")
|
return os.Getenv("HOME")
|
||||||
}
|
}
|
||||||
|
|
||||||
func findCWD() (string, error) {
|
|
||||||
serverFile, err := filepath.Abs(os.Args[0])
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return "", fmt.Errorf("Can't get absolute path for executable: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
path := filepath.Dir(serverFile)
|
|
||||||
realFile, err := filepath.EvalSymlinks(serverFile)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
if _, err = os.Stat(serverFile + ".exe"); err == nil {
|
|
||||||
realFile = filepath.Clean(serverFile + ".exe")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if err == nil && realFile != serverFile {
|
|
||||||
path = filepath.Dir(realFile)
|
|
||||||
}
|
|
||||||
|
|
||||||
return path, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func unmarshallConfigReader(in io.Reader, c map[string]interface{}, configType string) error {
|
func unmarshallConfigReader(in io.Reader, c map[string]interface{}, configType string) error {
|
||||||
buf := new(bytes.Buffer)
|
buf := new(bytes.Buffer)
|
||||||
buf.ReadFrom(in)
|
buf.ReadFrom(in)
|
||||||
|
@ -155,9 +132,14 @@ func unmarshallConfigReader(in io.Reader, c map[string]interface{}, configType s
|
||||||
}
|
}
|
||||||
|
|
||||||
case "toml":
|
case "toml":
|
||||||
if _, err := toml.Decode(buf.String(), &c); err != nil {
|
tree, err := toml.LoadReader(buf)
|
||||||
|
if err != nil {
|
||||||
return ConfigParseError{err}
|
return ConfigParseError{err}
|
||||||
}
|
}
|
||||||
|
tmap := tree.ToMap()
|
||||||
|
for k, v := range tmap {
|
||||||
|
c[k] = v
|
||||||
|
}
|
||||||
|
|
||||||
case "properties", "props", "prop":
|
case "properties", "props", "prop":
|
||||||
var p *properties.Properties
|
var p *properties.Properties
|
||||||
|
|
254
viper.go
254
viper.go
|
@ -23,7 +23,6 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
@ -33,6 +32,7 @@ import (
|
||||||
|
|
||||||
"github.com/fsnotify/fsnotify"
|
"github.com/fsnotify/fsnotify"
|
||||||
"github.com/mitchellh/mapstructure"
|
"github.com/mitchellh/mapstructure"
|
||||||
|
"github.com/spf13/afero"
|
||||||
"github.com/spf13/cast"
|
"github.com/spf13/cast"
|
||||||
jww "github.com/spf13/jwalterweatherman"
|
jww "github.com/spf13/jwalterweatherman"
|
||||||
"github.com/spf13/pflag"
|
"github.com/spf13/pflag"
|
||||||
|
@ -52,40 +52,40 @@ type remoteConfigFactory interface {
|
||||||
// RemoteConfig is optional, see the remote package
|
// RemoteConfig is optional, see the remote package
|
||||||
var RemoteConfig remoteConfigFactory
|
var RemoteConfig remoteConfigFactory
|
||||||
|
|
||||||
// Denotes encountering an unsupported
|
// UnsupportedConfigError denotes encountering an unsupported
|
||||||
// configuration filetype.
|
// configuration filetype.
|
||||||
type UnsupportedConfigError string
|
type UnsupportedConfigError string
|
||||||
|
|
||||||
// Returns the formatted configuration error.
|
// Error returns the formatted configuration error.
|
||||||
func (str UnsupportedConfigError) Error() string {
|
func (str UnsupportedConfigError) Error() string {
|
||||||
return fmt.Sprintf("Unsupported Config Type %q", string(str))
|
return fmt.Sprintf("Unsupported Config Type %q", string(str))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Denotes encountering an unsupported remote
|
// UnsupportedRemoteProviderError denotes encountering an unsupported remote
|
||||||
// provider. Currently only etcd and Consul are
|
// provider. Currently only etcd and Consul are
|
||||||
// supported.
|
// supported.
|
||||||
type UnsupportedRemoteProviderError string
|
type UnsupportedRemoteProviderError string
|
||||||
|
|
||||||
// Returns the formatted remote provider error.
|
// Error returns the formatted remote provider error.
|
||||||
func (str UnsupportedRemoteProviderError) Error() string {
|
func (str UnsupportedRemoteProviderError) Error() string {
|
||||||
return fmt.Sprintf("Unsupported Remote Provider Type %q", string(str))
|
return fmt.Sprintf("Unsupported Remote Provider Type %q", string(str))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Denotes encountering an error while trying to
|
// RemoteConfigError denotes encountering an error while trying to
|
||||||
// pull the configuration from the remote provider.
|
// pull the configuration from the remote provider.
|
||||||
type RemoteConfigError string
|
type RemoteConfigError string
|
||||||
|
|
||||||
// Returns the formatted remote provider error
|
// Error returns the formatted remote provider error
|
||||||
func (rce RemoteConfigError) Error() string {
|
func (rce RemoteConfigError) Error() string {
|
||||||
return fmt.Sprintf("Remote Configurations Error: %s", string(rce))
|
return fmt.Sprintf("Remote Configurations Error: %s", string(rce))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Denotes failing to find configuration file.
|
// ConfigFileNotFoundError denotes failing to find configuration file.
|
||||||
type ConfigFileNotFoundError struct {
|
type ConfigFileNotFoundError struct {
|
||||||
name, locations string
|
name, locations string
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns the formatted configuration error.
|
// Error returns the formatted configuration error.
|
||||||
func (fnfe ConfigFileNotFoundError) Error() string {
|
func (fnfe ConfigFileNotFoundError) Error() string {
|
||||||
return fmt.Sprintf("Config File %q Not Found in %q", fnfe.name, fnfe.locations)
|
return fmt.Sprintf("Config File %q Not Found in %q", fnfe.name, fnfe.locations)
|
||||||
}
|
}
|
||||||
|
@ -132,6 +132,9 @@ type Viper struct {
|
||||||
// A set of paths to look for the config file in
|
// A set of paths to look for the config file in
|
||||||
configPaths []string
|
configPaths []string
|
||||||
|
|
||||||
|
// The filesystem to read config from.
|
||||||
|
fs afero.Fs
|
||||||
|
|
||||||
// A set of remote providers to search for the configuration
|
// A set of remote providers to search for the configuration
|
||||||
remoteProviders []*defaultRemoteProvider
|
remoteProviders []*defaultRemoteProvider
|
||||||
|
|
||||||
|
@ -156,11 +159,12 @@ type Viper struct {
|
||||||
onConfigChange func(fsnotify.Event)
|
onConfigChange func(fsnotify.Event)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns an initialized Viper instance.
|
// New returns an initialized Viper instance.
|
||||||
func New() *Viper {
|
func New() *Viper {
|
||||||
v := new(Viper)
|
v := new(Viper)
|
||||||
v.keyDelim = "."
|
v.keyDelim = "."
|
||||||
v.configName = "config"
|
v.configName = "config"
|
||||||
|
v.fs = afero.NewOsFs()
|
||||||
v.config = make(map[string]interface{})
|
v.config = make(map[string]interface{})
|
||||||
v.override = make(map[string]interface{})
|
v.override = make(map[string]interface{})
|
||||||
v.defaults = make(map[string]interface{})
|
v.defaults = make(map[string]interface{})
|
||||||
|
@ -216,11 +220,11 @@ type RemoteProvider interface {
|
||||||
SecretKeyring() string
|
SecretKeyring() string
|
||||||
}
|
}
|
||||||
|
|
||||||
// Universally supported extensions.
|
// SupportedExts are universally supported extensions.
|
||||||
var SupportedExts []string = []string{"json", "toml", "yaml", "yml", "properties", "props", "prop", "hcl"}
|
var SupportedExts = []string{"json", "toml", "yaml", "yml", "properties", "props", "prop", "hcl"}
|
||||||
|
|
||||||
// Universally supported remote providers.
|
// SupportedRemoteProviders are universally supported remote providers.
|
||||||
var SupportedRemoteProviders []string = []string{"etcd", "consul"}
|
var SupportedRemoteProviders = []string{"etcd", "consul"}
|
||||||
|
|
||||||
func OnConfigChange(run func(in fsnotify.Event)) { v.OnConfigChange(run) }
|
func OnConfigChange(run func(in fsnotify.Event)) { v.OnConfigChange(run) }
|
||||||
func (v *Viper) OnConfigChange(run func(in fsnotify.Event)) {
|
func (v *Viper) OnConfigChange(run func(in fsnotify.Event)) {
|
||||||
|
@ -266,7 +270,7 @@ func (v *Viper) WatchConfig() {
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Explicitly define the path, name and extension of the config file
|
// SetConfigFile explicitly defines the path, name and extension of the config file
|
||||||
// Viper will use this and not check any of the config paths
|
// Viper will use this and not check any of the config paths
|
||||||
func SetConfigFile(in string) { v.SetConfigFile(in) }
|
func SetConfigFile(in string) { v.SetConfigFile(in) }
|
||||||
func (v *Viper) SetConfigFile(in string) {
|
func (v *Viper) SetConfigFile(in string) {
|
||||||
|
@ -275,7 +279,7 @@ func (v *Viper) SetConfigFile(in string) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Define a prefix that ENVIRONMENT variables will use.
|
// SetEnvPrefix defines a prefix that ENVIRONMENT variables will use.
|
||||||
// E.g. if your prefix is "spf", the env registry
|
// E.g. if your prefix is "spf", the env registry
|
||||||
// will look for env. variables that start with "SPF_"
|
// will look for env. variables that start with "SPF_"
|
||||||
func SetEnvPrefix(in string) { v.SetEnvPrefix(in) }
|
func SetEnvPrefix(in string) { v.SetEnvPrefix(in) }
|
||||||
|
@ -297,7 +301,7 @@ func (v *Viper) mergeWithEnvPrefix(in string) string {
|
||||||
// rewriting keys many things, Ex: Get('someKey') -> some_key
|
// rewriting keys many things, Ex: Get('someKey') -> some_key
|
||||||
// (cammel case to snake case for JSON keys perhaps)
|
// (cammel case to snake case for JSON keys perhaps)
|
||||||
|
|
||||||
// getEnv s a wrapper around os.Getenv which replaces characters in the original
|
// getEnv is a wrapper around os.Getenv which replaces characters in the original
|
||||||
// key. This allows env vars which have different keys then the config object
|
// key. This allows env vars which have different keys then the config object
|
||||||
// keys
|
// keys
|
||||||
func (v *Viper) getEnv(key string) string {
|
func (v *Viper) getEnv(key string) string {
|
||||||
|
@ -307,11 +311,11 @@ func (v *Viper) getEnv(key string) string {
|
||||||
return os.Getenv(key)
|
return os.Getenv(key)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return the file used to populate the config registry
|
// ConfigFileUsed returns the file used to populate the config registry
|
||||||
func ConfigFileUsed() string { return v.ConfigFileUsed() }
|
func ConfigFileUsed() string { return v.ConfigFileUsed() }
|
||||||
func (v *Viper) ConfigFileUsed() string { return v.configFile }
|
func (v *Viper) ConfigFileUsed() string { return v.configFile }
|
||||||
|
|
||||||
// Add a path for Viper to search for the config file in.
|
// AddConfigPath adds a path for Viper to search for the config file in.
|
||||||
// Can be called multiple times to define multiple search paths.
|
// Can be called multiple times to define multiple search paths.
|
||||||
func AddConfigPath(in string) { v.AddConfigPath(in) }
|
func AddConfigPath(in string) { v.AddConfigPath(in) }
|
||||||
func (v *Viper) AddConfigPath(in string) {
|
func (v *Viper) AddConfigPath(in string) {
|
||||||
|
@ -446,8 +450,12 @@ func (v *Viper) SetTypeByDefaultValue(enable bool) {
|
||||||
v.typeByDefValue = enable
|
v.typeByDefValue = enable
|
||||||
}
|
}
|
||||||
|
|
||||||
// Viper is essentially repository for configurations
|
// GetViper gets the global Viper instance.
|
||||||
// Get can retrieve any value given the key to use
|
func GetViper() *Viper {
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get can retrieve any value given the key to use.
|
||||||
// Get has the behavior of returning the value associated with the first
|
// Get has the behavior of returning the value associated with the first
|
||||||
// place from where it is set. Viper will check in the following order:
|
// place from where it is set. Viper will check in the following order:
|
||||||
// override, flag, env, config file, key/value store, default
|
// override, flag, env, config file, key/value store, default
|
||||||
|
@ -520,7 +528,7 @@ func (v *Viper) Get(key string) interface{} {
|
||||||
return val
|
return val
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns new Viper instance representing a sub tree of this instance
|
// Sub returns new Viper instance representing a sub tree of this instance.
|
||||||
func Sub(key string) *Viper { return v.Sub(key) }
|
func Sub(key string) *Viper { return v.Sub(key) }
|
||||||
func (v *Viper) Sub(key string) *Viper {
|
func (v *Viper) Sub(key string) *Viper {
|
||||||
subv := New()
|
subv := New()
|
||||||
|
@ -528,72 +536,77 @@ func (v *Viper) Sub(key string) *Viper {
|
||||||
if reflect.TypeOf(data).Kind() == reflect.Map {
|
if reflect.TypeOf(data).Kind() == reflect.Map {
|
||||||
subv.config = cast.ToStringMap(data)
|
subv.config = cast.ToStringMap(data)
|
||||||
return subv
|
return subv
|
||||||
} else {
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns the value associated with the key as a string
|
// GetString returns the value associated with the key as a string.
|
||||||
func GetString(key string) string { return v.GetString(key) }
|
func GetString(key string) string { return v.GetString(key) }
|
||||||
func (v *Viper) GetString(key string) string {
|
func (v *Viper) GetString(key string) string {
|
||||||
return cast.ToString(v.Get(key))
|
return cast.ToString(v.Get(key))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns the value associated with the key as a boolean
|
// GetBool returns the value associated with the key as a boolean.
|
||||||
func GetBool(key string) bool { return v.GetBool(key) }
|
func GetBool(key string) bool { return v.GetBool(key) }
|
||||||
func (v *Viper) GetBool(key string) bool {
|
func (v *Viper) GetBool(key string) bool {
|
||||||
return cast.ToBool(v.Get(key))
|
return cast.ToBool(v.Get(key))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns the value associated with the key as an integer
|
// GetInt returns the value associated with the key as an integer.
|
||||||
func GetInt(key string) int { return v.GetInt(key) }
|
func GetInt(key string) int { return v.GetInt(key) }
|
||||||
func (v *Viper) GetInt(key string) int {
|
func (v *Viper) GetInt(key string) int {
|
||||||
return cast.ToInt(v.Get(key))
|
return cast.ToInt(v.Get(key))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns the value associated with the key as a float64
|
// GetInt64 returns the value associated with the key as an integer.
|
||||||
|
func GetInt64(key string) int64 { return v.GetInt64(key) }
|
||||||
|
func (v *Viper) GetInt64(key string) int64 {
|
||||||
|
return cast.ToInt64(v.Get(key))
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetFloat64 returns the value associated with the key as a float64.
|
||||||
func GetFloat64(key string) float64 { return v.GetFloat64(key) }
|
func GetFloat64(key string) float64 { return v.GetFloat64(key) }
|
||||||
func (v *Viper) GetFloat64(key string) float64 {
|
func (v *Viper) GetFloat64(key string) float64 {
|
||||||
return cast.ToFloat64(v.Get(key))
|
return cast.ToFloat64(v.Get(key))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns the value associated with the key as time
|
// GetTime returns the value associated with the key as time.
|
||||||
func GetTime(key string) time.Time { return v.GetTime(key) }
|
func GetTime(key string) time.Time { return v.GetTime(key) }
|
||||||
func (v *Viper) GetTime(key string) time.Time {
|
func (v *Viper) GetTime(key string) time.Time {
|
||||||
return cast.ToTime(v.Get(key))
|
return cast.ToTime(v.Get(key))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns the value associated with the key as a duration
|
// GetDuration returns the value associated with the key as a duration.
|
||||||
func GetDuration(key string) time.Duration { return v.GetDuration(key) }
|
func GetDuration(key string) time.Duration { return v.GetDuration(key) }
|
||||||
func (v *Viper) GetDuration(key string) time.Duration {
|
func (v *Viper) GetDuration(key string) time.Duration {
|
||||||
return cast.ToDuration(v.Get(key))
|
return cast.ToDuration(v.Get(key))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns the value associated with the key as a slice of strings
|
// GetStringSlice returns the value associated with the key as a slice of strings.
|
||||||
func GetStringSlice(key string) []string { return v.GetStringSlice(key) }
|
func GetStringSlice(key string) []string { return v.GetStringSlice(key) }
|
||||||
func (v *Viper) GetStringSlice(key string) []string {
|
func (v *Viper) GetStringSlice(key string) []string {
|
||||||
return cast.ToStringSlice(v.Get(key))
|
return cast.ToStringSlice(v.Get(key))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns the value associated with the key as a map of interfaces
|
// GetStringMap returns the value associated with the key as a map of interfaces.
|
||||||
func GetStringMap(key string) map[string]interface{} { return v.GetStringMap(key) }
|
func GetStringMap(key string) map[string]interface{} { return v.GetStringMap(key) }
|
||||||
func (v *Viper) GetStringMap(key string) map[string]interface{} {
|
func (v *Viper) GetStringMap(key string) map[string]interface{} {
|
||||||
return cast.ToStringMap(v.Get(key))
|
return cast.ToStringMap(v.Get(key))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns the value associated with the key as a map of strings
|
// GetStringMapString returns the value associated with the key as a map of strings.
|
||||||
func GetStringMapString(key string) map[string]string { return v.GetStringMapString(key) }
|
func GetStringMapString(key string) map[string]string { return v.GetStringMapString(key) }
|
||||||
func (v *Viper) GetStringMapString(key string) map[string]string {
|
func (v *Viper) GetStringMapString(key string) map[string]string {
|
||||||
return cast.ToStringMapString(v.Get(key))
|
return cast.ToStringMapString(v.Get(key))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns the value associated with the key as a map to a slice of strings.
|
// GetStringMapStringSlice returns the value associated with the key as a map to a slice of strings.
|
||||||
func GetStringMapStringSlice(key string) map[string][]string { return v.GetStringMapStringSlice(key) }
|
func GetStringMapStringSlice(key string) map[string][]string { return v.GetStringMapStringSlice(key) }
|
||||||
func (v *Viper) GetStringMapStringSlice(key string) map[string][]string {
|
func (v *Viper) GetStringMapStringSlice(key string) map[string][]string {
|
||||||
return cast.ToStringMapStringSlice(v.Get(key))
|
return cast.ToStringMapStringSlice(v.Get(key))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns the size of the value associated with the given key
|
// GetSizeInBytes returns the size of the value associated with the given key
|
||||||
// in bytes.
|
// in bytes.
|
||||||
func GetSizeInBytes(key string) uint { return v.GetSizeInBytes(key) }
|
func GetSizeInBytes(key string) uint { return v.GetSizeInBytes(key) }
|
||||||
func (v *Viper) GetSizeInBytes(key string) uint {
|
func (v *Viper) GetSizeInBytes(key string) uint {
|
||||||
|
@ -601,13 +614,13 @@ func (v *Viper) GetSizeInBytes(key string) uint {
|
||||||
return parseSizeInBytes(sizeStr)
|
return parseSizeInBytes(sizeStr)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Takes a single key and unmarshals it into a Struct
|
// UnmarshalKey takes a single key and unmarshals it into a Struct.
|
||||||
func UnmarshalKey(key string, rawVal interface{}) error { return v.UnmarshalKey(key, rawVal) }
|
func UnmarshalKey(key string, rawVal interface{}) error { return v.UnmarshalKey(key, rawVal) }
|
||||||
func (v *Viper) UnmarshalKey(key string, rawVal interface{}) error {
|
func (v *Viper) UnmarshalKey(key string, rawVal interface{}) error {
|
||||||
return mapstructure.Decode(v.Get(key), rawVal)
|
return mapstructure.Decode(v.Get(key), rawVal)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Unmarshals the config into a Struct. Make sure that the tags
|
// Unmarshal unmarshals the config into a Struct. Make sure that the tags
|
||||||
// on the fields of the structure are properly set.
|
// on the fields of the structure are properly set.
|
||||||
func Unmarshal(rawVal interface{}) error { return v.Unmarshal(rawVal) }
|
func Unmarshal(rawVal interface{}) error { return v.Unmarshal(rawVal) }
|
||||||
func (v *Viper) Unmarshal(rawVal interface{}) error {
|
func (v *Viper) Unmarshal(rawVal interface{}) error {
|
||||||
|
@ -642,8 +655,8 @@ func decode(input interface{}, config *mapstructure.DecoderConfig) error {
|
||||||
return decoder.Decode(input)
|
return decoder.Decode(input)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Unmarshals the config into a Struct, erroring if a field is non-existant
|
// UnmarshalExact unmarshals the config into a Struct, erroring if a field is nonexistent
|
||||||
// in the destination struct
|
// in the destination struct.
|
||||||
func (v *Viper) UnmarshalExact(rawVal interface{}) error {
|
func (v *Viper) UnmarshalExact(rawVal interface{}) error {
|
||||||
config := defaultDecoderConfig(rawVal)
|
config := defaultDecoderConfig(rawVal)
|
||||||
config.ErrorUnused = true
|
config.ErrorUnused = true
|
||||||
|
@ -659,27 +672,27 @@ func (v *Viper) UnmarshalExact(rawVal interface{}) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Bind a full flag set to the configuration, using each flag's long
|
// BindPFlags binds a full flag set to the configuration, using each flag's long
|
||||||
// name as the config key.
|
// name as the config key.
|
||||||
func BindPFlags(flags *pflag.FlagSet) (err error) { return v.BindPFlags(flags) }
|
func BindPFlags(flags *pflag.FlagSet) error { return v.BindPFlags(flags) }
|
||||||
func (v *Viper) BindPFlags(flags *pflag.FlagSet) (err error) {
|
func (v *Viper) BindPFlags(flags *pflag.FlagSet) error {
|
||||||
return v.BindFlagValues(pflagValueSet{flags})
|
return v.BindFlagValues(pflagValueSet{flags})
|
||||||
}
|
}
|
||||||
|
|
||||||
// Bind a specific key to a pflag (as used by cobra)
|
// BindPFlag binds a specific key to a pflag (as used by cobra).
|
||||||
// Example(where serverCmd is a Cobra instance):
|
// Example (where serverCmd is a Cobra instance):
|
||||||
//
|
//
|
||||||
// serverCmd.Flags().Int("port", 1138, "Port to run Application server on")
|
// serverCmd.Flags().Int("port", 1138, "Port to run Application server on")
|
||||||
// Viper.BindPFlag("port", serverCmd.Flags().Lookup("port"))
|
// Viper.BindPFlag("port", serverCmd.Flags().Lookup("port"))
|
||||||
//
|
//
|
||||||
func BindPFlag(key string, flag *pflag.Flag) (err error) { return v.BindPFlag(key, flag) }
|
func BindPFlag(key string, flag *pflag.Flag) error { return v.BindPFlag(key, flag) }
|
||||||
func (v *Viper) BindPFlag(key string, flag *pflag.Flag) (err error) {
|
func (v *Viper) BindPFlag(key string, flag *pflag.Flag) error {
|
||||||
return v.BindFlagValue(key, pflagValue{flag})
|
return v.BindFlagValue(key, pflagValue{flag})
|
||||||
}
|
}
|
||||||
|
|
||||||
// Bind a full FlagValue set to the configuration, using each flag's long
|
// BindFlagValues binds a full FlagValue set to the configuration, using each flag's long
|
||||||
// name as the config key.
|
// name as the config key.
|
||||||
func BindFlagValues(flags FlagValueSet) (err error) { return v.BindFlagValues(flags) }
|
func BindFlagValues(flags FlagValueSet) error { return v.BindFlagValues(flags) }
|
||||||
func (v *Viper) BindFlagValues(flags FlagValueSet) (err error) {
|
func (v *Viper) BindFlagValues(flags FlagValueSet) (err error) {
|
||||||
flags.VisitAll(func(flag FlagValue) {
|
flags.VisitAll(func(flag FlagValue) {
|
||||||
if err = v.BindFlagValue(flag.Name(), flag); err != nil {
|
if err = v.BindFlagValue(flag.Name(), flag); err != nil {
|
||||||
|
@ -689,14 +702,14 @@ func (v *Viper) BindFlagValues(flags FlagValueSet) (err error) {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Bind a specific key to a FlagValue.
|
// BindFlagValue binds a specific key to a FlagValue.
|
||||||
// Example(where serverCmd is a Cobra instance):
|
// Example(where serverCmd is a Cobra instance):
|
||||||
//
|
//
|
||||||
// serverCmd.Flags().Int("port", 1138, "Port to run Application server on")
|
// serverCmd.Flags().Int("port", 1138, "Port to run Application server on")
|
||||||
// Viper.BindFlagValue("port", serverCmd.Flags().Lookup("port"))
|
// Viper.BindFlagValue("port", serverCmd.Flags().Lookup("port"))
|
||||||
//
|
//
|
||||||
func BindFlagValue(key string, flag FlagValue) (err error) { return v.BindFlagValue(key, flag) }
|
func BindFlagValue(key string, flag FlagValue) error { return v.BindFlagValue(key, flag) }
|
||||||
func (v *Viper) BindFlagValue(key string, flag FlagValue) (err error) {
|
func (v *Viper) BindFlagValue(key string, flag FlagValue) error {
|
||||||
if flag == nil {
|
if flag == nil {
|
||||||
return fmt.Errorf("flag for %q is nil", key)
|
return fmt.Errorf("flag for %q is nil", key)
|
||||||
}
|
}
|
||||||
|
@ -704,12 +717,12 @@ func (v *Viper) BindFlagValue(key string, flag FlagValue) (err error) {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Binds a Viper key to a ENV variable
|
// BindEnv binds a Viper key to a ENV variable.
|
||||||
// ENV variables are case sensitive
|
// ENV variables are case sensitive.
|
||||||
// If only a key is provided, it will use the env key matching the key, uppercased.
|
// If only a key is provided, it will use the env key matching the key, uppercased.
|
||||||
// EnvPrefix will be used when set when env name is not provided.
|
// EnvPrefix will be used when set when env name is not provided.
|
||||||
func BindEnv(input ...string) (err error) { return v.BindEnv(input...) }
|
func BindEnv(input ...string) error { return v.BindEnv(input...) }
|
||||||
func (v *Viper) BindEnv(input ...string) (err error) {
|
func (v *Viper) BindEnv(input ...string) error {
|
||||||
var key, envkey string
|
var key, envkey string
|
||||||
if len(input) == 0 {
|
if len(input) == 0 {
|
||||||
return fmt.Errorf("BindEnv missing key to bind to")
|
return fmt.Errorf("BindEnv missing key to bind to")
|
||||||
|
@ -728,10 +741,10 @@ func (v *Viper) BindEnv(input ...string) (err error) {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Given a key, find the value
|
// Given a key, find the value.
|
||||||
// Viper will check in the following order:
|
// Viper will check in the following order:
|
||||||
// flag, env, config file, key/value store, default
|
// flag, env, config file, key/value store, default.
|
||||||
// Viper will check to see if an alias exists first
|
// Viper will check to see if an alias exists first.
|
||||||
func (v *Viper) find(key string) interface{} {
|
func (v *Viper) find(key string) interface{} {
|
||||||
var val interface{}
|
var val interface{}
|
||||||
var exists bool
|
var exists bool
|
||||||
|
@ -742,12 +755,15 @@ func (v *Viper) find(key string) interface{} {
|
||||||
// PFlag Override first
|
// PFlag Override first
|
||||||
flag, exists := v.pflags[key]
|
flag, exists := v.pflags[key]
|
||||||
if exists && flag.HasChanged() {
|
if exists && flag.HasChanged() {
|
||||||
jww.TRACE.Println(key, "found in override (via pflag):", flag.ValueString())
|
jww.TRACE.Printf("%q found in pflag override (%s): %s", key, flag.ValueType(), flag.ValueString())
|
||||||
switch flag.ValueType() {
|
switch flag.ValueType() {
|
||||||
case "int", "int8", "int16", "int32", "int64":
|
case "int", "int8", "int16", "int32", "int64":
|
||||||
return cast.ToInt(flag.ValueString())
|
return cast.ToInt(flag.ValueString())
|
||||||
case "bool":
|
case "bool":
|
||||||
return cast.ToBool(flag.ValueString())
|
return cast.ToBool(flag.ValueString())
|
||||||
|
case "stringSlice":
|
||||||
|
s := strings.TrimPrefix(flag.ValueString(), "[")
|
||||||
|
return strings.TrimSuffix(s, "]")
|
||||||
default:
|
default:
|
||||||
return flag.ValueString()
|
return flag.ValueString()
|
||||||
}
|
}
|
||||||
|
@ -755,7 +771,7 @@ func (v *Viper) find(key string) interface{} {
|
||||||
|
|
||||||
val, exists = v.override[key]
|
val, exists = v.override[key]
|
||||||
if exists {
|
if exists {
|
||||||
jww.TRACE.Println(key, "found in override:", val)
|
jww.TRACE.Printf("%q found in override: %s", key, val)
|
||||||
return val
|
return val
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -763,25 +779,24 @@ func (v *Viper) find(key string) interface{} {
|
||||||
// even if it hasn't been registered, if automaticEnv is used,
|
// even if it hasn't been registered, if automaticEnv is used,
|
||||||
// check any Get request
|
// check any Get request
|
||||||
if val = v.getEnv(v.mergeWithEnvPrefix(key)); val != "" {
|
if val = v.getEnv(v.mergeWithEnvPrefix(key)); val != "" {
|
||||||
jww.TRACE.Println(key, "found in environment with val:", val)
|
jww.TRACE.Printf("%q found in environment: %s", key, val)
|
||||||
return val
|
return val
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
envkey, exists := v.env[key]
|
envkey, exists := v.env[key]
|
||||||
if exists {
|
if exists {
|
||||||
jww.TRACE.Println(key, "registered as env var", envkey)
|
jww.TRACE.Printf("%q registered as env var %q", key, envkey)
|
||||||
if val = v.getEnv(envkey); val != "" {
|
if val = v.getEnv(envkey); val != "" {
|
||||||
jww.TRACE.Println(envkey, "found in environment with val:", val)
|
jww.TRACE.Printf("%q found in environment: %s", envkey, val)
|
||||||
return val
|
return val
|
||||||
} else {
|
|
||||||
jww.TRACE.Println(envkey, "env value unset:")
|
|
||||||
}
|
}
|
||||||
|
jww.TRACE.Printf("%q env value unset", envkey)
|
||||||
}
|
}
|
||||||
|
|
||||||
val, exists = v.config[key]
|
val, exists = v.config[key]
|
||||||
if exists {
|
if exists {
|
||||||
jww.TRACE.Println(key, "found in config:", val)
|
jww.TRACE.Printf("%q found in config (%T): %s", key, val, val)
|
||||||
return val
|
return val
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -793,28 +808,30 @@ func (v *Viper) find(key string) interface{} {
|
||||||
if source != nil {
|
if source != nil {
|
||||||
if reflect.TypeOf(source).Kind() == reflect.Map {
|
if reflect.TypeOf(source).Kind() == reflect.Map {
|
||||||
val := v.searchMap(cast.ToStringMap(source), path[1:])
|
val := v.searchMap(cast.ToStringMap(source), path[1:])
|
||||||
jww.TRACE.Println(key, "found in nested config:", val)
|
if val != nil {
|
||||||
return val
|
jww.TRACE.Printf("%q found in nested config: %s", key, val)
|
||||||
|
return val
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
val, exists = v.kvstore[key]
|
val, exists = v.kvstore[key]
|
||||||
if exists {
|
if exists {
|
||||||
jww.TRACE.Println(key, "found in key/value store:", val)
|
jww.TRACE.Printf("%q found in key/value store: %s", key, val)
|
||||||
return val
|
return val
|
||||||
}
|
}
|
||||||
|
|
||||||
val, exists = v.defaults[key]
|
val, exists = v.defaults[key]
|
||||||
if exists {
|
if exists {
|
||||||
jww.TRACE.Println(key, "found in defaults:", val)
|
jww.TRACE.Printf("%q found in defaults: ", key, val)
|
||||||
return val
|
return val
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check to see if the key has been set in any of the data locations
|
// IsSet checks to see if the key has been set in any of the data locations.
|
||||||
func IsSet(key string) bool { return v.IsSet(key) }
|
func IsSet(key string) bool { return v.IsSet(key) }
|
||||||
func (v *Viper) IsSet(key string) bool {
|
func (v *Viper) IsSet(key string) bool {
|
||||||
path := strings.Split(key, v.keyDelim)
|
path := strings.Split(key, v.keyDelim)
|
||||||
|
@ -834,7 +851,7 @@ func (v *Viper) IsSet(key string) bool {
|
||||||
return val != nil
|
return val != nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Have Viper check ENV variables for all
|
// AutomaticEnv has Viper check ENV variables for all.
|
||||||
// keys set in config, default & flags
|
// keys set in config, default & flags
|
||||||
func AutomaticEnv() { v.AutomaticEnv() }
|
func AutomaticEnv() { v.AutomaticEnv() }
|
||||||
func (v *Viper) AutomaticEnv() {
|
func (v *Viper) AutomaticEnv() {
|
||||||
|
@ -893,12 +910,11 @@ func (v *Viper) realKey(key string) string {
|
||||||
if exists {
|
if exists {
|
||||||
jww.DEBUG.Println("Alias", key, "to", newkey)
|
jww.DEBUG.Println("Alias", key, "to", newkey)
|
||||||
return v.realKey(newkey)
|
return v.realKey(newkey)
|
||||||
} else {
|
|
||||||
return key
|
|
||||||
}
|
}
|
||||||
|
return key
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check to see if the given key (or an alias) is in the config file
|
// InConfig checks to see if the given key (or an alias) is in the config file.
|
||||||
func InConfig(key string) bool { return v.InConfig(key) }
|
func InConfig(key string) bool { return v.InConfig(key) }
|
||||||
func (v *Viper) InConfig(key string) bool {
|
func (v *Viper) InConfig(key string) bool {
|
||||||
// if the requested key is an alias, then return the proper key
|
// if the requested key is an alias, then return the proper key
|
||||||
|
@ -908,7 +924,7 @@ func (v *Viper) InConfig(key string) bool {
|
||||||
return exists
|
return exists
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set the default value for this key.
|
// SetDefault sets the default value for this key.
|
||||||
// Default only used when no value is provided by the user via flag, config or ENV.
|
// Default only used when no value is provided by the user via flag, config or ENV.
|
||||||
func SetDefault(key string, value interface{}) { v.SetDefault(key, value) }
|
func SetDefault(key string, value interface{}) { v.SetDefault(key, value) }
|
||||||
func (v *Viper) SetDefault(key string, value interface{}) {
|
func (v *Viper) SetDefault(key string, value interface{}) {
|
||||||
|
@ -917,9 +933,9 @@ func (v *Viper) SetDefault(key string, value interface{}) {
|
||||||
v.defaults[key] = value
|
v.defaults[key] = value
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sets the value for the key in the override regiser.
|
// Set sets the value for the key in the override regiser.
|
||||||
// Will be used instead of values obtained via
|
// Will be used instead of values obtained via
|
||||||
// flags, config file, ENV, default, or key/value store
|
// flags, config file, ENV, default, or key/value store.
|
||||||
func Set(key string, value interface{}) { v.Set(key, value) }
|
func Set(key string, value interface{}) { v.Set(key, value) }
|
||||||
func (v *Viper) Set(key string, value interface{}) {
|
func (v *Viper) Set(key string, value interface{}) {
|
||||||
// If alias passed in, then set the proper override
|
// If alias passed in, then set the proper override
|
||||||
|
@ -927,7 +943,7 @@ func (v *Viper) Set(key string, value interface{}) {
|
||||||
v.override[key] = value
|
v.override[key] = value
|
||||||
}
|
}
|
||||||
|
|
||||||
// Viper will discover and load the configuration file from disk
|
// ReadInConfig will discover and load the configuration file from disk
|
||||||
// and key/value stores, searching in one of the defined paths.
|
// and key/value stores, searching in one of the defined paths.
|
||||||
func ReadInConfig() error { return v.ReadInConfig() }
|
func ReadInConfig() error { return v.ReadInConfig() }
|
||||||
func (v *Viper) ReadInConfig() error {
|
func (v *Viper) ReadInConfig() error {
|
||||||
|
@ -936,7 +952,7 @@ func (v *Viper) ReadInConfig() error {
|
||||||
return UnsupportedConfigError(v.getConfigType())
|
return UnsupportedConfigError(v.getConfigType())
|
||||||
}
|
}
|
||||||
|
|
||||||
file, err := ioutil.ReadFile(v.getConfigFile())
|
file, err := afero.ReadFile(v.fs, v.getConfigFile())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -954,7 +970,7 @@ func (v *Viper) MergeInConfig() error {
|
||||||
return UnsupportedConfigError(v.getConfigType())
|
return UnsupportedConfigError(v.getConfigType())
|
||||||
}
|
}
|
||||||
|
|
||||||
file, err := ioutil.ReadFile(v.getConfigFile())
|
file, err := afero.ReadFile(v.fs, v.getConfigFile())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -962,7 +978,7 @@ func (v *Viper) MergeInConfig() error {
|
||||||
return v.MergeConfig(bytes.NewReader(file))
|
return v.MergeConfig(bytes.NewReader(file))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Viper will read a configuration file, setting existing keys to nil if the
|
// ReadConfig will read a configuration file, setting existing keys to nil if the
|
||||||
// key does not exist in the file.
|
// key does not exist in the file.
|
||||||
func ReadConfig(in io.Reader) error { return v.ReadConfig(in) }
|
func ReadConfig(in io.Reader) error { return v.ReadConfig(in) }
|
||||||
func (v *Viper) ReadConfig(in io.Reader) error {
|
func (v *Viper) ReadConfig(in io.Reader) error {
|
||||||
|
@ -1064,34 +1080,20 @@ func mergeMaps(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// func ReadBufConfig(buf *bytes.Buffer) error { return v.ReadBufConfig(buf) }
|
// ReadRemoteConfig attempts to get configuration from a remote source
|
||||||
// func (v *Viper) ReadBufConfig(buf *bytes.Buffer) error {
|
|
||||||
// v.config = make(map[string]interface{})
|
|
||||||
// return v.unmarshalReader(buf, v.config)
|
|
||||||
// }
|
|
||||||
|
|
||||||
// Attempts to get configuration from a remote source
|
|
||||||
// and read it in the remote configuration registry.
|
// and read it in the remote configuration registry.
|
||||||
func ReadRemoteConfig() error { return v.ReadRemoteConfig() }
|
func ReadRemoteConfig() error { return v.ReadRemoteConfig() }
|
||||||
func (v *Viper) ReadRemoteConfig() error {
|
func (v *Viper) ReadRemoteConfig() error {
|
||||||
err := v.getKeyValueConfig()
|
return v.getKeyValueConfig()
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func WatchRemoteConfig() error { return v.WatchRemoteConfig() }
|
func WatchRemoteConfig() error { return v.WatchRemoteConfig() }
|
||||||
func (v *Viper) WatchRemoteConfig() error {
|
func (v *Viper) WatchRemoteConfig() error {
|
||||||
err := v.watchKeyValueConfig()
|
return v.watchKeyValueConfig()
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Unmarshall a Reader into a map
|
// Unmarshall a Reader into a map.
|
||||||
// Should probably be an unexported function
|
// Should probably be an unexported function.
|
||||||
func unmarshalReader(in io.Reader, c map[string]interface{}) error {
|
func unmarshalReader(in io.Reader, c map[string]interface{}) error {
|
||||||
return v.unmarshalReader(in, c)
|
return v.unmarshalReader(in, c)
|
||||||
}
|
}
|
||||||
|
@ -1107,7 +1109,7 @@ func (v *Viper) insensitiviseMaps() {
|
||||||
insensitiviseMap(v.kvstore)
|
insensitiviseMap(v.kvstore)
|
||||||
}
|
}
|
||||||
|
|
||||||
// retrieve the first found remote configuration
|
// Retrieve the first found remote configuration.
|
||||||
func (v *Viper) getKeyValueConfig() error {
|
func (v *Viper) getKeyValueConfig() error {
|
||||||
if RemoteConfig == nil {
|
if RemoteConfig == nil {
|
||||||
return RemoteConfigError("Enable the remote features by doing a blank import of the viper/remote package: '_ github.com/spf13/viper/remote'")
|
return RemoteConfigError("Enable the remote features by doing a blank import of the viper/remote package: '_ github.com/spf13/viper/remote'")
|
||||||
|
@ -1124,8 +1126,7 @@ func (v *Viper) getKeyValueConfig() error {
|
||||||
return RemoteConfigError("No Files Found")
|
return RemoteConfigError("No Files Found")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v *Viper) getRemoteConfig(provider *defaultRemoteProvider) (map[string]interface{}, error) {
|
func (v *Viper) getRemoteConfig(provider RemoteProvider) (map[string]interface{}, error) {
|
||||||
|
|
||||||
reader, err := RemoteConfig.Get(provider)
|
reader, err := RemoteConfig.Get(provider)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -1134,7 +1135,7 @@ func (v *Viper) getRemoteConfig(provider *defaultRemoteProvider) (map[string]int
|
||||||
return v.kvstore, err
|
return v.kvstore, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// retrieve the first found remote configuration
|
// Retrieve the first found remote configuration.
|
||||||
func (v *Viper) watchKeyValueConfig() error {
|
func (v *Viper) watchKeyValueConfig() error {
|
||||||
for _, rp := range v.remoteProviders {
|
for _, rp := range v.remoteProviders {
|
||||||
val, err := v.watchRemoteConfig(rp)
|
val, err := v.watchRemoteConfig(rp)
|
||||||
|
@ -1147,7 +1148,7 @@ func (v *Viper) watchKeyValueConfig() error {
|
||||||
return RemoteConfigError("No Files Found")
|
return RemoteConfigError("No Files Found")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v *Viper) watchRemoteConfig(provider *defaultRemoteProvider) (map[string]interface{}, error) {
|
func (v *Viper) watchRemoteConfig(provider RemoteProvider) (map[string]interface{}, error) {
|
||||||
reader, err := RemoteConfig.Watch(provider)
|
reader, err := RemoteConfig.Watch(provider)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -1156,48 +1157,48 @@ func (v *Viper) watchRemoteConfig(provider *defaultRemoteProvider) (map[string]i
|
||||||
return v.kvstore, err
|
return v.kvstore, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return all keys regardless where they are set
|
// AllKeys returns all keys regardless where they are set.
|
||||||
func AllKeys() []string { return v.AllKeys() }
|
func AllKeys() []string { return v.AllKeys() }
|
||||||
func (v *Viper) AllKeys() []string {
|
func (v *Viper) AllKeys() []string {
|
||||||
m := map[string]struct{}{}
|
m := map[string]struct{}{}
|
||||||
|
|
||||||
for key, _ := range v.defaults {
|
for key := range v.defaults {
|
||||||
m[strings.ToLower(key)] = struct{}{}
|
m[strings.ToLower(key)] = struct{}{}
|
||||||
}
|
}
|
||||||
|
|
||||||
for key, _ := range v.pflags {
|
for key := range v.pflags {
|
||||||
m[strings.ToLower(key)] = struct{}{}
|
m[strings.ToLower(key)] = struct{}{}
|
||||||
}
|
}
|
||||||
|
|
||||||
for key, _ := range v.env {
|
for key := range v.env {
|
||||||
m[strings.ToLower(key)] = struct{}{}
|
m[strings.ToLower(key)] = struct{}{}
|
||||||
}
|
}
|
||||||
|
|
||||||
for key, _ := range v.config {
|
for key := range v.config {
|
||||||
m[strings.ToLower(key)] = struct{}{}
|
m[strings.ToLower(key)] = struct{}{}
|
||||||
}
|
}
|
||||||
|
|
||||||
for key, _ := range v.kvstore {
|
for key := range v.kvstore {
|
||||||
m[strings.ToLower(key)] = struct{}{}
|
m[strings.ToLower(key)] = struct{}{}
|
||||||
}
|
}
|
||||||
|
|
||||||
for key, _ := range v.override {
|
for key := range v.override {
|
||||||
m[strings.ToLower(key)] = struct{}{}
|
m[strings.ToLower(key)] = struct{}{}
|
||||||
}
|
}
|
||||||
|
|
||||||
for key, _ := range v.aliases {
|
for key := range v.aliases {
|
||||||
m[strings.ToLower(key)] = struct{}{}
|
m[strings.ToLower(key)] = struct{}{}
|
||||||
}
|
}
|
||||||
|
|
||||||
a := []string{}
|
a := []string{}
|
||||||
for x, _ := range m {
|
for x := range m {
|
||||||
a = append(a, x)
|
a = append(a, x)
|
||||||
}
|
}
|
||||||
|
|
||||||
return a
|
return a
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return all settings as a map[string]interface{}
|
// AllSettings returns all settings as a map[string]interface{}.
|
||||||
func AllSettings() map[string]interface{} { return v.AllSettings() }
|
func AllSettings() map[string]interface{} { return v.AllSettings() }
|
||||||
func (v *Viper) AllSettings() map[string]interface{} {
|
func (v *Viper) AllSettings() map[string]interface{} {
|
||||||
m := map[string]interface{}{}
|
m := map[string]interface{}{}
|
||||||
|
@ -1208,16 +1209,23 @@ func (v *Viper) AllSettings() map[string]interface{} {
|
||||||
return m
|
return m
|
||||||
}
|
}
|
||||||
|
|
||||||
// Name for the config file.
|
// SetFs sets the filesystem to use to read configuration.
|
||||||
|
func SetFs(fs afero.Fs) { v.SetFs(fs) }
|
||||||
|
func (v *Viper) SetFs(fs afero.Fs) {
|
||||||
|
v.fs = fs
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetConfigName sets name for the config file.
|
||||||
// Does not include extension.
|
// Does not include extension.
|
||||||
func SetConfigName(in string) { v.SetConfigName(in) }
|
func SetConfigName(in string) { v.SetConfigName(in) }
|
||||||
func (v *Viper) SetConfigName(in string) {
|
func (v *Viper) SetConfigName(in string) {
|
||||||
if in != "" {
|
if in != "" {
|
||||||
v.configName = in
|
v.configName = in
|
||||||
|
v.configFile = ""
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sets the type of the configuration returned by the
|
// SetConfigType sets the type of the configuration returned by the
|
||||||
// remote source, e.g. "json".
|
// remote source, e.g. "json".
|
||||||
func SetConfigType(in string) { v.SetConfigType(in) }
|
func SetConfigType(in string) { v.SetConfigType(in) }
|
||||||
func (v *Viper) SetConfigType(in string) {
|
func (v *Viper) SetConfigType(in string) {
|
||||||
|
@ -1236,9 +1244,9 @@ func (v *Viper) getConfigType() string {
|
||||||
|
|
||||||
if len(ext) > 1 {
|
if len(ext) > 1 {
|
||||||
return ext[1:]
|
return ext[1:]
|
||||||
} else {
|
|
||||||
return ""
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v *Viper) getConfigFile() string {
|
func (v *Viper) getConfigFile() string {
|
||||||
|
@ -1269,8 +1277,8 @@ func (v *Viper) searchInPath(in string) (filename string) {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
// search all configPaths for any config file.
|
// Search all configPaths for any config file.
|
||||||
// Returns the first path that exists (and is a config file)
|
// Returns the first path that exists (and is a config file).
|
||||||
func (v *Viper) findConfigFile() (string, error) {
|
func (v *Viper) findConfigFile() (string, error) {
|
||||||
|
|
||||||
jww.INFO.Println("Searching for config in ", v.configPaths)
|
jww.INFO.Println("Searching for config in ", v.configPaths)
|
||||||
|
@ -1284,7 +1292,7 @@ func (v *Viper) findConfigFile() (string, error) {
|
||||||
return "", ConfigFileNotFoundError{v.configName, fmt.Sprintf("%s", v.configPaths)}
|
return "", ConfigFileNotFoundError{v.configName, fmt.Sprintf("%s", v.configPaths)}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Prints all configuration registries for debugging
|
// Debug prints all configuration registries for debugging
|
||||||
// purposes.
|
// purposes.
|
||||||
func Debug() { v.Debug() }
|
func Debug() { v.Debug() }
|
||||||
func (v *Viper) Debug() {
|
func (v *Viper) Debug() {
|
||||||
|
|
|
@ -493,7 +493,7 @@ func TestBindPFlags(t *testing.T) {
|
||||||
"endpoint": "/public",
|
"endpoint": "/public",
|
||||||
}
|
}
|
||||||
|
|
||||||
for name, _ := range testValues {
|
for name := range testValues {
|
||||||
testValues[name] = flagSet.String(name, "", "test")
|
testValues[name] = flagSet.String(name, "", "test")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -765,6 +765,7 @@ func TestSub(t *testing.T) {
|
||||||
var yamlMergeExampleTgt = []byte(`
|
var yamlMergeExampleTgt = []byte(`
|
||||||
hello:
|
hello:
|
||||||
pop: 37890
|
pop: 37890
|
||||||
|
lagrenum: 765432101234567
|
||||||
world:
|
world:
|
||||||
- us
|
- us
|
||||||
- uk
|
- uk
|
||||||
|
@ -775,6 +776,7 @@ hello:
|
||||||
var yamlMergeExampleSrc = []byte(`
|
var yamlMergeExampleSrc = []byte(`
|
||||||
hello:
|
hello:
|
||||||
pop: 45000
|
pop: 45000
|
||||||
|
lagrenum: 7654321001234567
|
||||||
universe:
|
universe:
|
||||||
- mw
|
- mw
|
||||||
- ad
|
- ad
|
||||||
|
@ -792,6 +794,14 @@ func TestMergeConfig(t *testing.T) {
|
||||||
t.Fatalf("pop != 37890, = %d", pop)
|
t.Fatalf("pop != 37890, = %d", pop)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if pop := v.GetInt("hello.lagrenum"); pop != 765432101234567 {
|
||||||
|
t.Fatalf("lagrenum != 765432101234567, = %d", pop)
|
||||||
|
}
|
||||||
|
|
||||||
|
if pop := v.GetInt64("hello.lagrenum"); pop != int64(765432101234567) {
|
||||||
|
t.Fatalf("int64 lagrenum != 765432101234567, = %d", pop)
|
||||||
|
}
|
||||||
|
|
||||||
if world := v.GetStringSlice("hello.world"); len(world) != 4 {
|
if world := v.GetStringSlice("hello.world"); len(world) != 4 {
|
||||||
t.Fatalf("len(world) != 4, = %d", len(world))
|
t.Fatalf("len(world) != 4, = %d", len(world))
|
||||||
}
|
}
|
||||||
|
@ -808,6 +818,14 @@ func TestMergeConfig(t *testing.T) {
|
||||||
t.Fatalf("pop != 45000, = %d", pop)
|
t.Fatalf("pop != 45000, = %d", pop)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if pop := v.GetInt("hello.lagrenum"); pop != 7654321001234567 {
|
||||||
|
t.Fatalf("lagrenum != 7654321001234567, = %d", pop)
|
||||||
|
}
|
||||||
|
|
||||||
|
if pop := v.GetInt64("hello.lagrenum"); pop != int64(7654321001234567) {
|
||||||
|
t.Fatalf("int64 lagrenum != 7654321001234567, = %d", pop)
|
||||||
|
}
|
||||||
|
|
||||||
if world := v.GetStringSlice("hello.world"); len(world) != 4 {
|
if world := v.GetStringSlice("hello.world"); len(world) != 4 {
|
||||||
t.Fatalf("len(world) != 4, = %d", len(world))
|
t.Fatalf("len(world) != 4, = %d", len(world))
|
||||||
}
|
}
|
||||||
|
@ -885,3 +903,18 @@ func TestUnmarshalingWithAliases(t *testing.T) {
|
||||||
|
|
||||||
assert.Equal(t, &C, &config{Id: 1, FirstName: "Steve", Surname: "Owen"})
|
assert.Equal(t, &C, &config{Id: 1, FirstName: "Steve", Surname: "Owen"})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSetConfigNameClearsFileCache(t *testing.T) {
|
||||||
|
SetConfigFile("/tmp/config.yaml")
|
||||||
|
SetConfigName("default")
|
||||||
|
assert.Empty(t, v.getConfigFile())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestShadowedNestedValue(t *testing.T) {
|
||||||
|
polyester := "polyester"
|
||||||
|
initYAML()
|
||||||
|
SetDefault("clothing.shirt", polyester)
|
||||||
|
|
||||||
|
assert.Equal(t, GetString("clothing.jacket"), "leather")
|
||||||
|
assert.Equal(t, GetString("clothing.shirt"), polyester)
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue