viper.go 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154
  1. // Copyright © 2014 Steve Francia <spf@spf13.com>.
  2. //
  3. // Use of this source code is governed by an MIT-style
  4. // license that can be found in the LICENSE file.
  5. // Viper is an application configuration system.
  6. // It believes that applications can be configured a variety of ways
  7. // via flags, ENVIRONMENT variables, configuration files retrieved
  8. // from the file system, or a remote key/value store.
  9. // Each item takes precedence over the item below it:
  10. // overrides
  11. // flag
  12. // env
  13. // config
  14. // key/value store
  15. // default
  16. package viper
  17. import (
  18. "bytes"
  19. "encoding/csv"
  20. "errors"
  21. "fmt"
  22. "io"
  23. "log"
  24. "os"
  25. "path/filepath"
  26. "reflect"
  27. "strconv"
  28. "strings"
  29. "sync"
  30. "time"
  31. "github.com/fsnotify/fsnotify"
  32. "github.com/mitchellh/mapstructure"
  33. "github.com/spf13/afero"
  34. "github.com/spf13/cast"
  35. "github.com/spf13/pflag"
  36. "github.com/spf13/viper/internal/encoding"
  37. "github.com/spf13/viper/internal/encoding/dotenv"
  38. "github.com/spf13/viper/internal/encoding/hcl"
  39. "github.com/spf13/viper/internal/encoding/ini"
  40. "github.com/spf13/viper/internal/encoding/javaproperties"
  41. "github.com/spf13/viper/internal/encoding/json"
  42. "github.com/spf13/viper/internal/encoding/toml"
  43. "github.com/spf13/viper/internal/encoding/yaml"
  44. )
  45. // ConfigMarshalError happens when failing to marshal the configuration.
  46. type ConfigMarshalError struct {
  47. err error
  48. }
  49. // Error returns the formatted configuration error.
  50. func (e ConfigMarshalError) Error() string {
  51. return fmt.Sprintf("While marshaling config: %s", e.err.Error())
  52. }
  53. var v *Viper
  54. type RemoteResponse struct {
  55. Value []byte
  56. Error error
  57. }
  58. func init() {
  59. v = New()
  60. }
  61. type remoteConfigFactory interface {
  62. Get(rp RemoteProvider) (io.Reader, error)
  63. Watch(rp RemoteProvider) (io.Reader, error)
  64. WatchChannel(rp RemoteProvider) (<-chan *RemoteResponse, chan bool)
  65. }
  66. // RemoteConfig is optional, see the remote package
  67. var RemoteConfig remoteConfigFactory
  68. // UnsupportedConfigError denotes encountering an unsupported
  69. // configuration filetype.
  70. type UnsupportedConfigError string
  71. // Error returns the formatted configuration error.
  72. func (str UnsupportedConfigError) Error() string {
  73. return fmt.Sprintf("Unsupported Config Type %q", string(str))
  74. }
  75. // UnsupportedRemoteProviderError denotes encountering an unsupported remote
  76. // provider. Currently only etcd and Consul are supported.
  77. type UnsupportedRemoteProviderError string
  78. // Error returns the formatted remote provider error.
  79. func (str UnsupportedRemoteProviderError) Error() string {
  80. return fmt.Sprintf("Unsupported Remote Provider Type %q", string(str))
  81. }
  82. // RemoteConfigError denotes encountering an error while trying to
  83. // pull the configuration from the remote provider.
  84. type RemoteConfigError string
  85. // Error returns the formatted remote provider error
  86. func (rce RemoteConfigError) Error() string {
  87. return fmt.Sprintf("Remote Configurations Error: %s", string(rce))
  88. }
  89. // ConfigFileNotFoundError denotes failing to find configuration file.
  90. type ConfigFileNotFoundError struct {
  91. name, locations string
  92. }
  93. // Error returns the formatted configuration error.
  94. func (fnfe ConfigFileNotFoundError) Error() string {
  95. return fmt.Sprintf("Config File %q Not Found in %q", fnfe.name, fnfe.locations)
  96. }
  97. // ConfigFileAlreadyExistsError denotes failure to write new configuration file.
  98. type ConfigFileAlreadyExistsError string
  99. // Error returns the formatted error when configuration already exists.
  100. func (faee ConfigFileAlreadyExistsError) Error() string {
  101. return fmt.Sprintf("Config File %q Already Exists", string(faee))
  102. }
  103. // A DecoderConfigOption can be passed to viper.Unmarshal to configure
  104. // mapstructure.DecoderConfig options
  105. type DecoderConfigOption func(*mapstructure.DecoderConfig)
  106. // DecodeHook returns a DecoderConfigOption which overrides the default
  107. // DecoderConfig.DecodeHook value, the default is:
  108. //
  109. // mapstructure.ComposeDecodeHookFunc(
  110. // mapstructure.StringToTimeDurationHookFunc(),
  111. // mapstructure.StringToSliceHookFunc(","),
  112. // )
  113. func DecodeHook(hook mapstructure.DecodeHookFunc) DecoderConfigOption {
  114. return func(c *mapstructure.DecoderConfig) {
  115. c.DecodeHook = hook
  116. }
  117. }
  118. // Viper is a prioritized configuration registry. It
  119. // maintains a set of configuration sources, fetches
  120. // values to populate those, and provides them according
  121. // to the source's priority.
  122. // The priority of the sources is the following:
  123. // 1. overrides
  124. // 2. flags
  125. // 3. env. variables
  126. // 4. config file
  127. // 5. key/value store
  128. // 6. defaults
  129. //
  130. // For example, if values from the following sources were loaded:
  131. //
  132. // Defaults : {
  133. // "secret": "",
  134. // "user": "default",
  135. // "endpoint": "https://localhost"
  136. // }
  137. // Config : {
  138. // "user": "root"
  139. // "secret": "defaultsecret"
  140. // }
  141. // Env : {
  142. // "secret": "somesecretkey"
  143. // }
  144. //
  145. // The resulting config will have the following values:
  146. //
  147. // {
  148. // "secret": "somesecretkey",
  149. // "user": "root",
  150. // "endpoint": "https://localhost"
  151. // }
  152. //
  153. // Note: Vipers are not safe for concurrent Get() and Set() operations.
  154. type Viper struct {
  155. // Delimiter that separates a list of keys
  156. // used to access a nested value in one go
  157. keyDelim string
  158. // A set of paths to look for the config file in
  159. configPaths []string
  160. // The filesystem to read config from.
  161. fs afero.Fs
  162. // A set of remote providers to search for the configuration
  163. remoteProviders []*defaultRemoteProvider
  164. // Name of file to look for inside the path
  165. configName string
  166. configFile string
  167. configType string
  168. configPermissions os.FileMode
  169. envPrefix string
  170. // Specific commands for ini parsing
  171. iniLoadOptions ini.LoadOptions
  172. automaticEnvApplied bool
  173. envKeyReplacer StringReplacer
  174. allowEmptyEnv bool
  175. config map[string]interface{}
  176. override map[string]interface{}
  177. defaults map[string]interface{}
  178. kvstore map[string]interface{}
  179. pflags map[string]FlagValue
  180. env map[string][]string
  181. aliases map[string]string
  182. typeByDefValue bool
  183. onConfigChange func(fsnotify.Event)
  184. logger Logger
  185. // TODO: should probably be protected with a mutex
  186. encoderRegistry *encoding.EncoderRegistry
  187. decoderRegistry *encoding.DecoderRegistry
  188. }
  189. // New returns an initialized Viper instance.
  190. func New() *Viper {
  191. v := new(Viper)
  192. v.keyDelim = "."
  193. v.configName = "config"
  194. v.configPermissions = os.FileMode(0o644)
  195. v.fs = afero.NewOsFs()
  196. v.config = make(map[string]interface{})
  197. v.override = make(map[string]interface{})
  198. v.defaults = make(map[string]interface{})
  199. v.kvstore = make(map[string]interface{})
  200. v.pflags = make(map[string]FlagValue)
  201. v.env = make(map[string][]string)
  202. v.aliases = make(map[string]string)
  203. v.typeByDefValue = false
  204. v.logger = jwwLogger{}
  205. v.resetEncoding()
  206. return v
  207. }
  208. // Option configures Viper using the functional options paradigm popularized by Rob Pike and Dave Cheney.
  209. // If you're unfamiliar with this style,
  210. // see https://commandcenter.blogspot.com/2014/01/self-referential-functions-and-design.html and
  211. // https://dave.cheney.net/2014/10/17/functional-options-for-friendly-apis.
  212. type Option interface {
  213. apply(v *Viper)
  214. }
  215. type optionFunc func(v *Viper)
  216. func (fn optionFunc) apply(v *Viper) {
  217. fn(v)
  218. }
  219. // KeyDelimiter sets the delimiter used for determining key parts.
  220. // By default it's value is ".".
  221. func KeyDelimiter(d string) Option {
  222. return optionFunc(func(v *Viper) {
  223. v.keyDelim = d
  224. })
  225. }
  226. // StringReplacer applies a set of replacements to a string.
  227. type StringReplacer interface {
  228. // Replace returns a copy of s with all replacements performed.
  229. Replace(s string) string
  230. }
  231. // EnvKeyReplacer sets a replacer used for mapping environment variables to internal keys.
  232. func EnvKeyReplacer(r StringReplacer) Option {
  233. return optionFunc(func(v *Viper) {
  234. v.envKeyReplacer = r
  235. })
  236. }
  237. // NewWithOptions creates a new Viper instance.
  238. func NewWithOptions(opts ...Option) *Viper {
  239. v := New()
  240. for _, opt := range opts {
  241. opt.apply(v)
  242. }
  243. v.resetEncoding()
  244. return v
  245. }
  246. // Reset is intended for testing, will reset all to default settings.
  247. // In the public interface for the viper package so applications
  248. // can use it in their testing as well.
  249. func Reset() {
  250. v = New()
  251. SupportedExts = []string{"json", "toml", "yaml", "yml", "properties", "props", "prop", "hcl", "tfvars", "dotenv", "env", "ini"}
  252. SupportedRemoteProviders = []string{"etcd", "etcd3", "consul", "firestore"}
  253. }
  254. // TODO: make this lazy initialization instead
  255. func (v *Viper) resetEncoding() {
  256. encoderRegistry := encoding.NewEncoderRegistry()
  257. decoderRegistry := encoding.NewDecoderRegistry()
  258. {
  259. codec := yaml.Codec{}
  260. encoderRegistry.RegisterEncoder("yaml", codec)
  261. decoderRegistry.RegisterDecoder("yaml", codec)
  262. encoderRegistry.RegisterEncoder("yml", codec)
  263. decoderRegistry.RegisterDecoder("yml", codec)
  264. }
  265. {
  266. codec := json.Codec{}
  267. encoderRegistry.RegisterEncoder("json", codec)
  268. decoderRegistry.RegisterDecoder("json", codec)
  269. }
  270. {
  271. codec := toml.Codec{}
  272. encoderRegistry.RegisterEncoder("toml", codec)
  273. decoderRegistry.RegisterDecoder("toml", codec)
  274. }
  275. {
  276. codec := hcl.Codec{}
  277. encoderRegistry.RegisterEncoder("hcl", codec)
  278. decoderRegistry.RegisterDecoder("hcl", codec)
  279. encoderRegistry.RegisterEncoder("tfvars", codec)
  280. decoderRegistry.RegisterDecoder("tfvars", codec)
  281. }
  282. {
  283. codec := ini.Codec{
  284. KeyDelimiter: v.keyDelim,
  285. LoadOptions: v.iniLoadOptions,
  286. }
  287. encoderRegistry.RegisterEncoder("ini", codec)
  288. decoderRegistry.RegisterDecoder("ini", codec)
  289. }
  290. {
  291. codec := &javaproperties.Codec{
  292. KeyDelimiter: v.keyDelim,
  293. }
  294. encoderRegistry.RegisterEncoder("properties", codec)
  295. decoderRegistry.RegisterDecoder("properties", codec)
  296. encoderRegistry.RegisterEncoder("props", codec)
  297. decoderRegistry.RegisterDecoder("props", codec)
  298. encoderRegistry.RegisterEncoder("prop", codec)
  299. decoderRegistry.RegisterDecoder("prop", codec)
  300. }
  301. {
  302. codec := &dotenv.Codec{}
  303. encoderRegistry.RegisterEncoder("dotenv", codec)
  304. decoderRegistry.RegisterDecoder("dotenv", codec)
  305. encoderRegistry.RegisterEncoder("env", codec)
  306. decoderRegistry.RegisterDecoder("env", codec)
  307. }
  308. v.encoderRegistry = encoderRegistry
  309. v.decoderRegistry = decoderRegistry
  310. }
  311. type defaultRemoteProvider struct {
  312. provider string
  313. endpoint string
  314. path string
  315. secretKeyring string
  316. }
  317. func (rp defaultRemoteProvider) Provider() string {
  318. return rp.provider
  319. }
  320. func (rp defaultRemoteProvider) Endpoint() string {
  321. return rp.endpoint
  322. }
  323. func (rp defaultRemoteProvider) Path() string {
  324. return rp.path
  325. }
  326. func (rp defaultRemoteProvider) SecretKeyring() string {
  327. return rp.secretKeyring
  328. }
  329. // RemoteProvider stores the configuration necessary
  330. // to connect to a remote key/value store.
  331. // Optional secretKeyring to unencrypt encrypted values
  332. // can be provided.
  333. type RemoteProvider interface {
  334. Provider() string
  335. Endpoint() string
  336. Path() string
  337. SecretKeyring() string
  338. }
  339. // SupportedExts are universally supported extensions.
  340. var SupportedExts = []string{"json", "toml", "yaml", "yml", "properties", "props", "prop", "hcl", "tfvars", "dotenv", "env", "ini"}
  341. // SupportedRemoteProviders are universally supported remote providers.
  342. var SupportedRemoteProviders = []string{"etcd", "etcd3", "consul", "firestore"}
  343. // OnConfigChange sets the event handler that is called when a config file changes.
  344. func OnConfigChange(run func(in fsnotify.Event)) { v.OnConfigChange(run) }
  345. // OnConfigChange sets the event handler that is called when a config file changes.
  346. func (v *Viper) OnConfigChange(run func(in fsnotify.Event)) {
  347. v.onConfigChange = run
  348. }
  349. // WatchConfig starts watching a config file for changes.
  350. func WatchConfig() { v.WatchConfig() }
  351. // WatchConfig starts watching a config file for changes.
  352. func (v *Viper) WatchConfig() {
  353. initWG := sync.WaitGroup{}
  354. initWG.Add(1)
  355. go func() {
  356. watcher, err := newWatcher()
  357. if err != nil {
  358. log.Fatal(err)
  359. }
  360. defer watcher.Close()
  361. // we have to watch the entire directory to pick up renames/atomic saves in a cross-platform way
  362. filename, err := v.getConfigFile()
  363. if err != nil {
  364. log.Printf("error: %v\n", err)
  365. initWG.Done()
  366. return
  367. }
  368. configFile := filepath.Clean(filename)
  369. configDir, _ := filepath.Split(configFile)
  370. realConfigFile, _ := filepath.EvalSymlinks(filename)
  371. eventsWG := sync.WaitGroup{}
  372. eventsWG.Add(1)
  373. go func() {
  374. for {
  375. select {
  376. case event, ok := <-watcher.Events:
  377. if !ok { // 'Events' channel is closed
  378. eventsWG.Done()
  379. return
  380. }
  381. currentConfigFile, _ := filepath.EvalSymlinks(filename)
  382. // we only care about the config file with the following cases:
  383. // 1 - if the config file was modified or created
  384. // 2 - if the real path to the config file changed (eg: k8s ConfigMap replacement)
  385. if (filepath.Clean(event.Name) == configFile &&
  386. (event.Has(fsnotify.Write) || event.Has(fsnotify.Create))) ||
  387. (currentConfigFile != "" && currentConfigFile != realConfigFile) {
  388. realConfigFile = currentConfigFile
  389. err := v.ReadInConfig()
  390. if err != nil {
  391. log.Printf("error reading config file: %v\n", err)
  392. }
  393. if v.onConfigChange != nil {
  394. v.onConfigChange(event)
  395. }
  396. } else if filepath.Clean(event.Name) == configFile && event.Has(fsnotify.Remove) {
  397. eventsWG.Done()
  398. return
  399. }
  400. case err, ok := <-watcher.Errors:
  401. if ok { // 'Errors' channel is not closed
  402. log.Printf("watcher error: %v\n", err)
  403. }
  404. eventsWG.Done()
  405. return
  406. }
  407. }
  408. }()
  409. watcher.Add(configDir)
  410. initWG.Done() // done initializing the watch in this go routine, so the parent routine can move on...
  411. eventsWG.Wait() // now, wait for event loop to end in this go-routine...
  412. }()
  413. initWG.Wait() // make sure that the go routine above fully ended before returning
  414. }
  415. // SetConfigFile explicitly defines the path, name and extension of the config file.
  416. // Viper will use this and not check any of the config paths.
  417. func SetConfigFile(in string) { v.SetConfigFile(in) }
  418. func (v *Viper) SetConfigFile(in string) {
  419. if in != "" {
  420. v.configFile = in
  421. }
  422. }
  423. // SetEnvPrefix defines a prefix that ENVIRONMENT variables will use.
  424. // E.g. if your prefix is "spf", the env registry will look for env
  425. // variables that start with "SPF_".
  426. func SetEnvPrefix(in string) { v.SetEnvPrefix(in) }
  427. func (v *Viper) SetEnvPrefix(in string) {
  428. if in != "" {
  429. v.envPrefix = in
  430. }
  431. }
  432. func (v *Viper) mergeWithEnvPrefix(in string) string {
  433. if v.envPrefix != "" {
  434. return strings.ToUpper(v.envPrefix + "_" + in)
  435. }
  436. return strings.ToUpper(in)
  437. }
  438. // AllowEmptyEnv tells Viper to consider set,
  439. // but empty environment variables as valid values instead of falling back.
  440. // For backward compatibility reasons this is false by default.
  441. func AllowEmptyEnv(allowEmptyEnv bool) { v.AllowEmptyEnv(allowEmptyEnv) }
  442. func (v *Viper) AllowEmptyEnv(allowEmptyEnv bool) {
  443. v.allowEmptyEnv = allowEmptyEnv
  444. }
  445. // TODO: should getEnv logic be moved into find(). Can generalize the use of
  446. // rewriting keys many things, Ex: Get('someKey') -> some_key
  447. // (camel case to snake case for JSON keys perhaps)
  448. // getEnv is a wrapper around os.Getenv which replaces characters in the original
  449. // key. This allows env vars which have different keys than the config object
  450. // keys.
  451. func (v *Viper) getEnv(key string) (string, bool) {
  452. if v.envKeyReplacer != nil {
  453. key = v.envKeyReplacer.Replace(key)
  454. }
  455. val, ok := os.LookupEnv(key)
  456. return val, ok && (v.allowEmptyEnv || val != "")
  457. }
  458. // ConfigFileUsed returns the file used to populate the config registry.
  459. func ConfigFileUsed() string { return v.ConfigFileUsed() }
  460. func (v *Viper) ConfigFileUsed() string { return v.configFile }
  461. // AddConfigPath adds a path for Viper to search for the config file in.
  462. // Can be called multiple times to define multiple search paths.
  463. func AddConfigPath(in string) { v.AddConfigPath(in) }
  464. func (v *Viper) AddConfigPath(in string) {
  465. if in != "" {
  466. absin := absPathify(v.logger, in)
  467. v.logger.Info("adding path to search paths", "path", absin)
  468. if !stringInSlice(absin, v.configPaths) {
  469. v.configPaths = append(v.configPaths, absin)
  470. }
  471. }
  472. }
  473. // AddRemoteProvider adds a remote configuration source.
  474. // Remote Providers are searched in the order they are added.
  475. // provider is a string value: "etcd", "etcd3", "consul" or "firestore" are currently supported.
  476. // endpoint is the url. etcd requires http://ip:port consul requires ip:port
  477. // path is the path in the k/v store to retrieve configuration
  478. // To retrieve a config file called myapp.json from /configs/myapp.json
  479. // you should set path to /configs and set config name (SetConfigName()) to
  480. // "myapp"
  481. func AddRemoteProvider(provider, endpoint, path string) error {
  482. return v.AddRemoteProvider(provider, endpoint, path)
  483. }
  484. func (v *Viper) AddRemoteProvider(provider, endpoint, path string) error {
  485. if !stringInSlice(provider, SupportedRemoteProviders) {
  486. return UnsupportedRemoteProviderError(provider)
  487. }
  488. if provider != "" && endpoint != "" {
  489. v.logger.Info("adding remote provider", "provider", provider, "endpoint", endpoint)
  490. rp := &defaultRemoteProvider{
  491. endpoint: endpoint,
  492. provider: provider,
  493. path: path,
  494. }
  495. if !v.providerPathExists(rp) {
  496. v.remoteProviders = append(v.remoteProviders, rp)
  497. }
  498. }
  499. return nil
  500. }
  501. // AddSecureRemoteProvider adds a remote configuration source.
  502. // Secure Remote Providers are searched in the order they are added.
  503. // provider is a string value: "etcd", "etcd3", "consul" or "firestore" are currently supported.
  504. // endpoint is the url. etcd requires http://ip:port consul requires ip:port
  505. // secretkeyring is the filepath to your openpgp secret keyring. e.g. /etc/secrets/myring.gpg
  506. // path is the path in the k/v store to retrieve configuration
  507. // To retrieve a config file called myapp.json from /configs/myapp.json
  508. // you should set path to /configs and set config name (SetConfigName()) to
  509. // "myapp"
  510. // Secure Remote Providers are implemented with github.com/bketelsen/crypt
  511. func AddSecureRemoteProvider(provider, endpoint, path, secretkeyring string) error {
  512. return v.AddSecureRemoteProvider(provider, endpoint, path, secretkeyring)
  513. }
  514. func (v *Viper) AddSecureRemoteProvider(provider, endpoint, path, secretkeyring string) error {
  515. if !stringInSlice(provider, SupportedRemoteProviders) {
  516. return UnsupportedRemoteProviderError(provider)
  517. }
  518. if provider != "" && endpoint != "" {
  519. v.logger.Info("adding remote provider", "provider", provider, "endpoint", endpoint)
  520. rp := &defaultRemoteProvider{
  521. endpoint: endpoint,
  522. provider: provider,
  523. path: path,
  524. secretKeyring: secretkeyring,
  525. }
  526. if !v.providerPathExists(rp) {
  527. v.remoteProviders = append(v.remoteProviders, rp)
  528. }
  529. }
  530. return nil
  531. }
  532. func (v *Viper) providerPathExists(p *defaultRemoteProvider) bool {
  533. for _, y := range v.remoteProviders {
  534. if reflect.DeepEqual(y, p) {
  535. return true
  536. }
  537. }
  538. return false
  539. }
  540. // searchMap recursively searches for a value for path in source map.
  541. // Returns nil if not found.
  542. // Note: This assumes that the path entries and map keys are lower cased.
  543. func (v *Viper) searchMap(source map[string]interface{}, path []string) interface{} {
  544. if len(path) == 0 {
  545. return source
  546. }
  547. next, ok := source[path[0]]
  548. if ok {
  549. // Fast path
  550. if len(path) == 1 {
  551. return next
  552. }
  553. // Nested case
  554. switch next.(type) {
  555. case map[interface{}]interface{}:
  556. return v.searchMap(cast.ToStringMap(next), path[1:])
  557. case map[string]interface{}:
  558. // Type assertion is safe here since it is only reached
  559. // if the type of `next` is the same as the type being asserted
  560. return v.searchMap(next.(map[string]interface{}), path[1:])
  561. default:
  562. // got a value but nested key expected, return "nil" for not found
  563. return nil
  564. }
  565. }
  566. return nil
  567. }
  568. // searchIndexableWithPathPrefixes recursively searches for a value for path in source map/slice.
  569. //
  570. // While searchMap() considers each path element as a single map key or slice index, this
  571. // function searches for, and prioritizes, merged path elements.
  572. // e.g., if in the source, "foo" is defined with a sub-key "bar", and "foo.bar"
  573. // is also defined, this latter value is returned for path ["foo", "bar"].
  574. //
  575. // This should be useful only at config level (other maps may not contain dots
  576. // in their keys).
  577. //
  578. // Note: This assumes that the path entries and map keys are lower cased.
  579. func (v *Viper) searchIndexableWithPathPrefixes(source interface{}, path []string) interface{} {
  580. if len(path) == 0 {
  581. return source
  582. }
  583. // search for path prefixes, starting from the longest one
  584. for i := len(path); i > 0; i-- {
  585. prefixKey := strings.ToLower(strings.Join(path[0:i], v.keyDelim))
  586. var val interface{}
  587. switch sourceIndexable := source.(type) {
  588. case []interface{}:
  589. val = v.searchSliceWithPathPrefixes(sourceIndexable, prefixKey, i, path)
  590. case map[string]interface{}:
  591. val = v.searchMapWithPathPrefixes(sourceIndexable, prefixKey, i, path)
  592. }
  593. if val != nil {
  594. return val
  595. }
  596. }
  597. // not found
  598. return nil
  599. }
  600. // searchSliceWithPathPrefixes searches for a value for path in sourceSlice
  601. //
  602. // This function is part of the searchIndexableWithPathPrefixes recurring search and
  603. // should not be called directly from functions other than searchIndexableWithPathPrefixes.
  604. func (v *Viper) searchSliceWithPathPrefixes(
  605. sourceSlice []interface{},
  606. prefixKey string,
  607. pathIndex int,
  608. path []string,
  609. ) interface{} {
  610. // if the prefixKey is not a number or it is out of bounds of the slice
  611. index, err := strconv.Atoi(prefixKey)
  612. if err != nil || len(sourceSlice) <= index {
  613. return nil
  614. }
  615. next := sourceSlice[index]
  616. // Fast path
  617. if pathIndex == len(path) {
  618. return next
  619. }
  620. switch n := next.(type) {
  621. case map[interface{}]interface{}:
  622. return v.searchIndexableWithPathPrefixes(cast.ToStringMap(n), path[pathIndex:])
  623. case map[string]interface{}, []interface{}:
  624. return v.searchIndexableWithPathPrefixes(n, path[pathIndex:])
  625. default:
  626. // got a value but nested key expected, do nothing and look for next prefix
  627. }
  628. // not found
  629. return nil
  630. }
  631. // searchMapWithPathPrefixes searches for a value for path in sourceMap
  632. //
  633. // This function is part of the searchIndexableWithPathPrefixes recurring search and
  634. // should not be called directly from functions other than searchIndexableWithPathPrefixes.
  635. func (v *Viper) searchMapWithPathPrefixes(
  636. sourceMap map[string]interface{},
  637. prefixKey string,
  638. pathIndex int,
  639. path []string,
  640. ) interface{} {
  641. next, ok := sourceMap[prefixKey]
  642. if !ok {
  643. return nil
  644. }
  645. // Fast path
  646. if pathIndex == len(path) {
  647. return next
  648. }
  649. // Nested case
  650. switch n := next.(type) {
  651. case map[interface{}]interface{}:
  652. return v.searchIndexableWithPathPrefixes(cast.ToStringMap(n), path[pathIndex:])
  653. case map[string]interface{}, []interface{}:
  654. return v.searchIndexableWithPathPrefixes(n, path[pathIndex:])
  655. default:
  656. // got a value but nested key expected, do nothing and look for next prefix
  657. }
  658. // not found
  659. return nil
  660. }
  661. // isPathShadowedInDeepMap makes sure the given path is not shadowed somewhere
  662. // on its path in the map.
  663. // e.g., if "foo.bar" has a value in the given map, it “shadows”
  664. //
  665. // "foo.bar.baz" in a lower-priority map
  666. func (v *Viper) isPathShadowedInDeepMap(path []string, m map[string]interface{}) string {
  667. var parentVal interface{}
  668. for i := 1; i < len(path); i++ {
  669. parentVal = v.searchMap(m, path[0:i])
  670. if parentVal == nil {
  671. // not found, no need to add more path elements
  672. return ""
  673. }
  674. switch parentVal.(type) {
  675. case map[interface{}]interface{}:
  676. continue
  677. case map[string]interface{}:
  678. continue
  679. default:
  680. // parentVal is a regular value which shadows "path"
  681. return strings.Join(path[0:i], v.keyDelim)
  682. }
  683. }
  684. return ""
  685. }
  686. // isPathShadowedInFlatMap makes sure the given path is not shadowed somewhere
  687. // in a sub-path of the map.
  688. // e.g., if "foo.bar" has a value in the given map, it “shadows”
  689. //
  690. // "foo.bar.baz" in a lower-priority map
  691. func (v *Viper) isPathShadowedInFlatMap(path []string, mi interface{}) string {
  692. // unify input map
  693. var m map[string]interface{}
  694. switch mi.(type) {
  695. case map[string]string, map[string]FlagValue:
  696. m = cast.ToStringMap(mi)
  697. default:
  698. return ""
  699. }
  700. // scan paths
  701. var parentKey string
  702. for i := 1; i < len(path); i++ {
  703. parentKey = strings.Join(path[0:i], v.keyDelim)
  704. if _, ok := m[parentKey]; ok {
  705. return parentKey
  706. }
  707. }
  708. return ""
  709. }
  710. // isPathShadowedInAutoEnv makes sure the given path is not shadowed somewhere
  711. // in the environment, when automatic env is on.
  712. // e.g., if "foo.bar" has a value in the environment, it “shadows”
  713. //
  714. // "foo.bar.baz" in a lower-priority map
  715. func (v *Viper) isPathShadowedInAutoEnv(path []string) string {
  716. var parentKey string
  717. for i := 1; i < len(path); i++ {
  718. parentKey = strings.Join(path[0:i], v.keyDelim)
  719. if _, ok := v.getEnv(v.mergeWithEnvPrefix(parentKey)); ok {
  720. return parentKey
  721. }
  722. }
  723. return ""
  724. }
  725. // SetTypeByDefaultValue enables or disables the inference of a key value's
  726. // type when the Get function is used based upon a key's default value as
  727. // opposed to the value returned based on the normal fetch logic.
  728. //
  729. // For example, if a key has a default value of []string{} and the same key
  730. // is set via an environment variable to "a b c", a call to the Get function
  731. // would return a string slice for the key if the key's type is inferred by
  732. // the default value and the Get function would return:
  733. //
  734. // []string {"a", "b", "c"}
  735. //
  736. // Otherwise the Get function would return:
  737. //
  738. // "a b c"
  739. func SetTypeByDefaultValue(enable bool) { v.SetTypeByDefaultValue(enable) }
  740. func (v *Viper) SetTypeByDefaultValue(enable bool) {
  741. v.typeByDefValue = enable
  742. }
  743. // GetViper gets the global Viper instance.
  744. func GetViper() *Viper {
  745. return v
  746. }
  747. // Get can retrieve any value given the key to use.
  748. // Get is case-insensitive for a key.
  749. // Get has the behavior of returning the value associated with the first
  750. // place from where it is set. Viper will check in the following order:
  751. // override, flag, env, config file, key/value store, default
  752. //
  753. // Get returns an interface. For a specific value use one of the Get____ methods.
  754. func Get(key string) interface{} { return v.Get(key) }
  755. func (v *Viper) Get(key string) interface{} {
  756. lcaseKey := strings.ToLower(key)
  757. val := v.find(lcaseKey, true)
  758. if val == nil {
  759. return nil
  760. }
  761. if v.typeByDefValue {
  762. // TODO(bep) this branch isn't covered by a single test.
  763. valType := val
  764. path := strings.Split(lcaseKey, v.keyDelim)
  765. defVal := v.searchMap(v.defaults, path)
  766. if defVal != nil {
  767. valType = defVal
  768. }
  769. switch valType.(type) {
  770. case bool:
  771. return cast.ToBool(val)
  772. case string:
  773. return cast.ToString(val)
  774. case int32, int16, int8, int:
  775. return cast.ToInt(val)
  776. case uint:
  777. return cast.ToUint(val)
  778. case uint32:
  779. return cast.ToUint32(val)
  780. case uint64:
  781. return cast.ToUint64(val)
  782. case int64:
  783. return cast.ToInt64(val)
  784. case float64, float32:
  785. return cast.ToFloat64(val)
  786. case time.Time:
  787. return cast.ToTime(val)
  788. case time.Duration:
  789. return cast.ToDuration(val)
  790. case []string:
  791. return cast.ToStringSlice(val)
  792. case []int:
  793. return cast.ToIntSlice(val)
  794. }
  795. }
  796. return val
  797. }
  798. // Sub returns new Viper instance representing a sub tree of this instance.
  799. // Sub is case-insensitive for a key.
  800. func Sub(key string) *Viper { return v.Sub(key) }
  801. func (v *Viper) Sub(key string) *Viper {
  802. subv := New()
  803. data := v.Get(key)
  804. if data == nil {
  805. return nil
  806. }
  807. if reflect.TypeOf(data).Kind() == reflect.Map {
  808. subv.config = cast.ToStringMap(data)
  809. return subv
  810. }
  811. return nil
  812. }
  813. // GetString returns the value associated with the key as a string.
  814. func GetString(key string) string { return v.GetString(key) }
  815. func (v *Viper) GetString(key string) string {
  816. return cast.ToString(v.Get(key))
  817. }
  818. // GetBool returns the value associated with the key as a boolean.
  819. func GetBool(key string) bool { return v.GetBool(key) }
  820. func (v *Viper) GetBool(key string) bool {
  821. return cast.ToBool(v.Get(key))
  822. }
  823. // GetInt returns the value associated with the key as an integer.
  824. func GetInt(key string) int { return v.GetInt(key) }
  825. func (v *Viper) GetInt(key string) int {
  826. return cast.ToInt(v.Get(key))
  827. }
  828. // GetInt32 returns the value associated with the key as an integer.
  829. func GetInt32(key string) int32 { return v.GetInt32(key) }
  830. func (v *Viper) GetInt32(key string) int32 {
  831. return cast.ToInt32(v.Get(key))
  832. }
  833. // GetInt64 returns the value associated with the key as an integer.
  834. func GetInt64(key string) int64 { return v.GetInt64(key) }
  835. func (v *Viper) GetInt64(key string) int64 {
  836. return cast.ToInt64(v.Get(key))
  837. }
  838. // GetUint returns the value associated with the key as an unsigned integer.
  839. func GetUint(key string) uint { return v.GetUint(key) }
  840. func (v *Viper) GetUint(key string) uint {
  841. return cast.ToUint(v.Get(key))
  842. }
  843. // GetUint16 returns the value associated with the key as an unsigned integer.
  844. func GetUint16(key string) uint16 { return v.GetUint16(key) }
  845. func (v *Viper) GetUint16(key string) uint16 {
  846. return cast.ToUint16(v.Get(key))
  847. }
  848. // GetUint32 returns the value associated with the key as an unsigned integer.
  849. func GetUint32(key string) uint32 { return v.GetUint32(key) }
  850. func (v *Viper) GetUint32(key string) uint32 {
  851. return cast.ToUint32(v.Get(key))
  852. }
  853. // GetUint64 returns the value associated with the key as an unsigned integer.
  854. func GetUint64(key string) uint64 { return v.GetUint64(key) }
  855. func (v *Viper) GetUint64(key string) uint64 {
  856. return cast.ToUint64(v.Get(key))
  857. }
  858. // GetFloat64 returns the value associated with the key as a float64.
  859. func GetFloat64(key string) float64 { return v.GetFloat64(key) }
  860. func (v *Viper) GetFloat64(key string) float64 {
  861. return cast.ToFloat64(v.Get(key))
  862. }
  863. // GetTime returns the value associated with the key as time.
  864. func GetTime(key string) time.Time { return v.GetTime(key) }
  865. func (v *Viper) GetTime(key string) time.Time {
  866. return cast.ToTime(v.Get(key))
  867. }
  868. // GetDuration returns the value associated with the key as a duration.
  869. func GetDuration(key string) time.Duration { return v.GetDuration(key) }
  870. func (v *Viper) GetDuration(key string) time.Duration {
  871. return cast.ToDuration(v.Get(key))
  872. }
  873. // GetIntSlice returns the value associated with the key as a slice of int values.
  874. func GetIntSlice(key string) []int { return v.GetIntSlice(key) }
  875. func (v *Viper) GetIntSlice(key string) []int {
  876. return cast.ToIntSlice(v.Get(key))
  877. }
  878. // GetStringSlice returns the value associated with the key as a slice of strings.
  879. func GetStringSlice(key string) []string { return v.GetStringSlice(key) }
  880. func (v *Viper) GetStringSlice(key string) []string {
  881. return cast.ToStringSlice(v.Get(key))
  882. }
  883. // GetStringMap returns the value associated with the key as a map of interfaces.
  884. func GetStringMap(key string) map[string]interface{} { return v.GetStringMap(key) }
  885. func (v *Viper) GetStringMap(key string) map[string]interface{} {
  886. return cast.ToStringMap(v.Get(key))
  887. }
  888. // GetStringMapString returns the value associated with the key as a map of strings.
  889. func GetStringMapString(key string) map[string]string { return v.GetStringMapString(key) }
  890. func (v *Viper) GetStringMapString(key string) map[string]string {
  891. return cast.ToStringMapString(v.Get(key))
  892. }
  893. // GetStringMapStringSlice returns the value associated with the key as a map to a slice of strings.
  894. func GetStringMapStringSlice(key string) map[string][]string { return v.GetStringMapStringSlice(key) }
  895. func (v *Viper) GetStringMapStringSlice(key string) map[string][]string {
  896. return cast.ToStringMapStringSlice(v.Get(key))
  897. }
  898. // GetSizeInBytes returns the size of the value associated with the given key
  899. // in bytes.
  900. func GetSizeInBytes(key string) uint { return v.GetSizeInBytes(key) }
  901. func (v *Viper) GetSizeInBytes(key string) uint {
  902. sizeStr := cast.ToString(v.Get(key))
  903. return parseSizeInBytes(sizeStr)
  904. }
  905. // UnmarshalKey takes a single key and unmarshals it into a Struct.
  906. func UnmarshalKey(key string, rawVal interface{}, opts ...DecoderConfigOption) error {
  907. return v.UnmarshalKey(key, rawVal, opts...)
  908. }
  909. func (v *Viper) UnmarshalKey(key string, rawVal interface{}, opts ...DecoderConfigOption) error {
  910. return decode(v.Get(key), defaultDecoderConfig(rawVal, opts...))
  911. }
  912. // Unmarshal unmarshals the config into a Struct. Make sure that the tags
  913. // on the fields of the structure are properly set.
  914. func Unmarshal(rawVal interface{}, opts ...DecoderConfigOption) error {
  915. return v.Unmarshal(rawVal, opts...)
  916. }
  917. func (v *Viper) Unmarshal(rawVal interface{}, opts ...DecoderConfigOption) error {
  918. return decode(v.AllSettings(), defaultDecoderConfig(rawVal, opts...))
  919. }
  920. // defaultDecoderConfig returns default mapsstructure.DecoderConfig with suppot
  921. // of time.Duration values & string slices
  922. func defaultDecoderConfig(output interface{}, opts ...DecoderConfigOption) *mapstructure.DecoderConfig {
  923. c := &mapstructure.DecoderConfig{
  924. Metadata: nil,
  925. Result: output,
  926. WeaklyTypedInput: true,
  927. DecodeHook: mapstructure.ComposeDecodeHookFunc(
  928. mapstructure.StringToTimeDurationHookFunc(),
  929. mapstructure.StringToSliceHookFunc(","),
  930. ),
  931. }
  932. for _, opt := range opts {
  933. opt(c)
  934. }
  935. return c
  936. }
  937. // A wrapper around mapstructure.Decode that mimics the WeakDecode functionality
  938. func decode(input interface{}, config *mapstructure.DecoderConfig) error {
  939. decoder, err := mapstructure.NewDecoder(config)
  940. if err != nil {
  941. return err
  942. }
  943. return decoder.Decode(input)
  944. }
  945. // UnmarshalExact unmarshals the config into a Struct, erroring if a field is nonexistent
  946. // in the destination struct.
  947. func UnmarshalExact(rawVal interface{}, opts ...DecoderConfigOption) error {
  948. return v.UnmarshalExact(rawVal, opts...)
  949. }
  950. func (v *Viper) UnmarshalExact(rawVal interface{}, opts ...DecoderConfigOption) error {
  951. config := defaultDecoderConfig(rawVal, opts...)
  952. config.ErrorUnused = true
  953. return decode(v.AllSettings(), config)
  954. }
  955. // BindPFlags binds a full flag set to the configuration, using each flag's long
  956. // name as the config key.
  957. func BindPFlags(flags *pflag.FlagSet) error { return v.BindPFlags(flags) }
  958. func (v *Viper) BindPFlags(flags *pflag.FlagSet) error {
  959. return v.BindFlagValues(pflagValueSet{flags})
  960. }
  961. // BindPFlag binds a specific key to a pflag (as used by cobra).
  962. // Example (where serverCmd is a Cobra instance):
  963. //
  964. // serverCmd.Flags().Int("port", 1138, "Port to run Application server on")
  965. // Viper.BindPFlag("port", serverCmd.Flags().Lookup("port"))
  966. func BindPFlag(key string, flag *pflag.Flag) error { return v.BindPFlag(key, flag) }
  967. func (v *Viper) BindPFlag(key string, flag *pflag.Flag) error {
  968. if flag == nil {
  969. return fmt.Errorf("flag for %q is nil", key)
  970. }
  971. return v.BindFlagValue(key, pflagValue{flag})
  972. }
  973. // BindFlagValues binds a full FlagValue set to the configuration, using each flag's long
  974. // name as the config key.
  975. func BindFlagValues(flags FlagValueSet) error { return v.BindFlagValues(flags) }
  976. func (v *Viper) BindFlagValues(flags FlagValueSet) (err error) {
  977. flags.VisitAll(func(flag FlagValue) {
  978. if err = v.BindFlagValue(flag.Name(), flag); err != nil {
  979. return
  980. }
  981. })
  982. return nil
  983. }
  984. // BindFlagValue binds a specific key to a FlagValue.
  985. func BindFlagValue(key string, flag FlagValue) error { return v.BindFlagValue(key, flag) }
  986. func (v *Viper) BindFlagValue(key string, flag FlagValue) error {
  987. if flag == nil {
  988. return fmt.Errorf("flag for %q is nil", key)
  989. }
  990. v.pflags[strings.ToLower(key)] = flag
  991. return nil
  992. }
  993. // BindEnv binds a Viper key to a ENV variable.
  994. // ENV variables are case sensitive.
  995. // If only a key is provided, it will use the env key matching the key, uppercased.
  996. // If more arguments are provided, they will represent the env variable names that
  997. // should bind to this key and will be taken in the specified order.
  998. // EnvPrefix will be used when set when env name is not provided.
  999. func BindEnv(input ...string) error { return v.BindEnv(input...) }
  1000. func (v *Viper) BindEnv(input ...string) error {
  1001. if len(input) == 0 {
  1002. return fmt.Errorf("missing key to bind to")
  1003. }
  1004. key := strings.ToLower(input[0])
  1005. if len(input) == 1 {
  1006. v.env[key] = append(v.env[key], v.mergeWithEnvPrefix(key))
  1007. } else {
  1008. v.env[key] = append(v.env[key], input[1:]...)
  1009. }
  1010. return nil
  1011. }
  1012. // MustBindEnv wraps BindEnv in a panic.
  1013. // If there is an error binding an environment variable, MustBindEnv will
  1014. // panic.
  1015. func MustBindEnv(input ...string) { v.MustBindEnv(input...) }
  1016. func (v *Viper) MustBindEnv(input ...string) {
  1017. if err := v.BindEnv(input...); err != nil {
  1018. panic(fmt.Sprintf("error while binding environment variable: %v", err))
  1019. }
  1020. }
  1021. // Given a key, find the value.
  1022. //
  1023. // Viper will check to see if an alias exists first.
  1024. // Viper will then check in the following order:
  1025. // flag, env, config file, key/value store.
  1026. // Lastly, if no value was found and flagDefault is true, and if the key
  1027. // corresponds to a flag, the flag's default value is returned.
  1028. //
  1029. // Note: this assumes a lower-cased key given.
  1030. func (v *Viper) find(lcaseKey string, flagDefault bool) interface{} {
  1031. var (
  1032. val interface{}
  1033. exists bool
  1034. path = strings.Split(lcaseKey, v.keyDelim)
  1035. nested = len(path) > 1
  1036. )
  1037. // compute the path through the nested maps to the nested value
  1038. if nested && v.isPathShadowedInDeepMap(path, castMapStringToMapInterface(v.aliases)) != "" {
  1039. return nil
  1040. }
  1041. // if the requested key is an alias, then return the proper key
  1042. lcaseKey = v.realKey(lcaseKey)
  1043. path = strings.Split(lcaseKey, v.keyDelim)
  1044. nested = len(path) > 1
  1045. // Set() override first
  1046. val = v.searchMap(v.override, path)
  1047. if val != nil {
  1048. return val
  1049. }
  1050. if nested && v.isPathShadowedInDeepMap(path, v.override) != "" {
  1051. return nil
  1052. }
  1053. // PFlag override next
  1054. flag, exists := v.pflags[lcaseKey]
  1055. if exists && flag.HasChanged() {
  1056. switch flag.ValueType() {
  1057. case "int", "int8", "int16", "int32", "int64":
  1058. return cast.ToInt(flag.ValueString())
  1059. case "bool":
  1060. return cast.ToBool(flag.ValueString())
  1061. case "stringSlice", "stringArray":
  1062. s := strings.TrimPrefix(flag.ValueString(), "[")
  1063. s = strings.TrimSuffix(s, "]")
  1064. res, _ := readAsCSV(s)
  1065. return res
  1066. case "intSlice":
  1067. s := strings.TrimPrefix(flag.ValueString(), "[")
  1068. s = strings.TrimSuffix(s, "]")
  1069. res, _ := readAsCSV(s)
  1070. return cast.ToIntSlice(res)
  1071. case "stringToString":
  1072. return stringToStringConv(flag.ValueString())
  1073. default:
  1074. return flag.ValueString()
  1075. }
  1076. }
  1077. if nested && v.isPathShadowedInFlatMap(path, v.pflags) != "" {
  1078. return nil
  1079. }
  1080. // Env override next
  1081. if v.automaticEnvApplied {
  1082. // even if it hasn't been registered, if automaticEnv is used,
  1083. // check any Get request
  1084. if val, ok := v.getEnv(v.mergeWithEnvPrefix(lcaseKey)); ok {
  1085. return val
  1086. }
  1087. if nested && v.isPathShadowedInAutoEnv(path) != "" {
  1088. return nil
  1089. }
  1090. }
  1091. envkeys, exists := v.env[lcaseKey]
  1092. if exists {
  1093. for _, envkey := range envkeys {
  1094. if val, ok := v.getEnv(envkey); ok {
  1095. return val
  1096. }
  1097. }
  1098. }
  1099. if nested && v.isPathShadowedInFlatMap(path, v.env) != "" {
  1100. return nil
  1101. }
  1102. // Config file next
  1103. val = v.searchIndexableWithPathPrefixes(v.config, path)
  1104. if val != nil {
  1105. return val
  1106. }
  1107. if nested && v.isPathShadowedInDeepMap(path, v.config) != "" {
  1108. return nil
  1109. }
  1110. // K/V store next
  1111. val = v.searchMap(v.kvstore, path)
  1112. if val != nil {
  1113. return val
  1114. }
  1115. if nested && v.isPathShadowedInDeepMap(path, v.kvstore) != "" {
  1116. return nil
  1117. }
  1118. // Default next
  1119. val = v.searchMap(v.defaults, path)
  1120. if val != nil {
  1121. return val
  1122. }
  1123. if nested && v.isPathShadowedInDeepMap(path, v.defaults) != "" {
  1124. return nil
  1125. }
  1126. if flagDefault {
  1127. // last chance: if no value is found and a flag does exist for the key,
  1128. // get the flag's default value even if the flag's value has not been set.
  1129. if flag, exists := v.pflags[lcaseKey]; exists {
  1130. switch flag.ValueType() {
  1131. case "int", "int8", "int16", "int32", "int64":
  1132. return cast.ToInt(flag.ValueString())
  1133. case "bool":
  1134. return cast.ToBool(flag.ValueString())
  1135. case "stringSlice", "stringArray":
  1136. s := strings.TrimPrefix(flag.ValueString(), "[")
  1137. s = strings.TrimSuffix(s, "]")
  1138. res, _ := readAsCSV(s)
  1139. return res
  1140. case "intSlice":
  1141. s := strings.TrimPrefix(flag.ValueString(), "[")
  1142. s = strings.TrimSuffix(s, "]")
  1143. res, _ := readAsCSV(s)
  1144. return cast.ToIntSlice(res)
  1145. case "stringToString":
  1146. return stringToStringConv(flag.ValueString())
  1147. default:
  1148. return flag.ValueString()
  1149. }
  1150. }
  1151. // last item, no need to check shadowing
  1152. }
  1153. return nil
  1154. }
  1155. func readAsCSV(val string) ([]string, error) {
  1156. if val == "" {
  1157. return []string{}, nil
  1158. }
  1159. stringReader := strings.NewReader(val)
  1160. csvReader := csv.NewReader(stringReader)
  1161. return csvReader.Read()
  1162. }
  1163. // mostly copied from pflag's implementation of this operation here https://github.com/spf13/pflag/blob/master/string_to_string.go#L79
  1164. // alterations are: errors are swallowed, map[string]interface{} is returned in order to enable cast.ToStringMap
  1165. func stringToStringConv(val string) interface{} {
  1166. val = strings.Trim(val, "[]")
  1167. // An empty string would cause an empty map
  1168. if len(val) == 0 {
  1169. return map[string]interface{}{}
  1170. }
  1171. r := csv.NewReader(strings.NewReader(val))
  1172. ss, err := r.Read()
  1173. if err != nil {
  1174. return nil
  1175. }
  1176. out := make(map[string]interface{}, len(ss))
  1177. for _, pair := range ss {
  1178. kv := strings.SplitN(pair, "=", 2)
  1179. if len(kv) != 2 {
  1180. return nil
  1181. }
  1182. out[kv[0]] = kv[1]
  1183. }
  1184. return out
  1185. }
  1186. // IsSet checks to see if the key has been set in any of the data locations.
  1187. // IsSet is case-insensitive for a key.
  1188. func IsSet(key string) bool { return v.IsSet(key) }
  1189. func (v *Viper) IsSet(key string) bool {
  1190. lcaseKey := strings.ToLower(key)
  1191. val := v.find(lcaseKey, false)
  1192. return val != nil
  1193. }
  1194. // AutomaticEnv makes Viper check if environment variables match any of the existing keys
  1195. // (config, default or flags). If matching env vars are found, they are loaded into Viper.
  1196. func AutomaticEnv() { v.AutomaticEnv() }
  1197. func (v *Viper) AutomaticEnv() {
  1198. v.automaticEnvApplied = true
  1199. }
  1200. // SetEnvKeyReplacer sets the strings.Replacer on the viper object
  1201. // Useful for mapping an environmental variable to a key that does
  1202. // not match it.
  1203. func SetEnvKeyReplacer(r *strings.Replacer) { v.SetEnvKeyReplacer(r) }
  1204. func (v *Viper) SetEnvKeyReplacer(r *strings.Replacer) {
  1205. v.envKeyReplacer = r
  1206. }
  1207. // RegisterAlias creates an alias that provides another accessor for the same key.
  1208. // This enables one to change a name without breaking the application.
  1209. func RegisterAlias(alias string, key string) { v.RegisterAlias(alias, key) }
  1210. func (v *Viper) RegisterAlias(alias string, key string) {
  1211. v.registerAlias(alias, strings.ToLower(key))
  1212. }
  1213. func (v *Viper) registerAlias(alias string, key string) {
  1214. alias = strings.ToLower(alias)
  1215. if alias != key && alias != v.realKey(key) {
  1216. _, exists := v.aliases[alias]
  1217. if !exists {
  1218. // if we alias something that exists in one of the maps to another
  1219. // name, we'll never be able to get that value using the original
  1220. // name, so move the config value to the new realkey.
  1221. if val, ok := v.config[alias]; ok {
  1222. delete(v.config, alias)
  1223. v.config[key] = val
  1224. }
  1225. if val, ok := v.kvstore[alias]; ok {
  1226. delete(v.kvstore, alias)
  1227. v.kvstore[key] = val
  1228. }
  1229. if val, ok := v.defaults[alias]; ok {
  1230. delete(v.defaults, alias)
  1231. v.defaults[key] = val
  1232. }
  1233. if val, ok := v.override[alias]; ok {
  1234. delete(v.override, alias)
  1235. v.override[key] = val
  1236. }
  1237. v.aliases[alias] = key
  1238. }
  1239. } else {
  1240. v.logger.Warn("creating circular reference alias", "alias", alias, "key", key, "real_key", v.realKey(key))
  1241. }
  1242. }
  1243. func (v *Viper) realKey(key string) string {
  1244. newkey, exists := v.aliases[key]
  1245. if exists {
  1246. v.logger.Debug("key is an alias", "alias", key, "to", newkey)
  1247. return v.realKey(newkey)
  1248. }
  1249. return key
  1250. }
  1251. // InConfig checks to see if the given key (or an alias) is in the config file.
  1252. func InConfig(key string) bool { return v.InConfig(key) }
  1253. func (v *Viper) InConfig(key string) bool {
  1254. lcaseKey := strings.ToLower(key)
  1255. // if the requested key is an alias, then return the proper key
  1256. lcaseKey = v.realKey(lcaseKey)
  1257. path := strings.Split(lcaseKey, v.keyDelim)
  1258. return v.searchIndexableWithPathPrefixes(v.config, path) != nil
  1259. }
  1260. // SetDefault sets the default value for this key.
  1261. // SetDefault is case-insensitive for a key.
  1262. // Default only used when no value is provided by the user via flag, config or ENV.
  1263. func SetDefault(key string, value interface{}) { v.SetDefault(key, value) }
  1264. func (v *Viper) SetDefault(key string, value interface{}) {
  1265. // If alias passed in, then set the proper default
  1266. key = v.realKey(strings.ToLower(key))
  1267. value = toCaseInsensitiveValue(value)
  1268. path := strings.Split(key, v.keyDelim)
  1269. lastKey := strings.ToLower(path[len(path)-1])
  1270. deepestMap := deepSearch(v.defaults, path[0:len(path)-1])
  1271. // set innermost value
  1272. deepestMap[lastKey] = value
  1273. }
  1274. // Set sets the value for the key in the override register.
  1275. // Set is case-insensitive for a key.
  1276. // Will be used instead of values obtained via
  1277. // flags, config file, ENV, default, or key/value store.
  1278. func Set(key string, value interface{}) { v.Set(key, value) }
  1279. func (v *Viper) Set(key string, value interface{}) {
  1280. // If alias passed in, then set the proper override
  1281. key = v.realKey(strings.ToLower(key))
  1282. value = toCaseInsensitiveValue(value)
  1283. path := strings.Split(key, v.keyDelim)
  1284. lastKey := strings.ToLower(path[len(path)-1])
  1285. deepestMap := deepSearch(v.override, path[0:len(path)-1])
  1286. // set innermost value
  1287. deepestMap[lastKey] = value
  1288. }
  1289. // ReadInConfig will discover and load the configuration file from disk
  1290. // and key/value stores, searching in one of the defined paths.
  1291. func ReadInConfig() error { return v.ReadInConfig() }
  1292. func (v *Viper) ReadInConfig() error {
  1293. v.logger.Info("attempting to read in config file")
  1294. filename, err := v.getConfigFile()
  1295. if err != nil {
  1296. return err
  1297. }
  1298. if !stringInSlice(v.getConfigType(), SupportedExts) {
  1299. return UnsupportedConfigError(v.getConfigType())
  1300. }
  1301. v.logger.Debug("reading file", "file", filename)
  1302. file, err := afero.ReadFile(v.fs, filename)
  1303. if err != nil {
  1304. return err
  1305. }
  1306. config := make(map[string]interface{})
  1307. err = v.unmarshalReader(bytes.NewReader(file), config)
  1308. if err != nil {
  1309. return err
  1310. }
  1311. v.config = config
  1312. return nil
  1313. }
  1314. // MergeInConfig merges a new configuration with an existing config.
  1315. func MergeInConfig() error { return v.MergeInConfig() }
  1316. func (v *Viper) MergeInConfig() error {
  1317. v.logger.Info("attempting to merge in config file")
  1318. filename, err := v.getConfigFile()
  1319. if err != nil {
  1320. return err
  1321. }
  1322. if !stringInSlice(v.getConfigType(), SupportedExts) {
  1323. return UnsupportedConfigError(v.getConfigType())
  1324. }
  1325. file, err := afero.ReadFile(v.fs, filename)
  1326. if err != nil {
  1327. return err
  1328. }
  1329. return v.MergeConfig(bytes.NewReader(file))
  1330. }
  1331. // ReadConfig will read a configuration file, setting existing keys to nil if the
  1332. // key does not exist in the file.
  1333. func ReadConfig(in io.Reader) error { return v.ReadConfig(in) }
  1334. func (v *Viper) ReadConfig(in io.Reader) error {
  1335. v.config = make(map[string]interface{})
  1336. return v.unmarshalReader(in, v.config)
  1337. }
  1338. // MergeConfig merges a new configuration with an existing config.
  1339. func MergeConfig(in io.Reader) error { return v.MergeConfig(in) }
  1340. func (v *Viper) MergeConfig(in io.Reader) error {
  1341. cfg := make(map[string]interface{})
  1342. if err := v.unmarshalReader(in, cfg); err != nil {
  1343. return err
  1344. }
  1345. return v.MergeConfigMap(cfg)
  1346. }
  1347. // MergeConfigMap merges the configuration from the map given with an existing config.
  1348. // Note that the map given may be modified.
  1349. func MergeConfigMap(cfg map[string]interface{}) error { return v.MergeConfigMap(cfg) }
  1350. func (v *Viper) MergeConfigMap(cfg map[string]interface{}) error {
  1351. if v.config == nil {
  1352. v.config = make(map[string]interface{})
  1353. }
  1354. insensitiviseMap(cfg)
  1355. mergeMaps(cfg, v.config, nil)
  1356. return nil
  1357. }
  1358. // WriteConfig writes the current configuration to a file.
  1359. func WriteConfig() error { return v.WriteConfig() }
  1360. func (v *Viper) WriteConfig() error {
  1361. filename, err := v.getConfigFile()
  1362. if err != nil {
  1363. return err
  1364. }
  1365. return v.writeConfig(filename, true)
  1366. }
  1367. // SafeWriteConfig writes current configuration to file only if the file does not exist.
  1368. func SafeWriteConfig() error { return v.SafeWriteConfig() }
  1369. func (v *Viper) SafeWriteConfig() error {
  1370. if len(v.configPaths) < 1 {
  1371. return errors.New("missing configuration for 'configPath'")
  1372. }
  1373. return v.SafeWriteConfigAs(filepath.Join(v.configPaths[0], v.configName+"."+v.configType))
  1374. }
  1375. // WriteConfigAs writes current configuration to a given filename.
  1376. func WriteConfigAs(filename string) error { return v.WriteConfigAs(filename) }
  1377. func (v *Viper) WriteConfigAs(filename string) error {
  1378. return v.writeConfig(filename, true)
  1379. }
  1380. // SafeWriteConfigAs writes current configuration to a given filename if it does not exist.
  1381. func SafeWriteConfigAs(filename string) error { return v.SafeWriteConfigAs(filename) }
  1382. func (v *Viper) SafeWriteConfigAs(filename string) error {
  1383. alreadyExists, err := afero.Exists(v.fs, filename)
  1384. if alreadyExists && err == nil {
  1385. return ConfigFileAlreadyExistsError(filename)
  1386. }
  1387. return v.writeConfig(filename, false)
  1388. }
  1389. func (v *Viper) writeConfig(filename string, force bool) error {
  1390. v.logger.Info("attempting to write configuration to file")
  1391. var configType string
  1392. ext := filepath.Ext(filename)
  1393. if ext != "" && ext != filepath.Base(filename) {
  1394. configType = ext[1:]
  1395. } else {
  1396. configType = v.configType
  1397. }
  1398. if configType == "" {
  1399. return fmt.Errorf("config type could not be determined for %s", filename)
  1400. }
  1401. if !stringInSlice(configType, SupportedExts) {
  1402. return UnsupportedConfigError(configType)
  1403. }
  1404. if v.config == nil {
  1405. v.config = make(map[string]interface{})
  1406. }
  1407. flags := os.O_CREATE | os.O_TRUNC | os.O_WRONLY
  1408. if !force {
  1409. flags |= os.O_EXCL
  1410. }
  1411. f, err := v.fs.OpenFile(filename, flags, v.configPermissions)
  1412. if err != nil {
  1413. return err
  1414. }
  1415. defer f.Close()
  1416. if err := v.marshalWriter(f, configType); err != nil {
  1417. return err
  1418. }
  1419. return f.Sync()
  1420. }
  1421. // Unmarshal a Reader into a map.
  1422. // Should probably be an unexported function.
  1423. func unmarshalReader(in io.Reader, c map[string]interface{}) error {
  1424. return v.unmarshalReader(in, c)
  1425. }
  1426. func (v *Viper) unmarshalReader(in io.Reader, c map[string]interface{}) error {
  1427. buf := new(bytes.Buffer)
  1428. buf.ReadFrom(in)
  1429. switch format := strings.ToLower(v.getConfigType()); format {
  1430. case "yaml", "yml", "json", "toml", "hcl", "tfvars", "ini", "properties", "props", "prop", "dotenv", "env":
  1431. err := v.decoderRegistry.Decode(format, buf.Bytes(), c)
  1432. if err != nil {
  1433. return ConfigParseError{err}
  1434. }
  1435. }
  1436. insensitiviseMap(c)
  1437. return nil
  1438. }
  1439. // Marshal a map into Writer.
  1440. func (v *Viper) marshalWriter(f afero.File, configType string) error {
  1441. c := v.AllSettings()
  1442. switch configType {
  1443. case "yaml", "yml", "json", "toml", "hcl", "tfvars", "ini", "prop", "props", "properties", "dotenv", "env":
  1444. b, err := v.encoderRegistry.Encode(configType, c)
  1445. if err != nil {
  1446. return ConfigMarshalError{err}
  1447. }
  1448. _, err = f.WriteString(string(b))
  1449. if err != nil {
  1450. return ConfigMarshalError{err}
  1451. }
  1452. }
  1453. return nil
  1454. }
  1455. func keyExists(k string, m map[string]interface{}) string {
  1456. lk := strings.ToLower(k)
  1457. for mk := range m {
  1458. lmk := strings.ToLower(mk)
  1459. if lmk == lk {
  1460. return mk
  1461. }
  1462. }
  1463. return ""
  1464. }
  1465. func castToMapStringInterface(
  1466. src map[interface{}]interface{},
  1467. ) map[string]interface{} {
  1468. tgt := map[string]interface{}{}
  1469. for k, v := range src {
  1470. tgt[fmt.Sprintf("%v", k)] = v
  1471. }
  1472. return tgt
  1473. }
  1474. func castMapStringSliceToMapInterface(src map[string][]string) map[string]interface{} {
  1475. tgt := map[string]interface{}{}
  1476. for k, v := range src {
  1477. tgt[k] = v
  1478. }
  1479. return tgt
  1480. }
  1481. func castMapStringToMapInterface(src map[string]string) map[string]interface{} {
  1482. tgt := map[string]interface{}{}
  1483. for k, v := range src {
  1484. tgt[k] = v
  1485. }
  1486. return tgt
  1487. }
  1488. func castMapFlagToMapInterface(src map[string]FlagValue) map[string]interface{} {
  1489. tgt := map[string]interface{}{}
  1490. for k, v := range src {
  1491. tgt[k] = v
  1492. }
  1493. return tgt
  1494. }
  1495. // mergeMaps merges two maps. The `itgt` parameter is for handling go-yaml's
  1496. // insistence on parsing nested structures as `map[interface{}]interface{}`
  1497. // instead of using a `string` as the key for nest structures beyond one level
  1498. // deep. Both map types are supported as there is a go-yaml fork that uses
  1499. // `map[string]interface{}` instead.
  1500. func mergeMaps(
  1501. src, tgt map[string]interface{}, itgt map[interface{}]interface{},
  1502. ) {
  1503. for sk, sv := range src {
  1504. tk := keyExists(sk, tgt)
  1505. if tk == "" {
  1506. v.logger.Trace("", "tk", "\"\"", fmt.Sprintf("tgt[%s]", sk), sv)
  1507. tgt[sk] = sv
  1508. if itgt != nil {
  1509. itgt[sk] = sv
  1510. }
  1511. continue
  1512. }
  1513. tv, ok := tgt[tk]
  1514. if !ok {
  1515. v.logger.Trace("", fmt.Sprintf("ok[%s]", tk), false, fmt.Sprintf("tgt[%s]", sk), sv)
  1516. tgt[sk] = sv
  1517. if itgt != nil {
  1518. itgt[sk] = sv
  1519. }
  1520. continue
  1521. }
  1522. svType := reflect.TypeOf(sv)
  1523. tvType := reflect.TypeOf(tv)
  1524. v.logger.Trace(
  1525. "processing",
  1526. "key", sk,
  1527. "st", svType,
  1528. "tt", tvType,
  1529. "sv", sv,
  1530. "tv", tv,
  1531. )
  1532. switch ttv := tv.(type) {
  1533. case map[interface{}]interface{}:
  1534. v.logger.Trace("merging maps (must convert)")
  1535. tsv, ok := sv.(map[interface{}]interface{})
  1536. if !ok {
  1537. v.logger.Error(
  1538. "Could not cast sv to map[interface{}]interface{}",
  1539. "key", sk,
  1540. "st", svType,
  1541. "tt", tvType,
  1542. "sv", sv,
  1543. "tv", tv,
  1544. )
  1545. continue
  1546. }
  1547. ssv := castToMapStringInterface(tsv)
  1548. stv := castToMapStringInterface(ttv)
  1549. mergeMaps(ssv, stv, ttv)
  1550. case map[string]interface{}:
  1551. v.logger.Trace("merging maps")
  1552. tsv, ok := sv.(map[string]interface{})
  1553. if !ok {
  1554. v.logger.Error(
  1555. "Could not cast sv to map[string]interface{}",
  1556. "key", sk,
  1557. "st", svType,
  1558. "tt", tvType,
  1559. "sv", sv,
  1560. "tv", tv,
  1561. )
  1562. continue
  1563. }
  1564. mergeMaps(tsv, ttv, nil)
  1565. default:
  1566. v.logger.Trace("setting value")
  1567. tgt[tk] = sv
  1568. if itgt != nil {
  1569. itgt[tk] = sv
  1570. }
  1571. }
  1572. }
  1573. }
  1574. // ReadRemoteConfig attempts to get configuration from a remote source
  1575. // and read it in the remote configuration registry.
  1576. func ReadRemoteConfig() error { return v.ReadRemoteConfig() }
  1577. func (v *Viper) ReadRemoteConfig() error {
  1578. return v.getKeyValueConfig()
  1579. }
  1580. func WatchRemoteConfig() error { return v.WatchRemoteConfig() }
  1581. func (v *Viper) WatchRemoteConfig() error {
  1582. return v.watchKeyValueConfig()
  1583. }
  1584. func (v *Viper) WatchRemoteConfigOnChannel() error {
  1585. return v.watchKeyValueConfigOnChannel()
  1586. }
  1587. // Retrieve the first found remote configuration.
  1588. func (v *Viper) getKeyValueConfig() error {
  1589. if RemoteConfig == nil {
  1590. return RemoteConfigError("Enable the remote features by doing a blank import of the viper/remote package: '_ github.com/spf13/viper/remote'")
  1591. }
  1592. if len(v.remoteProviders) == 0 {
  1593. return RemoteConfigError("No Remote Providers")
  1594. }
  1595. for _, rp := range v.remoteProviders {
  1596. val, err := v.getRemoteConfig(rp)
  1597. if err != nil {
  1598. v.logger.Error(fmt.Errorf("get remote config: %w", err).Error())
  1599. continue
  1600. }
  1601. v.kvstore = val
  1602. return nil
  1603. }
  1604. return RemoteConfigError("No Files Found")
  1605. }
  1606. func (v *Viper) getRemoteConfig(provider RemoteProvider) (map[string]interface{}, error) {
  1607. reader, err := RemoteConfig.Get(provider)
  1608. if err != nil {
  1609. return nil, err
  1610. }
  1611. err = v.unmarshalReader(reader, v.kvstore)
  1612. return v.kvstore, err
  1613. }
  1614. // Retrieve the first found remote configuration.
  1615. func (v *Viper) watchKeyValueConfigOnChannel() error {
  1616. if len(v.remoteProviders) == 0 {
  1617. return RemoteConfigError("No Remote Providers")
  1618. }
  1619. for _, rp := range v.remoteProviders {
  1620. respc, _ := RemoteConfig.WatchChannel(rp)
  1621. // Todo: Add quit channel
  1622. go func(rc <-chan *RemoteResponse) {
  1623. for {
  1624. b := <-rc
  1625. reader := bytes.NewReader(b.Value)
  1626. v.unmarshalReader(reader, v.kvstore)
  1627. }
  1628. }(respc)
  1629. return nil
  1630. }
  1631. return RemoteConfigError("No Files Found")
  1632. }
  1633. // Retrieve the first found remote configuration.
  1634. func (v *Viper) watchKeyValueConfig() error {
  1635. if len(v.remoteProviders) == 0 {
  1636. return RemoteConfigError("No Remote Providers")
  1637. }
  1638. for _, rp := range v.remoteProviders {
  1639. val, err := v.watchRemoteConfig(rp)
  1640. if err != nil {
  1641. v.logger.Error(fmt.Errorf("watch remote config: %w", err).Error())
  1642. continue
  1643. }
  1644. v.kvstore = val
  1645. return nil
  1646. }
  1647. return RemoteConfigError("No Files Found")
  1648. }
  1649. func (v *Viper) watchRemoteConfig(provider RemoteProvider) (map[string]interface{}, error) {
  1650. reader, err := RemoteConfig.Watch(provider)
  1651. if err != nil {
  1652. return nil, err
  1653. }
  1654. err = v.unmarshalReader(reader, v.kvstore)
  1655. return v.kvstore, err
  1656. }
  1657. // AllKeys returns all keys holding a value, regardless of where they are set.
  1658. // Nested keys are returned with a v.keyDelim separator
  1659. func AllKeys() []string { return v.AllKeys() }
  1660. func (v *Viper) AllKeys() []string {
  1661. m := map[string]bool{}
  1662. // add all paths, by order of descending priority to ensure correct shadowing
  1663. m = v.flattenAndMergeMap(m, castMapStringToMapInterface(v.aliases), "")
  1664. m = v.flattenAndMergeMap(m, v.override, "")
  1665. m = v.mergeFlatMap(m, castMapFlagToMapInterface(v.pflags))
  1666. m = v.mergeFlatMap(m, castMapStringSliceToMapInterface(v.env))
  1667. m = v.flattenAndMergeMap(m, v.config, "")
  1668. m = v.flattenAndMergeMap(m, v.kvstore, "")
  1669. m = v.flattenAndMergeMap(m, v.defaults, "")
  1670. // convert set of paths to list
  1671. a := make([]string, 0, len(m))
  1672. for x := range m {
  1673. a = append(a, x)
  1674. }
  1675. return a
  1676. }
  1677. // flattenAndMergeMap recursively flattens the given map into a map[string]bool
  1678. // of key paths (used as a set, easier to manipulate than a []string):
  1679. // - each path is merged into a single key string, delimited with v.keyDelim
  1680. // - if a path is shadowed by an earlier value in the initial shadow map,
  1681. // it is skipped.
  1682. //
  1683. // The resulting set of paths is merged to the given shadow set at the same time.
  1684. func (v *Viper) flattenAndMergeMap(shadow map[string]bool, m map[string]interface{}, prefix string) map[string]bool {
  1685. if shadow != nil && prefix != "" && shadow[prefix] {
  1686. // prefix is shadowed => nothing more to flatten
  1687. return shadow
  1688. }
  1689. if shadow == nil {
  1690. shadow = make(map[string]bool)
  1691. }
  1692. var m2 map[string]interface{}
  1693. if prefix != "" {
  1694. prefix += v.keyDelim
  1695. }
  1696. for k, val := range m {
  1697. fullKey := prefix + k
  1698. switch val.(type) {
  1699. case map[string]interface{}:
  1700. m2 = val.(map[string]interface{})
  1701. case map[interface{}]interface{}:
  1702. m2 = cast.ToStringMap(val)
  1703. default:
  1704. // immediate value
  1705. shadow[strings.ToLower(fullKey)] = true
  1706. continue
  1707. }
  1708. // recursively merge to shadow map
  1709. shadow = v.flattenAndMergeMap(shadow, m2, fullKey)
  1710. }
  1711. return shadow
  1712. }
  1713. // mergeFlatMap merges the given maps, excluding values of the second map
  1714. // shadowed by values from the first map.
  1715. func (v *Viper) mergeFlatMap(shadow map[string]bool, m map[string]interface{}) map[string]bool {
  1716. // scan keys
  1717. outer:
  1718. for k := range m {
  1719. path := strings.Split(k, v.keyDelim)
  1720. // scan intermediate paths
  1721. var parentKey string
  1722. for i := 1; i < len(path); i++ {
  1723. parentKey = strings.Join(path[0:i], v.keyDelim)
  1724. if shadow[parentKey] {
  1725. // path is shadowed, continue
  1726. continue outer
  1727. }
  1728. }
  1729. // add key
  1730. shadow[strings.ToLower(k)] = true
  1731. }
  1732. return shadow
  1733. }
  1734. // AllSettings merges all settings and returns them as a map[string]interface{}.
  1735. func AllSettings() map[string]interface{} { return v.AllSettings() }
  1736. func (v *Viper) AllSettings() map[string]interface{} {
  1737. m := map[string]interface{}{}
  1738. // start from the list of keys, and construct the map one value at a time
  1739. for _, k := range v.AllKeys() {
  1740. value := v.Get(k)
  1741. if value == nil {
  1742. // should not happen, since AllKeys() returns only keys holding a value,
  1743. // check just in case anything changes
  1744. continue
  1745. }
  1746. path := strings.Split(k, v.keyDelim)
  1747. lastKey := strings.ToLower(path[len(path)-1])
  1748. deepestMap := deepSearch(m, path[0:len(path)-1])
  1749. // set innermost value
  1750. deepestMap[lastKey] = value
  1751. }
  1752. return m
  1753. }
  1754. // SetFs sets the filesystem to use to read configuration.
  1755. func SetFs(fs afero.Fs) { v.SetFs(fs) }
  1756. func (v *Viper) SetFs(fs afero.Fs) {
  1757. v.fs = fs
  1758. }
  1759. // SetConfigName sets name for the config file.
  1760. // Does not include extension.
  1761. func SetConfigName(in string) { v.SetConfigName(in) }
  1762. func (v *Viper) SetConfigName(in string) {
  1763. if in != "" {
  1764. v.configName = in
  1765. v.configFile = ""
  1766. }
  1767. }
  1768. // SetConfigType sets the type of the configuration returned by the
  1769. // remote source, e.g. "json".
  1770. func SetConfigType(in string) { v.SetConfigType(in) }
  1771. func (v *Viper) SetConfigType(in string) {
  1772. if in != "" {
  1773. v.configType = in
  1774. }
  1775. }
  1776. // SetConfigPermissions sets the permissions for the config file.
  1777. func SetConfigPermissions(perm os.FileMode) { v.SetConfigPermissions(perm) }
  1778. func (v *Viper) SetConfigPermissions(perm os.FileMode) {
  1779. v.configPermissions = perm.Perm()
  1780. }
  1781. // IniLoadOptions sets the load options for ini parsing.
  1782. func IniLoadOptions(in ini.LoadOptions) Option {
  1783. return optionFunc(func(v *Viper) {
  1784. v.iniLoadOptions = in
  1785. })
  1786. }
  1787. func (v *Viper) getConfigType() string {
  1788. if v.configType != "" {
  1789. return v.configType
  1790. }
  1791. cf, err := v.getConfigFile()
  1792. if err != nil {
  1793. return ""
  1794. }
  1795. ext := filepath.Ext(cf)
  1796. if len(ext) > 1 {
  1797. return ext[1:]
  1798. }
  1799. return ""
  1800. }
  1801. func (v *Viper) getConfigFile() (string, error) {
  1802. if v.configFile == "" {
  1803. cf, err := v.findConfigFile()
  1804. if err != nil {
  1805. return "", err
  1806. }
  1807. v.configFile = cf
  1808. }
  1809. return v.configFile, nil
  1810. }
  1811. // Debug prints all configuration registries for debugging
  1812. // purposes.
  1813. func Debug() { v.Debug() }
  1814. func DebugTo(w io.Writer) { v.DebugTo(w) }
  1815. func (v *Viper) Debug() { v.DebugTo(os.Stdout) }
  1816. func (v *Viper) DebugTo(w io.Writer) {
  1817. fmt.Fprintf(w, "Aliases:\n%#v\n", v.aliases)
  1818. fmt.Fprintf(w, "Override:\n%#v\n", v.override)
  1819. fmt.Fprintf(w, "PFlags:\n%#v\n", v.pflags)
  1820. fmt.Fprintf(w, "Env:\n%#v\n", v.env)
  1821. fmt.Fprintf(w, "Key/Value Store:\n%#v\n", v.kvstore)
  1822. fmt.Fprintf(w, "Config:\n%#v\n", v.config)
  1823. fmt.Fprintf(w, "Defaults:\n%#v\n", v.defaults)
  1824. }