From 2b4f7d3cde7da656a0fbb4ac4c6bbf7f033a51f9 Mon Sep 17 00:00:00 2001 From: victor23d Date: Sat, 22 Jun 2019 16:02:30 +0000 Subject: [PATCH 01/68] Add dotenv documentation in README.md --- README.md | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 24ffb20..62b002b 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,7 @@ to work within an application, and can handle all types of configuration needs and formats. It supports: * setting defaults -* reading from JSON, TOML, YAML, HCL, and Java properties config files +* reading from JSON, TOML, YAML, HCL, envfile and Java properties config files * live watching and re-reading of config files (optional) * reading from environment variables * reading from remote config systems (etcd or Consul), and watching changes @@ -46,7 +46,7 @@ Viper is here to help with that. Viper does the following for you: -1. Find, load, and unmarshal a configuration file in JSON, TOML, YAML, HCL, or Java properties formats. +1. Find, load, and unmarshal a configuration file in JSON, TOML, YAML, HCL, envfile or Java properties formats. 2. Provide a mechanism to set default values for your different configuration options. 3. Provide a mechanism to set override values for options specified through @@ -87,7 +87,7 @@ viper.SetDefault("Taxonomies", map[string]string{"tag": "tags", "category": "cat ### Reading Config Files Viper requires minimal configuration so it knows where to look for config files. -Viper supports JSON, TOML, YAML, HCL, and Java Properties files. Viper can search multiple paths, but +Viper supports JSON, TOML, YAML, HCL, envfile and Java Properties files. Viper can search multiple paths, but currently a single Viper instance only supports a single configuration file. Viper does not default to any configuration search paths leaving defaults decision to an application. @@ -372,7 +372,7 @@ package: `import _ "github.com/spf13/viper/remote"` -Viper will read a config string (as JSON, TOML, YAML or HCL) retrieved from a path +Viper will read a config string (as JSON, TOML, YAML, HCL or envfile) retrieved from a path in a Key/Value store such as etcd or Consul. These values take precedence over default values, but are overridden by configuration values retrieved from disk, flags, or environment variables. @@ -407,7 +407,7 @@ how to use Consul. #### etcd ```go viper.AddRemoteProvider("etcd", "http://127.0.0.1:4001","/config/hugo.json") -viper.SetConfigType("json") // because there is no file extension in a stream of bytes, supported extensions are "json", "toml", "yaml", "yml", "properties", "props", "prop" +viper.SetConfigType("json") // because there is no file extension in a stream of bytes, supported extensions are "json", "toml", "yaml", "yml", "properties", "props", "prop", "env", "dotenv" err := viper.ReadRemoteConfig() ``` @@ -435,7 +435,7 @@ fmt.Println(viper.Get("hostname")) // myhostname.com ```go viper.AddSecureRemoteProvider("etcd","http://127.0.0.1:4001","/config/hugo.json","/etc/secrets/mykeyring.gpg") -viper.SetConfigType("json") // because there is no file extension in a stream of bytes, supported extensions are "json", "toml", "yaml", "yml", "properties", "props", "prop" +viper.SetConfigType("json") // because there is no file extension in a stream of bytes, supported extensions are "json", "toml", "yaml", "yml", "properties", "props", "prop", "env", "dotenv" err := viper.ReadRemoteConfig() ``` @@ -446,7 +446,7 @@ err := viper.ReadRemoteConfig() var runtime_viper = viper.New() runtime_viper.AddRemoteProvider("etcd", "http://127.0.0.1:4001", "/config/hugo.yml") -runtime_viper.SetConfigType("yaml") // because there is no file extension in a stream of bytes, supported extensions are "json", "toml", "yaml", "yml", "properties", "props", "prop" +runtime_viper.SetConfigType("yaml") // because there is no file extension in a stream of bytes, supported extensions are "json", "toml", "yaml", "yml", "properties", "props", "prop", "env", "dotenv" // read from remote config the first time. err := runtime_viper.ReadRemoteConfig() From 96441b4b7414b184eb59823c0e6ca5494e4706d8 Mon Sep 17 00:00:00 2001 From: Bruce Wang Date: Thu, 20 Jun 2019 15:08:30 +0800 Subject: [PATCH 02/68] BindEnv details specified. Now less confusion. --- README.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 62b002b..f2537ce 100644 --- a/README.md +++ b/README.md @@ -225,9 +225,8 @@ prefix. `BindEnv` takes one or two parameters. The first parameter is the key name, the second is the name of the environment variable. The name of the environment variable is case sensitive. If the ENV variable name is not provided, then -Viper will automatically assume that the key name matches the ENV variable name, -but the ENV variable is IN ALL CAPS. When you explicitly provide the ENV -variable name, it **does not** automatically add the prefix. +Viper will automatically assume that the ENV variable is the prefix + "_" + the key name in ALL CAPS. When you explicitly provide the ENV +variable name which is the second parameter, it **does not** automatically add the prefix, which means if the second parameter is "id", the environment variable is just plain "ID". One important thing to recognize when working with ENV variables is that the value will be read each time it is accessed. Viper does not fix the value when From d34be8d9ee3308fecdadaecf8139cabeaa4a64a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1rk=20S=C3=A1gi-Kaz=C3=A1r?= Date: Sat, 13 Jul 2019 11:32:26 +0200 Subject: [PATCH 03/68] Improve BindEnv details --- README.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index f2537ce..5fdaa48 100644 --- a/README.md +++ b/README.md @@ -225,8 +225,9 @@ prefix. `BindEnv` takes one or two parameters. The first parameter is the key name, the second is the name of the environment variable. The name of the environment variable is case sensitive. If the ENV variable name is not provided, then -Viper will automatically assume that the ENV variable is the prefix + "_" + the key name in ALL CAPS. When you explicitly provide the ENV -variable name which is the second parameter, it **does not** automatically add the prefix, which means if the second parameter is "id", the environment variable is just plain "ID". +Viper will automatically assume that the ENV variable matches the following format: prefix + "_" + the key name in ALL CAPS. When you explicitly provide the ENV variable name (the second parameter), +it **does not** automatically add the prefix. For example if the second parameter is "id", +Viper will look for the ENV variable "ID". One important thing to recognize when working with ENV variables is that the value will be read each time it is accessed. Viper does not fix the value when From 5ae3a072d1057b77d443b65ed0927998111ec766 Mon Sep 17 00:00:00 2001 From: lucperkins Date: Fri, 21 Jun 2019 18:43:31 -0700 Subject: [PATCH 04/68] Add documentation for no config file found Signed-off-by: lucperkins --- README.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/README.md b/README.md index 5fdaa48..f4dc3bd 100644 --- a/README.md +++ b/README.md @@ -107,6 +107,20 @@ if err != nil { // Handle errors reading the config file } ``` +You can handle the specific case where no config file is found like this: + +```go +if err := viper.ReadInConfig(); err != nil { + if _, ok := err.(viper.ConfigFileNotFoundError); ok { + // Config file not found; ignore error if desired + } else { + // Config file was found but another error was produced + } +} + +// Config file found and successfully parsed +``` + ### Writing Config Files Reading from config files is useful, but at times you want to store all modifications made at run time. From 72cbe340cb4939713d5f43c01ccd82fa6155bdd9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1rk=20S=C3=A1gi-Kaz=C3=A1r?= Date: Sat, 13 Jul 2019 11:42:53 +0200 Subject: [PATCH 05/68] Improve travis --- .travis.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index bb83057..d122f22 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,10 +4,12 @@ language: go env: global: - - GO111MODULE="on" + - GO111MODULE="on" + - GOFLAGS="-mod=readonly" go: - 1.11.x + - 1.12.x - tip os: @@ -27,5 +29,3 @@ script: after_success: - go get -u -d github.com/spf13/hugo - cd $GOPATH/src/github.com/spf13/hugo && make && ./hugo -s docs && cd - - -sudo: false From b8221cf4ee9174f124f60bdcf24944da0b0b5643 Mon Sep 17 00:00:00 2001 From: AGirard Date: Wed, 19 Jun 2019 16:05:58 +0200 Subject: [PATCH 06/68] Add GetIntSlice helper method --- README.md | 1 + viper.go | 6 ++++++ viper_test.go | 11 +++++++++++ 3 files changed, 18 insertions(+) diff --git a/README.md b/README.md index f4dc3bd..171f51c 100644 --- a/README.md +++ b/README.md @@ -496,6 +496,7 @@ The following functions and methods exist: * `GetBool(key string) : bool` * `GetFloat64(key string) : float64` * `GetInt(key string) : int` + * `GetIntSlice(key string) : []int` * `GetString(key string) : string` * `GetStringMap(key string) : map[string]interface{}` * `GetStringMapString(key string) : map[string]string` diff --git a/viper.go b/viper.go index 0699c4c..881bf0c 100644 --- a/viper.go +++ b/viper.go @@ -797,6 +797,12 @@ func (v *Viper) GetDuration(key string) time.Duration { return cast.ToDuration(v.Get(key)) } +// GetIntSlice returns the value associated with the key as a slice of strings. +func GetIntSlice(key string) []int { return v.GetIntSlice(key) } +func (v *Viper) GetIntSlice(key string) []int { + return cast.ToIntSlice(v.Get(key)) +} + // GetStringSlice returns the value associated with the key as a slice of strings. func GetStringSlice(key string) []string { return v.GetStringSlice(key) } func (v *Viper) GetStringSlice(key string) []string { diff --git a/viper_test.go b/viper_test.go index c40c971..75afaae 100644 --- a/viper_test.go +++ b/viper_test.go @@ -1262,6 +1262,9 @@ hello: universe: - mw - ad + ints: + - 1 + - 2 fu: bar `) @@ -1328,6 +1331,10 @@ func TestMergeConfig(t *testing.T) { t.Fatalf("len(universe) != 2, = %d", len(universe)) } + if ints := v.GetIntSlice("hello.ints"); len(ints) != 2 { + t.Fatalf("len(ints) != 2, = %d", len(ints)) + } + if fu := v.GetString("fu"); fu != "bar" { t.Fatalf("fu != \"bar\", = %s", fu) } @@ -1368,6 +1375,10 @@ func TestMergeConfigNoMerge(t *testing.T) { t.Fatalf("len(universe) != 2, = %d", len(universe)) } + if ints := v.GetIntSlice("hello.ints"); len(ints) != 2 { + t.Fatalf("len(ints) != 2, = %d", len(ints)) + } + if fu := v.GetString("fu"); fu != "bar" { t.Fatalf("fu != \"bar\", = %s", fu) } From 7fdad0204e0e1c63db2813b0882ebe61722e2e79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1rk=20S=C3=A1gi-Kaz=C3=A1r?= Date: Sat, 13 Jul 2019 11:57:28 +0200 Subject: [PATCH 07/68] Fix typo --- viper.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/viper.go b/viper.go index 881bf0c..859a86f 100644 --- a/viper.go +++ b/viper.go @@ -797,7 +797,7 @@ func (v *Viper) GetDuration(key string) time.Duration { return cast.ToDuration(v.Get(key)) } -// GetIntSlice returns the value associated with the key as a slice of strings. +// GetIntSlice returns the value associated with the key as a slice of int values. func GetIntSlice(key string) []int { return v.GetIntSlice(key) } func (v *Viper) GetIntSlice(key string) []int { return cast.ToIntSlice(v.Get(key)) From 275a36d0a03ab384fbf34cca7fbee094d95fc0b2 Mon Sep 17 00:00:00 2001 From: CodeLingo Bot Date: Wed, 13 Feb 2019 00:48:45 +0000 Subject: [PATCH 08/68] Fix function comments based on best practices from Effective Go Signed-off-by: CodeLingo Bot --- flags.go | 2 +- viper.go | 4 ++-- viper_test.go | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/flags.go b/flags.go index dd32f4e..b5ddbf5 100644 --- a/flags.go +++ b/flags.go @@ -36,7 +36,7 @@ type pflagValue struct { flag *pflag.Flag } -// HasChanges returns whether the flag has changes or not. +// HasChanged returns whether the flag has changes or not. func (p pflagValue) HasChanged() bool { return p.flag.Changed } diff --git a/viper.go b/viper.go index 859a86f..5c441b4 100644 --- a/viper.go +++ b/viper.go @@ -226,7 +226,7 @@ func New() *Viper { return v } -// Intended for testing, will reset all to default settings. +// Reset: Intended for testing, will reset all to default settings. // In the public interface for the viper package so applications // can use it in their testing as well. func Reset() { @@ -1142,7 +1142,7 @@ func (v *Viper) SetEnvKeyReplacer(r *strings.Replacer) { v.envKeyReplacer = r } -// Aliases provide another accessor for the same key. +// RegisterAlias: Aliases provide another accessor for the same key. // This enables one to change a name without breaking the application func RegisterAlias(alias string, key string) { v.RegisterAlias(alias, key) } func (v *Viper) RegisterAlias(alias string, key string) { diff --git a/viper_test.go b/viper_test.go index 75afaae..f91791f 100644 --- a/viper_test.go +++ b/viper_test.go @@ -1774,7 +1774,7 @@ func BenchmarkGet(b *testing.B) { } } -// This is the "perfect result" for the above. +// BenchmarkGetBoolFromMap is the "perfect result" for the above. func BenchmarkGetBoolFromMap(b *testing.B) { m := make(map[string]bool) key := "BenchmarkGetBool" From e6d1c6bc9a6debfae12cfd0285a24d1b2fc0b31a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1rk=20S=C3=A1gi-Kaz=C3=A1r?= Date: Sat, 13 Jul 2019 12:04:26 +0200 Subject: [PATCH 09/68] Improve godoc --- viper.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/viper.go b/viper.go index 5c441b4..74ea372 100644 --- a/viper.go +++ b/viper.go @@ -226,7 +226,7 @@ func New() *Viper { return v } -// Reset: Intended for testing, will reset all to default settings. +// Reset is intended for testing, will reset all to default settings. // In the public interface for the viper package so applications // can use it in their testing as well. func Reset() { @@ -1142,8 +1142,8 @@ func (v *Viper) SetEnvKeyReplacer(r *strings.Replacer) { v.envKeyReplacer = r } -// RegisterAlias: Aliases provide another accessor for the same key. -// This enables one to change a name without breaking the application +// RegisterAlias creates an alias that provides another accessor for the same key. +// This enables one to change a name without breaking the application. func RegisterAlias(alias string, key string) { v.RegisterAlias(alias, key) } func (v *Viper) RegisterAlias(alias string, key string) { v.registerAlias(alias, strings.ToLower(key)) From e325492b82e97dec8208f8a2362ee9efe9023261 Mon Sep 17 00:00:00 2001 From: Nicolas Martin Date: Thu, 3 Jan 2019 15:31:09 +0100 Subject: [PATCH 10/68] Add missing call to initWF.Done() --- viper.go | 1 + 1 file changed, 1 insertion(+) diff --git a/viper.go b/viper.go index 74ea372..363a6f3 100644 --- a/viper.go +++ b/viper.go @@ -295,6 +295,7 @@ func (v *Viper) WatchConfig() { filename, err := v.getConfigFile() if err != nil { log.Printf("error: %v\n", err) + initWG.Done() return } From e02bc9eca55d5fc66221bc0aeeaaa77410603914 Mon Sep 17 00:00:00 2001 From: bpizzi Date: Fri, 27 Jul 2018 10:10:14 +0200 Subject: [PATCH 11/68] Fixed missing f.Close() in writeConfig() Defering can cause trouble because we're writing to the file outside the function where the defering is registered, calling f.Sync() ensures that bytes are flushed to disk even is Close() is called too soon. --- viper.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/viper.go b/viper.go index 363a6f3..79c334e 100644 --- a/viper.go +++ b/viper.go @@ -1378,7 +1378,13 @@ func (v *Viper) writeConfig(filename string, force bool) error { if err != nil { return err } - return v.marshalWriter(f, configType) + defer f.Close() + + if err := v.marshalWriter(f, configType); err != nil { + return err + } + + return f.Sync() } // Unmarshal a Reader into a map. From cdccc8152cf6202c7ce866570a7390d81bf02fbd Mon Sep 17 00:00:00 2001 From: Rodrigo Chiossi Date: Tue, 30 Jan 2018 10:15:47 +0000 Subject: [PATCH 12/68] Fix SafeWriteConfig If the config file does not exist and the force flag is not set, OpenFile would not use O_CREATE flag, causing viper to fail with error "File not exist" and to not create the config file. This patch changes the behavior of writeConfig() so that if force is set to false, OpenFile will use O_EXCL flag, thus failing if the file already exists or creating a new file otherwise. Signed-off-by: Rodrigo Chiossi --- viper.go | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/viper.go b/viper.go index 79c334e..b89a96a 100644 --- a/viper.go +++ b/viper.go @@ -1364,15 +1364,9 @@ func (v *Viper) writeConfig(filename string, force bool) error { if v.config == nil { v.config = make(map[string]interface{}) } - var flags int - if force == true { - flags = os.O_CREATE | os.O_TRUNC | os.O_WRONLY - } else { - if _, err := os.Stat(filename); os.IsNotExist(err) { - flags = os.O_WRONLY - } else { - return fmt.Errorf("File: %s exists. Use WriteConfig to overwrite.", filename) - } + flags := os.O_CREATE | os.O_TRUNC | os.O_WRONLY + if !force { + flags |= os.O_EXCL } f, err := v.fs.OpenFile(filename, flags, v.configPermissions) if err != nil { From d65fa7608b54d861eae8524279cdc7e52f4dd432 Mon Sep 17 00:00:00 2001 From: Christian Muehlhaeuser Date: Sat, 20 Jul 2019 00:03:44 +0200 Subject: [PATCH 13/68] Fixed typo Just a nitpicky typo fix. --- viper.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/viper.go b/viper.go index b89a96a..07ef850 100644 --- a/viper.go +++ b/viper.go @@ -345,7 +345,7 @@ func (v *Viper) WatchConfig() { } }() watcher.Add(configDir) - initWG.Done() // done initalizing the watch in this go routine, so the parent routine can move on... + initWG.Done() // done initializing the watch in this go routine, so the parent routine can move on... eventsWG.Wait() // now, wait for event loop to end in this go-routine... }() initWG.Wait() // make sure that the go routine above fully ended before returning From 1b33e8258e07ea1da9064b06ee29a0d0831b693d Mon Sep 17 00:00:00 2001 From: Christian Muehlhaeuser Date: Sat, 20 Jul 2019 00:10:42 +0200 Subject: [PATCH 14/68] Add error handling Added error handling around ineffectual err assignments. Please review thoroughly. --- viper.go | 3 +++ viper_test.go | 1 + 2 files changed, 4 insertions(+) diff --git a/viper.go b/viper.go index 07ef850..712c5e3 100644 --- a/viper.go +++ b/viper.go @@ -1469,6 +1469,9 @@ func (v *Viper) marshalWriter(f afero.File, configType string) error { case "hcl": b, err := json.Marshal(c) + if err != nil { + return ConfigMarshalError{err} + } ast, err := hcl.Parse(string(b)) if err != nil { return ConfigMarshalError{err} diff --git a/viper_test.go b/viper_test.go index f91791f..c1ea262 100644 --- a/viper_test.go +++ b/viper_test.go @@ -952,6 +952,7 @@ func TestDirsSearch(t *testing.T) { v.SetDefault(`key`, `default`) entries, err := ioutil.ReadDir(root) + assert.Nil(t, err) for _, e := range entries { if e.IsDir() { v.AddConfigPath(e.Name()) From 33bf76add3b746c1d275f0a5451b4d5750b2f2ff Mon Sep 17 00:00:00 2001 From: Christian Muehlhaeuser Date: Sat, 20 Jul 2019 00:07:23 +0200 Subject: [PATCH 15/68] Simplify code Removed unnecessary conversions. --- viper.go | 4 ++-- viper_test.go | 6 ++---- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/viper.go b/viper.go index 712c5e3..c65881a 100644 --- a/viper.go +++ b/viper.go @@ -1402,7 +1402,7 @@ func (v *Viper) unmarshalReader(in io.Reader, c map[string]interface{}) error { } case "hcl": - obj, err := hcl.Parse(string(buf.Bytes())) + obj, err := hcl.Parse(buf.String()) if err != nil { return ConfigParseError{err} } @@ -1772,7 +1772,7 @@ func (v *Viper) flattenAndMergeMap(shadow map[string]bool, m map[string]interfac func (v *Viper) mergeFlatMap(shadow map[string]bool, m map[string]interface{}) map[string]bool { // scan keys outer: - for k, _ := range m { + for k := range m { path := strings.Split(k, v.keyDelim) // scan intermediate paths var parentKey string diff --git a/viper_test.go b/viper_test.go index c1ea262..592551c 100644 --- a/viper_test.go +++ b/viper_test.go @@ -8,7 +8,6 @@ package viper import ( "bytes" "encoding/json" - "fmt" "io" "io/ioutil" "os" @@ -263,7 +262,7 @@ func (s *stringValue) Type() string { } func (s *stringValue) String() string { - return fmt.Sprintf("%s", *s) + return string(*s) } func TestBasics(t *testing.T) { @@ -498,8 +497,7 @@ func TestAllKeys(t *testing.T) { dob, _ := time.Parse(time.RFC3339, "1979-05-27T07:32:00Z") all := map[string]interface{}{"owner": map[string]interface{}{"organization": "MongoDB", "bio": "MongoDB Chief Developer Advocate & Hacker at Large", "dob": dob}, "title": "TOML Example", "ppu": 0.55, "eyes": "brown", "clothing": map[string]interface{}{"trousers": "denim", "jacket": "leather", "pants": map[string]interface{}{"size": "large"}}, "id": "0001", "batters": map[string]interface{}{"batter": []interface{}{map[string]interface{}{"type": "Regular"}, map[string]interface{}{"type": "Chocolate"}, map[string]interface{}{"type": "Blueberry"}, map[string]interface{}{"type": "Devil's Food"}}}, "hacker": true, "beard": true, "hobbies": []interface{}{"skateboarding", "snowboarding", "go"}, "age": 35, "type": "donut", "newkey": "remote", "name": "Cake", "p_id": "0001", "p_ppu": "0.55", "p_name": "Cake", "p_batters": map[string]interface{}{"batter": map[string]interface{}{"type": "Regular"}}, "p_type": "donut", "foos": []map[string]interface{}{map[string]interface{}{"foo": []map[string]interface{}{map[string]interface{}{"key": 1}, map[string]interface{}{"key": 2}, map[string]interface{}{"key": 3}, map[string]interface{}{"key": 4}}}}, "title_dotenv": "DotEnv Example", "type_dotenv": "donut", "name_dotenv": "Cake"} - var allkeys sort.StringSlice - allkeys = AllKeys() + allkeys := sort.StringSlice(AllKeys()) allkeys.Sort() ks.Sort() From e697d557b7f549ddf91098cef2ad6e92176dbcdf Mon Sep 17 00:00:00 2001 From: TwiN Date: Fri, 16 Aug 2019 17:15:10 -0400 Subject: [PATCH 16/68] Fix small typo --- viper.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/viper.go b/viper.go index c65881a..5133de7 100644 --- a/viper.go +++ b/viper.go @@ -3,7 +3,7 @@ // Use of this source code is governed by an MIT-style // license that can be found in the LICENSE file. -// Viper is a application configuration system. +// Viper is an application configuration system. // It believes that applications can be configured a variety of ways // via flags, ENVIRONMENT variables, configuration files retrieved // from the file system, or a remote key/value store. From bd1db6bb8c597678e6e65e9d8f364ed0ead2721b Mon Sep 17 00:00:00 2001 From: Mark Sagi-Kazar Date: Thu, 29 Aug 2019 15:29:27 +0200 Subject: [PATCH 17/68] Run go mod tidy --- go.mod | 1 + go.sum | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/go.mod b/go.mod index 9ee26ef..a1ea058 100644 --- a/go.mod +++ b/go.mod @@ -28,6 +28,7 @@ require ( github.com/spf13/jwalterweatherman v1.0.0 github.com/spf13/pflag v1.0.3 github.com/stretchr/testify v1.2.2 + github.com/subosito/gotenv v1.2.0 github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5 // indirect github.com/ugorji/go v1.1.4 // indirect github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2 // indirect diff --git a/go.sum b/go.sum index a718a4d..6c22453 100644 --- a/go.sum +++ b/go.sum @@ -71,8 +71,6 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= -github.com/magiconair/properties v1.8.0 h1:LLgXmsheXeRoUOBOjtwPQCWIYqM/LU1ayDtDePerRcY= -github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ= github.com/magiconair/properties v1.8.1 h1:ZC2Vc7/ZFkGmsVC9KvOjumD+G5lXy2RtTKyzRKO2BQ4= github.com/magiconair/properties v1.8.1/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ= github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU= @@ -117,6 +115,8 @@ github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnIn github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +github.com/subosito/gotenv v1.2.0 h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s= +github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw= github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5 h1:LnC5Kc/wtumK+WB441p7ynQJzVuNRJiqddSIE3IlSEQ= github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U= github.com/ugorji/go v1.1.4 h1:j4s+tAvLfL3bZyefP2SEWmhBzmuIlH/eqNuPdFPgngw= From 99520c81d86ee7d0ee2ef143c8576be0c7533827 Mon Sep 17 00:00:00 2001 From: inkychris Date: Sat, 16 Mar 2019 11:10:32 +0000 Subject: [PATCH 18/68] Implemented ability to unmarshal keys containing dots to structs. Changed formatting of test objects for better git diffing and readibility. Fixed failing tests on Windows. --- viper.go | 20 +++++++- viper_test.go | 123 ++++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 134 insertions(+), 9 deletions(-) diff --git a/viper.go b/viper.go index 5133de7..491799c 100644 --- a/viper.go +++ b/viper.go @@ -1789,6 +1789,24 @@ outer: return shadow } +// Converts a fully qualified map key into a list of relative +// map keys, allowing for keys to contain the delimiter themselves +func keyComponents(v *Viper, key string) []string { + var result []string + components := strings.Split(key, v.keyDelim) + for index := 0; index < len(components); index++ { + potentialKey := strings.Join(components[0:index], v.keyDelim) + if v.Get(potentialKey) != nil { + result = append(result, potentialKey) + } + } + result = append(result, key) + for i := len(result) - 1; i > 0; i-- { + result[i] = strings.Replace(result[i], result[i-1]+v.keyDelim, "", 1) + } + return result +} + // AllSettings merges all settings and returns them as a map[string]interface{}. func AllSettings() map[string]interface{} { return v.AllSettings() } func (v *Viper) AllSettings() map[string]interface{} { @@ -1801,7 +1819,7 @@ func (v *Viper) AllSettings() map[string]interface{} { // check just in case anything changes continue } - path := strings.Split(k, v.keyDelim) + path := keyComponents(v, k) lastKey := strings.ToLower(path[len(path)-1]) deepestMap := deepSearch(m, path[0:len(path)-1]) // set innermost value diff --git a/viper_test.go b/viper_test.go index 592551c..3c15ed2 100644 --- a/viper_test.go +++ b/viper_test.go @@ -13,6 +13,7 @@ import ( "os" "os/exec" "path" + "path/filepath" "reflect" "runtime" "sort" @@ -45,6 +46,10 @@ clothing: age: 35 eyes : brown beard: true +emails: + steve@hacker.com: + created: 01/02/03 + active: true `) var yamlExampleWithExtras = []byte(`Existing: true @@ -207,11 +212,16 @@ func initHcl() { func initDirs(t *testing.T) (string, string, func()) { var ( - testDirs = []string{`a a`, `b`, `c\c`, `D_`} + testDirs = []string{`a a`, `b`, `C_`} config = `improbable` ) + if runtime.GOOS != "windows" { + testDirs = append(testDirs, `d\d`) + } + root, err := ioutil.TempDir("", "") + require.NoError(t, err, "Failed to create temporary directory") cleanup := true defer func() { @@ -224,7 +234,7 @@ func initDirs(t *testing.T) (string, string, func()) { assert.Nil(t, err) err = os.Chdir(root) - assert.Nil(t, err) + require.Nil(t, err) for _, dir := range testDirs { err = os.Mkdir(dir, 0750) @@ -415,7 +425,10 @@ func TestEmptyEnv(t *testing.T) { BindEnv("type") // Empty environment variable BindEnv("name") // Bound, but not set environment variable - os.Clearenv() + os.Unsetenv("type") + os.Unsetenv("TYPE") + os.Unsetenv("name") + os.Unsetenv("NAME") os.Setenv("TYPE", "") @@ -431,7 +444,10 @@ func TestEmptyEnv_Allowed(t *testing.T) { BindEnv("type") // Empty environment variable BindEnv("name") // Bound, but not set environment variable - os.Clearenv() + os.Unsetenv("type") + os.Unsetenv("TYPE") + os.Unsetenv("name") + os.Unsetenv("NAME") os.Setenv("TYPE", "") @@ -491,11 +507,98 @@ func TestSetEnvKeyReplacer(t *testing.T) { func TestAllKeys(t *testing.T) { initConfigs() - ks := sort.StringSlice{"title", "newkey", "owner.organization", "owner.dob", "owner.bio", "name", "beard", "ppu", "batters.batter", "hobbies", "clothing.jacket", "clothing.trousers", "clothing.pants.size", "age", "hacker", "id", "type", "eyes", "p_id", "p_ppu", "p_batters.batter.type", "p_type", "p_name", "foos", - "title_dotenv", "type_dotenv", "name_dotenv", + ks := sort.StringSlice{ + "title", + "newkey", + "owner.organization", + "owner.dob", + "owner.bio", + "name", + "beard", + "ppu", + "batters.batter", + "hobbies", + "clothing.jacket", + "clothing.trousers", + "clothing.pants.size", + "age", + "hacker", + "id", + "type", + "eyes", + "p_id", + "p_ppu", + "p_batters.batter.type", + "p_type", + "p_name", + "foos", + "title_dotenv", + "type_dotenv", + "name_dotenv", + "emails.steve@hacker.com.active", + "emails.steve@hacker.com.created", } dob, _ := time.Parse(time.RFC3339, "1979-05-27T07:32:00Z") - all := map[string]interface{}{"owner": map[string]interface{}{"organization": "MongoDB", "bio": "MongoDB Chief Developer Advocate & Hacker at Large", "dob": dob}, "title": "TOML Example", "ppu": 0.55, "eyes": "brown", "clothing": map[string]interface{}{"trousers": "denim", "jacket": "leather", "pants": map[string]interface{}{"size": "large"}}, "id": "0001", "batters": map[string]interface{}{"batter": []interface{}{map[string]interface{}{"type": "Regular"}, map[string]interface{}{"type": "Chocolate"}, map[string]interface{}{"type": "Blueberry"}, map[string]interface{}{"type": "Devil's Food"}}}, "hacker": true, "beard": true, "hobbies": []interface{}{"skateboarding", "snowboarding", "go"}, "age": 35, "type": "donut", "newkey": "remote", "name": "Cake", "p_id": "0001", "p_ppu": "0.55", "p_name": "Cake", "p_batters": map[string]interface{}{"batter": map[string]interface{}{"type": "Regular"}}, "p_type": "donut", "foos": []map[string]interface{}{map[string]interface{}{"foo": []map[string]interface{}{map[string]interface{}{"key": 1}, map[string]interface{}{"key": 2}, map[string]interface{}{"key": 3}, map[string]interface{}{"key": 4}}}}, "title_dotenv": "DotEnv Example", "type_dotenv": "donut", "name_dotenv": "Cake"} + all := map[string]interface{}{ + "owner": map[string]interface{}{ + "organization": "MongoDB", + "bio": "MongoDB Chief Developer Advocate & Hacker at Large", + "dob": dob, + }, + "title": "TOML Example", + "ppu": 0.55, + "emails": map[string]interface{}{ + "steve@hacker.com": map[string]interface{}{ + "active": true, + "created": "01/02/03", + }, + }, + "eyes": "brown", + "clothing": map[string]interface{}{ + "trousers": "denim", + "jacket": "leather", + "pants": map[string]interface{}{"size": "large"}, + }, + "id": "0001", + "batters": map[string]interface{}{ + "batter": []interface{}{ + map[string]interface{}{"type": "Regular"}, + map[string]interface{}{"type": "Chocolate"}, + map[string]interface{}{"type": "Blueberry"}, + map[string]interface{}{"type": "Devil's Food"}, + }, + }, + "hacker": true, + "beard": true, + "hobbies": []interface{}{ + "skateboarding", + "snowboarding", + "go", + }, + "age": 35, + "type": "donut", + "newkey": "remote", + "name": "Cake", + "p_id": "0001", + "p_ppu": "0.55", + "p_name": "Cake", + "p_batters": map[string]interface{}{ + "batter": map[string]interface{}{"type": "Regular"}, + }, + "p_type": "donut", + "foos": []map[string]interface{}{ + { + "foo": []map[string]interface{}{ + {"key": 1}, + {"key": 2}, + {"key": 3}, + {"key": 4}}, + }, + }, + "title_dotenv": "DotEnv Example", + "type_dotenv": "donut", + "name_dotenv": "Cake", + } allkeys := sort.StringSlice(AllKeys()) allkeys.Sort() @@ -960,7 +1063,7 @@ func TestDirsSearch(t *testing.T) { err = v.ReadInConfig() assert.Nil(t, err) - assert.Equal(t, `value is `+path.Base(v.configPaths[0]), v.GetString(`key`)) + assert.Equal(t, `value is `+filepath.Base(v.configPaths[0]), v.GetString(`key`)) } func TestWrongDirsSearchNotFound(t *testing.T) { @@ -1213,6 +1316,10 @@ clothing: pants: size: large trousers: denim +emails: + steve@hacker.com: + active: true + created: 01/02/03 eyes: brown hacker: true hobbies: From 583f79b3ea0db3590b23667b3130094ca14c1dac Mon Sep 17 00:00:00 2001 From: Lars Lehtonen Date: Wed, 18 Sep 2019 08:47:54 -0700 Subject: [PATCH 19/68] remote: fix two dropped errors --- remote/remote.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/remote/remote.go b/remote/remote.go index 810d070..02cf5a9 100644 --- a/remote/remote.go +++ b/remote/remote.go @@ -77,11 +77,12 @@ func getConfigManager(rp viper.RemoteProvider) (crypt.ConfigManager, error) { var err error if rp.SecretKeyring() != "" { - kr, err := os.Open(rp.SecretKeyring()) - defer kr.Close() + var kr *os.File + kr, err = os.Open(rp.SecretKeyring()) if err != nil { return nil, err } + defer kr.Close() if rp.Provider() == "etcd" { cm, err = crypt.NewEtcdConfigManager([]string{rp.Endpoint()}, kr) } else { From c171232d3abcdff8a584d1083fd5c11cfc5bf5c3 Mon Sep 17 00:00:00 2001 From: Mark Sagi-Kazar Date: Fri, 27 Sep 2019 21:50:09 +0200 Subject: [PATCH 20/68] Add go version to the mod file --- go.mod | 2 ++ 1 file changed, 2 insertions(+) diff --git a/go.mod b/go.mod index a1ea058..b24a373 100644 --- a/go.mod +++ b/go.mod @@ -1,5 +1,7 @@ module github.com/spf13/viper +go 1.12 + require ( github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6 // indirect github.com/coreos/bbolt v1.3.2 // indirect From 371f39c3ab6ffb983f89c737291fe9ec8a5096c7 Mon Sep 17 00:00:00 2001 From: Mark Sagi-Kazar Date: Fri, 27 Sep 2019 21:51:10 +0200 Subject: [PATCH 21/68] Add Go 1.13 to travis --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index d122f22..ed677bb 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,6 +10,7 @@ env: go: - 1.11.x - 1.12.x + - 1.13.x - tip os: From 398adc5a7da8f7fb1dda192beb2a9db63f797445 Mon Sep 17 00:00:00 2001 From: Mark Sagi-Kazar Date: Fri, 27 Sep 2019 17:09:18 +0200 Subject: [PATCH 22/68] Add breaking test that requires reverting #673 --- viper_test.go | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/viper_test.go b/viper_test.go index 3c15ed2..5416bd6 100644 --- a/viper_test.go +++ b/viper_test.go @@ -254,7 +254,7 @@ func initDirs(t *testing.T) (string, string, func()) { } } -//stubs for PFlag Values +// stubs for PFlag Values type stringValue string func newStringValue(val string, p *string) *stringValue { @@ -864,7 +864,7 @@ func TestBindPFlag(t *testing.T) { assert.Equal(t, testString, Get("testvalue")) flag.Value.Set("testing_mutate") - flag.Changed = true //hack for pflag usage + flag.Changed = true // hack for pflag usage assert.Equal(t, "testing_mutate", Get("testvalue")) @@ -1856,6 +1856,23 @@ func TestWatchFile(t *testing.T) { } +func TestUnmarshal_DotSeparatorBackwardCompatibility(t *testing.T) { + flags := pflag.NewFlagSet("test", pflag.ContinueOnError) + flags.String("foo.bar", "cobra_flag", "") + + v := New() + assert.NoError(t, v.BindPFlags(flags)) + + config := &struct { + Foo struct { + Bar string + } + }{} + + assert.NoError(t, v.Unmarshal(config)) + assert.Equal(t, "cobra_flag", config.Foo.Bar) +} + func BenchmarkGetBool(b *testing.B) { key := "BenchmarkGetBool" v = New() From 71509d28871fd3eeda5f56fb097e65d318c578d6 Mon Sep 17 00:00:00 2001 From: Mark Sagi-Kazar Date: Fri, 27 Sep 2019 17:18:11 +0200 Subject: [PATCH 23/68] Revert the effective changes of #673 --- viper.go | 20 +------------------- viper_test.go | 18 +----------------- 2 files changed, 2 insertions(+), 36 deletions(-) diff --git a/viper.go b/viper.go index 491799c..5133de7 100644 --- a/viper.go +++ b/viper.go @@ -1789,24 +1789,6 @@ outer: return shadow } -// Converts a fully qualified map key into a list of relative -// map keys, allowing for keys to contain the delimiter themselves -func keyComponents(v *Viper, key string) []string { - var result []string - components := strings.Split(key, v.keyDelim) - for index := 0; index < len(components); index++ { - potentialKey := strings.Join(components[0:index], v.keyDelim) - if v.Get(potentialKey) != nil { - result = append(result, potentialKey) - } - } - result = append(result, key) - for i := len(result) - 1; i > 0; i-- { - result[i] = strings.Replace(result[i], result[i-1]+v.keyDelim, "", 1) - } - return result -} - // AllSettings merges all settings and returns them as a map[string]interface{}. func AllSettings() map[string]interface{} { return v.AllSettings() } func (v *Viper) AllSettings() map[string]interface{} { @@ -1819,7 +1801,7 @@ func (v *Viper) AllSettings() map[string]interface{} { // check just in case anything changes continue } - path := keyComponents(v, k) + path := strings.Split(k, v.keyDelim) lastKey := strings.ToLower(path[len(path)-1]) deepestMap := deepSearch(m, path[0:len(path)-1]) // set innermost value diff --git a/viper_test.go b/viper_test.go index 5416bd6..f8364a0 100644 --- a/viper_test.go +++ b/viper_test.go @@ -46,10 +46,6 @@ clothing: age: 35 eyes : brown beard: true -emails: - steve@hacker.com: - created: 01/02/03 - active: true `) var yamlExampleWithExtras = []byte(`Existing: true @@ -535,8 +531,6 @@ func TestAllKeys(t *testing.T) { "title_dotenv", "type_dotenv", "name_dotenv", - "emails.steve@hacker.com.active", - "emails.steve@hacker.com.created", } dob, _ := time.Parse(time.RFC3339, "1979-05-27T07:32:00Z") all := map[string]interface{}{ @@ -547,13 +541,7 @@ func TestAllKeys(t *testing.T) { }, "title": "TOML Example", "ppu": 0.55, - "emails": map[string]interface{}{ - "steve@hacker.com": map[string]interface{}{ - "active": true, - "created": "01/02/03", - }, - }, - "eyes": "brown", + "eyes": "brown", "clothing": map[string]interface{}{ "trousers": "denim", "jacket": "leather", @@ -1316,10 +1304,6 @@ clothing: pants: size: large trousers: denim -emails: - steve@hacker.com: - active: true - created: 01/02/03 eyes: brown hacker: true hobbies: From 40e41dd2240a2ae3f6b0a908ad9ddc6cefd5e456 Mon Sep 17 00:00:00 2001 From: Navid Shaikh Date: Wed, 16 Oct 2019 13:09:46 +0530 Subject: [PATCH 24/68] Updates YAML to v2.2.4 Make sure we're not vulnerable to CVE-2019-11253. See https://github.com/kubernetes/kubernetes/issues/83253 --- go.mod | 2 +- go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index b24a373..d7d702f 100644 --- a/go.mod +++ b/go.mod @@ -42,5 +42,5 @@ require ( golang.org/x/net v0.0.0-20190522155817-f3200d17e092 // indirect golang.org/x/time v0.0.0-20190308202827-9d24e82272b4 // indirect google.golang.org/grpc v1.21.0 // indirect - gopkg.in/yaml.v2 v2.2.2 + gopkg.in/yaml.v2 v2.2.4 ) diff --git a/go.sum b/go.sum index 6c22453..f0c87ff 100644 --- a/go.sum +++ b/go.sum @@ -175,6 +175,6 @@ gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8 gopkg.in/resty.v1 v1.12.0/go.mod h1:mDo4pnntr5jdWRML875a/NmxYqAlA73dVijT2AXvQQo= gopkg.in/yaml.v2 v2.0.0-20170812160011-eb3733d160e7/go.mod h1:JAlM8MvJe8wmxCU4Bli9HhUf9+ttbYbLASfIpnQbh74= gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= -gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= -gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.2.4 h1:/eiJrUcujPVeJ3xlSWaiNi3uSVmDGBK1pDHUHAnao1I= +gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= From 72b022eb357a56469725dcd03918449e2278d02e Mon Sep 17 00:00:00 2001 From: Vivek V Date: Wed, 9 Oct 2019 19:06:40 +0530 Subject: [PATCH 25/68] Added capacity to slice creation --- viper.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/viper.go b/viper.go index 5133de7..e9010bc 100644 --- a/viper.go +++ b/viper.go @@ -1723,7 +1723,7 @@ func (v *Viper) AllKeys() []string { m = v.flattenAndMergeMap(m, v.defaults, "") // convert set of paths to list - a := []string{} + a := make([]string, 0, len(m)) for x := range m { a = append(a, x) } From d1c60d9e69e7499dc680a8552b4ea75c2fd2e91b Mon Sep 17 00:00:00 2001 From: Pedro Silva Date: Wed, 6 Nov 2019 13:54:13 +0000 Subject: [PATCH 26/68] Support config files with no extensions (#722) * Support config files with no extensions * Update README informing config files without extension are supported --- README.md | 2 ++ viper.go | 4 ++++ viper_test.go | 16 ++++++++++++++++ 3 files changed, 22 insertions(+) diff --git a/README.md b/README.md index 171f51c..fe292d0 100644 --- a/README.md +++ b/README.md @@ -121,6 +121,8 @@ if err := viper.ReadInConfig(); err != nil { // Config file found and successfully parsed ``` +*NOTE:* You can also have a file without an extension and specify the format programmaticaly. For those configuration files that lie in the home of the user without any extension like `.bashrc` + ### Writing Config Files Reading from config files is useful, but at times you want to store all modifications made at run time. diff --git a/viper.go b/viper.go index e9010bc..c64094a 100644 --- a/viper.go +++ b/viper.go @@ -1881,6 +1881,10 @@ func (v *Viper) searchInPath(in string) (filename string) { } } + if b, _ := exists(v.fs, filepath.Join(in, v.configName)); b { + return filepath.Join(in, v.configName) + } + return "" } diff --git a/viper_test.go b/viper_test.go index f8364a0..8dcfd1a 100644 --- a/viper_test.go +++ b/viper_test.go @@ -278,6 +278,22 @@ func TestBasics(t *testing.T) { assert.NoError(t, err) } +func TestSearchInPath(t *testing.T) { + filename := ".dotfilenoext" + path := "/tmp" + file := filepath.Join(path, filename) + SetConfigName(filename) + AddConfigPath(path) + _, createErr := v.fs.Create(file) + defer func() { + _ = v.fs.Remove(file) + }() + assert.NoError(t, createErr) + filename, err := v.getConfigFile() + assert.Equal(t, file, filename) + assert.NoError(t, err) +} + func TestDefault(t *testing.T) { SetDefault("age", 45) assert.Equal(t, 45, Get("age")) From 01d7d76eb0020ae2fd1275899065ddfdd9f6a528 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Dunglas?= Date: Fri, 8 Nov 2019 01:09:09 +0100 Subject: [PATCH 27/68] Add Mercure in the list of projects using Viper --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index fe292d0..3be3b4b 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,7 @@ Many Go projects are built using Viper including: * [BloomApi](https://www.bloomapi.com/) * [doctl](https://github.com/digitalocean/doctl) * [Clairctl](https://github.com/jgsqware/clairctl) +* [Mercure](https://mercure.rocks) [![Build Status](https://travis-ci.org/spf13/viper.svg)](https://travis-ci.org/spf13/viper) [![Join the chat at https://gitter.im/spf13/viper](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/spf13/viper?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) [![GoDoc](https://godoc.org/github.com/spf13/viper?status.svg)](https://godoc.org/github.com/spf13/viper) From 4e1ebbdaba650cf4bb51d8a7e241b80b8d6d9553 Mon Sep 17 00:00:00 2001 From: Benoit Masson Date: Tue, 11 Apr 2017 17:49:10 +0200 Subject: [PATCH 28/68] Added tests to TestIsSet() Added tests for: - nested elements - environment values - flags (currently fails => IsSet() always returns true, due to the default value of the flag) --- viper_test.go | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/viper_test.go b/viper_test.go index 8dcfd1a..677a14b 100644 --- a/viper_test.go +++ b/viper_test.go @@ -1039,12 +1039,47 @@ func TestReadBufConfig(t *testing.T) { func TestIsSet(t *testing.T) { v := New() v.SetConfigType("yaml") + + /* config and defaults */ v.ReadConfig(bytes.NewBuffer(yamlExample)) + v.SetDefault("clothing.shoes", "sneakers") + + assert.True(t, v.IsSet("clothing")) assert.True(t, v.IsSet("clothing.jacket")) assert.False(t, v.IsSet("clothing.jackets")) + assert.True(t, v.IsSet("clothing.shoes")) + + /* state change */ assert.False(t, v.IsSet("helloworld")) v.Set("helloworld", "fubar") assert.True(t, v.IsSet("helloworld")) + + /* env */ + v.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) + v.BindEnv("eyes") + v.BindEnv("foo") + v.BindEnv("clothing.hat") + v.BindEnv("clothing.hats") + os.Setenv("FOO", "bar") + os.Setenv("CLOTHING_HAT", "bowler") + + assert.True(t, v.IsSet("eyes")) // in the config file + assert.True(t, v.IsSet("foo")) // in the environment + assert.True(t, v.IsSet("clothing.hat")) // in the environment + assert.False(t, v.IsSet("clothing.hats")) // not defined + + /* flags */ + flagset := pflag.NewFlagSet("testisset", pflag.ContinueOnError) + flagset.Bool("foobaz", false, "foobaz") + flagset.Bool("barbaz", false, "barbaz") + foobaz, barbaz := flagset.Lookup("foobaz"), flagset.Lookup("barbaz") + v.BindPFlag("foobaz", foobaz) + v.BindPFlag("barbaz", barbaz) + barbaz.Value.Set("true") + barbaz.Changed = true // hack for pflag usage + + assert.False(t, v.IsSet("foobaz")) + assert.True(t, v.IsSet("barbaz")) } func TestDirsSearch(t *testing.T) { From 9e353e395e9f619d63334d59bfacdd987e701431 Mon Sep 17 00:00:00 2001 From: Benoit Masson Date: Tue, 11 Apr 2017 22:15:44 +0200 Subject: [PATCH 29/68] find() looks for flag's default values only when requested. Default value should be looked for by Get(), but not by IsSet(). This logic should remain inside find(), to make sure that shadowing of keys is handled properly. Fixes Issue #276. --- viper.go | 58 +++++++++++++++++++++++++++++++------------------------- 1 file changed, 32 insertions(+), 26 deletions(-) diff --git a/viper.go b/viper.go index c64094a..4c98c50 100644 --- a/viper.go +++ b/viper.go @@ -670,7 +670,7 @@ func GetViper() *Viper { func Get(key string) interface{} { return v.Get(key) } func (v *Viper) Get(key string) interface{} { lcaseKey := strings.ToLower(key) - val := v.find(lcaseKey) + val := v.find(lcaseKey, true) if val == nil { return nil } @@ -977,11 +977,15 @@ func (v *Viper) BindEnv(input ...string) error { } // 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. +// Viper will then check in the following order: +// flag, env, config file, key/value store. +// Lastly, if no value was found and flagDefault is true, and if the key +// corresponds to a flag, the flag's default value is returned. +// // Note: this assumes a lower-cased key given. -func (v *Viper) find(lcaseKey string) interface{} { +func (v *Viper) find(lcaseKey string, flagDefault bool) interface{} { var ( val interface{} @@ -1083,29 +1087,31 @@ func (v *Viper) find(lcaseKey string) interface{} { return nil } - // last chance: if no other value is returned and a flag does exist for the value, - // get the flag's value even if the flag's value has not changed - if flag, exists := v.pflags[lcaseKey]; exists { - switch flag.ValueType() { - case "int", "int8", "int16", "int32", "int64": - return cast.ToInt(flag.ValueString()) - case "bool": - return cast.ToBool(flag.ValueString()) - case "stringSlice": - s := strings.TrimPrefix(flag.ValueString(), "[") - s = strings.TrimSuffix(s, "]") - res, _ := readAsCSV(s) - return res - case "intSlice": - s := strings.TrimPrefix(flag.ValueString(), "[") - s = strings.TrimSuffix(s, "]") - res, _ := readAsCSV(s) - return cast.ToIntSlice(res) - default: - return flag.ValueString() + if flagDefault { + // last chance: if no value is found and a flag does exist for the key, + // get the flag's default value even if the flag's value has not been set. + if flag, exists := v.pflags[lcaseKey]; exists { + switch flag.ValueType() { + case "int", "int8", "int16", "int32", "int64": + return cast.ToInt(flag.ValueString()) + case "bool": + return cast.ToBool(flag.ValueString()) + case "stringSlice": + s := strings.TrimPrefix(flag.ValueString(), "[") + s = strings.TrimSuffix(s, "]") + res, _ := readAsCSV(s) + return res + case "intSlice": + s := strings.TrimPrefix(flag.ValueString(), "[") + s = strings.TrimSuffix(s, "]") + res, _ := readAsCSV(s) + return cast.ToIntSlice(res) + default: + return flag.ValueString() + } } + // last item, no need to check shadowing } - // last item, no need to check shadowing return nil } @@ -1124,7 +1130,7 @@ func readAsCSV(val string) ([]string, error) { func IsSet(key string) bool { return v.IsSet(key) } func (v *Viper) IsSet(key string) bool { lcaseKey := strings.ToLower(key) - val := v.find(lcaseKey) + val := v.find(lcaseKey, false) return val != nil } From a7084797945bd6ddcca91e4981b82c7393e6dca1 Mon Sep 17 00:00:00 2001 From: javaducky Date: Fri, 13 Sep 2019 09:29:19 -0500 Subject: [PATCH 30/68] Updating SafeWriteConfig and SafeWriteConfigAs to match documented behavior. Methods should throw an error if the config file already exists or if no configpath is configured when not explicitly requesting a write path. --- viper.go | 27 ++++++++++++++--- viper_test.go | 80 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 103 insertions(+), 4 deletions(-) diff --git a/viper.go b/viper.go index 4c98c50..6167aff 100644 --- a/viper.go +++ b/viper.go @@ -115,6 +115,22 @@ func (fnfe ConfigFileNotFoundError) Error() string { return fmt.Sprintf("Config File %q Not Found in %q", fnfe.name, fnfe.locations) } +// ConfigFileAlreadyExistsError denotes failure to write new configuration file. +type ConfigFileAlreadyExistsError string + +// Error returns the formatted error when configuration already exists. +func (faee ConfigFileAlreadyExistsError) Error() string { + return fmt.Sprintf("Config File %q Already Exists", string(faee)) +} + +// MissingConfigurationError denotes a required configuration setting has not been provided. +type MissingConfigurationError string + +// Error returns the formatted error when a required configuration element has not been provided. +func (mce MissingConfigurationError) Error() string { + return fmt.Sprintf("Missing Configuration for %q", string(mce)) +} + // A DecoderConfigOption can be passed to viper.Unmarshal to configure // mapstructure.DecoderConfig options type DecoderConfigOption func(*mapstructure.DecoderConfig) @@ -1337,11 +1353,10 @@ func (v *Viper) WriteConfig() error { // SafeWriteConfig writes current configuration to file only if the file does not exist. func SafeWriteConfig() error { return v.SafeWriteConfig() } func (v *Viper) SafeWriteConfig() error { - filename, err := v.getConfigFile() - if err != nil { - return err + if len(v.configPaths) < 1 { + return MissingConfigurationError("configPath") } - return v.writeConfig(filename, false) + return v.SafeWriteConfigAs(filepath.Join(v.configPaths[0], v.configName+"."+v.configType)) } // WriteConfigAs writes current configuration to a given filename. @@ -1353,6 +1368,10 @@ func (v *Viper) WriteConfigAs(filename string) error { // SafeWriteConfigAs writes current configuration to a given filename if it does not exist. func SafeWriteConfigAs(filename string) error { return v.SafeWriteConfigAs(filename) } func (v *Viper) SafeWriteConfigAs(filename string) error { + handle, err := v.fs.Stat(filename) + if handle != nil && err == nil { + return ConfigFileAlreadyExistsError(filename) + } return v.writeConfig(filename, false) } diff --git a/viper_test.go b/viper_test.go index 677a14b..7651787 100644 --- a/viper_test.go +++ b/viper_test.go @@ -1384,6 +1384,86 @@ func TestWriteConfigYAML(t *testing.T) { assert.Equal(t, yamlWriteExpected, read) } +func TestSafeWriteConfig(t *testing.T) { + v := New() + fs := afero.NewMemMapFs() + v.SetFs(fs) + v.AddConfigPath("/test") + v.SetConfigName("c") + v.SetConfigType("yaml") + err := v.ReadConfig(bytes.NewBuffer(yamlExample)) + if err != nil { + t.Fatal(err) + } + if err = v.SafeWriteConfig(); err != nil { + t.Fatal(err) + } + read, err := afero.ReadFile(fs, "/test/c.yaml") + if err != nil { + t.Fatal(err) + } + assert.Equal(t, yamlWriteExpected, read) +} + +func TestSafeWriteConfigWithMissingConfigPath(t *testing.T) { + v := New() + fs := afero.NewMemMapFs() + v.SetFs(fs) + v.SetConfigName("c") + v.SetConfigType("yaml") + err := v.SafeWriteConfig() + if err == nil { + t.Fatal("Expected exception") + } + _, ok := err.(MissingConfigurationError) + assert.True(t, ok, "Expected MissingConfigurationError") +} + +func TestSafeWriteConfigWithExistingFile(t *testing.T) { + v := New() + fs := afero.NewMemMapFs() + fs.Create("/test/c.yaml") + v.SetFs(fs) + v.AddConfigPath("/test") + v.SetConfigName("c") + v.SetConfigType("yaml") + err := v.SafeWriteConfig() + if err == nil { + t.Fatal("Expected exception") + } + _, ok := err.(ConfigFileAlreadyExistsError) + assert.True(t, ok, "Expected ConfigFileAlreadyExistsError") +} + +func TestSafeWriteAsConfig(t *testing.T) { + v := New() + fs := afero.NewMemMapFs() + v.SetFs(fs) + err := v.ReadConfig(bytes.NewBuffer(yamlExample)) + if err != nil { + t.Fatal(err) + } + if err = v.SafeWriteConfigAs("/test/c.yaml"); err != nil { + t.Fatal(err) + } + if _, err = afero.ReadFile(fs, "/test/c.yaml"); err != nil { + t.Fatal(err) + } +} + +func TestSafeWriteConfigAsWithExistingFile(t *testing.T) { + v := New() + fs := afero.NewMemMapFs() + fs.Create("/test/c.yaml") + v.SetFs(fs) + err := v.SafeWriteConfigAs("/test/c.yaml") + if err == nil { + t.Fatal("Expected exception") + } + _, ok := err.(ConfigFileAlreadyExistsError) + assert.True(t, ok, "Expected ConfigFileAlreadyExistsError") +} + var yamlMergeExampleTgt = []byte(` hello: pop: 37890 From 3a19b6e0d99b8272ed919058bfbba07730296dad Mon Sep 17 00:00:00 2001 From: javaducky Date: Tue, 3 Dec 2019 19:14:08 -0600 Subject: [PATCH 31/68] Review updates to utilize afero for file checks and updated checks on unit tests --- viper.go | 15 ++++----------- viper_test.go | 32 +++++++------------------------- 2 files changed, 11 insertions(+), 36 deletions(-) diff --git a/viper.go b/viper.go index 6167aff..a475e95 100644 --- a/viper.go +++ b/viper.go @@ -23,6 +23,7 @@ import ( "bytes" "encoding/csv" "encoding/json" + "errors" "fmt" "io" "log" @@ -123,14 +124,6 @@ func (faee ConfigFileAlreadyExistsError) Error() string { return fmt.Sprintf("Config File %q Already Exists", string(faee)) } -// MissingConfigurationError denotes a required configuration setting has not been provided. -type MissingConfigurationError string - -// Error returns the formatted error when a required configuration element has not been provided. -func (mce MissingConfigurationError) Error() string { - return fmt.Sprintf("Missing Configuration for %q", string(mce)) -} - // A DecoderConfigOption can be passed to viper.Unmarshal to configure // mapstructure.DecoderConfig options type DecoderConfigOption func(*mapstructure.DecoderConfig) @@ -1354,7 +1347,7 @@ func (v *Viper) WriteConfig() error { func SafeWriteConfig() error { return v.SafeWriteConfig() } func (v *Viper) SafeWriteConfig() error { if len(v.configPaths) < 1 { - return MissingConfigurationError("configPath") + return errors.New("Missing configuration for 'configPath'") } return v.SafeWriteConfigAs(filepath.Join(v.configPaths[0], v.configName+"."+v.configType)) } @@ -1368,8 +1361,8 @@ func (v *Viper) WriteConfigAs(filename string) error { // SafeWriteConfigAs writes current configuration to a given filename if it does not exist. func SafeWriteConfigAs(filename string) error { return v.SafeWriteConfigAs(filename) } func (v *Viper) SafeWriteConfigAs(filename string) error { - handle, err := v.fs.Stat(filename) - if handle != nil && err == nil { + alreadyExists, err := afero.Exists(v.fs, filename) + if alreadyExists && err == nil { return ConfigFileAlreadyExistsError(filename) } return v.writeConfig(filename, false) diff --git a/viper_test.go b/viper_test.go index 7651787..85c2d02 100644 --- a/viper_test.go +++ b/viper_test.go @@ -1391,17 +1391,10 @@ func TestSafeWriteConfig(t *testing.T) { v.AddConfigPath("/test") v.SetConfigName("c") v.SetConfigType("yaml") - err := v.ReadConfig(bytes.NewBuffer(yamlExample)) - if err != nil { - t.Fatal(err) - } - if err = v.SafeWriteConfig(); err != nil { - t.Fatal(err) - } + require.NoError(t, v.ReadConfig(bytes.NewBuffer(yamlExample))) + require.NoError(t, v.SafeWriteConfig()) read, err := afero.ReadFile(fs, "/test/c.yaml") - if err != nil { - t.Fatal(err) - } + require.NoError(t, err) assert.Equal(t, yamlWriteExpected, read) } @@ -1411,12 +1404,7 @@ func TestSafeWriteConfigWithMissingConfigPath(t *testing.T) { v.SetFs(fs) v.SetConfigName("c") v.SetConfigType("yaml") - err := v.SafeWriteConfig() - if err == nil { - t.Fatal("Expected exception") - } - _, ok := err.(MissingConfigurationError) - assert.True(t, ok, "Expected MissingConfigurationError") + require.EqualError(t, v.SafeWriteConfig(), "Missing configuration for 'configPath'") } func TestSafeWriteConfigWithExistingFile(t *testing.T) { @@ -1428,9 +1416,7 @@ func TestSafeWriteConfigWithExistingFile(t *testing.T) { v.SetConfigName("c") v.SetConfigType("yaml") err := v.SafeWriteConfig() - if err == nil { - t.Fatal("Expected exception") - } + require.Error(t, err) _, ok := err.(ConfigFileAlreadyExistsError) assert.True(t, ok, "Expected ConfigFileAlreadyExistsError") } @@ -1443,9 +1429,7 @@ func TestSafeWriteAsConfig(t *testing.T) { if err != nil { t.Fatal(err) } - if err = v.SafeWriteConfigAs("/test/c.yaml"); err != nil { - t.Fatal(err) - } + require.NoError(t, v.SafeWriteConfigAs("/test/c.yaml")) if _, err = afero.ReadFile(fs, "/test/c.yaml"); err != nil { t.Fatal(err) } @@ -1457,9 +1441,7 @@ func TestSafeWriteConfigAsWithExistingFile(t *testing.T) { fs.Create("/test/c.yaml") v.SetFs(fs) err := v.SafeWriteConfigAs("/test/c.yaml") - if err == nil { - t.Fatal("Expected exception") - } + require.Error(t, err) _, ok := err.(ConfigFileAlreadyExistsError) assert.True(t, ok, "Expected ConfigFileAlreadyExistsError") } From 351bfe9719e59c39c308342edbf087a6cb65f30b Mon Sep 17 00:00:00 2001 From: Matti R Date: Tue, 3 Dec 2019 10:46:17 -0500 Subject: [PATCH 32/68] loop through ini sections pass tests write out ini file & tests go fmt Update viper_test.go fix test gofmt --- viper.go | 38 ++++++++++++++++++++++++++++++++-- viper_test.go | 57 +++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 91 insertions(+), 4 deletions(-) diff --git a/viper.go b/viper.go index a475e95..6014bf7 100644 --- a/viper.go +++ b/viper.go @@ -47,6 +47,7 @@ import ( jww "github.com/spf13/jwalterweatherman" "github.com/spf13/pflag" "github.com/subosito/gotenv" + "gopkg.in/ini.v1" ) // ConfigMarshalError happens when failing to marshal the configuration. @@ -240,7 +241,7 @@ func New() *Viper { // can use it in their testing as well. func Reset() { v = New() - SupportedExts = []string{"json", "toml", "yaml", "yml", "properties", "props", "prop", "hcl", "dotenv", "env"} + SupportedExts = []string{"json", "toml", "yaml", "yml", "properties", "props", "prop", "hcl", "dotenv", "env", "ini"} SupportedRemoteProviders = []string{"etcd", "consul"} } @@ -279,7 +280,7 @@ type RemoteProvider interface { } // SupportedExts are universally supported extensions. -var SupportedExts = []string{"json", "toml", "yaml", "yml", "properties", "props", "prop", "hcl", "dotenv", "env"} +var SupportedExts = []string{"json", "toml", "yaml", "yml", "properties", "props", "prop", "hcl", "dotenv", "env", "ini"} // SupportedRemoteProviders are universally supported remote providers. var SupportedRemoteProviders = []string{"etcd", "consul"} @@ -1462,6 +1463,23 @@ func (v *Viper) unmarshalReader(in io.Reader, c map[string]interface{}) error { // set innermost value deepestMap[lastKey] = value } + + case "ini": + cfg := ini.Empty() + err := cfg.Append(buf.Bytes()) + if err != nil { + return ConfigParseError{err} + } + sections := cfg.Sections() + for i := 0; i < len(sections); i++ { + section := sections[i] + keys := section.Keys() + for j := 0; j < len(keys); j++ { + key := keys[j] + value := cfg.Section(section.Name()).Key(key.Name()).String() + c[section.Name()+"."+key.Name()] = value + } + } } insensitiviseMap(c) @@ -1545,6 +1563,22 @@ func (v *Viper) marshalWriter(f afero.File, configType string) error { if _, err = f.WriteString(string(b)); err != nil { return ConfigMarshalError{err} } + + case "ini": + keys := v.AllKeys() + cfg := ini.Empty() + ini.PrettyFormat = false + for i := 0; i < len(keys); i++ { + key := keys[i] + lastSep := strings.LastIndex(key, ".") + sectionName := key[:(lastSep)] + keyName := key[(lastSep + 1):] + if sectionName == "default" { + sectionName = "" + } + cfg.Section(sectionName).Key(keyName).SetValue(Get(key).(string)) + } + cfg.WriteTo(f) } return nil } diff --git a/viper_test.go b/viper_test.go index 85c2d02..f8349ac 100644 --- a/viper_test.go +++ b/viper_test.go @@ -118,6 +118,24 @@ var remoteExample = []byte(`{ "newkey":"remote" }`) +var iniExample = []byte(`; Package name +NAME = ini +; Package version +VERSION = v1 +; Package import path +IMPORT_PATH = gopkg.in/%(NAME)s.%(VERSION)s + +# Information about package author +# Bio can be written in multiple lines. +[author] +NAME = Unknwon ; Succeeding comment +E-MAIL = fake@localhost +GITHUB = https://github.com/%(NAME)s +BIO = """Gopher. +Coding addict. +Good man. +""" # Succeeding comment`) + func initConfigs() { Reset() var r io.Reader @@ -148,6 +166,10 @@ func initConfigs() { SetConfigType("json") remote := bytes.NewReader(remoteExample) unmarshalReader(remote, v.kvstore) + + SetConfigType("ini") + r = bytes.NewReader(iniExample) + unmarshalReader(r, v.config) } func initConfig(typ, config string) { @@ -204,6 +226,14 @@ func initHcl() { unmarshalReader(r, v.config) } +func initIni() { + Reset() + SetConfigType("ini") + r := bytes.NewReader(iniExample) + + unmarshalReader(r, v.config) +} + // make directories for testing func initDirs(t *testing.T) (string, string, func()) { @@ -396,6 +426,11 @@ func TestHCL(t *testing.T) { assert.NotEqual(t, "cronut", Get("type")) } +func TestIni(t *testing.T) { + initIni() + assert.Equal(t, "ini", Get("default.name")) +} + func TestRemotePrecedence(t *testing.T) { initJSON() @@ -521,6 +556,10 @@ func TestAllKeys(t *testing.T) { ks := sort.StringSlice{ "title", + "author.bio", + "author.e-mail", + "author.github", + "author.name", "newkey", "owner.organization", "owner.dob", @@ -532,6 +571,9 @@ func TestAllKeys(t *testing.T) { "hobbies", "clothing.jacket", "clothing.trousers", + "default.import_path", + "default.name", + "default.version", "clothing.pants.size", "age", "hacker", @@ -556,13 +598,24 @@ func TestAllKeys(t *testing.T) { "dob": dob, }, "title": "TOML Example", - "ppu": 0.55, - "eyes": "brown", + "author": map[string]interface{}{ + "e-mail": "fake@localhost", + "github": "https://github.com/Unknwon", + "name": "Unknwon", + "bio": "Gopher.\nCoding addict.\nGood man.\n", + }, + "ppu": 0.55, + "eyes": "brown", "clothing": map[string]interface{}{ "trousers": "denim", "jacket": "leather", "pants": map[string]interface{}{"size": "large"}, }, + "default": map[string]interface{}{ + "import_path": "gopkg.in/ini.v1", + "name": "ini", + "version": "v1", + }, "id": "0001", "batters": map[string]interface{}{ "batter": []interface{}{ From 8feab54f0ebb72554cdaf00ab1b958df1d4d855f Mon Sep 17 00:00:00 2001 From: Mark Sagi-Kazar Date: Fri, 6 Dec 2019 10:56:50 +0100 Subject: [PATCH 33/68] Tidy dependencies --- go.mod | 2 ++ go.sum | 12 ++++++++++++ 2 files changed, 14 insertions(+) diff --git a/go.mod b/go.mod index d7d702f..0e358cb 100644 --- a/go.mod +++ b/go.mod @@ -24,6 +24,7 @@ require ( github.com/mitchellh/mapstructure v1.1.2 github.com/pelletier/go-toml v1.2.0 github.com/prometheus/client_golang v0.9.3 // indirect + github.com/smartystreets/goconvey v1.6.4 // indirect github.com/soheilhy/cmux v0.1.4 // indirect github.com/spf13/afero v1.1.2 github.com/spf13/cast v1.3.0 @@ -42,5 +43,6 @@ require ( golang.org/x/net v0.0.0-20190522155817-f3200d17e092 // indirect golang.org/x/time v0.0.0-20190308202827-9d24e82272b4 // indirect google.golang.org/grpc v1.21.0 // indirect + gopkg.in/ini.v1 v1.51.0 gopkg.in/yaml.v2 v2.2.4 ) diff --git a/go.sum b/go.sum index f0c87ff..d75aee2 100644 --- a/go.sum +++ b/go.sum @@ -48,6 +48,8 @@ github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5y github.com/google/btree v1.0.0 h1:0udJVsspx3VBr5FwtLhQQtuAsVc79tTq0ocGIPAU6qo= github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ= github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= +github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8= +github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY= github.com/gorilla/websocket v1.4.0 h1:WDFjx/TMzVgy9VdMMQi2K2Emtwi2QcUQsztZ/zLaH/Q= github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ= github.com/grpc-ecosystem/go-grpc-middleware v1.0.0 h1:Iju5GlWwrvL6UBg4zJJt3btmonfrMlCDdsejg4CZE7c= @@ -60,6 +62,8 @@ github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4= github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= github.com/jonboulle/clockwork v0.1.0 h1:VKV+ZcuP6l3yW9doeqz6ziZGgcynBVQO+obU0+0hcPo= github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo= +github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo= +github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU= github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w= github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q= github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= @@ -101,6 +105,10 @@ github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40T github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg= github.com/sirupsen/logrus v1.2.0 h1:juTguoYk5qI21pwyTXY3B3Y5cOTH3ZUyZCg1v/mihuo= github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= +github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d h1:zE9ykElWQ6/NYmHa3jpm/yHnI4xSofP+UP6SpjHcSeM= +github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc= +github.com/smartystreets/goconvey v1.6.4 h1:fv0U8FUIMPNf1L9lnHLvLhgicrIVChEkdzIKYqbNC9s= +github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA= github.com/soheilhy/cmux v0.1.4 h1:0HKaf1o97UwFjHH9o5XsHUOF+tqmdA7KEzXLpiyaw0E= github.com/soheilhy/cmux v0.1.4/go.mod h1:IM3LyeVVIOuxMH7sFAkER9+bJ4dT7Ms6E4xg4kGIyLM= github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= @@ -161,6 +169,8 @@ golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxb golang.org/x/tools v0.0.0-20180221164845-07fd8470d635/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= +golang.org/x/tools v0.0.0-20190328211700-ab21143f2384 h1:TFlARGu6Czu1z7q93HTxcP1P+/ZFC/IKythI5RzrnRg= +golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8 h1:Nw54tB0rB7hY/N0NQvRW8DG4Yk3Q6T9cu9RcFQDu1tc= google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc= @@ -172,6 +182,8 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/ini.v1 v1.51.0 h1:AQvPpx3LzTDM0AjnIRlVFwFFGC+npRopjZxLJj6gdno= +gopkg.in/ini.v1 v1.51.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= gopkg.in/resty.v1 v1.12.0/go.mod h1:mDo4pnntr5jdWRML875a/NmxYqAlA73dVijT2AXvQQo= gopkg.in/yaml.v2 v2.0.0-20170812160011-eb3733d160e7/go.mod h1:JAlM8MvJe8wmxCU4Bli9HhUf9+ttbYbLASfIpnQbh74= gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= From abdeaff171c284e967de4cf67341108cbce28804 Mon Sep 17 00:00:00 2001 From: Mark Sagi-Kazar Date: Fri, 6 Dec 2019 11:41:59 +0100 Subject: [PATCH 34/68] Add golangci config --- .golangci.yml | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 .golangci.yml diff --git a/.golangci.yml b/.golangci.yml new file mode 100644 index 0000000..bd0ded4 --- /dev/null +++ b/.golangci.yml @@ -0,0 +1,32 @@ +linters-settings: + golint: + min-confidence: 0.1 + goimports: + local-prefixes: github.com/spf13/viper + +linters: + enable-all: true + disable: + - funlen + - maligned + + # TODO: fix me + - gofmt + - goimports + - whitespace + - deadcode + - unused + - wsl + - golint + - stylecheck + - gochecknoinits + - misspell + - gosimple + - gochecknoglobals + - errcheck + - lll + - godox + - scopelint + - gocyclo + - gocognit + - gocritic \ No newline at end of file From d52c544291b7e11c17883f507b5ed2289f607b38 Mon Sep 17 00:00:00 2001 From: Mark Sagi-Kazar Date: Fri, 6 Dec 2019 11:48:32 +0100 Subject: [PATCH 35/68] Add Makefile --- .gitignore | 3 +++ Makefile | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 Makefile diff --git a/.gitignore b/.gitignore index 01b5c44..876b1fd 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ +/bin/ +/build/ + # Compiled Object files, Static and Dynamic libs (Shared Objects) *.o *.a diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..048280e --- /dev/null +++ b/Makefile @@ -0,0 +1,71 @@ +# A Self-Documenting Makefile: http://marmelab.com/blog/2016/02/29/auto-documented-makefile.html + +OS = $(shell uname | tr A-Z a-z) + +# Build variables +BUILD_DIR ?= build +ifeq (${VERBOSE}, 1) +ifeq ($(filter -v,${GOARGS}),) + GOARGS += -v +endif +TEST_FORMAT = short-verbose +endif + +# Dependency versions +GOTESTSUM_VERSION = 0.3.5 +GOLANGCI_VERSION = 1.21.0 + +# Add the ability to override some variables +# Use with care +-include override.mk + +.PHONY: clear +clear: ## Clear the working area and the project + rm -rf bin/ + +.PHONY: check +check: lint ## Run tests and linters + +bin/gotestsum: bin/gotestsum-${GOTESTSUM_VERSION} + @ln -sf gotestsum-${GOTESTSUM_VERSION} bin/gotestsum +bin/gotestsum-${GOTESTSUM_VERSION}: + @mkdir -p bin + curl -L https://github.com/gotestyourself/gotestsum/releases/download/v${GOTESTSUM_VERSION}/gotestsum_${GOTESTSUM_VERSION}_${OS}_amd64.tar.gz | tar -zOxf - gotestsum > ./bin/gotestsum-${GOTESTSUM_VERSION} && chmod +x ./bin/gotestsum-${GOTESTSUM_VERSION} + +TEST_PKGS ?= ./... +TEST_REPORT_NAME ?= results.xml +.PHONY: test +test: TEST_REPORT ?= main +test: TEST_FORMAT ?= short +test: SHELL = /bin/bash +test: bin/gotestsum ## Run tests + @mkdir -p ${BUILD_DIR}/test_results/${TEST_REPORT} + bin/gotestsum --no-summary=skipped --junitfile ${BUILD_DIR}/test_results/${TEST_REPORT}/${TEST_REPORT_NAME} --format ${TEST_FORMAT} -- $(filter-out -v,${GOARGS}) $(if ${TEST_PKGS},${TEST_PKGS},./...) + +bin/golangci-lint: bin/golangci-lint-${GOLANGCI_VERSION} + @ln -sf golangci-lint-${GOLANGCI_VERSION} bin/golangci-lint +bin/golangci-lint-${GOLANGCI_VERSION}: + @mkdir -p bin + curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | BINARY=golangci-lint bash -s -- v${GOLANGCI_VERSION} + @mv bin/golangci-lint $@ + +.PHONY: lint +lint: bin/golangci-lint ## Run linter + bin/golangci-lint run + +.PHONY: fix +fix: bin/golangci-lint ## Fix lint violations + bin/golangci-lint run --fix + +.PHONY: list +list: ## List all make targets + @${MAKE} -pRrn : -f $(MAKEFILE_LIST) 2>/dev/null | awk -v RS= -F: '/^# File/,/^# Finished Make data base/ {if ($$1 !~ "^[#.]") {print $$1}}' | egrep -v -e '^[^[:alnum:]]' -e '^$@$$' | sort + +.PHONY: help +.DEFAULT_GOAL := help +help: + @grep -h -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}' + +# Variable outputting/exporting rules +var-%: ; @echo $($*) +varexport-%: ; @echo $*=$($*) From fae3a8186794f7e4ded729ca28a373286557d9a3 Mon Sep 17 00:00:00 2001 From: Mark Sagi-Kazar Date: Fri, 6 Dec 2019 11:50:04 +0100 Subject: [PATCH 36/68] Add ide integration --- .gitignore | 46 ++++++++++++------------------- .idea/externalDependencies.xml | 7 +++++ .idea/go.imports.xml | 8 ++++++ .idea/modules.xml | 8 ++++++ .idea/runConfigurations/Check.xml | 7 +++++ .idea/runConfigurations/Lint.xml | 8 ++++++ .idea/runConfigurations/Tests.xml | 23 ++++++++++++++++ .idea/viper.iml | 9 ++++++ 8 files changed, 87 insertions(+), 29 deletions(-) create mode 100644 .idea/externalDependencies.xml create mode 100644 .idea/go.imports.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/runConfigurations/Check.xml create mode 100644 .idea/runConfigurations/Lint.xml create mode 100644 .idea/runConfigurations/Tests.xml create mode 100644 .idea/viper.iml diff --git a/.gitignore b/.gitignore index 876b1fd..d6941f3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,32 +1,20 @@ /bin/ /build/ +/var/ +/vendor/ -# Compiled Object files, Static and Dynamic libs (Shared Objects) -*.o -*.a -*.so - -# Folders -_obj -_test - -# Architecture specific extensions/prefixes -*.[568vq] -[568vq].out - -*.cgo1.go -*.cgo2.c -_cgo_defun.c -_cgo_gotypes.go -_cgo_export.* - -_testmain.go - -*.exe -*.test -*.bench - -.vscode - -# exclude dependencies in the `/vendor` folder -vendor +# IDE integration +/.vscode/* +!/.vscode/launch.json +!/.vscode/tasks.json +/.idea/* +!/.idea/codeStyles/ +!/.idea/copyright/ +!/.idea/dataSources.xml +!/.idea/*.iml +!/.idea/externalDependencies.xml +!/.idea/go.imports.xml +!/.idea/modules.xml +!/.idea/runConfigurations/ +!/.idea/scopes/ +!/.idea/sqldialects.xml diff --git a/.idea/externalDependencies.xml b/.idea/externalDependencies.xml new file mode 100644 index 0000000..3ee9345 --- /dev/null +++ b/.idea/externalDependencies.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/.idea/go.imports.xml b/.idea/go.imports.xml new file mode 100644 index 0000000..560a90c --- /dev/null +++ b/.idea/go.imports.xml @@ -0,0 +1,8 @@ + + + + + diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..1a6ea8e --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/runConfigurations/Check.xml b/.idea/runConfigurations/Check.xml new file mode 100644 index 0000000..8477e06 --- /dev/null +++ b/.idea/runConfigurations/Check.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/.idea/runConfigurations/Lint.xml b/.idea/runConfigurations/Lint.xml new file mode 100644 index 0000000..45aa432 --- /dev/null +++ b/.idea/runConfigurations/Lint.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/runConfigurations/Tests.xml b/.idea/runConfigurations/Tests.xml new file mode 100644 index 0000000..f5b3013 --- /dev/null +++ b/.idea/runConfigurations/Tests.xml @@ -0,0 +1,23 @@ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/viper.iml b/.idea/viper.iml new file mode 100644 index 0000000..5e764c4 --- /dev/null +++ b/.idea/viper.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file From 78a0e37a24d05866553b2038e3c780301c390deb Mon Sep 17 00:00:00 2001 From: Mark Sagi-Kazar Date: Fri, 6 Dec 2019 12:22:07 +0100 Subject: [PATCH 37/68] Add github actions workflow --- .github/workflows/.editorconfig | 2 ++ .github/workflows/ci.yml | 35 +++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 .github/workflows/.editorconfig create mode 100644 .github/workflows/ci.yml diff --git a/.github/workflows/.editorconfig b/.github/workflows/.editorconfig new file mode 100644 index 0000000..7bd3346 --- /dev/null +++ b/.github/workflows/.editorconfig @@ -0,0 +1,2 @@ +[*.yml] +indent_size = 2 diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..576e740 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,35 @@ +name: CI + +on: + push: + branches: + - master + pull_request: + +jobs: + build: + name: Build + runs-on: ubuntu-latest + strategy: + max-parallel: 10 + matrix: + go: ['1.11', '1.12', '1.13'] + env: + VERBOSE: 1 + GOFLAGS: -mod=readonly + GOPROXY: https://proxy.golang.org + + steps: + - name: Set up Go + uses: actions/setup-go@v1 + with: + go-version: ${{ matrix.go }} + + - name: Checkout code + uses: actions/checkout@v1 + + - name: Run tests + run: make test + + - name: Run linter + run: make lint From e316012b4d8512bf8627dedd6f51dcb996f8128e Mon Sep 17 00:00:00 2001 From: Mark Sagi-Kazar Date: Fri, 6 Dec 2019 12:37:45 +0100 Subject: [PATCH 38/68] Fix gofmt --- .golangci.yml | 1 - viper_test.go | 10 +++++----- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index bd0ded4..f0adecd 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -11,7 +11,6 @@ linters: - maligned # TODO: fix me - - gofmt - goimports - whitespace - deadcode diff --git a/viper_test.go b/viper_test.go index f8349ac..94a8e75 100644 --- a/viper_test.go +++ b/viper_test.go @@ -1049,18 +1049,18 @@ func TestFindsNestedKeys(t *testing.T) { "owner.dob": dob, "beard": true, "foos": []map[string]interface{}{ - map[string]interface{}{ + { "foo": []map[string]interface{}{ - map[string]interface{}{ + { "key": 1, }, - map[string]interface{}{ + { "key": 2, }, - map[string]interface{}{ + { "key": 3, }, - map[string]interface{}{ + { "key": 4, }, }, From cea8b9dfcd3639e2745275adb2d4c9bb8ff5d054 Mon Sep 17 00:00:00 2001 From: Mark Sagi-Kazar Date: Fri, 6 Dec 2019 12:38:20 +0100 Subject: [PATCH 39/68] Fix goimports --- .golangci.yml | 1 - remote/remote.go | 1 + 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/.golangci.yml b/.golangci.yml index f0adecd..8c83fc1 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -11,7 +11,6 @@ linters: - maligned # TODO: fix me - - goimports - whitespace - deadcode - unused diff --git a/remote/remote.go b/remote/remote.go index 02cf5a9..9321e6c 100644 --- a/remote/remote.go +++ b/remote/remote.go @@ -12,6 +12,7 @@ import ( "os" "github.com/spf13/viper" + crypt "github.com/xordataexchange/crypt/config" ) From 7b5adba7887e2e9ad518267c39f860d683aded6f Mon Sep 17 00:00:00 2001 From: Mark Sagi-Kazar Date: Fri, 6 Dec 2019 12:39:08 +0100 Subject: [PATCH 40/68] Fix whitespace --- .golangci.yml | 1 - remote/remote.go | 2 -- viper.go | 1 - viper_test.go | 14 -------------- 4 files changed, 18 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 8c83fc1..ff0f80c 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -11,7 +11,6 @@ linters: - maligned # TODO: fix me - - whitespace - deadcode - unused - wsl diff --git a/remote/remote.go b/remote/remote.go index 9321e6c..6e8a534 100644 --- a/remote/remote.go +++ b/remote/remote.go @@ -64,9 +64,7 @@ func (rc remoteConfigProvider) WatchChannel(rp viper.RemoteProvider) (<-chan *vi Error: resp.Error, Value: resp.Value, } - } - } }(cryptoResponseCh, viperResponsCh, quitwc, quit) diff --git a/viper.go b/viper.go index 6014bf7..5326cb9 100644 --- a/viper.go +++ b/viper.go @@ -996,7 +996,6 @@ func (v *Viper) BindEnv(input ...string) error { // // Note: this assumes a lower-cased key given. func (v *Viper) find(lcaseKey string, flagDefault bool) interface{} { - var ( val interface{} exists bool diff --git a/viper_test.go b/viper_test.go index 94a8e75..b611449 100644 --- a/viper_test.go +++ b/viper_test.go @@ -236,7 +236,6 @@ func initIni() { // make directories for testing func initDirs(t *testing.T) (string, string, func()) { - var ( testDirs = []string{`a a`, `b`, `C_`} config = `improbable` @@ -463,7 +462,6 @@ func TestEnv(t *testing.T) { AutomaticEnv() assert.Equal(t, "crunk", Get("name")) - } func TestEmptyEnv(t *testing.T) { @@ -812,7 +810,6 @@ func TestBindPFlags(t *testing.T) { for name, expected := range mutatedTestValues { assert.Equal(t, expected, v.Get(name)) } - } func TestBindPFlagsStringSlice(t *testing.T) { @@ -924,7 +921,6 @@ func TestBindPFlag(t *testing.T) { flag.Changed = true // hack for pflag usage assert.Equal(t, "testing_mutate", Get("testvalue")) - } func TestBoundCaseSensitivity(t *testing.T) { @@ -946,7 +942,6 @@ func TestBoundCaseSensitivity(t *testing.T) { BindPFlag("eYEs", flag) assert.Equal(t, "green", Get("eyes")) - } func TestSizeInBytes(t *testing.T) { @@ -1069,10 +1064,8 @@ func TestFindsNestedKeys(t *testing.T) { } for key, expectedValue := range expected { - assert.Equal(t, expectedValue, v.Get(key)) } - } func TestReadBufConfig(t *testing.T) { @@ -1136,7 +1129,6 @@ func TestIsSet(t *testing.T) { } func TestDirsSearch(t *testing.T) { - root, config, cleanup := initDirs(t) defer cleanup() @@ -1159,7 +1151,6 @@ func TestDirsSearch(t *testing.T) { } func TestWrongDirsSearchNotFound(t *testing.T) { - _, config, cleanup := initDirs(t) defer cleanup() @@ -1179,7 +1170,6 @@ func TestWrongDirsSearchNotFound(t *testing.T) { } func TestWrongDirsSearchNotFoundForMerge(t *testing.T) { - _, config, cleanup := initDirs(t) defer cleanup() @@ -1679,7 +1669,6 @@ func TestMergeConfigMap(t *testing.T) { } assert(1234) - } func TestUnmarshalingWithAliases(t *testing.T) { @@ -1718,7 +1707,6 @@ func TestSetConfigNameClearsFileCache(t *testing.T) { } func TestShadowedNestedValue(t *testing.T) { - config := `name: steve clothing: jacket: leather @@ -1898,7 +1886,6 @@ func doTestCaseInsensitive(t *testing.T, typ, config string) { assert.Equal(t, 3, cast.ToInt(Get("ef.ijk"))) assert.Equal(t, 4, cast.ToInt(Get("ef.lm.no"))) assert.Equal(t, 5, cast.ToInt(Get("ef.lm.p.q"))) - } func newViperWithConfigFile(t *testing.T) (*Viper, string, func()) { @@ -2003,7 +1990,6 @@ func TestWatchFile(t *testing.T) { require.Nil(t, err) assert.Equal(t, "baz", v.Get("foo")) }) - } func TestUnmarshal_DotSeparatorBackwardCompatibility(t *testing.T) { From 29bb3ee94fd1c9e5148f546b70c8e8578bcbf75f Mon Sep 17 00:00:00 2001 From: Mark Sagi-Kazar Date: Fri, 6 Dec 2019 12:41:03 +0100 Subject: [PATCH 41/68] Fix deadcode --- .golangci.yml | 2 -- viper.go | 4 ---- 2 files changed, 6 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index ff0f80c..79b853e 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -11,8 +11,6 @@ linters: - maligned # TODO: fix me - - deadcode - - unused - wsl - golint - stylecheck diff --git a/viper.go b/viper.go index 5326cb9..30768dd 100644 --- a/viper.go +++ b/viper.go @@ -1368,7 +1368,6 @@ func (v *Viper) SafeWriteConfigAs(filename string) error { return v.writeConfig(filename, false) } -func writeConfig(filename string, force bool) error { return v.writeConfig(filename, force) } func (v *Viper) writeConfig(filename string, force bool) error { jww.INFO.Println("Attempting to write configuration to file.") ext := filepath.Ext(filename) @@ -1486,9 +1485,6 @@ func (v *Viper) unmarshalReader(in io.Reader, c map[string]interface{}) error { } // Marshal a map into Writer. -func marshalWriter(f afero.File, configType string) error { - return v.marshalWriter(f, configType) -} func (v *Viper) marshalWriter(f afero.File, configType string) error { c := v.AllSettings() switch configType { From 8b7fbcaa4b23c999fbfe219fb4b5d9e54b81356a Mon Sep 17 00:00:00 2001 From: Mark Sagi-Kazar Date: Fri, 6 Dec 2019 12:46:23 +0100 Subject: [PATCH 42/68] Fix golint --- .golangci.yml | 2 -- viper.go | 6 +++--- viper_test.go | 2 +- 3 files changed, 4 insertions(+), 6 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 79b853e..1ce846c 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -12,8 +12,6 @@ linters: # TODO: fix me - wsl - - golint - - stylecheck - gochecknoinits - misspell - gosimple diff --git a/viper.go b/viper.go index 30768dd..a944cde 100644 --- a/viper.go +++ b/viper.go @@ -970,7 +970,7 @@ func BindEnv(input ...string) error { return v.BindEnv(input...) } func (v *Viper) BindEnv(input ...string) error { var key, envkey string if len(input) == 0 { - return fmt.Errorf("BindEnv missing key to bind to") + return fmt.Errorf("missing key to bind to") } key = strings.ToLower(input[0]) @@ -1347,7 +1347,7 @@ func (v *Viper) WriteConfig() error { func SafeWriteConfig() error { return v.SafeWriteConfig() } func (v *Viper) SafeWriteConfig() error { if len(v.configPaths) < 1 { - return errors.New("Missing configuration for 'configPath'") + return errors.New("missing configuration for 'configPath'") } return v.SafeWriteConfigAs(filepath.Join(v.configPaths[0], v.configName+"."+v.configType)) } @@ -1372,7 +1372,7 @@ func (v *Viper) writeConfig(filename string, force bool) error { jww.INFO.Println("Attempting to write configuration to file.") ext := filepath.Ext(filename) if len(ext) <= 1 { - return fmt.Errorf("Filename: %s requires valid extension.", filename) + return fmt.Errorf("filename: %s requires valid extension", filename) } configType := ext[1:] if !stringInSlice(configType, SupportedExts) { diff --git a/viper_test.go b/viper_test.go index b611449..624a34d 100644 --- a/viper_test.go +++ b/viper_test.go @@ -1447,7 +1447,7 @@ func TestSafeWriteConfigWithMissingConfigPath(t *testing.T) { v.SetFs(fs) v.SetConfigName("c") v.SetConfigType("yaml") - require.EqualError(t, v.SafeWriteConfig(), "Missing configuration for 'configPath'") + require.EqualError(t, v.SafeWriteConfig(), "missing configuration for 'configPath'") } func TestSafeWriteConfigWithExistingFile(t *testing.T) { From 2fd264d3d10f7e10b42605c8ca2054eddc5ae2ef Mon Sep 17 00:00:00 2001 From: Mark Sagi-Kazar Date: Fri, 6 Dec 2019 12:48:55 +0100 Subject: [PATCH 43/68] Fix misspell --- .golangci.yml | 1 - viper_test.go | 6 +++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 1ce846c..0ea9249 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -13,7 +13,6 @@ linters: # TODO: fix me - wsl - gochecknoinits - - misspell - gosimple - gochecknoglobals - errcheck diff --git a/viper_test.go b/viper_test.go index 624a34d..c1cf557 100644 --- a/viper_test.go +++ b/viper_test.go @@ -128,7 +128,7 @@ IMPORT_PATH = gopkg.in/%(NAME)s.%(VERSION)s # Information about package author # Bio can be written in multiple lines. [author] -NAME = Unknwon ; Succeeding comment +NAME = Unknown ; Succeeding comment E-MAIL = fake@localhost GITHUB = https://github.com/%(NAME)s BIO = """Gopher. @@ -598,8 +598,8 @@ func TestAllKeys(t *testing.T) { "title": "TOML Example", "author": map[string]interface{}{ "e-mail": "fake@localhost", - "github": "https://github.com/Unknwon", - "name": "Unknwon", + "github": "https://github.com/Unknown", + "name": "Unknown", "bio": "Gopher.\nCoding addict.\nGood man.\n", }, "ppu": 0.55, From c4fedd192b21b2f4263af0934957a77af8cdbd7a Mon Sep 17 00:00:00 2001 From: Mark Sagi-Kazar Date: Fri, 6 Dec 2019 12:49:13 +0100 Subject: [PATCH 44/68] Fix check target --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 048280e..e39b8b5 100644 --- a/Makefile +++ b/Makefile @@ -24,7 +24,7 @@ clear: ## Clear the working area and the project rm -rf bin/ .PHONY: check -check: lint ## Run tests and linters +check: test lint ## Run tests and linters bin/gotestsum: bin/gotestsum-${GOTESTSUM_VERSION} @ln -sf gotestsum-${GOTESTSUM_VERSION} bin/gotestsum From 6895c083d6ac8d19385f406381a3f5638e13dfc2 Mon Sep 17 00:00:00 2001 From: Mark Sagi-Kazar Date: Fri, 6 Dec 2019 13:51:40 +0100 Subject: [PATCH 45/68] INI is supported --- README.md | 7 ------- 1 file changed, 7 deletions(-) diff --git a/README.md b/README.md index 3be3b4b..bdb8d99 100644 --- a/README.md +++ b/README.md @@ -718,13 +718,6 @@ different vipers. ## Q & A -Q: Why not INI files? - -A: Ini files are pretty awful. There’s no standard format, and they are hard to -validate. Viper is designed to work with JSON, TOML or YAML files. If someone -really wants to add this feature, I’d be happy to merge it. It’s easy to specify -which formats your application will permit. - Q: Why is it called “Viper”? A: Viper is designed to be a [companion](http://en.wikipedia.org/wiki/Viper_(G.I._Joe)) From 52836e66ad50d64d9927b74101262204a8685457 Mon Sep 17 00:00:00 2001 From: Mark Sagi-Kazar Date: Fri, 6 Dec 2019 13:54:28 +0100 Subject: [PATCH 46/68] Improve badges --- README.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index bdb8d99..4a5ba67 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,10 @@ Go configuration with fangs! +[![Actions](https://github.com/spf13/viper/workflows/CI/badge.svg)](https://github.com/spf13/viper) +[![Join the chat at https://gitter.im/spf13/viper](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/spf13/viper?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) +[![GoDoc](https://godoc.org/github.com/spf13/viper?status.svg)](https://godoc.org/github.com/spf13/viper) + Many Go projects are built using Viper including: * [Hugo](http://gohugo.io) @@ -14,7 +18,6 @@ Many Go projects are built using Viper including: * [Clairctl](https://github.com/jgsqware/clairctl) * [Mercure](https://mercure.rocks) -[![Build Status](https://travis-ci.org/spf13/viper.svg)](https://travis-ci.org/spf13/viper) [![Join the chat at https://gitter.im/spf13/viper](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/spf13/viper?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) [![GoDoc](https://godoc.org/github.com/spf13/viper?status.svg)](https://godoc.org/github.com/spf13/viper) ## Install ```console From b6ced70067882412976750d782a0df83d6b59eea Mon Sep 17 00:00:00 2001 From: Mark Sagi-Kazar Date: Fri, 6 Dec 2019 13:58:19 +0100 Subject: [PATCH 47/68] Generic improvements --- README.md | 35 +++++++++++++++++------------------ 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 4a5ba67..a6c9a3e 100644 --- a/README.md +++ b/README.md @@ -20,10 +20,12 @@ Many Go projects are built using Viper including: ## Install + ```console -go get -u github.com/spf13/viper +go get github.com/spf13/viper ``` + ## What is Viper? Viper is a complete configuration solution for Go applications including 12-Factor apps. It is designed @@ -39,8 +41,8 @@ and formats. It supports: * reading from buffer * setting explicit values -Viper can be thought of as a registry for all of your applications -configuration needs. +Viper can be thought of as a registry for all of your applications configuration needs. + ## Why Viper? @@ -50,34 +52,31 @@ Viper is here to help with that. Viper does the following for you: -1. Find, load, and unmarshal a configuration file in JSON, TOML, YAML, HCL, envfile or Java properties formats. -2. Provide a mechanism to set default values for your different - configuration options. -3. Provide a mechanism to set override values for options specified through - command line flags. -4. Provide an alias system to easily rename parameters without breaking existing - code. -5. Make it easy to tell the difference between when a user has provided a - command line or config file which is the same as the default. +1. Find, load, and unmarshal a configuration file in JSON, TOML, YAML, HCL, INI, envfile or Java properties formats. +2. Provide a mechanism to set default values for your different configuration options. +3. Provide a mechanism to set override values for options specified through command line flags. +4. Provide an alias system to easily rename parameters without breaking existing code. +5. Make it easy to tell the difference between when a user has provided a command line or config file which is the same as the default. -Viper uses the following precedence order. Each item takes precedence over the -item below it: +Viper uses the following precedence order. Each item takes precedence over the item below it: - * explicit call to Set + * explicit call to `Set` * flag * env * config * key/value store * default -Viper configuration keys are case insensitive. +**Important:** Viper configuration keys are case insensitive. +There are ongoing discussions about making that optional. + ## Putting Values into Viper ### Establishing Defaults A good configuration system will support default values. A default value is not -required for a key, but it’s useful in the event that a key hasn’t been set via +required for a key, but it’s useful in the event that a key hasn't been set via config file, environment variable, remote configuration or flag. Examples: @@ -91,7 +90,7 @@ viper.SetDefault("Taxonomies", map[string]string{"tag": "tags", "category": "cat ### Reading Config Files Viper requires minimal configuration so it knows where to look for config files. -Viper supports JSON, TOML, YAML, HCL, envfile and Java Properties files. Viper can search multiple paths, but +Viper supports JSON, TOML, YAML, HCL, INI, envfile and Java Properties files. Viper can search multiple paths, but currently a single Viper instance only supports a single configuration file. Viper does not default to any configuration search paths leaving defaults decision to an application. From a73303ee8947b4498218d22e361db6f448ce6654 Mon Sep 17 00:00:00 2001 From: Mark Sagi-Kazar Date: Wed, 6 Nov 2019 12:40:41 +0100 Subject: [PATCH 48/68] Add key delimiter setter --- viper.go | 19 ++++++++++---- viper_test.go | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+), 5 deletions(-) diff --git a/viper.go b/viper.go index a944cde..0e3eeaf 100644 --- a/viper.go +++ b/viper.go @@ -34,14 +34,14 @@ import ( "sync" "time" - yaml "gopkg.in/yaml.v2" + "gopkg.in/yaml.v2" "github.com/fsnotify/fsnotify" "github.com/hashicorp/hcl" "github.com/hashicorp/hcl/hcl/printer" "github.com/magiconair/properties" "github.com/mitchellh/mapstructure" - toml "github.com/pelletier/go-toml" + "github.com/pelletier/go-toml" "github.com/spf13/afero" "github.com/spf13/cast" jww "github.com/spf13/jwalterweatherman" @@ -245,6 +245,15 @@ func Reset() { SupportedRemoteProviders = []string{"etcd", "consul"} } +// SetKeyDelimiter sets the delimiter used for determining key parts. +// By default it's value is ".". +func SetKeyDelimiter(keyDelim string) { v.SetKeyDelimiter(keyDelim) } +func (v *Viper) SetKeyDelimiter(keyDelim string) { + if keyDelim != "" { + v.keyDelim = keyDelim + } +} + type defaultRemoteProvider struct { provider string endpoint string @@ -1720,7 +1729,7 @@ func (v *Viper) getRemoteConfig(provider RemoteProvider) (map[string]interface{} func (v *Viper) watchKeyValueConfigOnChannel() error { for _, rp := range v.remoteProviders { respc, _ := RemoteConfig.WatchChannel(rp) - //Todo: Add quit channel + // Todo: Add quit channel go func(rc <-chan *RemoteResponse) { for { b := <-rc @@ -1756,7 +1765,7 @@ func (v *Viper) watchRemoteConfig(provider RemoteProvider) (map[string]interface } // AllKeys returns all keys holding a value, regardless of where they are set. -// Nested keys are returned with a v.keyDelim (= ".") separator +// Nested keys are returned with a v.keyDelim separator func AllKeys() []string { return v.AllKeys() } func (v *Viper) AllKeys() []string { m := map[string]bool{} @@ -1779,7 +1788,7 @@ func (v *Viper) AllKeys() []string { // flattenAndMergeMap recursively flattens the given map into a map[string]bool // of key paths (used as a set, easier to manipulate than a []string): -// - each path is merged into a single key string, delimited with v.keyDelim (= ".") +// - each path is merged into a single key string, delimited with v.keyDelim // - if a path is shadowed by an earlier value in the initial shadow map, // it is skipped. // The resulting set of paths is merged to the given shadow set at the same time. diff --git a/viper_test.go b/viper_test.go index c1cf557..baf0998 100644 --- a/viper_test.go +++ b/viper_test.go @@ -2009,6 +2009,74 @@ func TestUnmarshal_DotSeparatorBackwardCompatibility(t *testing.T) { assert.Equal(t, "cobra_flag", config.Foo.Bar) } +var yamlExampleWithDot = []byte(`Hacker: true +name: steve +hobbies: + - skateboarding + - snowboarding + - go +clothing: + jacket: leather + trousers: denim + pants: + size: large +age: 35 +eyes : brown +beard: true +emails: + steve@hacker.com: + created: 01/02/03 + active: true +`) + +func TestSetKeyDelimiter(t *testing.T) { + v := New() + v.SetKeyDelimiter("::") + v.SetConfigType("yaml") + r := strings.NewReader(string(yamlExampleWithDot)) + + err := v.unmarshalReader(r, v.config) + require.NoError(t, err) + + values := map[string]interface{}{ + "image": map[string]interface{}{ + "repository": "someImage", + "tag": "1.0.0", + }, + "ingress": map[string]interface{}{ + "annotations": map[string]interface{}{ + "traefik.frontend.rule.type": "PathPrefix", + "traefik.ingress.kubernetes.io/ssl-redirect": "true", + }, + }, + } + + v.SetDefault("charts::values", values) + + assert.Equal(t, "leather", v.GetString("clothing::jacket")) + assert.Equal(t, "01/02/03", v.GetString("emails::steve@hacker.com::created")) + + type config struct { + Charts struct { + Values map[string]interface{} + } + } + + expected := config{ + Charts: struct { + Values map[string]interface{} + }{ + Values: values, + }, + } + + var actual config + + assert.NoError(t, v.Unmarshal(&actual)) + + assert.Equal(t, expected, actual) +} + func BenchmarkGetBool(b *testing.B) { key := "BenchmarkGetBool" v = New() From 9a405be5c05f793c04480d3137cf4880d147ccbe Mon Sep 17 00:00:00 2001 From: Mark Sagi-Kazar Date: Wed, 6 Nov 2019 12:54:38 +0100 Subject: [PATCH 49/68] Add SetKeyDelimiter to the README --- README.md | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/README.md b/README.md index a6c9a3e..f211b06 100644 --- a/README.md +++ b/README.md @@ -663,6 +663,33 @@ if err != nil { } ``` +If you want to unmarshal configuration where the keys themselves contain dot (the default key delimiter), +you have to change the delimiter: + +```go +v := viper.New() +v.SetKeyDelimiter("::") + +v.SetDefault("chart::values", map[string]interface{}{ + "ingress": map[string]interface{}{ + "annotations": map[string]interface{}{ + "traefik.frontend.rule.type": "PathPrefix", + "traefik.ingress.kubernetes.io/ssl-redirect": "true", + }, + }, +}) + +type config struct { + Chart struct{ + Values map[string]interface{} + } +} + +var C config + +viper.Unmarshal(&C) +``` + Viper uses [github.com/mitchellh/mapstructure](https://github.com/mitchellh/mapstructure) under the hood for unmarshaling values which uses `mapstructure` tags by default. ### Marshalling to string From a842b8f61840c754255435f3c7b131ca89080c78 Mon Sep 17 00:00:00 2001 From: Mark Sagi-Kazar Date: Fri, 8 Nov 2019 14:23:20 +0100 Subject: [PATCH 50/68] Replace SetKeyDelimiter with functional options --- README.md | 3 +-- viper.go | 42 +++++++++++++++++++++++++++++++++--------- viper_test.go | 5 ++--- 3 files changed, 36 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index f211b06..857588a 100644 --- a/README.md +++ b/README.md @@ -667,8 +667,7 @@ If you want to unmarshal configuration where the keys themselves contain dot (th you have to change the delimiter: ```go -v := viper.New() -v.SetKeyDelimiter("::") +v := viper.NewWithOptions(viper.KeyDelimiter("::")) v.SetDefault("chart::values", map[string]interface{}{ "ingress": map[string]interface{}{ diff --git a/viper.go b/viper.go index 0e3eeaf..dcfd2b0 100644 --- a/viper.go +++ b/viper.go @@ -236,6 +236,39 @@ func New() *Viper { return v } +// Option configures Viper using the functional options paradigm popularized by Rob Pike and Dave Cheney. +// If you're unfamiliar with this style, +// see https://commandcenter.blogspot.com/2014/01/self-referential-functions-and-design.html and +// https://dave.cheney.net/2014/10/17/functional-options-for-friendly-apis. +type Option interface { + apply(v *Viper) +} + +type optionFunc func(v *Viper) + +func (fn optionFunc) apply(v *Viper) { + fn(v) +} + +// KeyDelimiter sets the delimiter used for determining key parts. +// By default it's value is ".". +func KeyDelimiter(d string) Option { + return optionFunc(func(v *Viper) { + v.keyDelim = d + }) +} + +// NewWithOptions creates a new Viper instance. +func NewWithOptions(opts ...Option) *Viper { + v := New() + + for _, opt := range opts { + opt.apply(v) + } + + return v +} + // Reset is intended for testing, will reset all to default settings. // In the public interface for the viper package so applications // can use it in their testing as well. @@ -245,15 +278,6 @@ func Reset() { SupportedRemoteProviders = []string{"etcd", "consul"} } -// SetKeyDelimiter sets the delimiter used for determining key parts. -// By default it's value is ".". -func SetKeyDelimiter(keyDelim string) { v.SetKeyDelimiter(keyDelim) } -func (v *Viper) SetKeyDelimiter(keyDelim string) { - if keyDelim != "" { - v.keyDelim = keyDelim - } -} - type defaultRemoteProvider struct { provider string endpoint string diff --git a/viper_test.go b/viper_test.go index baf0998..9f4dff3 100644 --- a/viper_test.go +++ b/viper_test.go @@ -2029,9 +2029,8 @@ emails: active: true `) -func TestSetKeyDelimiter(t *testing.T) { - v := New() - v.SetKeyDelimiter("::") +func TestKeyDelimiter(t *testing.T) { + v := NewWithOptions(KeyDelimiter("::")) v.SetConfigType("yaml") r := strings.NewReader(string(yamlExampleWithDot)) From bdf2db0ff8cf8f8fdc1148cb3d8ef40f05209714 Mon Sep 17 00:00:00 2001 From: Mark Sagi-Kazar Date: Fri, 6 Dec 2019 13:41:38 +0100 Subject: [PATCH 51/68] Fix goimports --- viper.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/viper.go b/viper.go index dcfd2b0..4e4d6da 100644 --- a/viper.go +++ b/viper.go @@ -34,8 +34,6 @@ import ( "sync" "time" - "gopkg.in/yaml.v2" - "github.com/fsnotify/fsnotify" "github.com/hashicorp/hcl" "github.com/hashicorp/hcl/hcl/printer" @@ -48,6 +46,7 @@ import ( "github.com/spf13/pflag" "github.com/subosito/gotenv" "gopkg.in/ini.v1" + "gopkg.in/yaml.v2" ) // ConfigMarshalError happens when failing to marshal the configuration. From 6fcf985c5ab78a80370c86454e9556b277c0212c Mon Sep 17 00:00:00 2001 From: Mark Sagi-Kazar Date: Fri, 6 Dec 2019 13:45:02 +0100 Subject: [PATCH 52/68] Fix readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 857588a..b0392c9 100644 --- a/README.md +++ b/README.md @@ -686,7 +686,7 @@ type config struct { var C config -viper.Unmarshal(&C) +v.Unmarshal(&C) ``` Viper uses [github.com/mitchellh/mapstructure](https://github.com/mitchellh/mapstructure) under the hood for unmarshaling values which uses `mapstructure` tags by default. From 4ad4c8df70ec1bed8329e5d986f09d34f66200b4 Mon Sep 17 00:00:00 2001 From: Mark Sagi-Kazar Date: Fri, 6 Dec 2019 14:11:31 +0100 Subject: [PATCH 53/68] Add string replacer interface and env key replacer option --- README.md | 3 +++ viper.go | 15 ++++++++++++++- viper_test.go | 9 +++++++++ 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index b0392c9..327308b 100644 --- a/README.md +++ b/README.md @@ -263,6 +263,9 @@ keys to an extent. This is useful if you want to use `-` or something in your `Get()` calls, but want your environmental variables to use `_` delimiters. An example of using it can be found in `viper_test.go`. +Alternatively, you can use `EnvKeyReplacer` with `NewWithOptions` factory function. +Unlike `SetEnvKeyReplacer`, it accepts a `StringReplacer` interface allowing you to write custom string replacing logic. + By default empty environment variables are considered unset and will fall back to the next configuration source. To treat empty environment variables as set, use the `AllowEmptyEnv` method. diff --git a/viper.go b/viper.go index 4e4d6da..19f15d8 100644 --- a/viper.go +++ b/viper.go @@ -197,7 +197,7 @@ type Viper struct { envPrefix string automaticEnvApplied bool - envKeyReplacer *strings.Replacer + envKeyReplacer StringReplacer allowEmptyEnv bool config map[string]interface{} @@ -257,6 +257,19 @@ func KeyDelimiter(d string) Option { }) } +// StringReplacer applies a set of replacements to a string. +type StringReplacer interface { + // Replace returns a copy of s with all replacements performed. + Replace(s string) string +} + +// EnvKeyReplacer sets a replacer used for mapping environment variables to internal keys. +func EnvKeyReplacer(r StringReplacer) Option { + return optionFunc(func(v *Viper) { + v.envKeyReplacer = r + }) +} + // NewWithOptions creates a new Viper instance. func NewWithOptions(opts ...Option) *Viper { v := New() diff --git a/viper_test.go b/viper_test.go index 9f4dff3..8f010c7 100644 --- a/viper_test.go +++ b/viper_test.go @@ -549,6 +549,15 @@ func TestSetEnvKeyReplacer(t *testing.T) { assert.Equal(t, "30s", Get("refresh-interval")) } +func TestEnvKeyReplacer(t *testing.T) { + v := NewWithOptions(EnvKeyReplacer(strings.NewReplacer("-", "_"))) + + v.AutomaticEnv() + _ = os.Setenv("REFRESH_INTERVAL", "30s") + + assert.Equal(t, "30s", v.Get("refresh-interval")) +} + func TestAllKeys(t *testing.T) { initConfigs() From bcb420b705de7762472dda0861823d053d7f4cbd Mon Sep 17 00:00:00 2001 From: Gregory Haynes Date: Fri, 8 Jun 2018 20:32:49 +0000 Subject: [PATCH 54/68] Add global UnmarshalExact method There is no helper method for UnmarshalExact which calls the corresponding method on the global viper instance. --- viper.go | 1 + 1 file changed, 1 insertion(+) diff --git a/viper.go b/viper.go index 19f15d8..6085590 100644 --- a/viper.go +++ b/viper.go @@ -949,6 +949,7 @@ func decode(input interface{}, config *mapstructure.DecoderConfig) error { // UnmarshalExact unmarshals the config into a Struct, erroring if a field is nonexistent // in the destination struct. +func UnmarshalExact(rawVal interface{}) error { return v.UnmarshalExact(rawVal) } func (v *Viper) UnmarshalExact(rawVal interface{}) error { config := defaultDecoderConfig(rawVal) config.ErrorUnused = true From 5ad4bc05cf581ee54efd78c7fd58202609d10857 Mon Sep 17 00:00:00 2001 From: Mark Sagi-Kazar Date: Fri, 6 Dec 2019 14:23:11 +0100 Subject: [PATCH 55/68] Add decoder opts to unmarshal exact --- viper.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/viper.go b/viper.go index 6085590..eb2f517 100644 --- a/viper.go +++ b/viper.go @@ -949,9 +949,11 @@ func decode(input interface{}, config *mapstructure.DecoderConfig) error { // UnmarshalExact unmarshals the config into a Struct, erroring if a field is nonexistent // in the destination struct. -func UnmarshalExact(rawVal interface{}) error { return v.UnmarshalExact(rawVal) } -func (v *Viper) UnmarshalExact(rawVal interface{}) error { - config := defaultDecoderConfig(rawVal) +func UnmarshalExact(rawVal interface{}, opts ...DecoderConfigOption) error { + return v.UnmarshalExact(rawVal, opts...) +} +func (v *Viper) UnmarshalExact(rawVal interface{}, opts ...DecoderConfigOption) error { + config := defaultDecoderConfig(rawVal, opts...) config.ErrorUnused = true err := decode(v.AllSettings(), config) From eabbc68a3ecd5cf8c11a2f84dbda5e7a38493b2f Mon Sep 17 00:00:00 2001 From: Mark Sagi-Kazar Date: Fri, 6 Dec 2019 17:23:59 +0100 Subject: [PATCH 56/68] Fix file path lookup --- util.go | 6 +++--- viper_test.go | 20 ++++++++++++++++++++ 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/util.go b/util.go index 952cad4..b788969 100644 --- a/util.go +++ b/util.go @@ -114,11 +114,11 @@ func absPathify(inPath string) string { return "" } -// Check if File / Directory Exists +// Check if file Exists func exists(fs afero.Fs, path string) (bool, error) { - _, err := fs.Stat(path) + stat, err := fs.Stat(path) if err == nil { - return true, nil + return !stat.IsDir(), nil } if os.IsNotExist(err) { return false, nil diff --git a/viper_test.go b/viper_test.go index 8f010c7..fcd1b0e 100644 --- a/viper_test.go +++ b/viper_test.go @@ -323,6 +323,26 @@ func TestSearchInPath(t *testing.T) { assert.NoError(t, err) } +func TestSearchInPath_FilesOnly(t *testing.T) { + fs := afero.NewMemMapFs() + + err := fs.Mkdir("/tmp/config", 0777) + require.NoError(t, err) + + _, err = fs.Create("/tmp/config/config.yaml") + require.NoError(t, err) + + v := New() + + v.SetFs(fs) + v.AddConfigPath("/tmp") + v.AddConfigPath("/tmp/config") + + filename, err := v.getConfigFile() + assert.Equal(t, "/tmp/config/config.yaml", filename) + assert.NoError(t, err) +} + func TestDefault(t *testing.T) { SetDefault("age", 45) assert.Equal(t, 45, Get("age")) From 06ab5a4b62072ad7af49cf178f79d83aaf3e9d92 Mon Sep 17 00:00:00 2001 From: Mark Sagi-Kazar Date: Wed, 8 Jan 2020 10:55:18 +0100 Subject: [PATCH 57/68] Add documentation about unmarshaling into embedded structs --- README.md | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/README.md b/README.md index 327308b..2bb54fa 100644 --- a/README.md +++ b/README.md @@ -692,6 +692,37 @@ var C config v.Unmarshal(&C) ``` +Viper also supports unmarshaling into embedded structs: + +```go +/* +Example config: + +module: + enabled: true + token: 89h3f98hbwf987h3f98wenf89ehf +*/ +type config struct { + Module struct { + Enabled bool + + moduleConfig `mapstructure:",squash"` + } +} + +// moduleConfig could be in a module specific package +type moduleConfig struct { + Token string +} + +var C config + +err := viper.Unmarshal(&C) +if err != nil { + t.Fatalf("unable to decode into struct, %v", err) +} +``` + Viper uses [github.com/mitchellh/mapstructure](https://github.com/mitchellh/mapstructure) under the hood for unmarshaling values which uses `mapstructure` tags by default. ### Marshalling to string From 9cd571279d195d6a02b5103f0301d8310dd4338d Mon Sep 17 00:00:00 2001 From: Pedro Silva Date: Thu, 16 Jan 2020 18:23:50 +0000 Subject: [PATCH 58/68] Extensionless files only allowed when config type is set (#827) * Only consider files without extension if the config type is explicitly specified * Hides unused variable in test * First check for config type then for file without extension --- README.md | 3 ++- viper.go | 6 ++++-- viper_test.go | 18 ++++++++++++++++++ 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 2bb54fa..a574677 100644 --- a/README.md +++ b/README.md @@ -101,6 +101,7 @@ where a configuration file is expected. ```go viper.SetConfigName("config") // name of config file (without extension) +viper.SetConfigType("yaml") // REQUIRED if the config file does not have the extension in the name viper.AddConfigPath("/etc/appname/") // path to look for the config file in viper.AddConfigPath("$HOME/.appname") // call multiple times to add many search paths viper.AddConfigPath(".") // optionally look for config in the working directory @@ -124,7 +125,7 @@ if err := viper.ReadInConfig(); err != nil { // Config file found and successfully parsed ``` -*NOTE:* You can also have a file without an extension and specify the format programmaticaly. For those configuration files that lie in the home of the user without any extension like `.bashrc` +*NOTE [since 1.6]:* You can also have a file without an extension and specify the format programmaticaly. For those configuration files that lie in the home of the user without any extension like `.bashrc` ### Writing Config Files diff --git a/viper.go b/viper.go index eb2f517..265eae7 100644 --- a/viper.go +++ b/viper.go @@ -1976,8 +1976,10 @@ func (v *Viper) searchInPath(in string) (filename string) { } } - if b, _ := exists(v.fs, filepath.Join(in, v.configName)); b { - return filepath.Join(in, v.configName) + if v.configType != "" { + if b, _ := exists(v.fs, filepath.Join(in, v.configName)); b { + return filepath.Join(in, v.configName) + } } return "" diff --git a/viper_test.go b/viper_test.go index fcd1b0e..f9dc34a 100644 --- a/viper_test.go +++ b/viper_test.go @@ -307,11 +307,29 @@ func TestBasics(t *testing.T) { assert.NoError(t, err) } +func TestSearchInPath_WithoutConfigTypeSet(t *testing.T) { + filename := ".dotfilenoext" + path := "/tmp" + file := filepath.Join(path, filename) + SetConfigName(filename) + AddConfigPath(path) + _, createErr := v.fs.Create(file) + defer func() { + _ = v.fs.Remove(file) + }() + assert.NoError(t, createErr) + _, err := v.getConfigFile() + // unless config type is set, files without extension + // are not considered + assert.Error(t, err) +} + func TestSearchInPath(t *testing.T) { filename := ".dotfilenoext" path := "/tmp" file := filepath.Join(path, filename) SetConfigName(filename) + SetConfigType("yaml") AddConfigPath(path) _, createErr := v.fs.Create(file) defer func() { From 4525543ce4fe90f7970f5e2cdc300b8ffc8c0582 Mon Sep 17 00:00:00 2001 From: Mark Sagi-Kazar Date: Thu, 16 Jan 2020 19:27:10 +0100 Subject: [PATCH 59/68] Improve CI --- .github/workflows/ci.yml | 3 +-- Makefile | 17 +++++++++++------ 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 576e740..e4b85ea 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -11,7 +11,6 @@ jobs: name: Build runs-on: ubuntu-latest strategy: - max-parallel: 10 matrix: go: ['1.11', '1.12', '1.13'] env: @@ -26,7 +25,7 @@ jobs: go-version: ${{ matrix.go }} - name: Checkout code - uses: actions/checkout@v1 + uses: actions/checkout@v2 - name: Run tests run: make test diff --git a/Makefile b/Makefile index e39b8b5..1c2cab0 100644 --- a/Makefile +++ b/Makefile @@ -1,9 +1,12 @@ # A Self-Documenting Makefile: http://marmelab.com/blog/2016/02/29/auto-documented-makefile.html OS = $(shell uname | tr A-Z a-z) +export PATH := $(abspath bin/):${PATH} # Build variables BUILD_DIR ?= build +export CGO_ENABLED ?= 0 +export GOOS = $(shell go env GOOS) ifeq (${VERBOSE}, 1) ifeq ($(filter -v,${GOARGS}),) GOARGS += -v @@ -12,7 +15,7 @@ TEST_FORMAT = short-verbose endif # Dependency versions -GOTESTSUM_VERSION = 0.3.5 +GOTESTSUM_VERSION = 0.4.0 GOLANGCI_VERSION = 1.21.0 # Add the ability to override some variables @@ -33,20 +36,19 @@ bin/gotestsum-${GOTESTSUM_VERSION}: curl -L https://github.com/gotestyourself/gotestsum/releases/download/v${GOTESTSUM_VERSION}/gotestsum_${GOTESTSUM_VERSION}_${OS}_amd64.tar.gz | tar -zOxf - gotestsum > ./bin/gotestsum-${GOTESTSUM_VERSION} && chmod +x ./bin/gotestsum-${GOTESTSUM_VERSION} TEST_PKGS ?= ./... -TEST_REPORT_NAME ?= results.xml .PHONY: test -test: TEST_REPORT ?= main test: TEST_FORMAT ?= short test: SHELL = /bin/bash +test: export CGO_ENABLED=1 test: bin/gotestsum ## Run tests - @mkdir -p ${BUILD_DIR}/test_results/${TEST_REPORT} - bin/gotestsum --no-summary=skipped --junitfile ${BUILD_DIR}/test_results/${TEST_REPORT}/${TEST_REPORT_NAME} --format ${TEST_FORMAT} -- $(filter-out -v,${GOARGS}) $(if ${TEST_PKGS},${TEST_PKGS},./...) + @mkdir -p ${BUILD_DIR} + bin/gotestsum --no-summary=skipped --junitfile ${BUILD_DIR}/coverage.xml --format ${TEST_FORMAT} -- -race -coverprofile=${BUILD_DIR}/coverage.txt -covermode=atomic $(filter-out -v,${GOARGS}) $(if ${TEST_PKGS},${TEST_PKGS},./...) bin/golangci-lint: bin/golangci-lint-${GOLANGCI_VERSION} @ln -sf golangci-lint-${GOLANGCI_VERSION} bin/golangci-lint bin/golangci-lint-${GOLANGCI_VERSION}: @mkdir -p bin - curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | BINARY=golangci-lint bash -s -- v${GOLANGCI_VERSION} + curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | bash -s -- -b ./bin/ v${GOLANGCI_VERSION} @mv bin/golangci-lint $@ .PHONY: lint @@ -57,6 +59,9 @@ lint: bin/golangci-lint ## Run linter fix: bin/golangci-lint ## Fix lint violations bin/golangci-lint run --fix +# Add custom targets here +-include custom.mk + .PHONY: list list: ## List all make targets @${MAKE} -pRrn : -f $(MAKEFILE_LIST) 2>/dev/null | awk -v RS= -F: '/^# File/,/^# Finished Make data base/ {if ($$1 !~ "^[#.]") {print $$1}}' | egrep -v -e '^[^[:alnum:]]' -e '^$@$$' | sort From 16fb4b9d29c9e2ee368a9261553fb8820b352349 Mon Sep 17 00:00:00 2001 From: Mark Sagi-Kazar Date: Thu, 16 Jan 2020 19:37:42 +0100 Subject: [PATCH 60/68] Add editorconfig --- .editorconfig | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 .editorconfig diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..63afcbc --- /dev/null +++ b/.editorconfig @@ -0,0 +1,15 @@ +root = true + +[*] +charset = utf-8 +end_of_line = lf +indent_size = 4 +indent_style = space +insert_final_newline = true +trim_trailing_whitespace = true + +[*.go] +indent_style = tab + +[{Makefile, *.mk}] +indent_style = tab From 1b7d3e05fd0f5288ff0c562809b68d123464ec00 Mon Sep 17 00:00:00 2001 From: Mark Sagi-Kazar Date: Thu, 16 Jan 2020 19:37:50 +0100 Subject: [PATCH 61/68] Improve golangci config --- .golangci.yml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.golangci.yml b/.golangci.yml index 0ea9249..a0755ce 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -21,4 +21,7 @@ linters: - scopelint - gocyclo - gocognit - - gocritic \ No newline at end of file + - gocritic + +service: + golangci-lint-version: 1.21.x From df9f4af2115f087cee156dd2ec1c60f1a47575b6 Mon Sep 17 00:00:00 2001 From: Mark Sagi-Kazar Date: Thu, 16 Jan 2020 19:38:02 +0100 Subject: [PATCH 62/68] Remove travis config --- .travis.yml | 32 -------------------------------- 1 file changed, 32 deletions(-) delete mode 100644 .travis.yml diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index ed677bb..0000000 --- a/.travis.yml +++ /dev/null @@ -1,32 +0,0 @@ -go_import_path: github.com/spf13/viper - -language: go - -env: - global: - - GO111MODULE="on" - - GOFLAGS="-mod=readonly" - -go: - - 1.11.x - - 1.12.x - - 1.13.x - - tip - -os: - - linux - - osx - -matrix: - allow_failures: - - go: tip - fast_finish: true - -script: - - go install ./... - - diff -u <(echo -n) <(gofmt -d .) - - 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 - From f2cbaea4c2e08954512f2cd2619033bdb4f87969 Mon Sep 17 00:00:00 2001 From: Mark Sagi-Kazar Date: Thu, 16 Jan 2020 19:54:31 +0100 Subject: [PATCH 63/68] Fix copy-paste error in documentation Fixes #825 --- viper.go | 5 ----- 1 file changed, 5 deletions(-) diff --git a/viper.go b/viper.go index 265eae7..a8f84b5 100644 --- a/viper.go +++ b/viper.go @@ -996,11 +996,6 @@ func (v *Viper) BindFlagValues(flags FlagValueSet) (err error) { } // BindFlagValue binds a specific key to a FlagValue. -// 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")) -// func BindFlagValue(key string, flag FlagValue) error { return v.BindFlagValue(key, flag) } func (v *Viper) BindFlagValue(key string, flag FlagValue) error { if flag == nil { From 7ddaa61d67ad0ace0e25f1e90d2c034ee9e56d0a Mon Sep 17 00:00:00 2001 From: aeneasr Date: Wed, 19 Feb 2020 23:14:44 +0100 Subject: [PATCH 64/68] Remove .idea directory --- .gitignore | 16 ---------------- .idea/externalDependencies.xml | 7 ------- .idea/go.imports.xml | 8 -------- .idea/modules.xml | 8 -------- .idea/runConfigurations/Check.xml | 7 ------- .idea/runConfigurations/Lint.xml | 8 -------- .idea/runConfigurations/Tests.xml | 23 ----------------------- .idea/viper.iml | 9 --------- 8 files changed, 86 deletions(-) delete mode 100644 .idea/externalDependencies.xml delete mode 100644 .idea/go.imports.xml delete mode 100644 .idea/modules.xml delete mode 100644 .idea/runConfigurations/Check.xml delete mode 100644 .idea/runConfigurations/Lint.xml delete mode 100644 .idea/runConfigurations/Tests.xml delete mode 100644 .idea/viper.iml diff --git a/.gitignore b/.gitignore index d6941f3..0007f97 100644 --- a/.gitignore +++ b/.gitignore @@ -2,19 +2,3 @@ /build/ /var/ /vendor/ - -# IDE integration -/.vscode/* -!/.vscode/launch.json -!/.vscode/tasks.json -/.idea/* -!/.idea/codeStyles/ -!/.idea/copyright/ -!/.idea/dataSources.xml -!/.idea/*.iml -!/.idea/externalDependencies.xml -!/.idea/go.imports.xml -!/.idea/modules.xml -!/.idea/runConfigurations/ -!/.idea/scopes/ -!/.idea/sqldialects.xml diff --git a/.idea/externalDependencies.xml b/.idea/externalDependencies.xml deleted file mode 100644 index 3ee9345..0000000 --- a/.idea/externalDependencies.xml +++ /dev/null @@ -1,7 +0,0 @@ - - - - - - - \ No newline at end of file diff --git a/.idea/go.imports.xml b/.idea/go.imports.xml deleted file mode 100644 index 560a90c..0000000 --- a/.idea/go.imports.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - diff --git a/.idea/modules.xml b/.idea/modules.xml deleted file mode 100644 index 1a6ea8e..0000000 --- a/.idea/modules.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/.idea/runConfigurations/Check.xml b/.idea/runConfigurations/Check.xml deleted file mode 100644 index 8477e06..0000000 --- a/.idea/runConfigurations/Check.xml +++ /dev/null @@ -1,7 +0,0 @@ - - - - - - - \ No newline at end of file diff --git a/.idea/runConfigurations/Lint.xml b/.idea/runConfigurations/Lint.xml deleted file mode 100644 index 45aa432..0000000 --- a/.idea/runConfigurations/Lint.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/.idea/runConfigurations/Tests.xml b/.idea/runConfigurations/Tests.xml deleted file mode 100644 index f5b3013..0000000 --- a/.idea/runConfigurations/Tests.xml +++ /dev/null @@ -1,23 +0,0 @@ - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/.idea/viper.iml b/.idea/viper.iml deleted file mode 100644 index 5e764c4..0000000 --- a/.idea/viper.iml +++ /dev/null @@ -1,9 +0,0 @@ - - - - - - - - - \ No newline at end of file From 502400c0d97d71d9c21ad9171ac0a8b0a73126aa Mon Sep 17 00:00:00 2001 From: Alexey Maslov Date: Thu, 16 Jan 2020 23:49:29 +0800 Subject: [PATCH 65/68] changed github.com/xordataexchange/crypt to github.com/bketelsen/crypt. --- README.md | 4 ++-- go.mod | 3 ++- go.sum | 2 ++ remote/remote.go | 2 +- viper.go | 2 +- 5 files changed, 8 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index a574677..8489098 100644 --- a/README.md +++ b/README.md @@ -400,7 +400,7 @@ in a Key/Value store such as etcd or Consul. These values take precedence over default values, but are overridden by configuration values retrieved from disk, flags, or environment variables. -Viper uses [crypt](https://github.com/xordataexchange/crypt) to retrieve +Viper uses [crypt](https://github.com/bketelsen/crypt) to retrieve configuration from the K/V store, which means that you can store your configuration values encrypted and have them automatically decrypted if you have the correct gpg keyring. Encryption is optional. @@ -412,7 +412,7 @@ independently of it. K/V store. `crypt` defaults to etcd on http://127.0.0.1:4001. ```bash -$ go get github.com/xordataexchange/crypt/bin/crypt +$ go get github.com/bketelsen/crypt/bin/crypt $ crypt set -plaintext /config/hugo.json /Users/hugo/settings/config.json ``` diff --git a/go.mod b/go.mod index 0e358cb..3e6543c 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.12 require ( github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6 // indirect + github.com/bketelsen/crypt v0.0.2 github.com/coreos/bbolt v1.3.2 // indirect github.com/coreos/etcd v3.3.10+incompatible // indirect github.com/coreos/go-semver v0.2.0 // indirect @@ -35,7 +36,7 @@ require ( github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5 // indirect github.com/ugorji/go v1.1.4 // indirect github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2 // indirect - github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77 + github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77 // indirect go.etcd.io/bbolt v1.3.2 // indirect go.uber.org/atomic v1.4.0 // indirect go.uber.org/multierr v1.1.0 // indirect diff --git a/go.sum b/go.sum index d75aee2..b692ce1 100644 --- a/go.sum +++ b/go.sum @@ -9,6 +9,8 @@ github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5 github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q= github.com/beorn7/perks v1.0.0 h1:HWo1m869IqiPhD389kmkxeTalrjNbbJTC8LXupb+sl0= github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8= +github.com/bketelsen/crypt v0.0.2 h1:DLaR79EdTGtaM+6I9L8pk+UWJJqLZZozG0BsjB6VCgU= +github.com/bketelsen/crypt v0.0.2/go.mod h1:QoWTRmAOKpT3wAaYuPKutmL1eJqFHPUTt8EzaKE8zLc= github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc= github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= github.com/coreos/bbolt v1.3.2 h1:wZwiHHUieZCquLkDL0B8UhzreNWsPHooDAG3q34zk0s= diff --git a/remote/remote.go b/remote/remote.go index 6e8a534..3956fac 100644 --- a/remote/remote.go +++ b/remote/remote.go @@ -13,7 +13,7 @@ import ( "github.com/spf13/viper" - crypt "github.com/xordataexchange/crypt/config" + crypt "github.com/bketelsen/crypt/config" ) type remoteConfigProvider struct{} diff --git a/viper.go b/viper.go index a8f84b5..60b8773 100644 --- a/viper.go +++ b/viper.go @@ -513,7 +513,7 @@ func (v *Viper) AddRemoteProvider(provider, endpoint, path string) error { // To retrieve a config file called myapp.json from /configs/myapp.json // you should set path to /configs and set config name (SetConfigName()) to // "myapp" -// Secure Remote Providers are implemented with github.com/xordataexchange/crypt +// Secure Remote Providers are implemented with github.com/bketelsen/crypt func AddSecureRemoteProvider(provider, endpoint, path, secretkeyring string) error { return v.AddSecureRemoteProvider(provider, endpoint, path, secretkeyring) } From 9c81997cb1908f9597ae77f71d3eaf33d1e14483 Mon Sep 17 00:00:00 2001 From: Alexey Maslov Date: Fri, 17 Jan 2020 00:20:46 +0800 Subject: [PATCH 66/68] added firestore support. --- go.mod | 13 +-- go.sum | 226 +++++++++++++++++++++++++++++++++++++++++++---- remote/remote.go | 14 ++- viper.go | 8 +- 4 files changed, 226 insertions(+), 35 deletions(-) diff --git a/go.mod b/go.mod index 3e6543c..b7fbd36 100644 --- a/go.mod +++ b/go.mod @@ -3,18 +3,14 @@ module github.com/spf13/viper go 1.12 require ( - github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6 // indirect - github.com/bketelsen/crypt v0.0.2 + github.com/bketelsen/crypt v0.0.3-0.20200106085610-5cbc8cc4026c github.com/coreos/bbolt v1.3.2 // indirect - github.com/coreos/etcd v3.3.10+incompatible // indirect - github.com/coreos/go-semver v0.2.0 // indirect github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e // indirect github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f // indirect github.com/dgrijalva/jwt-go v3.2.0+incompatible // indirect github.com/fsnotify/fsnotify v1.4.7 github.com/gogo/protobuf v1.2.1 // indirect github.com/golang/groupcache v0.0.0-20190129154638-5b532d6fd5ef // indirect - github.com/google/btree v1.0.0 // indirect github.com/gorilla/websocket v1.4.0 // indirect github.com/grpc-ecosystem/go-grpc-middleware v1.0.0 // indirect github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 // indirect @@ -31,19 +27,14 @@ require ( github.com/spf13/cast v1.3.0 github.com/spf13/jwalterweatherman v1.0.0 github.com/spf13/pflag v1.0.3 - github.com/stretchr/testify v1.2.2 + github.com/stretchr/testify v1.3.0 github.com/subosito/gotenv v1.2.0 github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5 // indirect - github.com/ugorji/go v1.1.4 // indirect github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2 // indirect - github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77 // indirect go.etcd.io/bbolt v1.3.2 // indirect go.uber.org/atomic v1.4.0 // indirect go.uber.org/multierr v1.1.0 // indirect go.uber.org/zap v1.10.0 // indirect - golang.org/x/net v0.0.0-20190522155817-f3200d17e092 // indirect - golang.org/x/time v0.0.0-20190308202827-9d24e82272b4 // indirect - google.golang.org/grpc v1.21.0 // indirect gopkg.in/ini.v1 v1.51.0 gopkg.in/yaml.v2 v2.2.4 ) diff --git a/go.sum b/go.sum index b692ce1..f490ae9 100644 --- a/go.sum +++ b/go.sum @@ -1,37 +1,62 @@ cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= +cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= +cloud.google.com/go v0.38.0/go.mod h1:990N+gfupTy94rShfmMCWGDn0LpTmnzTp2qbd1dvSRU= +cloud.google.com/go v0.44.1/go.mod h1:iSa0KzasP4Uvy3f1mN/7PiObzGgflwredwwASm/v6AU= +cloud.google.com/go v0.44.2/go.mod h1:60680Gw3Yr4ikxnPRS/oxxkBccT6SA1yMk63TGekxKY= +cloud.google.com/go v0.45.1/go.mod h1:RpBamKRgapWJb87xiFSdk4g1CME7QZg3uwTez+TSTjc= +cloud.google.com/go v0.46.3 h1:AVXDdKsrtX33oR9fbCMu/+c1o8Ofjq6Ku/MInaLVg5Y= +cloud.google.com/go v0.46.3/go.mod h1:a6bKKbmY7er1mI7TEI4lsAkts/mkhTSZK8w33B4RAg0= +cloud.google.com/go/bigquery v1.0.1 h1:hL+ycaJpVE9M7nLoiXb/Pn10ENE2u+oddxbD8uu0ZVU= +cloud.google.com/go/bigquery v1.0.1/go.mod h1:i/xbL2UlR5RvWAURpBYZTtm/cXjCha9lbfbpx4poX+o= +cloud.google.com/go/datastore v1.0.0 h1:Kt+gOPPp2LEPWp8CSfxhsM8ik9CcyE/gYu+0r+RnZvM= +cloud.google.com/go/datastore v1.0.0/go.mod h1:LXYbyblFSglQ5pkeyhO+Qmw7ukd3C+pD7TKLgZqpHYE= +cloud.google.com/go/firestore v1.1.0 h1:9x7Bx0A9R5/M9jibeJeZWqjeVEIxYW9fZYqB9a70/bY= +cloud.google.com/go/firestore v1.1.0/go.mod h1:ulACoGHTpvq5r8rxGJ4ddJZBZqakUQqClKRT5SZwBmk= +cloud.google.com/go/pubsub v1.0.1 h1:W9tAK3E57P75u0XLLR82LZyw8VpAnhmyTOxW9qzmyj8= +cloud.google.com/go/pubsub v1.0.1/go.mod h1:R0Gpsv3s54REJCy4fxDixWD93lHJMoZTyQ2kNxGRt3I= +cloud.google.com/go/storage v1.0.0 h1:VV2nUM3wwLLGh9lSABFgZMjInyUbJeaRSE64WuAIQ+4= +cloud.google.com/go/storage v1.0.0/go.mod h1:IhtSnM/ZTZV8YYJWCY8RULGVqBDmpoyjwiyrjsg+URw= +dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU= github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= +github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo= github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU= github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= -github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6 h1:G1bPvciwNyF7IUmKXNt9Ak3m6u9DE1rF+RmtIkBpVdA= -github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8= +github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e/go.mod h1:3U/XgcO3hCbHZ8TKRvWD2dDTCfh9M9ya+I9JpbB7O8o= +github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da h1:8GUt8eRujhVEGZFFEjBj46YV4rDjvGrNxb0KMWYkL2I= +github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da/go.mod h1:Q73ZrmVTwzkszR9V5SSuryQ31EELlFMUz1kKyl939pY= +github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8= github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q= github.com/beorn7/perks v1.0.0 h1:HWo1m869IqiPhD389kmkxeTalrjNbbJTC8LXupb+sl0= github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8= -github.com/bketelsen/crypt v0.0.2 h1:DLaR79EdTGtaM+6I9L8pk+UWJJqLZZozG0BsjB6VCgU= -github.com/bketelsen/crypt v0.0.2/go.mod h1:QoWTRmAOKpT3wAaYuPKutmL1eJqFHPUTt8EzaKE8zLc= +github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs= +github.com/bketelsen/crypt v0.0.3-0.20200106085610-5cbc8cc4026c h1:+0HFd5KSZ/mm3JmhmrDukiId5iR6w4+BdFtfSy4yWIc= +github.com/bketelsen/crypt v0.0.3-0.20200106085610-5cbc8cc4026c/go.mod h1:MKsuJmJgSg28kpZDP6UIiPt0e0Oz0kqKNGyRaWEPv84= github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc= github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= github.com/coreos/bbolt v1.3.2 h1:wZwiHHUieZCquLkDL0B8UhzreNWsPHooDAG3q34zk0s= github.com/coreos/bbolt v1.3.2/go.mod h1:iRUV2dpdMOn7Bo10OQBFzIJO9kkE559Wcmn+qkEiiKk= -github.com/coreos/etcd v3.3.10+incompatible h1:jFneRYjIvLMLhDLCzuTuU4rSJUjRplcJQ7pD7MnhC04= -github.com/coreos/etcd v3.3.10+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE= -github.com/coreos/go-semver v0.2.0 h1:3Jm3tLmsgAYcjC+4Up7hJrFBPr+n7rAqYeSw/SZazuY= -github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk= +github.com/coreos/etcd v3.3.13+incompatible h1:8F3hqu9fGYLBifCmRCJsicFqDx/D68Rt3q1JMazcgBQ= +github.com/coreos/etcd v3.3.13+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE= +github.com/coreos/go-semver v0.3.0 h1:wkHLiw0WNATZnSG7epLsujiMCgPAc9xhjJ4tgnAxmfM= +github.com/coreos/go-semver v0.3.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk= github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e h1:Wf6HqHfScWJN9/ZjdUKyjop4mf3Qdd+1TvvltAvM3m8= github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4= github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f h1:lBNOc5arjvs8E5mO2tbpBpLoyyu8B6e44T7hJy6potg= github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM= github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no= +github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4= github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= github.com/ghodss/yaml v1.0.0 h1:wQHKEahhL6wmXdzwWG11gIVCkOv05bNOh+Rxn0yngAk= github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= +github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU= github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as= github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE= github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk= @@ -44,12 +69,27 @@ github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfU github.com/golang/groupcache v0.0.0-20190129154638-5b532d6fd5ef h1:veQD95Isof8w9/WXiA+pa3tz3fJXkt5B7QaRBrM62gk= github.com/golang/groupcache v0.0.0-20190129154638-5b532d6fd5ef/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A= +github.com/golang/mock v1.2.0/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A= +github.com/golang/mock v1.3.1/go.mod h1:sBzyDLLjw3U8JLTeZvSv8jJB+tU5PVekmnlKIyFUx0Y= github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.3.1 h1:YF8+flBXS5eO826T4nzqPrxfhQThhXl0YzfuUPu4SBg= github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/protobuf v1.3.2 h1:6nsPYzhq5kReh6QImI3k5qWzO4PEbvbIW2cwSfR/6xs= +github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ= github.com/google/btree v1.0.0 h1:0udJVsspx3VBr5FwtLhQQtuAsVc79tTq0ocGIPAU6qo= github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ= github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= +github.com/google/go-cmp v0.3.0 h1:crn/baboCvb5fXaQ0IJ1SGTsTVrWpDsCWC8EGETZijY= +github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= +github.com/google/martian v2.1.0+incompatible h1:/CP5g8u/VJHijgedC/Legn3BAbAaWPgecwXBIDzw5no= +github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs= +github.com/google/pprof v0.0.0-20181206194817-3ea8567a2e57/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc= +github.com/google/pprof v0.0.0-20190515194954-54271f7e092f/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc= +github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI= +github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg= +github.com/googleapis/gax-go/v2 v2.0.5 h1:sjZBwGj9Jlw33ImPtvFviGYvseOtDM7hkSKB7+Tv3SM= +github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk= github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8= github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY= github.com/gorilla/websocket v1.4.0 h1:WDFjx/TMzVgy9VdMMQi2K2Emtwi2QcUQsztZ/zLaH/Q= @@ -60,10 +100,46 @@ github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 h1:Ovs26xHkKqVztRpIrF/92Bcuy github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk= github.com/grpc-ecosystem/grpc-gateway v1.9.0 h1:bM6ZAFZmc/wPFaRDi0d5L7hGEZEx/2u+Tmr2evNHDiI= github.com/grpc-ecosystem/grpc-gateway v1.9.0/go.mod h1:vNeuVxBJEsws4ogUvrchl83t/GYV9WGTSLVdBhOQFDY= +github.com/hashicorp/consul/api v1.1.0 h1:BNQPM9ytxj6jbjjdRPioQ94T6YXriSopn0i8COv6SRA= +github.com/hashicorp/consul/api v1.1.0/go.mod h1:VmuI/Lkw1nC05EYQWNKwWGbkg+FbDBtguAZLlVdkD9Q= +github.com/hashicorp/consul/sdk v0.1.1 h1:LnuDWGNsoajlhGyHJvuWW6FVqRl8JOTPqS6CPTsYjhY= +github.com/hashicorp/consul/sdk v0.1.1/go.mod h1:VKf9jXwCTEY1QZP2MOLRhb5i/I/ssyNV1vwHyQBF0x8= +github.com/hashicorp/errwrap v1.0.0 h1:hLrqtEDnRye3+sgx6z4qVLNuviH3MR5aQ0ykNJa/UYA= +github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4= +github.com/hashicorp/go-cleanhttp v0.5.1 h1:dH3aiDG9Jvb5r5+bYHsikaOUIpcM0xvgMXVoDkXMzJM= +github.com/hashicorp/go-cleanhttp v0.5.1/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80= +github.com/hashicorp/go-immutable-radix v1.0.0 h1:AKDB1HM5PWEA7i4nhcpwOrO2byshxBjXVn/J/3+z5/0= +github.com/hashicorp/go-immutable-radix v1.0.0/go.mod h1:0y9vanUI8NX6FsYoO3zeMjhV/C5i9g4Q3DwcSNZ4P60= +github.com/hashicorp/go-msgpack v0.5.3 h1:zKjpN5BK/P5lMYrLmBHdBULWbJ0XpYR+7NGzqkZzoD4= +github.com/hashicorp/go-msgpack v0.5.3/go.mod h1:ahLV/dePpqEmjfWmKiqvPkv/twdG7iPBM1vqhUKIvfM= +github.com/hashicorp/go-multierror v1.0.0 h1:iVjPR7a6H0tWELX5NxNe7bYopibicUzc7uPribsnS6o= +github.com/hashicorp/go-multierror v1.0.0/go.mod h1:dHtQlpGsu+cZNNAkkCN/P3hoUDHhCYQXV3UM06sGGrk= +github.com/hashicorp/go-rootcerts v1.0.0 h1:Rqb66Oo1X/eSV1x66xbDccZjhJigjg0+e82kpwzSwCI= +github.com/hashicorp/go-rootcerts v1.0.0/go.mod h1:K6zTfqpRlCUIjkwsN4Z+hiSfzSTQa6eBIzfwKfwNnHU= +github.com/hashicorp/go-sockaddr v1.0.0 h1:GeH6tui99pF4NJgfnhp+L6+FfobzVW3Ah46sLo0ICXs= +github.com/hashicorp/go-sockaddr v1.0.0/go.mod h1:7Xibr9yA9JjQq1JpNB2Vw7kxv8xerXegt+ozgdvDeDU= +github.com/hashicorp/go-syslog v1.0.0/go.mod h1:qPfqrKkXGihmCqbJM2mZgkZGvKG1dFdvsLplgctolz4= +github.com/hashicorp/go-uuid v1.0.0/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro= +github.com/hashicorp/go-uuid v1.0.1 h1:fv1ep09latC32wFoVwnqcnKJGnMSdBanPczbHAYm1BE= +github.com/hashicorp/go-uuid v1.0.1/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro= +github.com/hashicorp/go.net v0.0.1/go.mod h1:hjKkEWcCURg++eb33jQU7oqQcI9XDCnUzHA0oac0k90= +github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= +github.com/hashicorp/golang-lru v0.5.1 h1:0hERBMJE1eitiLkihrMvRVBYAkpHzc/J3QdDN+dAcgU= +github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4= github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= +github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO+LraFDTW64= +github.com/hashicorp/mdns v1.0.0/go.mod h1:tL+uN++7HEJ6SQLQ2/p+z2pH24WQKWjBPkE0mNTz8vQ= +github.com/hashicorp/memberlist v0.1.3 h1:EmmoJme1matNzb+hMpDuR/0sbJSUisxyqBGG676r31M= +github.com/hashicorp/memberlist v0.1.3/go.mod h1:ajVTdAv/9Im8oMAAj5G31PhhMCZJV2pPBoIllUwCN7I= +github.com/hashicorp/serf v0.8.2 h1:YZ7UKsJv+hKjqGVUUbtE3HNj79Eln2oQ75tniF6iPt0= +github.com/hashicorp/serf v0.8.2/go.mod h1:6hOLApaqBFA1NXqRQAsxw9QxuDEvNxSQRwA/JwenrHc= github.com/jonboulle/clockwork v0.1.0 h1:VKV+ZcuP6l3yW9doeqz6ziZGgcynBVQO+obU0+0hcPo= github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo= +github.com/json-iterator/go v1.1.6 h1:MrUvLMLTMxbqFJ9kzlvat/rYZqZnW3u4wkLzWTaFwKs= +github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU= +github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024 h1:rBMNdlhTLzJjJSDIjNEXX1Pz3Hmwmz91v+zycvx9PJc= +github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU= github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo= github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU= github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w= @@ -79,18 +155,39 @@ github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/magiconair/properties v1.8.1 h1:ZC2Vc7/ZFkGmsVC9KvOjumD+G5lXy2RtTKyzRKO2BQ4= github.com/magiconair/properties v1.8.1/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ= +github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU= +github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4= github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU= github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= +github.com/miekg/dns v1.0.14 h1:9jZdLNd/P4+SfEJ0TNyxYpsK8N4GtfylBLqtbYN1sbA= +github.com/miekg/dns v1.0.14/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg= +github.com/mitchellh/cli v1.0.0/go.mod h1:hNIlj7HEI86fIcpObd7a0FcrxTWetlwJDGcceTlRvqc= +github.com/mitchellh/go-homedir v1.0.0 h1:vKb8ShqSby24Yrqr/yDYkuFz8d0WUjys40rvnGC8aR0= +github.com/mitchellh/go-homedir v1.0.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= +github.com/mitchellh/go-testing-interface v1.0.0 h1:fzU/JVNcaqHQEcVFAKeR41fkiLdIPrefOvVG1VZ96U0= +github.com/mitchellh/go-testing-interface v1.0.0/go.mod h1:kRemZodwjscx+RGhAo8eIhFbs2+BFgRtFPeD/KE+zxI= +github.com/mitchellh/gox v0.4.0/go.mod h1:Sd9lOJ0+aimLBi73mGofS1ycjY8lL3uZM3JPS42BGNg= +github.com/mitchellh/iochan v1.0.0/go.mod h1:JwYml1nuB7xOzsp52dPpHFffvOCDupsG0QubkSMEySY= +github.com/mitchellh/mapstructure v0.0.0-20160808181253-ca63d7c062ee/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= github.com/mitchellh/mapstructure v1.1.2 h1:fmNYVwqnSfB9mZU6OS2O6GsXM+wcskZDuKQzvN1EDeE= github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/reflect2 v1.0.1 h1:9f412s+6RmYXLWZSEzVVgPGK7C2PphHj5RJrvfx9AWI= +github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U= +github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c h1:Lgl0gzECD8GnQ5QCWA8o6BtfL6mDH5rQgM4/fX3avOs= +github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc= github.com/pelletier/go-toml v1.2.0 h1:T5zMGML61Wp+FlcbWjRDT7yAxhJNAiPPLOFECq181zc= github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic= github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw= github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= +github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI= github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw= github.com/prometheus/client_golang v0.9.3 h1:9iH4JKXLzFbOAdtqv/a+j8aewx2Y8lAjAydhbaScPF8= github.com/prometheus/client_golang v0.9.3/go.mod h1:/TN21ttK/J9q6uSwhBd54HahCDft0ttaMvbicHlPoso= @@ -105,6 +202,10 @@ github.com/prometheus/procfs v0.0.0-20190507164030-5867b95ac084 h1:sofwID9zm4tzr github.com/prometheus/procfs v0.0.0-20190507164030-5867b95ac084/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA= github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40TwIPHuXU= github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg= +github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= +github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts= +github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529 h1:nn5Wsu0esKSJiIVhscUtVbo7ada43DJhG55ua/hjS5I= +github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc= github.com/sirupsen/logrus v1.2.0 h1:juTguoYk5qI21pwyTXY3B3Y5cOTH3ZUyZCg1v/mihuo= github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d h1:zE9ykElWQ6/NYmHa3jpm/yHnI4xSofP+UP6SpjHcSeM= @@ -122,21 +223,23 @@ github.com/spf13/jwalterweatherman v1.0.0 h1:XHEdyB+EcvlqZamSM4ZOMGlc93t6AcsBEu9 github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb68N+wFjFa4jdeBTo= github.com/spf13/pflag v1.0.3 h1:zPAT6CGy6wXeQ7NtTnaTerfKOsV6V6F8agHXFiazDkg= github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/subosito/gotenv v1.2.0 h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s= github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw= github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5 h1:LnC5Kc/wtumK+WB441p7ynQJzVuNRJiqddSIE3IlSEQ= github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U= -github.com/ugorji/go v1.1.4 h1:j4s+tAvLfL3bZyefP2SEWmhBzmuIlH/eqNuPdFPgngw= -github.com/ugorji/go v1.1.4/go.mod h1:uQMGLiO92mf5W77hV/PUCpI3pbzQx3CRekS0kk+RGrc= github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2 h1:eY9dn8+vbi4tKz5Qo6v2eYzo7kUS51QINcR5jNpbZS8= github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU= -github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77 h1:ESFSdwYZvkeru3RtdrYueztKhOBCSAAzS4Gf+k0tEow= -github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q= go.etcd.io/bbolt v1.3.2 h1:Z/90sZLPOeCy2PwprqkFa25PdkusRzaj9P8zm/KNyvk= go.etcd.io/bbolt v1.3.2/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU= +go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU= +go.opencensus.io v0.22.0 h1:C9hSCOW830chIVkdja34wa6Ky+IzWllkUinR+BtRZd4= +go.opencensus.io v0.22.0/go.mod h1:+kGneAE2xo2IficOXnaByMWTGM9T73dGwxeWcUqIpI8= go.uber.org/atomic v1.4.0 h1:cxzIVoETapQEqDhQu3QfnvXAV4AlzcvUCxkVUFw3+EU= go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE= go.uber.org/multierr v1.1.0 h1:HoEmRHQPVSqub6w2z2d2EOVs2fjyFRGyofhKuyDq0QI= @@ -144,46 +247,132 @@ go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/ go.uber.org/zap v1.10.0 h1:ORx85nbTijNz8ljznvCMR1ZBIPKFn3jQrag10X2AsuM= go.uber.org/zap v1.10.0/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q= golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= +golang.org/x/crypto v0.0.0-20181029021203-45a5f77698d3/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2 h1:VklqNMn3ovrHsnt90PveolxSbWFaJdECFbxSq0Mqo2M= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5 h1:58fnuSXlxZmFdJyvtTFVmVhcMLU6v5fEb/ok4wyqtNU= +golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= +golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= +golang.org/x/exp v0.0.0-20190510132918-efd6b22b2522/go.mod h1:ZjyILWgesfNpC6sMxTJOJm9Kp84zZh5NQWvqDGG3Qr8= +golang.org/x/exp v0.0.0-20190829153037-c13cbed26979/go.mod h1:86+5VVa7VpoJ4kLfm080zCjGlMRFzhUhsZKEZO7MGek= +golang.org/x/exp v0.0.0-20191030013958-a1ab85dbe136 h1:A1gGSx58LAGVHUUsOf7IiR0u8Xb6W51gRwfDBhkdcaw= +golang.org/x/exp v0.0.0-20191030013958-a1ab85dbe136/go.mod h1:JXzH8nQsPlswgeRAPE3MuO9GYsAcnJvJ4vnMwN/5qkY= +golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js= +golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0= golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= +golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU= +golang.org/x/lint v0.0.0-20190301231843-5614ed5bae6f/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= +golang.org/x/lint v0.0.0-20190409202823-959b441ac422/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= +golang.org/x/lint v0.0.0-20190909230951-414d861bb4ac/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= +golang.org/x/lint v0.0.0-20190930215403-16217165b5de h1:5hukYrvBGR8/eNkX5mdUezrA6JiaEZDtJb9Ei+1LlBs= +golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= +golang.org/x/mobile v0.0.0-20190312151609-d3739f865fa6/go.mod h1:z+o9i4GpDbdi3rU15maQ/Ox0txvL9dWGYEHz965HBQE= +golang.org/x/mobile v0.0.0-20190719004257-d2bd2a29d028/go.mod h1:E/iHnbuqvinMTCcRqshq8CkpyQDoeVncDDYHnLhea+o= +golang.org/x/mod v0.0.0-20190513183733-4bf6d317e70e/go.mod h1:mXi4GBBbnImb6dmsKGUJ2LatrhH/nqhxcFungHvyanc= +golang.org/x/mod v0.1.0/go.mod h1:0QHyrYULN0/3qlju5TqG8bIK38QM8yzMo5ekMj3DlcY= +golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20181023162649-9b4f9f5ad519/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20181201002055-351d144fa1fc/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20181220203305-927f97764cc3/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= -golang.org/x/net v0.0.0-20190522155817-f3200d17e092 h1:4QSRKanuywn15aTZvI/mIDEgPQpswuFndXpOj3rKEco= -golang.org/x/net v0.0.0-20190522155817-f3200d17e092/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190501004415-9ce7a6920f09/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190503192946-f4e77d36d62c/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859 h1:R/3boaszxrf1GEUWTVDzSKVwLmSJpwZ1yqXm8j0v2QI= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= +golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= +golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45 h1:SVwTIAaPC2U/AvvLNZ2a7OVsmBpC8L5BlwK1whH3hm0= +golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20190423024810-112230192c58 h1:8gQV6CLnAEikrhgkHFbMAEhagSSnXWGV915qUMm9mrU= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20180823144017-11551d06cbcc/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20181026203630-95b1ffbd15a5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20181107165924-66b7b1311ac8/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a h1:1BGLXjeY4akVXGgbC9HugT3Jv3hCI0z56oJR5vAMgBU= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190312061237-fead79001313/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190502145724-3ef323f4f1fd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190507160741-ecd444e8653b/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190606165138-5da285871e9c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190624142023-c5567b49c5d0 h1:HyfiK1WMnHj5FXFXatD+Qs1A/xC2Run6RzeW1SyHxpc= +golang.org/x/sys v0.0.0-20190624142023-c5567b49c5d0/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs= +golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= +golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.0.0-20190308202827-9d24e82272b4 h1:SvFZT6jyqRaOeXpc5h/JSfZenJ2O330aBsf7JfSUXmQ= golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/tools v0.0.0-20180221164845-07fd8470d635/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY= golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= +golang.org/x/tools v0.0.0-20190312151545-0bb0c0a6e846/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= +golang.org/x/tools v0.0.0-20190312170243-e65039ee4138/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= golang.org/x/tools v0.0.0-20190328211700-ab21143f2384 h1:TFlARGu6Czu1z7q93HTxcP1P+/ZFC/IKythI5RzrnRg= golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= +golang.org/x/tools v0.0.0-20190425150028-36563e24a262/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= +golang.org/x/tools v0.0.0-20190506145303-2d16b83fe98c/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= +golang.org/x/tools v0.0.0-20190606124116-d0a3d012864b/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc= +golang.org/x/tools v0.0.0-20190621195816-6e04913cbbac/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc= +golang.org/x/tools v0.0.0-20190628153133-6cdbf07be9d0/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc= +golang.org/x/tools v0.0.0-20190816200558-6889da9d5479/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.0.0-20190911174233-4f2ddba30aff/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.0.0-20191012152004-8de300cfc20a/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.0.0-20191112195655-aa38f8e97acc h1:NCy3Ohtk6Iny5V/reW2Ktypo4zIpWBdRJ1uFMjBxdg8= +golang.org/x/tools v0.0.0-20191112195655-aa38f8e97acc/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +google.golang.org/api v0.4.0/go.mod h1:8k5glujaEP+g9n7WNsDg8QP6cUVNI86fCNMcbazEtwE= +google.golang.org/api v0.7.0/go.mod h1:WtwebWUNSVBH/HAw79HIFXZNqEvBhG+Ra+ax0hx3E3M= +google.golang.org/api v0.8.0/go.mod h1:o4eAsZoiT+ibD93RtjEohWalFOjRDx6CVaqeizhEnKg= +google.golang.org/api v0.9.0/go.mod h1:o4eAsZoiT+ibD93RtjEohWalFOjRDx6CVaqeizhEnKg= +google.golang.org/api v0.13.0 h1:Q3Ui3V3/CVinFWFiW39Iw0kMuVrRzYX0wN6OPFp0lTA= +google.golang.org/api v0.13.0/go.mod h1:iLdEw5Ide6rF15KTC1Kkl0iskquN2gFfn9o9XIsbkAI= google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= +google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= +google.golang.org/appengine v1.5.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= +google.golang.org/appengine v1.6.1 h1:QzqyMA1tlu6CgqCDUtU9V+ZKhLFT2dkJuANu5QaxI3I= +google.golang.org/appengine v1.6.1/go.mod h1:i06prIuMbXzDqacNJfV5OdTW448YApPu5ww/cMBSeb0= google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8 h1:Nw54tB0rB7hY/N0NQvRW8DG4Yk3Q6T9cu9RcFQDu1tc= google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc= +google.golang.org/genproto v0.0.0-20190307195333-5fe7a883aa19/go.mod h1:VzzqZJRnGkLBvHegQrXjBqPurQTc5/KpmUdxsrq26oE= +google.golang.org/genproto v0.0.0-20190418145605-e7d98fc518a7/go.mod h1:VzzqZJRnGkLBvHegQrXjBqPurQTc5/KpmUdxsrq26oE= +google.golang.org/genproto v0.0.0-20190425155659-357c62f0e4bb/go.mod h1:VzzqZJRnGkLBvHegQrXjBqPurQTc5/KpmUdxsrq26oE= +google.golang.org/genproto v0.0.0-20190502173448-54afdca5d873/go.mod h1:VzzqZJRnGkLBvHegQrXjBqPurQTc5/KpmUdxsrq26oE= +google.golang.org/genproto v0.0.0-20190801165951-fa694d86fc64/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc= +google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc= +google.golang.org/genproto v0.0.0-20190911173649-1774047e7e51/go.mod h1:IbNlFCBrqXvoKpeg0TB2l7cyZUmoaFKYIwrEpbDKLA8= +google.golang.org/genproto v0.0.0-20191108220845-16a3f7862a1a h1:Ob5/580gVHBJZgXnff1cZDbG+xLtMVE5mDRTe+nIsX4= +google.golang.org/genproto v0.0.0-20191108220845-16a3f7862a1a/go.mod h1:n3cpQtvxv34hfy77yVDNjmbRyujviMdxYliBSkLhpCc= google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c= -google.golang.org/grpc v1.21.0 h1:G+97AoqBnmZIT91cLG/EkCoK9NSelj64P8bOHHNmGn0= -google.golang.org/grpc v1.21.0/go.mod h1:oYelfM1adQP15Ek0mdvEgi9Df8B9CZIaU1084ijfRaM= +google.golang.org/grpc v1.20.1/go.mod h1:10oTOabMzJvdu6/UiuZezV6QK5dSlG84ov/aaiqXj38= +google.golang.org/grpc v1.21.1 h1:j6XxA85m/6txkUCHvzlV5f+HBNl/1r5cZ2A/3IEFOO8= +google.golang.org/grpc v1.21.1/go.mod h1:oYelfM1adQP15Ek0mdvEgi9Df8B9CZIaU1084ijfRaM= gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= gopkg.in/ini.v1 v1.51.0 h1:AQvPpx3LzTDM0AjnIRlVFwFFGC+npRopjZxLJj6gdno= gopkg.in/ini.v1 v1.51.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= gopkg.in/resty.v1 v1.12.0/go.mod h1:mDo4pnntr5jdWRML875a/NmxYqAlA73dVijT2AXvQQo= @@ -192,3 +381,8 @@ gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.4 h1:/eiJrUcujPVeJ3xlSWaiNi3uSVmDGBK1pDHUHAnao1I= gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= +honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= +honnef.co/go/tools v0.0.0-20190418001031-e561f6794a2a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= +honnef.co/go/tools v0.0.1-2019.2.3 h1:3JgtbtFHMiCmsznwGVTUWbgGov+pVqnlf1dEJTNAXeM= +honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg= +rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8= diff --git a/remote/remote.go b/remote/remote.go index 3956fac..6bdaa9a 100644 --- a/remote/remote.go +++ b/remote/remote.go @@ -82,15 +82,21 @@ func getConfigManager(rp viper.RemoteProvider) (crypt.ConfigManager, error) { return nil, err } defer kr.Close() - if rp.Provider() == "etcd" { + switch rp.Provider() { + case "etcd": cm, err = crypt.NewEtcdConfigManager([]string{rp.Endpoint()}, kr) - } else { + case "firestore": + cm, err = crypt.NewFirestoreConfigManager([]string{rp.Endpoint()}, kr) + default: cm, err = crypt.NewConsulConfigManager([]string{rp.Endpoint()}, kr) } } else { - if rp.Provider() == "etcd" { + switch rp.Provider() { + case "etcd": cm, err = crypt.NewStandardEtcdConfigManager([]string{rp.Endpoint()}) - } else { + case "firestore": + cm, err = crypt.NewStandardFirestoreConfigManager([]string{rp.Endpoint()}) + default: cm, err = crypt.NewStandardConsulConfigManager([]string{rp.Endpoint()}) } } diff --git a/viper.go b/viper.go index 60b8773..ba5e7e6 100644 --- a/viper.go +++ b/viper.go @@ -287,7 +287,7 @@ func NewWithOptions(opts ...Option) *Viper { func Reset() { v = New() SupportedExts = []string{"json", "toml", "yaml", "yml", "properties", "props", "prop", "hcl", "dotenv", "env", "ini"} - SupportedRemoteProviders = []string{"etcd", "consul"} + SupportedRemoteProviders = []string{"etcd", "consul", "firestore"} } type defaultRemoteProvider struct { @@ -328,7 +328,7 @@ type RemoteProvider interface { var SupportedExts = []string{"json", "toml", "yaml", "yml", "properties", "props", "prop", "hcl", "dotenv", "env", "ini"} // SupportedRemoteProviders are universally supported remote providers. -var SupportedRemoteProviders = []string{"etcd", "consul"} +var SupportedRemoteProviders = []string{"etcd", "consul", "firestore"} func OnConfigChange(run func(in fsnotify.Event)) { v.OnConfigChange(run) } func (v *Viper) OnConfigChange(run func(in fsnotify.Event)) { @@ -477,7 +477,7 @@ func (v *Viper) AddConfigPath(in string) { // AddRemoteProvider adds a remote configuration source. // Remote Providers are searched in the order they are added. -// provider is a string value, "etcd" or "consul" are currently supported. +// provider is a string value: "etcd", "consul" or "firestore" are currently supported. // endpoint is the url. etcd requires http://ip:port consul requires ip:port // path is the path in the k/v store to retrieve configuration // To retrieve a config file called myapp.json from /configs/myapp.json @@ -506,7 +506,7 @@ func (v *Viper) AddRemoteProvider(provider, endpoint, path string) error { // AddSecureRemoteProvider adds a remote configuration source. // Secure Remote Providers are searched in the order they are added. -// provider is a string value, "etcd" or "consul" are currently supported. +// provider is a string value: "etcd", "consul" or "firestore" are currently supported. // endpoint is the url. etcd requires http://ip:port consul requires ip:port // secretkeyring is the filepath to your openpgp secret keyring. e.g. /etc/secrets/myring.gpg // path is the path in the k/v store to retrieve configuration From b31a49291e7b00e5ba5db5d08c232755a458d1e1 Mon Sep 17 00:00:00 2001 From: Alexey Maslov Date: Fri, 31 Jan 2020 23:46:40 +0800 Subject: [PATCH 67/68] updated docs in README. --- README.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/README.md b/README.md index 8489098..45e72f2 100644 --- a/README.md +++ b/README.md @@ -454,6 +454,16 @@ fmt.Println(viper.Get("port")) // 8080 fmt.Println(viper.Get("hostname")) // myhostname.com ``` +#### Firestore + +```go +viper.AddRemoteProvider("firestore", "google-cloud-project-id", "collection/document") +viper.SetConfigType("json") // Config's format: "json", "toml", "yaml", "yml" +err := viper.ReadRemoteConfig() +``` + +Of course, you're allowed to use `SecureRemoteProvider` also + ### Remote Key/Value Store Example - Encrypted ```go From 97ee7adfef4882d78c0ef26e22a0c8a7a8bf6776 Mon Sep 17 00:00:00 2001 From: Gustavo Bazan Date: Wed, 19 Feb 2020 23:41:04 +0000 Subject: [PATCH 68/68] Add support to save file with no extension (#813) * Add support to save file with no extension The support introduced for files with no file extension is only partial as trying to save the config file would fail with ` requires valid extension` This adds support to saving such files --- viper.go | 13 +- viper_test.go | 370 +++++++++++++++++++++++++++++++------------------- 2 files changed, 237 insertions(+), 146 deletions(-) diff --git a/viper.go b/viper.go index ba5e7e6..7b12b36 100644 --- a/viper.go +++ b/viper.go @@ -1413,11 +1413,18 @@ func (v *Viper) SafeWriteConfigAs(filename string) error { func (v *Viper) writeConfig(filename string, force bool) error { jww.INFO.Println("Attempting to write configuration to file.") + var configType string + ext := filepath.Ext(filename) - if len(ext) <= 1 { - return fmt.Errorf("filename: %s requires valid extension", filename) + if ext != "" { + configType = ext[1:] + } else { + configType = v.configType } - configType := ext[1:] + if configType == "" { + return fmt.Errorf("config type could not be determined for %s", filename) + } + if !stringInSlice(configType, SupportedExts) { return UnsupportedConfigError(configType) } diff --git a/viper_test.go b/viper_test.go index f9dc34a..b8ceccb 100644 --- a/viper_test.go +++ b/viper_test.go @@ -1279,26 +1279,6 @@ var hclWriteExpected = []byte(`"foos" = { "type" = "donut"`) -func TestWriteConfigHCL(t *testing.T) { - v := New() - fs := afero.NewMemMapFs() - v.SetFs(fs) - v.SetConfigName("c") - v.SetConfigType("hcl") - err := v.ReadConfig(bytes.NewBuffer(hclExample)) - if err != nil { - t.Fatal(err) - } - if err := v.WriteConfigAs("c.hcl"); err != nil { - t.Fatal(err) - } - read, err := afero.ReadFile(fs, "c.hcl") - if err != nil { - t.Fatal(err) - } - assert.Equal(t, hclWriteExpected, read) -} - var jsonWriteExpected = []byte(`{ "batters": { "batter": [ @@ -1322,26 +1302,6 @@ var jsonWriteExpected = []byte(`{ "type": "donut" }`) -func TestWriteConfigJson(t *testing.T) { - v := New() - fs := afero.NewMemMapFs() - v.SetFs(fs) - v.SetConfigName("c") - v.SetConfigType("json") - err := v.ReadConfig(bytes.NewBuffer(jsonExample)) - if err != nil { - t.Fatal(err) - } - if err := v.WriteConfigAs("c.json"); err != nil { - t.Fatal(err) - } - read, err := afero.ReadFile(fs, "c.json") - if err != nil { - t.Fatal(err) - } - assert.Equal(t, jsonWriteExpected, read) -} - var propertiesWriteExpected = []byte(`p_id = 0001 p_type = donut p_name = Cake @@ -1349,95 +1309,6 @@ p_ppu = 0.55 p_batters.batter.type = Regular `) -func TestWriteConfigProperties(t *testing.T) { - v := New() - fs := afero.NewMemMapFs() - v.SetFs(fs) - v.SetConfigName("c") - v.SetConfigType("properties") - err := v.ReadConfig(bytes.NewBuffer(propertiesExample)) - if err != nil { - t.Fatal(err) - } - if err := v.WriteConfigAs("c.properties"); err != nil { - t.Fatal(err) - } - read, err := afero.ReadFile(fs, "c.properties") - if err != nil { - t.Fatal(err) - } - assert.Equal(t, propertiesWriteExpected, read) -} - -func TestWriteConfigTOML(t *testing.T) { - fs := afero.NewMemMapFs() - v := New() - v.SetFs(fs) - v.SetConfigName("c") - v.SetConfigType("toml") - err := v.ReadConfig(bytes.NewBuffer(tomlExample)) - if err != nil { - t.Fatal(err) - } - if err := v.WriteConfigAs("c.toml"); err != nil { - t.Fatal(err) - } - - // The TOML String method does not order the contents. - // Therefore, we must read the generated file and compare the data. - v2 := New() - v2.SetFs(fs) - v2.SetConfigName("c") - v2.SetConfigType("toml") - v2.SetConfigFile("c.toml") - err = v2.ReadInConfig() - if err != nil { - t.Fatal(err) - } - - assert.Equal(t, v.GetString("title"), v2.GetString("title")) - assert.Equal(t, v.GetString("owner.bio"), v2.GetString("owner.bio")) - assert.Equal(t, v.GetString("owner.dob"), v2.GetString("owner.dob")) - assert.Equal(t, v.GetString("owner.organization"), v2.GetString("owner.organization")) -} - -var dotenvWriteExpected = []byte(` -TITLE="DotEnv Write Example" -NAME=Oreo -KIND=Biscuit -`) - -func TestWriteConfigDotEnv(t *testing.T) { - fs := afero.NewMemMapFs() - v := New() - v.SetFs(fs) - v.SetConfigName("c") - v.SetConfigType("env") - err := v.ReadConfig(bytes.NewBuffer(dotenvWriteExpected)) - if err != nil { - t.Fatal(err) - } - if err := v.WriteConfigAs("c.env"); err != nil { - t.Fatal(err) - } - - // The TOML String method does not order the contents. - // Therefore, we must read the generated file and compare the data. - v2 := New() - v2.SetFs(fs) - v2.SetConfigName("c") - v2.SetConfigType("env") - v2.SetConfigFile("c.env") - err = v2.ReadInConfig() - if err != nil { - t.Fatal(err) - } - - assert.Equal(t, v.GetString("title"), v2.GetString("title")) - assert.Equal(t, v.GetString("type"), v2.GetString("type")) - assert.Equal(t, v.GetString("kind"), v2.GetString("kind")) -} - var yamlWriteExpected = []byte(`age: 35 beard: true clothing: @@ -1454,24 +1325,237 @@ hobbies: name: steve `) -func TestWriteConfigYAML(t *testing.T) { - v := New() +func TestWriteConfig(t *testing.T) { fs := afero.NewMemMapFs() - v.SetFs(fs) - v.SetConfigName("c") - v.SetConfigType("yaml") - err := v.ReadConfig(bytes.NewBuffer(yamlExample)) - if err != nil { - t.Fatal(err) + testCases := map[string]struct { + configName string + inConfigType string + outConfigType string + fileName string + input []byte + expectedContent []byte + }{ + "hcl with file extension": { + configName: "c", + inConfigType: "hcl", + outConfigType: "hcl", + fileName: "c.hcl", + input: hclExample, + expectedContent: hclWriteExpected, + }, + "hcl without file extension": { + configName: "c", + inConfigType: "hcl", + outConfigType: "hcl", + fileName: "c", + input: hclExample, + expectedContent: hclWriteExpected, + }, + "hcl with file extension and mismatch type": { + configName: "c", + inConfigType: "hcl", + outConfigType: "json", + fileName: "c.hcl", + input: hclExample, + expectedContent: hclWriteExpected, + }, + "json with file extension": { + configName: "c", + inConfigType: "json", + outConfigType: "json", + fileName: "c.json", + input: jsonExample, + expectedContent: jsonWriteExpected, + }, + "json without file extension": { + configName: "c", + inConfigType: "json", + outConfigType: "json", + fileName: "c", + input: jsonExample, + expectedContent: jsonWriteExpected, + }, + "json with file extension and mismatch type": { + configName: "c", + inConfigType: "json", + outConfigType: "hcl", + fileName: "c.json", + input: jsonExample, + expectedContent: jsonWriteExpected, + }, + "properties with file extension": { + configName: "c", + inConfigType: "properties", + outConfigType: "properties", + fileName: "c.properties", + input: propertiesExample, + expectedContent: propertiesWriteExpected, + }, + "properties without file extension": { + configName: "c", + inConfigType: "properties", + outConfigType: "properties", + fileName: "c", + input: propertiesExample, + expectedContent: propertiesWriteExpected, + }, + "yaml with file extension": { + configName: "c", + inConfigType: "yaml", + outConfigType: "yaml", + fileName: "c.yaml", + input: yamlExample, + expectedContent: yamlWriteExpected, + }, + "yaml without file extension": { + configName: "c", + inConfigType: "yaml", + outConfigType: "yaml", + fileName: "c", + input: yamlExample, + expectedContent: yamlWriteExpected, + }, + "yaml with file extension and mismatch type": { + configName: "c", + inConfigType: "yaml", + outConfigType: "json", + fileName: "c.yaml", + input: yamlExample, + expectedContent: yamlWriteExpected, + }, } - if err := v.WriteConfigAs("c.yaml"); err != nil { - t.Fatal(err) + for name, tc := range testCases { + t.Run(name, func(t *testing.T) { + v := New() + v.SetFs(fs) + v.SetConfigName(tc.fileName) + v.SetConfigType(tc.inConfigType) + + err := v.ReadConfig(bytes.NewBuffer(tc.input)) + if err != nil { + t.Fatal(err) + } + v.SetConfigType(tc.outConfigType) + if err := v.WriteConfigAs(tc.fileName); err != nil { + t.Fatal(err) + } + read, err := afero.ReadFile(fs, tc.fileName) + if err != nil { + t.Fatal(err) + } + assert.Equal(t, tc.expectedContent, read) + }) } - read, err := afero.ReadFile(fs, "c.yaml") - if err != nil { - t.Fatal(err) +} + +func TestWriteConfigTOML(t *testing.T) { + fs := afero.NewMemMapFs() + + testCases := map[string]struct { + configName string + configType string + fileName string + input []byte + }{ + "with file extension": { + configName: "c", + configType: "toml", + fileName: "c.toml", + input: tomlExample, + }, + "without file extension": { + configName: "c", + configType: "toml", + fileName: "c", + input: tomlExample, + }, + } + for name, tc := range testCases { + t.Run(name, func(t *testing.T) { + v := New() + v.SetFs(fs) + v.SetConfigName(tc.configName) + v.SetConfigType(tc.configType) + err := v.ReadConfig(bytes.NewBuffer(tc.input)) + if err != nil { + t.Fatal(err) + } + if err := v.WriteConfigAs(tc.fileName); err != nil { + t.Fatal(err) + } + + // The TOML String method does not order the contents. + // Therefore, we must read the generated file and compare the data. + v2 := New() + v2.SetFs(fs) + v2.SetConfigName(tc.configName) + v2.SetConfigType(tc.configType) + v2.SetConfigFile(tc.fileName) + err = v2.ReadInConfig() + if err != nil { + t.Fatal(err) + } + + assert.Equal(t, v.GetString("title"), v2.GetString("title")) + assert.Equal(t, v.GetString("owner.bio"), v2.GetString("owner.bio")) + assert.Equal(t, v.GetString("owner.dob"), v2.GetString("owner.dob")) + assert.Equal(t, v.GetString("owner.organization"), v2.GetString("owner.organization")) + }) + } +} + +func TestWriteConfigDotEnv(t *testing.T) { + fs := afero.NewMemMapFs() + testCases := map[string]struct { + configName string + configType string + fileName string + input []byte + }{ + "with file extension": { + configName: "c", + configType: "env", + fileName: "c.env", + input: dotenvExample, + }, + "without file extension": { + configName: "c", + configType: "env", + fileName: "c", + input: dotenvExample, + }, + } + for name, tc := range testCases { + t.Run(name, func(t *testing.T) { + v := New() + v.SetFs(fs) + v.SetConfigName(tc.configName) + v.SetConfigType(tc.configType) + err := v.ReadConfig(bytes.NewBuffer(tc.input)) + if err != nil { + t.Fatal(err) + } + if err := v.WriteConfigAs(tc.fileName); err != nil { + t.Fatal(err) + } + + // The TOML String method does not order the contents. + // Therefore, we must read the generated file and compare the data. + v2 := New() + v2.SetFs(fs) + v2.SetConfigName(tc.configName) + v2.SetConfigType(tc.configType) + v2.SetConfigFile(tc.fileName) + err = v2.ReadInConfig() + if err != nil { + t.Fatal(err) + } + + assert.Equal(t, v.GetString("title_dotenv"), v2.GetString("title_dotenv")) + assert.Equal(t, v.GetString("type_dotenv"), v2.GetString("type_dotenv")) + assert.Equal(t, v.GetString("kind_dotenv"), v2.GetString("kind_dotenv")) + }) } - assert.Equal(t, yamlWriteExpected, read) } func TestSafeWriteConfig(t *testing.T) {