forked from LaconicNetwork/kompose
Updated Vendoring
This commit is contained in:
+41
-98
@@ -21,7 +21,6 @@ package viper
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/csv"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
@@ -41,11 +40,6 @@ import (
|
||||
|
||||
var v *Viper
|
||||
|
||||
type RemoteResponse struct {
|
||||
Value []byte
|
||||
Error error
|
||||
}
|
||||
|
||||
func init() {
|
||||
v = New()
|
||||
}
|
||||
@@ -53,7 +47,6 @@ func init() {
|
||||
type remoteConfigFactory interface {
|
||||
Get(rp RemoteProvider) (io.Reader, error)
|
||||
Watch(rp RemoteProvider) (io.Reader, error)
|
||||
WatchChannel(rp RemoteProvider) (<-chan *RemoteResponse, chan bool)
|
||||
}
|
||||
|
||||
// RemoteConfig is optional, see the remote package
|
||||
@@ -69,7 +62,8 @@ func (str UnsupportedConfigError) Error() string {
|
||||
}
|
||||
|
||||
// UnsupportedRemoteProviderError denotes encountering an unsupported remote
|
||||
// provider. Currently only etcd and Consul are supported.
|
||||
// provider. Currently only etcd and Consul are
|
||||
// supported.
|
||||
type UnsupportedRemoteProviderError string
|
||||
|
||||
// Error returns the formatted remote provider error.
|
||||
@@ -282,8 +276,8 @@ func (v *Viper) WatchConfig() {
|
||||
}()
|
||||
}
|
||||
|
||||
// SetConfigFile explicitly defines the path, name and extension of the config file.
|
||||
// Viper will use this and not check any of the config paths.
|
||||
// SetConfigFile explicitly defines the path, name and extension of the config file
|
||||
// Viper will use this and not check any of the config paths
|
||||
func SetConfigFile(in string) { v.SetConfigFile(in) }
|
||||
func (v *Viper) SetConfigFile(in string) {
|
||||
if in != "" {
|
||||
@@ -292,8 +286,8 @@ func (v *Viper) SetConfigFile(in string) {
|
||||
}
|
||||
|
||||
// SetEnvPrefix defines a prefix that ENVIRONMENT variables will use.
|
||||
// E.g. if your prefix is "spf", the env registry will look for env
|
||||
// variables that start with "SPF_".
|
||||
// E.g. if your prefix is "spf", the env registry
|
||||
// will look for env. variables that start with "SPF_"
|
||||
func SetEnvPrefix(in string) { v.SetEnvPrefix(in) }
|
||||
func (v *Viper) SetEnvPrefix(in string) {
|
||||
if in != "" {
|
||||
@@ -311,11 +305,11 @@ func (v *Viper) mergeWithEnvPrefix(in string) string {
|
||||
|
||||
// TODO: should getEnv logic be moved into find(). Can generalize the use of
|
||||
// rewriting keys many things, Ex: Get('someKey') -> some_key
|
||||
// (camel case to snake case for JSON keys perhaps)
|
||||
// (cammel case to snake case for JSON keys perhaps)
|
||||
|
||||
// getEnv is a wrapper around os.Getenv which replaces characters in the original
|
||||
// key. This allows env vars which have different keys than the config object
|
||||
// keys.
|
||||
// key. This allows env vars which have different keys then the config object
|
||||
// keys
|
||||
func (v *Viper) getEnv(key string) string {
|
||||
if v.envKeyReplacer != nil {
|
||||
key = v.envKeyReplacer.Replace(key)
|
||||
@@ -323,7 +317,7 @@ func (v *Viper) getEnv(key string) string {
|
||||
return os.Getenv(key)
|
||||
}
|
||||
|
||||
// ConfigFileUsed returns 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 (v *Viper) ConfigFileUsed() string { return v.configFile }
|
||||
|
||||
@@ -596,33 +590,32 @@ func (v *Viper) Get(key string) interface{} {
|
||||
return nil
|
||||
}
|
||||
|
||||
valType := val
|
||||
if v.typeByDefValue {
|
||||
// TODO(bep) this branch isn't covered by a single test.
|
||||
valType := val
|
||||
path := strings.Split(lcaseKey, v.keyDelim)
|
||||
defVal := v.searchMap(v.defaults, path)
|
||||
if defVal != nil {
|
||||
valType = defVal
|
||||
}
|
||||
|
||||
switch valType.(type) {
|
||||
case bool:
|
||||
return cast.ToBool(val)
|
||||
case string:
|
||||
return cast.ToString(val)
|
||||
case int64, int32, int16, int8, int:
|
||||
return cast.ToInt(val)
|
||||
case float64, float32:
|
||||
return cast.ToFloat64(val)
|
||||
case time.Time:
|
||||
return cast.ToTime(val)
|
||||
case time.Duration:
|
||||
return cast.ToDuration(val)
|
||||
case []string:
|
||||
return cast.ToStringSlice(val)
|
||||
}
|
||||
}
|
||||
|
||||
switch valType.(type) {
|
||||
case bool:
|
||||
return cast.ToBool(val)
|
||||
case string:
|
||||
return cast.ToString(val)
|
||||
case int64, int32, int16, int8, int:
|
||||
return cast.ToInt(val)
|
||||
case float64, float32:
|
||||
return cast.ToFloat64(val)
|
||||
case time.Time:
|
||||
return cast.ToTime(val)
|
||||
case time.Duration:
|
||||
return cast.ToDuration(val)
|
||||
case []string:
|
||||
return cast.ToStringSlice(val)
|
||||
}
|
||||
return val
|
||||
}
|
||||
|
||||
@@ -720,15 +713,7 @@ func (v *Viper) GetSizeInBytes(key string) uint {
|
||||
// UnmarshalKey takes a single key and unmarshals it into a Struct.
|
||||
func UnmarshalKey(key string, rawVal interface{}) error { return v.UnmarshalKey(key, rawVal) }
|
||||
func (v *Viper) UnmarshalKey(key string, rawVal interface{}) error {
|
||||
err := decode(v.Get(key), defaultDecoderConfig(rawVal))
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
v.insensitiviseMaps()
|
||||
|
||||
return nil
|
||||
return mapstructure.Decode(v.Get(key), rawVal)
|
||||
}
|
||||
|
||||
// Unmarshal unmarshals the config into a Struct. Make sure that the tags
|
||||
@@ -747,16 +732,13 @@ func (v *Viper) Unmarshal(rawVal interface{}) error {
|
||||
}
|
||||
|
||||
// defaultDecoderConfig returns default mapsstructure.DecoderConfig with suppot
|
||||
// of time.Duration values & string slices
|
||||
// of time.Duration values
|
||||
func defaultDecoderConfig(output interface{}) *mapstructure.DecoderConfig {
|
||||
return &mapstructure.DecoderConfig{
|
||||
Metadata: nil,
|
||||
Result: output,
|
||||
WeaklyTypedInput: true,
|
||||
DecodeHook: mapstructure.ComposeDecodeHookFunc(
|
||||
mapstructure.StringToTimeDurationHookFunc(),
|
||||
mapstructure.StringToSliceHookFunc(","),
|
||||
),
|
||||
DecodeHook: mapstructure.StringToTimeDurationHookFunc(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -817,7 +799,7 @@ func (v *Viper) BindFlagValues(flags FlagValueSet) (err error) {
|
||||
}
|
||||
|
||||
// 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")
|
||||
// Viper.BindFlagValue("port", serverCmd.Flags().Lookup("port"))
|
||||
@@ -898,9 +880,7 @@ func (v *Viper) find(lcaseKey string) interface{} {
|
||||
return cast.ToBool(flag.ValueString())
|
||||
case "stringSlice":
|
||||
s := strings.TrimPrefix(flag.ValueString(), "[")
|
||||
s = strings.TrimSuffix(s, "]")
|
||||
res, _ := readAsCSV(s)
|
||||
return res
|
||||
return strings.TrimSuffix(s, "]")
|
||||
default:
|
||||
return flag.ValueString()
|
||||
}
|
||||
@@ -967,9 +947,7 @@ func (v *Viper) find(lcaseKey string) interface{} {
|
||||
return cast.ToBool(flag.ValueString())
|
||||
case "stringSlice":
|
||||
s := strings.TrimPrefix(flag.ValueString(), "[")
|
||||
s = strings.TrimSuffix(s, "]")
|
||||
res, _ := readAsCSV(s)
|
||||
return res
|
||||
return strings.TrimSuffix(s, "]")
|
||||
default:
|
||||
return flag.ValueString()
|
||||
}
|
||||
@@ -979,15 +957,6 @@ func (v *Viper) find(lcaseKey string) interface{} {
|
||||
return nil
|
||||
}
|
||||
|
||||
func readAsCSV(val string) ([]string, error) {
|
||||
if val == "" {
|
||||
return []string{}, nil
|
||||
}
|
||||
stringReader := strings.NewReader(val)
|
||||
csvReader := csv.NewReader(stringReader)
|
||||
return csvReader.Read()
|
||||
}
|
||||
|
||||
// IsSet checks to see if the key has been set in any of the data locations.
|
||||
// IsSet is case-insensitive for a key.
|
||||
func IsSet(key string) bool { return v.IsSet(key) }
|
||||
@@ -1124,30 +1093,24 @@ func (v *Viper) ReadInConfig() error {
|
||||
return err
|
||||
}
|
||||
|
||||
config := make(map[string]interface{})
|
||||
v.config = make(map[string]interface{})
|
||||
|
||||
err = v.unmarshalReader(bytes.NewReader(file), config)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
v.config = config
|
||||
return nil
|
||||
return v.unmarshalReader(bytes.NewReader(file), v.config)
|
||||
}
|
||||
|
||||
// MergeInConfig merges a new configuration with an existing config.
|
||||
func MergeInConfig() error { return v.MergeInConfig() }
|
||||
func (v *Viper) MergeInConfig() error {
|
||||
jww.INFO.Println("Attempting to merge in config file")
|
||||
if !stringInSlice(v.getConfigType(), SupportedExts) {
|
||||
return UnsupportedConfigError(v.getConfigType())
|
||||
}
|
||||
|
||||
filename, err := v.getConfigFile()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if !stringInSlice(v.getConfigType(), SupportedExts) {
|
||||
return UnsupportedConfigError(v.getConfigType())
|
||||
}
|
||||
|
||||
file, err := afero.ReadFile(v.fs, filename)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -1286,11 +1249,7 @@ func (v *Viper) WatchRemoteConfig() error {
|
||||
return v.watchKeyValueConfig()
|
||||
}
|
||||
|
||||
func (v *Viper) WatchRemoteConfigOnChannel() error {
|
||||
return v.watchKeyValueConfigOnChannel()
|
||||
}
|
||||
|
||||
// Unmarshal a Reader into a map.
|
||||
// Unmarshall a Reader into a map.
|
||||
// Should probably be an unexported function.
|
||||
func unmarshalReader(in io.Reader, c map[string]interface{}) error {
|
||||
return v.unmarshalReader(in, c)
|
||||
@@ -1333,23 +1292,6 @@ func (v *Viper) getRemoteConfig(provider RemoteProvider) (map[string]interface{}
|
||||
return v.kvstore, err
|
||||
}
|
||||
|
||||
// Retrieve the first found remote configuration.
|
||||
func (v *Viper) watchKeyValueConfigOnChannel() error {
|
||||
for _, rp := range v.remoteProviders {
|
||||
respc, _ := RemoteConfig.WatchChannel(rp)
|
||||
//Todo: Add quit channel
|
||||
go func(rc <-chan *RemoteResponse) {
|
||||
for {
|
||||
b := <-rc
|
||||
reader := bytes.NewReader(b.Value)
|
||||
v.unmarshalReader(reader, v.kvstore)
|
||||
}
|
||||
}(respc)
|
||||
return nil
|
||||
}
|
||||
return RemoteConfigError("No Files Found")
|
||||
}
|
||||
|
||||
// Retrieve the first found remote configuration.
|
||||
func (v *Viper) watchKeyValueConfig() error {
|
||||
for _, rp := range v.remoteProviders {
|
||||
@@ -1549,6 +1491,7 @@ func (v *Viper) searchInPath(in string) (filename string) {
|
||||
// Search all configPaths for any config file.
|
||||
// Returns the first path that exists (and is a config file).
|
||||
func (v *Viper) findConfigFile() (string, error) {
|
||||
|
||||
jww.INFO.Println("Searching for config in ", v.configPaths)
|
||||
|
||||
for _, cp := range v.configPaths {
|
||||
|
||||
Reference in New Issue
Block a user