mirror of
https://github.com/spf13/viper
synced 2024-12-22 19:47:01 +00:00
Adding documentation inline. Moving Reset() to viper_test.go
This commit is contained in:
parent
29f1858f87
commit
18a87c05c6
3 changed files with 85 additions and 59 deletions
29
util.go
29
util.go
|
@ -11,13 +11,18 @@
|
||||||
package viper
|
package viper
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/BurntSushi/toml"
|
||||||
jww "github.com/spf13/jwalterweatherman"
|
jww "github.com/spf13/jwalterweatherman"
|
||||||
|
"gopkg.in/yaml.v1"
|
||||||
)
|
)
|
||||||
|
|
||||||
func insensativiseMap(m map[string]interface{}) {
|
func insensativiseMap(m map[string]interface{}) {
|
||||||
|
@ -110,3 +115,27 @@ func findCWD() (string, error) {
|
||||||
|
|
||||||
return path, nil
|
return path, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func marshallConfigReader(in io.Reader, c map[string]interface{}, configType string) {
|
||||||
|
buf := new(bytes.Buffer)
|
||||||
|
buf.ReadFrom(in)
|
||||||
|
|
||||||
|
switch configType {
|
||||||
|
case "yaml", "yml":
|
||||||
|
if err := yaml.Unmarshal(buf.Bytes(), &c); err != nil {
|
||||||
|
jww.ERROR.Fatalf("Error parsing config: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
case "json":
|
||||||
|
if err := json.Unmarshal(buf.Bytes(), &c); err != nil {
|
||||||
|
jww.ERROR.Fatalf("Error parsing config: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
case "toml":
|
||||||
|
if _, err := toml.Decode(buf.String(), &c); err != nil {
|
||||||
|
jww.ERROR.Fatalf("Error parsing config: %s", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
insensativiseMap(c)
|
||||||
|
}
|
||||||
|
|
108
viper.go
108
viper.go
|
@ -20,7 +20,6 @@ package viper
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
@ -30,14 +29,12 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/BurntSushi/toml"
|
|
||||||
"github.com/kr/pretty"
|
"github.com/kr/pretty"
|
||||||
"github.com/mitchellh/mapstructure"
|
"github.com/mitchellh/mapstructure"
|
||||||
"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"
|
||||||
crypt "github.com/xordataexchange/crypt/config"
|
crypt "github.com/xordataexchange/crypt/config"
|
||||||
"gopkg.in/yaml.v1"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var v *viper
|
var v *viper
|
||||||
|
@ -64,6 +61,8 @@ func (rce RemoteConfigError) Error() string {
|
||||||
return fmt.Sprintf("Remote Configurations Error: %s", string(rce))
|
return fmt.Sprintf("Remote Configurations Error: %s", string(rce))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// A viper is a unexported struct. Use New() to create a new instance of viper
|
||||||
|
// or use the functions for a "global instance"
|
||||||
type viper struct {
|
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
|
||||||
|
@ -85,6 +84,7 @@ type viper struct {
|
||||||
aliases map[string]string
|
aliases map[string]string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// The prescribed way to create a new Viper
|
||||||
func New() *viper {
|
func New() *viper {
|
||||||
v := new(viper)
|
v := new(viper)
|
||||||
v.configName = "config"
|
v.configName = "config"
|
||||||
|
@ -212,6 +212,39 @@ func (v *viper) providerPathExists(p *remoteProvider) bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Viper is essentially repository for configurations
|
||||||
|
// Get can retrieve any value given the key to use
|
||||||
|
// 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:
|
||||||
|
// flag, env, config file, key/value store, default
|
||||||
|
//
|
||||||
|
// Get returns an interface. For a specific value use one of the Get____ methods.
|
||||||
|
func Get(key string) interface{} { return v.Get(key) }
|
||||||
|
func (v *viper) Get(key string) interface{} {
|
||||||
|
key = strings.ToLower(key)
|
||||||
|
val := v.find(key)
|
||||||
|
|
||||||
|
if val == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
switch val.(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 []string:
|
||||||
|
return val
|
||||||
|
}
|
||||||
|
return val
|
||||||
|
}
|
||||||
|
|
||||||
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(Get(key))
|
return cast.ToString(Get(key))
|
||||||
|
@ -329,6 +362,10 @@ func (v *viper) BindEnv(input ...string) (err error) {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Given a key, find the value
|
||||||
|
// Viper will check in the following order:
|
||||||
|
// flag, env, config file, key/value store, default
|
||||||
|
// 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
|
||||||
|
@ -383,34 +420,7 @@ func (v *viper) find(key string) interface{} {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get returns an interface..
|
// Check to see if the key has been set in any of the data locations
|
||||||
// Must be typecast or used by something that will typecast
|
|
||||||
func Get(key string) interface{} { return v.Get(key) }
|
|
||||||
func (v *viper) Get(key string) interface{} {
|
|
||||||
key = strings.ToLower(key)
|
|
||||||
val := v.find(key)
|
|
||||||
|
|
||||||
if val == nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
switch val.(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 []string:
|
|
||||||
return val
|
|
||||||
}
|
|
||||||
return val
|
|
||||||
}
|
|
||||||
|
|
||||||
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 {
|
||||||
t := v.Get(key)
|
t := v.Get(key)
|
||||||
|
@ -475,6 +485,7 @@ func (v *viper) realKey(key string) string {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check 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
|
||||||
|
@ -530,29 +541,11 @@ func (v *viper) ReadRemoteConfig() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Marshall a Reader into a map
|
||||||
|
// Should probably be an unexported function
|
||||||
func MarshallReader(in io.Reader, c map[string]interface{}) { v.MarshallReader(in, c) }
|
func MarshallReader(in io.Reader, c map[string]interface{}) { v.MarshallReader(in, c) }
|
||||||
func (v *viper) MarshallReader(in io.Reader, c map[string]interface{}) {
|
func (v *viper) MarshallReader(in io.Reader, c map[string]interface{}) {
|
||||||
buf := new(bytes.Buffer)
|
marshallConfigReader(in, c, v.getConfigType())
|
||||||
buf.ReadFrom(in)
|
|
||||||
|
|
||||||
switch v.getConfigType() {
|
|
||||||
case "yaml", "yml":
|
|
||||||
if err := yaml.Unmarshal(buf.Bytes(), &c); err != nil {
|
|
||||||
jww.ERROR.Fatalf("Error parsing config: %s", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
case "json":
|
|
||||||
if err := json.Unmarshal(buf.Bytes(), &c); err != nil {
|
|
||||||
jww.ERROR.Fatalf("Error parsing config: %s", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
case "toml":
|
|
||||||
if _, err := toml.Decode(buf.String(), &c); err != nil {
|
|
||||||
jww.ERROR.Fatalf("Error parsing config: %s", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
insensativiseMap(c)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v *viper) insensativiseMaps() {
|
func (v *viper) insensativiseMaps() {
|
||||||
|
@ -609,6 +602,7 @@ func (v *viper) getRemoteConfig(provider *remoteProvider) (map[string]interface{
|
||||||
return v.kvstore, err
|
return v.kvstore, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Return 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{}{}
|
||||||
|
@ -637,6 +631,7 @@ func (v *viper) AllKeys() []string {
|
||||||
return a
|
return a
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Return 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{}{}
|
||||||
|
@ -706,6 +701,8 @@ func (v *viper) searchInPath(in string) (filename string) {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// search all configPaths for any 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)
|
||||||
|
|
||||||
|
@ -746,10 +743,3 @@ func (v *viper) Debug() {
|
||||||
fmt.Println("Aliases:")
|
fmt.Println("Aliases:")
|
||||||
pretty.Println(v.aliases)
|
pretty.Println(v.aliases)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Intended for testing, will reset all to default settings.
|
|
||||||
func Reset() {
|
|
||||||
v = New()
|
|
||||||
SupportedExts = []string{"json", "toml", "yaml", "yml"}
|
|
||||||
SupportedRemoteProviders = []string{"etcd", "consul"}
|
|
||||||
}
|
|
||||||
|
|
|
@ -54,6 +54,13 @@ var jsonExample = []byte(`{
|
||||||
}
|
}
|
||||||
}`)
|
}`)
|
||||||
|
|
||||||
|
// Intended for testing, will reset all to default settings.
|
||||||
|
func Reset() {
|
||||||
|
v = New()
|
||||||
|
SupportedExts = []string{"json", "toml", "yaml", "yml"}
|
||||||
|
SupportedRemoteProviders = []string{"etcd", "consul"}
|
||||||
|
}
|
||||||
|
|
||||||
var remoteExample = []byte(`{
|
var remoteExample = []byte(`{
|
||||||
"id":"0002",
|
"id":"0002",
|
||||||
"type":"cronut",
|
"type":"cronut",
|
||||||
|
|
Loading…
Reference in a new issue