viper.go 58 KB

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