Commit Graph

256 Commits

Author SHA1 Message Date
Séra Zoltán fa3412d7ea fix cannot write hidden file without extension (#1017) 2021-09-19 09:28:14 +02:00
Vasily Ovchinnikov e606f7496e fix: made `InConfig` process paths correctly 2021-09-16 10:32:19 +02:00
Mark Sagi-Kazar a02f9864fa feat(encoding): Integrate the new encoding package into Viper
Signed-off-by: Mark Sagi-Kazar <mark.sagikazar@gmail.com>
2021-06-25 14:26:08 +02:00
Mikhail f. Shiryaev bd03865899 Add a proper processing for pflag.StringArray 2021-06-25 13:55:36 +02:00
Carlos Henrique Guardão Gandarez acd965b54e Add ini load options argument
Signed-off-by: Mark Sagi-Kazar <mark.sagikazar@gmail.com>
2021-06-16 01:55:16 +02:00
Carolyn Van Slyck cdb5e5976f Fix merging a key into a nil target
When merging a key where the target value is nil, the type of the target
and source do not match. What currently happens is an error is logged
and the key is skipped.

I have changed it so that it does the same thing as when the target key
is missing: copy the source value to the target.

Signed-off-by: Carolyn Van Slyck <me@carolynvanslyck.com>
2021-05-17 08:55:31 +02:00
Mark Sagi-Kazar 36be6bf91f feat: make sure Viper compiles on WASM
fsnotify is not available on WASM,
so config watching is not going to work.

Signed-off-by: Mark Sagi-Kazar <mark.sagikazar@gmail.com>
2021-04-24 10:24:23 +02:00
Chris Waldon 727a41c38a doc: add a note about concurent Get/Set to godoc 2021-04-15 18:20:20 +02:00
r-stepanenko fb4eafdd97
fix INI saving panics (#1075) 2021-03-10 14:46:19 +01:00
Mark Sagi-Kazar 8c89438499
docs: improve AutomaticEnv documentation
Fixes #1000

Signed-off-by: Mark Sagi-Kazar <mark.sagikazar@gmail.com>
2020-10-08 02:46:11 +02:00
dylandreimerink 33bcdc91ea
Added support for accessing slices (#861)
* Added support for accessing slices

* Processed PR feedback
- renamed searchMapWithPathPrefixes to searchIndexableWithPathPrefixes
- moved source type specific search logic to speparate functions
- Inverted if statments to avoid the arrow pattern

* Quickly return from searchSliceWithPathPrefixes and searchMapWithPathPrefixes functions without intermediate variables
2020-10-04 20:07:34 +02:00
Gabriel Aszalos ea890285a5 Update docs 2020-09-30 12:44:21 +02:00
Gabriel Aszalos b655224c01 Allow BindEnv to register multiple environment variables.
This change modifies BindEnv to permit a list of environment variable
names in order to support multiple env. vars. for the same config key.
When this form is used, env. keys take precedence in the written order.

Closes #971
2020-09-30 12:44:21 +02:00
Dan Rollo a0285163e1 ensure BindPFlag() detects a nil flag parameter before wrapping in pflagValue. 2020-09-23 19:29:16 +02:00
Mark Sagi-Kazar a5152092c6 Improve lint rules
Signed-off-by: Mark Sagi-Kazar <mark.sagikazar@gmail.com>
2020-09-11 17:53:37 +02:00
Mark Sagi-Kazar 387404d518 Add log for remote config errors
Signed-off-by: Mark Sagi-Kazar <mark.sagikazar@gmail.com>
2020-09-09 22:37:34 +02:00
Oleg Butuzov 13494e8047 Skipping error != nil checks in favor of the fast return. 2020-07-16 06:22:21 +02:00
Trevor Foster 3856c05f99
Fix: Getting the value of a StringToString pflag (#874)
* add parsing for stringToString flags

* add logic to return flags default if not val set, add a test

* extract parsing into single func

* add a few more cases

* return nil if unable to parse instead of panicing

* return map[string]interface in order to work with cast.ToStringMap

* mostly copy pflags implementation of the conversion to a stringtostring
2020-05-09 11:38:39 +02:00
flow00 c6ee9808ab fix_ini_save_error 2020-05-05 12:50:28 +02:00
Gustavo Bazan 97ee7adfef
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 `<file name> requires valid extension`
This adds support to saving such files
2020-02-20 00:41:04 +01:00
Alexey Maslov 9c81997cb1 added firestore support. 2020-02-20 00:36:32 +01:00
Alexey Maslov 502400c0d9 changed github.com/xordataexchange/crypt to github.com/bketelsen/crypt. 2020-02-20 00:36:32 +01:00
Mark Sagi-Kazar f2cbaea4c2
Fix copy-paste error in documentation
Fixes #825
2020-01-16 19:54:31 +01:00
Pedro Silva 9cd571279d 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
2020-01-16 19:23:50 +01:00
Mark Sagi-Kazar 5ad4bc05cf Add decoder opts to unmarshal exact 2019-12-06 14:27:22 +01:00
Gregory Haynes bcb420b705 Add global UnmarshalExact method
There is no helper method for UnmarshalExact which calls the
corresponding method on the global viper instance.
2019-12-06 14:15:02 +01:00
Mark Sagi-Kazar 4ad4c8df70 Add string replacer interface and env key replacer option 2019-12-06 14:13:46 +01:00
Mark Sagi-Kazar bdf2db0ff8 Fix goimports 2019-12-06 14:01:42 +01:00
Mark Sagi-Kazar a842b8f618 Replace SetKeyDelimiter with functional options 2019-12-06 14:01:42 +01:00
Mark Sagi-Kazar a73303ee89 Add key delimiter setter 2019-12-06 14:01:42 +01:00
Mark Sagi-Kazar 8b7fbcaa4b Fix golint 2019-12-06 13:00:52 +01:00
Mark Sagi-Kazar 29bb3ee94f Fix deadcode 2019-12-06 13:00:52 +01:00
Mark Sagi-Kazar 7b5adba788 Fix whitespace 2019-12-06 13:00:52 +01:00
Matti R 351bfe9719 loop through ini sections
pass tests

write out ini file & tests

go fmt

Update viper_test.go
fix test

gofmt
2019-12-06 09:02:54 +01:00
javaducky 3a19b6e0d9 Review updates to utilize afero for file checks and updated checks on unit tests 2019-12-06 08:36:52 +01:00
javaducky a708479794 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.
2019-12-06 08:36:52 +01:00
Benoit Masson 9e353e395e 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.
2019-11-29 21:16:13 +01:00
Pedro Silva d1c60d9e69 Support config files with no extensions (#722)
* Support config files with no extensions

* Update README informing config files without extension are supported
2019-11-06 14:54:13 +01:00
Vivek V 72b022eb35 Added capacity to slice creation 2019-11-01 11:15:09 +01:00
Mark Sagi-Kazar 71509d2887 Revert the effective changes of #673 2019-09-28 12:02:57 +02:00
inkychris 99520c81d8 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.
2019-09-11 16:03:08 +02:00
TwiN e697d557b7 Fix small typo 2019-08-17 00:18:41 +02:00
Christian Muehlhaeuser 33bf76add3 Simplify code
Removed unnecessary conversions.
2019-07-29 18:37:00 +02:00
Christian Muehlhaeuser 1b33e8258e Add error handling
Added error handling around ineffectual err assignments. Please review
thoroughly.
2019-07-28 14:50:13 +02:00
Christian Muehlhaeuser d65fa7608b Fixed typo
Just a nitpicky typo fix.
2019-07-28 14:47:04 +02:00
Rodrigo Chiossi cdccc8152c 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 <rodrigo.chiossi@intel.com>
2019-07-28 12:33:29 +02:00
bpizzi e02bc9eca5 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.
2019-07-13 12:28:02 +02:00
Nicolas Martin e325492b82 Add missing call to initWF.Done() 2019-07-13 12:13:30 +02:00
Márk Sági-Kazár e6d1c6bc9a Improve godoc 2019-07-13 12:11:16 +02:00
CodeLingo Bot 275a36d0a0 Fix function comments based on best practices from Effective Go
Signed-off-by: CodeLingo Bot <bot@codelingo.io>
2019-07-13 12:11:16 +02:00
Márk Sági-Kazár 7fdad0204e Fix typo 2019-07-13 12:04:36 +02:00
AGirard b8221cf4ee Add GetIntSlice helper method 2019-07-13 12:04:36 +02:00
Márk Sági-Kazár ad5ed02fa4 Add support for int slice flags (#637)
* Add support for int slice flags

* Add int slice test to unmarshal
2019-06-11 16:51:57 -04:00
mexisme 3620d3d9e1 Support `.env` format files (#528)
* Support `.env` format files

* Missing "dotenv" from SupportedExtns
2019-06-11 16:47:43 -04:00
Mitch Connors 7a605a50e6 Uint Support (#681)
* add GetUint/GetUint32/GetUint64

* Add Get(string) support for uint.
2019-04-08 10:06:45 -04:00
Kris fccfc2c271 Add API for setting file permissions 2019-04-04 14:02:01 +02:00
Bjørn Erik Pedersen 9e56dacc08 Remove superflous insensitiviseMaps in Unmarshal methods
Fixes #482
2019-03-13 10:29:51 +01:00
Bjørn Erik Pedersen 6d33b5a963
Make the map in MergeConfigMap case insensitive 2018-12-07 11:03:36 +01:00
Bjørn Erik Pedersen 3535c75fa8 Add MergeConfigMap
Fixes #605
2018-12-05 17:13:47 +01:00
Márk Sági-Kazár b7a3b95476 Lookup environment variables instead of checking if the value is empty
This commit adds an `AllowEmptyEnv` option that, default off, that when set will allow set, but empty, environment variables

Fixes #317
2018-11-06 22:53:21 +01:00
Andrew Stuart 62edee3196
Revert "Cater for case-sensitive dependencies (#463)"
This reverts commit b56071875a.
2018-09-29 21:41:27 -07:00
Panagiotis Moustafellos b56071875a Cater for case-sensitive dependencies (#463)
Switching jww from jwalterweatherman to jWalterWeatherman for cases when vgo is broken
2018-09-29 20:32:38 -07:00
Andrew Stuart 8e194e8ad2
Merge branch 'master' into mergefix/Issue284_Kubernetes_config 2018-09-28 01:05:04 -07:00
Andrew Stuart 2c12c60302
Fix nil pointer on watch function (#568) 2018-09-28 00:53:21 -07:00
Dr. Tobias Quathamer 0ac2068de9 Fix overflow error on 32 bit architectures (#340)
* Handle int64 separately for 32 bit architectures

* Remove tests which result in an overflow error on 32 bit architectures
2018-09-01 14:59:01 -06:00
Aarti Parikh 05116ad639 Revert "fix dep wrong case (#484)"
This reverts commit b7a62b2c00.
2018-08-28 02:34:36 -06:00
kun b7a62b2c00 fix dep wrong case (#484) 2018-08-28 01:37:55 -06:00
Robin Brämer e436d04e6d correct a comment on viper.Set() (#553)
correct regiser with register
2018-08-28 01:29:26 -06:00
Xavier Coulon 41f829b2c9 refactor test to avoid negative counter on WG
Signed-off-by: Xavier Coulon <xcoulon@redhat.com>
2018-08-06 09:29:13 +02:00
Xavier Coulon e12d3d32d1 Merge branch 'master' into Issue284_Kubernetes_config 2018-08-06 08:18:53 +02:00
Chris Reeves 907c19d40d Support customising mapstructure.DecoderConfig for Unmarshal
* Added a new `DecoderConfigOption` type allowing the user to write custom
  functions that can override the default mapstructure.DecoderConfig
  settings

* Added a new `DecodeHook` function which returns
  a `DecoderConfigOption`. This allows the user to easily set their own
  Decode hooks when Unmarshaling

* Updated Unmarshal, UnmarshalKey and defaultDecoderConfig to support variadic
  trailing `DecoderConfigOption` functions to allow for customisation of
  the default  mapstructure.DecoderConfig

* Added a test case with example usage
2018-08-03 14:57:45 +02:00
Xavier Coulon c1250e5dd7 apply review comments
use masks for checking the events.

Signed-off-by: Xavier Coulon <xcoulon@redhat.com>
2018-07-13 10:30:23 +02:00
Xavier Coulon 242f4890f5 Refactor with WaitGroup and check channel is open
Signed-off-by: Xavier Coulon <xcoulon@redhat.com>
2018-05-24 10:09:29 +02:00
Travis Jeffery 15738813a0 Add GetInt32 2018-05-07 09:10:07 +02:00
Bjørn Erik Pedersen b5e8006cbe
Undexport GetConfigFile
It was exported in previous commit, but we have GetConfigFileUsed -- so use that.
2018-03-19 19:50:19 +01:00
Bjørn Erik Pedersen 00ed41cdba
Export and fix GetConfigFile 2018-03-19 19:12:24 +01:00
Xavier Coulon e0f7631cf3 WatchConfig and Kubernetes (#284)
Support override of symlink to config file
Include tests for WatchConfig of regular files, as well
as config file which links to a folder which is itself a
link to another folder in the same "watch dir" (the way
Kubernetes exposes config files from ConfigMaps mounted
on a volume in a Pod)

Also:
- Add synchronization with WaitGroup to ensure that the WatchConfig
is properly started before returning
- Remove the watcher when the Config file is removed.

Fixes #284

Signed-off-by: Xavier Coulon <xcoulon@redhat.com>
2018-01-03 10:37:18 +01:00
Adam Sherwood 1a0c4a370c Feature/write config (#287)
* Added method to write into TOML file.

* Added functionality to export configuration based on config type. The feature supports JSON and TOML.

* Added method to write into YAML file.

* Fixed the issue of incorrect defer and error checking order. The error checking must be first otherwise it will cause panic.

* Add WriteConfig methods

* Add support for toml

* Add shared write function and safe methods

* Fix incorrectly modified imports

* Remove extra comments

* Fix spelling

* Make marshal spelling consistent throughout

* Add support for remaining configuration types

This commit moves a significant portion of the code back to viper.go to
facilitate having access to the object when reading the files. The purpose is to
add properties to the viper object at read time, so that we can add the comments
back to the file when writing.

* Add tests for each written file type

* Modify test for updated HCL specification

* Modify to only support HCL write in Go 1.7

* Revert "Modify to only support HCL write in Go 1.7"

This reverts commit 12b34bc4eb92cbf8ebfd56b79519f448607e3e51.

* Need to truncate the file before writing

* Write all settings including overrides

* Use filename variable

* Lint remote.go

* Fix toml return count error
2017-12-06 23:26:31 -05:00
Jeff Lindsay 4dddf7c62e Allow exists util function to take afero.Fs so it can be used with non-deafult instances of Viper (#405)
Signed-off-by: Jeff Lindsay <progrium@gmail.com>
2017-11-09 21:57:16 +01:00
Bjørn Erik Pedersen d9cca5ef33 Go fmt viper.go 2017-09-29 23:06:42 +02:00
Alexander Krasnukhin 266e588e9e Add string slice support to defaultDecoderConfig
This way it correctly decodes string slices as well.
2017-09-29 21:44:38 +02:00
Nick Miyake 8ac2e2e20f Run gofmt on all existing code
Also:

* Add check to .travis.yml that verifies that all code is gofmt-compliant
* Touch up some newlines

See #351
2017-07-22 23:47:47 -06:00
Nick Miyake f257d19100 Fix a few minor typos/formatting issues with comments
See #350
2017-07-22 23:39:01 -06:00
Nikola Kovacs 9766537120 Fix grammar/typo in comment for getEnv()
See #310
2017-07-22 23:09:41 -06:00
Albert Nigmatzianov c1de95864d Prevent redundant type assertion in Get
There is no need to assert variables which already have appropriate
type.

name              old time/op    new time/op    delta
GetBool-4            554ns ± 3%     493ns ± 8%  -10.86%  (p=0.000 n=14+15)
Get-4                484ns ± 4%     414ns ± 7%  -14.37%  (p=0.000 n=14+15)
GetBoolFromMap-4    8.38ns ± 6%    7.83ns ± 7%   -6.59%  (p=0.000 n=15+15)

name              old alloc/op   new alloc/op   delta
GetBool-4            65.0B ± 0%     64.0B ± 0%   -1.54%  (p=0.000 n=15+15)
Get-4                64.0B ± 0%     64.0B ± 0%     ~     (all equal)
GetBoolFromMap-4     0.00B          0.00B          ~     (all equal)

name              old allocs/op  new allocs/op  delta
GetBool-4             5.00 ± 0%      4.00 ± 0%  -20.00%  (p=0.000 n=15+15)
Get-4                 5.00 ± 0%      4.00 ± 0%  -20.00%  (p=0.000 n=15+15)
GetBoolFromMap-4      0.00           0.00          ~     (all equal)
2017-06-19 14:43:13 +02:00
Paweł Szczur 0967fc9ace Properly handle string slice values 2017-04-17 10:08:15 +02:00
Miguel Eduardo Gil Biraud 5d46e70da8 Fix UnmarshalKey handling of time.Duration
* Failing test for key unmarshaling with nested structs containing time.Duration

* Fix UnmarshalKey to use of defaultDecoderConfig
2017-04-10 11:26:50 +02:00
Wolfgang Friedl 84f94806c6 Avoid the start of go-routines which are never get stopped 2017-03-15 14:43:09 +01:00
Bjørn Erik Pedersen 0b5690fd87 Revert "remote: Avoid the start of go-routines which are never get stopped"
This reverts commit 11ca61e888.
2017-03-15 08:10:26 +01:00
Wolfgang Friedl 11ca61e888 remote: Avoid the start of go-routines which are never get stopped 2017-03-15 08:08:46 +01:00
Cameron Moore d90f2bb139 Only save config on success in ReadInConfig
If the user creates a invalid config file while watching for config
changes, the previous, valid config is not retained.  This commit only
overwrites the running config if unmarshalling was successful.
2017-02-17 17:31:47 +01:00
Kevin GEORGES 5ed0fc31f7 Fix MergeInConfig error return
UnsupportedConfigError was returned if config file not found

* Swap getConfigFile and getConfigType call
* Add a unit test
2016-12-13 10:38:49 +01:00
Albert Nigmatzianov 651d9d916a Document case-insensitivity for key taking methods 2016-10-29 23:33:52 +02:00
Bjørn Erik Pedersen 80ab6657f9 Copy and insensitivise maps in Set
Fixes #261
Closes #265
2016-10-24 21:20:41 +02:00
Benoît Masson 285f151019 Fixed AllKeys() to include env values added with BindEnv()
* Fixed: values bound with BindEnv added to AllKeys()

Cast was not working, and v.env wasn't used when merging keys.

Rewrote explicit and specific casts for maps storing strings or FlagValues.

* Added: test for BindEnv() and AllKeys()

To make sure AllSettings() and Unmarshal() will consider environment
variables added with BindEnv().
2016-10-23 23:04:21 +02:00
Benoît Masson 50515b700e Increase performance of nested keys search
* Fixed: insensitiviseMaps and tests

All keys (even nested ones) are now lower-cased recursively.

On the way, map[interface{}]interface{} are cast to map[string]interface{}

* Changed: simplified find() fast path and increase performance

Removed searchMapForKey(), fast path directly integrated into searchMap() and
searchMapWithPathPrefixes()
=> more generic (searchMapForKey() wasn't called everywhere it should have)

At the same time, significantly speed up searchMap() and searchMapWithPathPrefixes(),
which are still used for nested keys: the assumption that map keys are all
lower-cased allows to perform
    val = m[key]
instead of
    for k, v := range m {
      if strings.ToLower(k) == strings.ToLower(key) {
        val = v
      }
    }
=> i.e., directly access the map instead of enumerate the keys
2016-10-14 11:24:45 +02:00
Jonathan Anderson 44208030b3 Allow errors to propagate from getConfigFile(). (#161)
- propagate ConfigFileNotFoundError instead of using unsupported config type error when config file is not found
2016-10-13 13:33:30 +02:00
Bjørn Erik Pedersen 51f23d1f1c Restore performance for the simple case
```
BenchmarkGetBool-4            1021          481           -52.89%
BenchmarkGet-4                879           403           -54.15%
BenchmarkGetBoolFromMap-4     6.56          6.40          -2.44%

benchmark                     old allocs     new allocs     delta
BenchmarkGetBool-4            6              4              -33.33%
BenchmarkGet-4                6              4              -33.33%
BenchmarkGetBoolFromMap-4     0              0              +0.00%

benchmark                     old bytes     new bytes     delta
BenchmarkGetBool-4            113           49            -56.64%
BenchmarkGet-4                112           48            -57.14%
BenchmarkGetBoolFromMap-4     0             0             +0.00%
```

Fixes #249
Fixes https://github.com/spf13/hugo/issues/2536
2016-10-10 13:40:38 +02:00
Benoît Masson ec4eb2fa85 Nested maps (#195)
Fixes #71, #93, #158, #168, #209, #141, #160, #162, #190

* Fixed: indentation in comment
* Fixed: Get() returns nil when nested element not found
* Fixed: insensitiviseMaps() made recursive so that nested keys are lowercased
* Fixed: order of expected<=>actual in assert.Equal() statements
* Fixed: find() looks into "overrides" first
* Fixed: TestBindPFlags() to use a new Viper instance
* Fixed: removed extra aliases from display in Debug()
* Added: test for checking precedence of dot-containing keys.
* Fixed: Set() and SetDefault() insert nested values
* Added: tests for overriding nested values
* Changed: AllKeys() includes all keys / AllSettings() includes overridden nested values
* Added: test for shadowed nested key
* Fixed: properties parsing generates nested maps
* Fixed: Get() and IsSet() work correctly on nested values
* Changed: modifier README.md to reflect changes
2016-10-08 10:00:18 +02:00
Cameron Moore 670c42a85b Check for nil in viper.Sub
Fixes #191
2016-10-06 01:22:39 +02:00