viper.go 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316
  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 a 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. "fmt"
  20. "io"
  21. "log"
  22. "os"
  23. "path/filepath"
  24. "reflect"
  25. "strings"
  26. "time"
  27. "github.com/fsnotify/fsnotify"
  28. "github.com/mitchellh/mapstructure"
  29. "github.com/spf13/afero"
  30. "github.com/spf13/cast"
  31. jww "github.com/spf13/jwalterweatherman"
  32. "github.com/spf13/pflag"
  33. )
  34. var v *Viper
  35. func init() {
  36. v = New()
  37. }
  38. type remoteConfigFactory interface {
  39. Get(rp RemoteProvider) (io.Reader, error)
  40. Watch(rp RemoteProvider) (io.Reader, error)
  41. }
  42. // RemoteConfig is optional, see the remote package
  43. var RemoteConfig remoteConfigFactory
  44. // Denotes encountering an unsupported
  45. // configuration filetype.
  46. type UnsupportedConfigError string
  47. // Returns the formatted configuration error.
  48. func (str UnsupportedConfigError) Error() string {
  49. return fmt.Sprintf("Unsupported Config Type %q", string(str))
  50. }
  51. // Denotes encountering an unsupported remote
  52. // provider. Currently only etcd and Consul are
  53. // supported.
  54. type UnsupportedRemoteProviderError string
  55. // Returns the formatted remote provider error.
  56. func (str UnsupportedRemoteProviderError) Error() string {
  57. return fmt.Sprintf("Unsupported Remote Provider Type %q", string(str))
  58. }
  59. // Denotes encountering an error while trying to
  60. // pull the configuration from the remote provider.
  61. type RemoteConfigError string
  62. // Returns the formatted remote provider error
  63. func (rce RemoteConfigError) Error() string {
  64. return fmt.Sprintf("Remote Configurations Error: %s", string(rce))
  65. }
  66. // Denotes failing to find configuration file.
  67. type ConfigFileNotFoundError struct {
  68. name, locations string
  69. }
  70. // Returns the formatted configuration error.
  71. func (fnfe ConfigFileNotFoundError) Error() string {
  72. return fmt.Sprintf("Config File %q Not Found in %q", fnfe.name, fnfe.locations)
  73. }
  74. // Viper is a prioritized configuration registry. It
  75. // maintains a set of configuration sources, fetches
  76. // values to populate those, and provides them according
  77. // to the source's priority.
  78. // The priority of the sources is the following:
  79. // 1. overrides
  80. // 2. flags
  81. // 3. env. variables
  82. // 4. config file
  83. // 5. key/value store
  84. // 6. defaults
  85. //
  86. // For example, if values from the following sources were loaded:
  87. //
  88. // Defaults : {
  89. // "secret": "",
  90. // "user": "default",
  91. // "endpoint": "https://localhost"
  92. // }
  93. // Config : {
  94. // "user": "root"
  95. // "secret": "defaultsecret"
  96. // }
  97. // Env : {
  98. // "secret": "somesecretkey"
  99. // }
  100. //
  101. // The resulting config will have the following values:
  102. //
  103. // {
  104. // "secret": "somesecretkey",
  105. // "user": "root",
  106. // "endpoint": "https://localhost"
  107. // }
  108. type Viper struct {
  109. // Delimiter that separates a list of keys
  110. // used to access a nested value in one go
  111. keyDelim string
  112. // A set of paths to look for the config file in
  113. configPaths []string
  114. // The filesystem to read config from.
  115. fs afero.Fs
  116. // A set of remote providers to search for the configuration
  117. remoteProviders []*defaultRemoteProvider
  118. // Name of file to look for inside the path
  119. configName string
  120. configFile string
  121. configType string
  122. envPrefix string
  123. automaticEnvApplied bool
  124. envKeyReplacer *strings.Replacer
  125. config map[string]interface{}
  126. override map[string]interface{}
  127. defaults map[string]interface{}
  128. kvstore map[string]interface{}
  129. pflags map[string]FlagValue
  130. env map[string]string
  131. aliases map[string]string
  132. typeByDefValue bool
  133. onConfigChange func(fsnotify.Event)
  134. }
  135. // Returns an initialized Viper instance.
  136. func New() *Viper {
  137. v := new(Viper)
  138. v.keyDelim = "."
  139. v.configName = "config"
  140. v.fs = afero.NewOsFs()
  141. v.config = make(map[string]interface{})
  142. v.override = make(map[string]interface{})
  143. v.defaults = make(map[string]interface{})
  144. v.kvstore = make(map[string]interface{})
  145. v.pflags = make(map[string]FlagValue)
  146. v.env = make(map[string]string)
  147. v.aliases = make(map[string]string)
  148. v.typeByDefValue = false
  149. return v
  150. }
  151. // Intended for testing, will reset all to default settings.
  152. // In the public interface for the viper package so applications
  153. // can use it in their testing as well.
  154. func Reset() {
  155. v = New()
  156. SupportedExts = []string{"json", "toml", "yaml", "yml", "hcl"}
  157. SupportedRemoteProviders = []string{"etcd", "consul"}
  158. }
  159. type defaultRemoteProvider struct {
  160. provider string
  161. endpoint string
  162. path string
  163. secretKeyring string
  164. }
  165. func (rp defaultRemoteProvider) Provider() string {
  166. return rp.provider
  167. }
  168. func (rp defaultRemoteProvider) Endpoint() string {
  169. return rp.endpoint
  170. }
  171. func (rp defaultRemoteProvider) Path() string {
  172. return rp.path
  173. }
  174. func (rp defaultRemoteProvider) SecretKeyring() string {
  175. return rp.secretKeyring
  176. }
  177. // RemoteProvider stores the configuration necessary
  178. // to connect to a remote key/value store.
  179. // Optional secretKeyring to unencrypt encrypted values
  180. // can be provided.
  181. type RemoteProvider interface {
  182. Provider() string
  183. Endpoint() string
  184. Path() string
  185. SecretKeyring() string
  186. }
  187. // Universally supported extensions.
  188. var SupportedExts []string = []string{"json", "toml", "yaml", "yml", "properties", "props", "prop", "hcl"}
  189. // Universally supported remote providers.
  190. var SupportedRemoteProviders []string = []string{"etcd", "consul"}
  191. func OnConfigChange(run func(in fsnotify.Event)) { v.OnConfigChange(run) }
  192. func (v *Viper) OnConfigChange(run func(in fsnotify.Event)) {
  193. v.onConfigChange = run
  194. }
  195. func WatchConfig() { v.WatchConfig() }
  196. func (v *Viper) WatchConfig() {
  197. go func() {
  198. watcher, err := fsnotify.NewWatcher()
  199. if err != nil {
  200. log.Fatal(err)
  201. }
  202. defer watcher.Close()
  203. // we have to watch the entire directory to pick up renames/atomic saves in a cross-platform way
  204. configFile := filepath.Clean(v.getConfigFile())
  205. configDir, _ := filepath.Split(configFile)
  206. done := make(chan bool)
  207. go func() {
  208. for {
  209. select {
  210. case event := <-watcher.Events:
  211. // we only care about the config file
  212. if filepath.Clean(event.Name) == configFile {
  213. if event.Op&fsnotify.Write == fsnotify.Write || event.Op&fsnotify.Create == fsnotify.Create {
  214. err := v.ReadInConfig()
  215. if err != nil {
  216. log.Println("error:", err)
  217. }
  218. v.onConfigChange(event)
  219. }
  220. }
  221. case err := <-watcher.Errors:
  222. log.Println("error:", err)
  223. }
  224. }
  225. }()
  226. watcher.Add(configDir)
  227. <-done
  228. }()
  229. }
  230. // Explicitly define the path, name and extension of the config file
  231. // Viper will use this and not check any of the config paths
  232. func SetConfigFile(in string) { v.SetConfigFile(in) }
  233. func (v *Viper) SetConfigFile(in string) {
  234. if in != "" {
  235. v.configFile = in
  236. }
  237. }
  238. // Define a prefix that ENVIRONMENT variables will use.
  239. // E.g. if your prefix is "spf", the env registry
  240. // will look for env. variables that start with "SPF_"
  241. func SetEnvPrefix(in string) { v.SetEnvPrefix(in) }
  242. func (v *Viper) SetEnvPrefix(in string) {
  243. if in != "" {
  244. v.envPrefix = in
  245. }
  246. }
  247. func (v *Viper) mergeWithEnvPrefix(in string) string {
  248. if v.envPrefix != "" {
  249. return strings.ToUpper(v.envPrefix + "_" + in)
  250. }
  251. return strings.ToUpper(in)
  252. }
  253. // TODO: should getEnv logic be moved into find(). Can generalize the use of
  254. // rewriting keys many things, Ex: Get('someKey') -> some_key
  255. // (cammel case to snake case for JSON keys perhaps)
  256. // getEnv s a wrapper around os.Getenv which replaces characters in the original
  257. // key. This allows env vars which have different keys then the config object
  258. // keys
  259. func (v *Viper) getEnv(key string) string {
  260. if v.envKeyReplacer != nil {
  261. key = v.envKeyReplacer.Replace(key)
  262. }
  263. return os.Getenv(key)
  264. }
  265. // Return the file used to populate the config registry
  266. func ConfigFileUsed() string { return v.ConfigFileUsed() }
  267. func (v *Viper) ConfigFileUsed() string { return v.configFile }
  268. // Add a path for Viper to search for the config file in.
  269. // Can be called multiple times to define multiple search paths.
  270. func AddConfigPath(in string) { v.AddConfigPath(in) }
  271. func (v *Viper) AddConfigPath(in string) {
  272. if in != "" {
  273. absin := absPathify(in)
  274. jww.INFO.Println("adding", absin, "to paths to search")
  275. if !stringInSlice(absin, v.configPaths) {
  276. v.configPaths = append(v.configPaths, absin)
  277. }
  278. }
  279. }
  280. // AddRemoteProvider adds a remote configuration source.
  281. // Remote Providers are searched in the order they are added.
  282. // provider is a string value, "etcd" or "consul" are currently supported.
  283. // endpoint is the url. etcd requires http://ip:port consul requires ip:port
  284. // path is the path in the k/v store to retrieve configuration
  285. // To retrieve a config file called myapp.json from /configs/myapp.json
  286. // you should set path to /configs and set config name (SetConfigName()) to
  287. // "myapp"
  288. func AddRemoteProvider(provider, endpoint, path string) error {
  289. return v.AddRemoteProvider(provider, endpoint, path)
  290. }
  291. func (v *Viper) AddRemoteProvider(provider, endpoint, path string) error {
  292. if !stringInSlice(provider, SupportedRemoteProviders) {
  293. return UnsupportedRemoteProviderError(provider)
  294. }
  295. if provider != "" && endpoint != "" {
  296. jww.INFO.Printf("adding %s:%s to remote provider list", provider, endpoint)
  297. rp := &defaultRemoteProvider{
  298. endpoint: endpoint,
  299. provider: provider,
  300. path: path,
  301. }
  302. if !v.providerPathExists(rp) {
  303. v.remoteProviders = append(v.remoteProviders, rp)
  304. }
  305. }
  306. return nil
  307. }
  308. // AddSecureRemoteProvider adds a remote configuration source.
  309. // Secure Remote Providers are searched in the order they are added.
  310. // provider is a string value, "etcd" or "consul" are currently supported.
  311. // endpoint is the url. etcd requires http://ip:port consul requires ip:port
  312. // secretkeyring is the filepath to your openpgp secret keyring. e.g. /etc/secrets/myring.gpg
  313. // path is the path in the k/v store to retrieve configuration
  314. // To retrieve a config file called myapp.json from /configs/myapp.json
  315. // you should set path to /configs and set config name (SetConfigName()) to
  316. // "myapp"
  317. // Secure Remote Providers are implemented with github.com/xordataexchange/crypt
  318. func AddSecureRemoteProvider(provider, endpoint, path, secretkeyring string) error {
  319. return v.AddSecureRemoteProvider(provider, endpoint, path, secretkeyring)
  320. }
  321. func (v *Viper) AddSecureRemoteProvider(provider, endpoint, path, secretkeyring string) error {
  322. if !stringInSlice(provider, SupportedRemoteProviders) {
  323. return UnsupportedRemoteProviderError(provider)
  324. }
  325. if provider != "" && endpoint != "" {
  326. jww.INFO.Printf("adding %s:%s to remote provider list", provider, endpoint)
  327. rp := &defaultRemoteProvider{
  328. endpoint: endpoint,
  329. provider: provider,
  330. path: path,
  331. secretKeyring: secretkeyring,
  332. }
  333. if !v.providerPathExists(rp) {
  334. v.remoteProviders = append(v.remoteProviders, rp)
  335. }
  336. }
  337. return nil
  338. }
  339. func (v *Viper) providerPathExists(p *defaultRemoteProvider) bool {
  340. for _, y := range v.remoteProviders {
  341. if reflect.DeepEqual(y, p) {
  342. return true
  343. }
  344. }
  345. return false
  346. }
  347. func (v *Viper) searchMap(source map[string]interface{}, path []string) interface{} {
  348. if len(path) == 0 {
  349. return source
  350. }
  351. var ok bool
  352. var next interface{}
  353. for k, v := range source {
  354. if strings.ToLower(k) == strings.ToLower(path[0]) {
  355. ok = true
  356. next = v
  357. break
  358. }
  359. }
  360. if ok {
  361. switch next.(type) {
  362. case map[interface{}]interface{}:
  363. return v.searchMap(cast.ToStringMap(next), path[1:])
  364. case map[string]interface{}:
  365. // Type assertion is safe here since it is only reached
  366. // if the type of `next` is the same as the type being asserted
  367. return v.searchMap(next.(map[string]interface{}), path[1:])
  368. default:
  369. return next
  370. }
  371. } else {
  372. return nil
  373. }
  374. }
  375. // SetTypeByDefaultValue enables or disables the inference of a key value's
  376. // type when the Get function is used based upon a key's default value as
  377. // opposed to the value returned based on the normal fetch logic.
  378. //
  379. // For example, if a key has a default value of []string{} and the same key
  380. // is set via an environment variable to "a b c", a call to the Get function
  381. // would return a string slice for the key if the key's type is inferred by
  382. // the default value and the Get function would return:
  383. //
  384. // []string {"a", "b", "c"}
  385. //
  386. // Otherwise the Get function would return:
  387. //
  388. // "a b c"
  389. func SetTypeByDefaultValue(enable bool) { v.SetTypeByDefaultValue(enable) }
  390. func (v *Viper) SetTypeByDefaultValue(enable bool) {
  391. v.typeByDefValue = enable
  392. }
  393. // GetViper gets the global Viper instance.
  394. func GetViper() *Viper {
  395. return v
  396. }
  397. // Viper is essentially repository for configurations
  398. // Get can retrieve any value given the key to use
  399. // Get has the behavior of returning the value associated with the first
  400. // place from where it is set. Viper will check in the following order:
  401. // override, flag, env, config file, key/value store, default
  402. //
  403. // Get returns an interface. For a specific value use one of the Get____ methods.
  404. func Get(key string) interface{} { return v.Get(key) }
  405. func (v *Viper) Get(key string) interface{} {
  406. path := strings.Split(key, v.keyDelim)
  407. lcaseKey := strings.ToLower(key)
  408. val := v.find(lcaseKey)
  409. if val == nil {
  410. source := v.find(strings.ToLower(path[0]))
  411. if source != nil {
  412. if reflect.TypeOf(source).Kind() == reflect.Map {
  413. val = v.searchMap(cast.ToStringMap(source), path[1:])
  414. }
  415. }
  416. }
  417. // if no other value is returned and a flag does exist for the value,
  418. // get the flag's value even if the flag's value has not changed
  419. if val == nil {
  420. if flag, exists := v.pflags[lcaseKey]; exists {
  421. jww.TRACE.Println(key, "get pflag default", val)
  422. switch flag.ValueType() {
  423. case "int", "int8", "int16", "int32", "int64":
  424. val = cast.ToInt(flag.ValueString())
  425. case "bool":
  426. val = cast.ToBool(flag.ValueString())
  427. default:
  428. val = flag.ValueString()
  429. }
  430. }
  431. }
  432. if val == nil {
  433. return nil
  434. }
  435. var valType interface{}
  436. if !v.typeByDefValue {
  437. valType = val
  438. } else {
  439. defVal, defExists := v.defaults[lcaseKey]
  440. if defExists {
  441. valType = defVal
  442. } else {
  443. valType = val
  444. }
  445. }
  446. switch valType.(type) {
  447. case bool:
  448. return cast.ToBool(val)
  449. case string:
  450. return cast.ToString(val)
  451. case int64, int32, int16, int8, int:
  452. return cast.ToInt(val)
  453. case float64, float32:
  454. return cast.ToFloat64(val)
  455. case time.Time:
  456. return cast.ToTime(val)
  457. case time.Duration:
  458. return cast.ToDuration(val)
  459. case []string:
  460. return cast.ToStringSlice(val)
  461. }
  462. return val
  463. }
  464. // Returns new Viper instance representing a sub tree of this instance
  465. func Sub(key string) *Viper { return v.Sub(key) }
  466. func (v *Viper) Sub(key string) *Viper {
  467. subv := New()
  468. data := v.Get(key)
  469. if reflect.TypeOf(data).Kind() == reflect.Map {
  470. subv.config = cast.ToStringMap(data)
  471. return subv
  472. } else {
  473. return nil
  474. }
  475. }
  476. // Returns the value associated with the key as a string
  477. func GetString(key string) string { return v.GetString(key) }
  478. func (v *Viper) GetString(key string) string {
  479. return cast.ToString(v.Get(key))
  480. }
  481. // Returns the value associated with the key as a boolean
  482. func GetBool(key string) bool { return v.GetBool(key) }
  483. func (v *Viper) GetBool(key string) bool {
  484. return cast.ToBool(v.Get(key))
  485. }
  486. // Returns the value associated with the key as an integer
  487. func GetInt(key string) int { return v.GetInt(key) }
  488. func (v *Viper) GetInt(key string) int {
  489. return cast.ToInt(v.Get(key))
  490. }
  491. // Returns the value associated with the key as an integer
  492. func GetInt64(key string) int64 { return v.GetInt64(key) }
  493. func (v *Viper) GetInt64(key string) int64 {
  494. return cast.ToInt64(v.Get(key))
  495. }
  496. // Returns the value associated with the key as a float64
  497. func GetFloat64(key string) float64 { return v.GetFloat64(key) }
  498. func (v *Viper) GetFloat64(key string) float64 {
  499. return cast.ToFloat64(v.Get(key))
  500. }
  501. // Returns the value associated with the key as time
  502. func GetTime(key string) time.Time { return v.GetTime(key) }
  503. func (v *Viper) GetTime(key string) time.Time {
  504. return cast.ToTime(v.Get(key))
  505. }
  506. // Returns the value associated with the key as a duration
  507. func GetDuration(key string) time.Duration { return v.GetDuration(key) }
  508. func (v *Viper) GetDuration(key string) time.Duration {
  509. return cast.ToDuration(v.Get(key))
  510. }
  511. // Returns the value associated with the key as a slice of strings
  512. func GetStringSlice(key string) []string { return v.GetStringSlice(key) }
  513. func (v *Viper) GetStringSlice(key string) []string {
  514. return cast.ToStringSlice(v.Get(key))
  515. }
  516. // Returns the value associated with the key as a map of interfaces
  517. func GetStringMap(key string) map[string]interface{} { return v.GetStringMap(key) }
  518. func (v *Viper) GetStringMap(key string) map[string]interface{} {
  519. return cast.ToStringMap(v.Get(key))
  520. }
  521. // Returns the value associated with the key as a map of strings
  522. func GetStringMapString(key string) map[string]string { return v.GetStringMapString(key) }
  523. func (v *Viper) GetStringMapString(key string) map[string]string {
  524. return cast.ToStringMapString(v.Get(key))
  525. }
  526. // Returns the value associated with the key as a map to a slice of strings.
  527. func GetStringMapStringSlice(key string) map[string][]string { return v.GetStringMapStringSlice(key) }
  528. func (v *Viper) GetStringMapStringSlice(key string) map[string][]string {
  529. return cast.ToStringMapStringSlice(v.Get(key))
  530. }
  531. // Returns the size of the value associated with the given key
  532. // in bytes.
  533. func GetSizeInBytes(key string) uint { return v.GetSizeInBytes(key) }
  534. func (v *Viper) GetSizeInBytes(key string) uint {
  535. sizeStr := cast.ToString(v.Get(key))
  536. return parseSizeInBytes(sizeStr)
  537. }
  538. // Takes a single key and unmarshals it into a Struct
  539. func UnmarshalKey(key string, rawVal interface{}) error { return v.UnmarshalKey(key, rawVal) }
  540. func (v *Viper) UnmarshalKey(key string, rawVal interface{}) error {
  541. return mapstructure.Decode(v.Get(key), rawVal)
  542. }
  543. // Unmarshals the config into a Struct. Make sure that the tags
  544. // on the fields of the structure are properly set.
  545. func Unmarshal(rawVal interface{}) error { return v.Unmarshal(rawVal) }
  546. func (v *Viper) Unmarshal(rawVal interface{}) error {
  547. err := mapstructure.WeakDecode(v.AllSettings(), rawVal)
  548. if err != nil {
  549. return err
  550. }
  551. v.insensitiviseMaps()
  552. return nil
  553. }
  554. // A wrapper around mapstructure.Decode that mimics the WeakDecode functionality
  555. // while erroring on non existing vals in the destination struct
  556. func weakDecodeExact(input, output interface{}) error {
  557. config := &mapstructure.DecoderConfig{
  558. ErrorUnused: true,
  559. Metadata: nil,
  560. Result: output,
  561. WeaklyTypedInput: true,
  562. }
  563. decoder, err := mapstructure.NewDecoder(config)
  564. if err != nil {
  565. return err
  566. }
  567. return decoder.Decode(input)
  568. }
  569. // Unmarshals the config into a Struct, erroring if a field is non-existant
  570. // in the destination struct
  571. func (v *Viper) UnmarshalExact(rawVal interface{}) error {
  572. err := weakDecodeExact(v.AllSettings(), rawVal)
  573. if err != nil {
  574. return err
  575. }
  576. v.insensitiviseMaps()
  577. return nil
  578. }
  579. // Bind a full flag set to the configuration, using each flag's long
  580. // name as the config key.
  581. func BindPFlags(flags *pflag.FlagSet) (err error) { return v.BindPFlags(flags) }
  582. func (v *Viper) BindPFlags(flags *pflag.FlagSet) (err error) {
  583. return v.BindFlagValues(pflagValueSet{flags})
  584. }
  585. // Bind a specific key to a pflag (as used by cobra).
  586. // Example (where serverCmd is a Cobra instance):
  587. //
  588. // serverCmd.Flags().Int("port", 1138, "Port to run Application server on")
  589. // Viper.BindPFlag("port", serverCmd.Flags().Lookup("port"))
  590. //
  591. func BindPFlag(key string, flag *pflag.Flag) (err error) { return v.BindPFlag(key, flag) }
  592. func (v *Viper) BindPFlag(key string, flag *pflag.Flag) (err error) {
  593. return v.BindFlagValue(key, pflagValue{flag})
  594. }
  595. // Bind a full FlagValue set to the configuration, using each flag's long
  596. // name as the config key.
  597. func BindFlagValues(flags FlagValueSet) (err error) { return v.BindFlagValues(flags) }
  598. func (v *Viper) BindFlagValues(flags FlagValueSet) (err error) {
  599. flags.VisitAll(func(flag FlagValue) {
  600. if err = v.BindFlagValue(flag.Name(), flag); err != nil {
  601. return
  602. }
  603. })
  604. return nil
  605. }
  606. // Bind a specific key to a FlagValue.
  607. // Example(where serverCmd is a Cobra instance):
  608. //
  609. // serverCmd.Flags().Int("port", 1138, "Port to run Application server on")
  610. // Viper.BindFlagValue("port", serverCmd.Flags().Lookup("port"))
  611. //
  612. func BindFlagValue(key string, flag FlagValue) (err error) { return v.BindFlagValue(key, flag) }
  613. func (v *Viper) BindFlagValue(key string, flag FlagValue) (err error) {
  614. if flag == nil {
  615. return fmt.Errorf("flag for %q is nil", key)
  616. }
  617. v.pflags[strings.ToLower(key)] = flag
  618. return nil
  619. }
  620. // Binds a Viper key to a ENV variable
  621. // ENV variables are case sensitive
  622. // If only a key is provided, it will use the env key matching the key, uppercased.
  623. // EnvPrefix will be used when set when env name is not provided.
  624. func BindEnv(input ...string) (err error) { return v.BindEnv(input...) }
  625. func (v *Viper) BindEnv(input ...string) (err error) {
  626. var key, envkey string
  627. if len(input) == 0 {
  628. return fmt.Errorf("BindEnv missing key to bind to")
  629. }
  630. key = strings.ToLower(input[0])
  631. if len(input) == 1 {
  632. envkey = v.mergeWithEnvPrefix(key)
  633. } else {
  634. envkey = input[1]
  635. }
  636. v.env[key] = envkey
  637. return nil
  638. }
  639. // Given a key, find the value
  640. // Viper will check in the following order:
  641. // flag, env, config file, key/value store, default
  642. // Viper will check to see if an alias exists first
  643. func (v *Viper) find(key string) interface{} {
  644. var val interface{}
  645. var exists bool
  646. // if the requested key is an alias, then return the proper key
  647. key = v.realKey(key)
  648. // PFlag Override first
  649. flag, exists := v.pflags[key]
  650. if exists && flag.HasChanged() {
  651. jww.TRACE.Println(key, "found in override (via pflag):", flag.ValueString())
  652. switch flag.ValueType() {
  653. case "int", "int8", "int16", "int32", "int64":
  654. return cast.ToInt(flag.ValueString())
  655. case "bool":
  656. return cast.ToBool(flag.ValueString())
  657. default:
  658. return flag.ValueString()
  659. }
  660. }
  661. val, exists = v.override[key]
  662. if exists {
  663. jww.TRACE.Println(key, "found in override:", val)
  664. return val
  665. }
  666. if v.automaticEnvApplied {
  667. // even if it hasn't been registered, if automaticEnv is used,
  668. // check any Get request
  669. if val = v.getEnv(v.mergeWithEnvPrefix(key)); val != "" {
  670. jww.TRACE.Println(key, "found in environment with val:", val)
  671. return val
  672. }
  673. }
  674. envkey, exists := v.env[key]
  675. if exists {
  676. jww.TRACE.Println(key, "registered as env var", envkey)
  677. if val = v.getEnv(envkey); val != "" {
  678. jww.TRACE.Println(envkey, "found in environment with val:", val)
  679. return val
  680. } else {
  681. jww.TRACE.Println(envkey, "env value unset:")
  682. }
  683. }
  684. val, exists = v.config[key]
  685. if exists {
  686. jww.TRACE.Println(key, "found in config:", val)
  687. return val
  688. }
  689. // Test for nested config parameter
  690. if strings.Contains(key, v.keyDelim) {
  691. path := strings.Split(key, v.keyDelim)
  692. source := v.find(path[0])
  693. if source != nil {
  694. if reflect.TypeOf(source).Kind() == reflect.Map {
  695. val := v.searchMap(cast.ToStringMap(source), path[1:])
  696. jww.TRACE.Println(key, "found in nested config:", val)
  697. return val
  698. }
  699. }
  700. }
  701. val, exists = v.kvstore[key]
  702. if exists {
  703. jww.TRACE.Println(key, "found in key/value store:", val)
  704. return val
  705. }
  706. val, exists = v.defaults[key]
  707. if exists {
  708. jww.TRACE.Println(key, "found in defaults:", val)
  709. return val
  710. }
  711. return nil
  712. }
  713. // Check to see if the key has been set in any of the data locations
  714. func IsSet(key string) bool { return v.IsSet(key) }
  715. func (v *Viper) IsSet(key string) bool {
  716. path := strings.Split(key, v.keyDelim)
  717. lcaseKey := strings.ToLower(key)
  718. val := v.find(lcaseKey)
  719. if val == nil {
  720. source := v.find(strings.ToLower(path[0]))
  721. if source != nil {
  722. if reflect.TypeOf(source).Kind() == reflect.Map {
  723. val = v.searchMap(cast.ToStringMap(source), path[1:])
  724. }
  725. }
  726. }
  727. return val != nil
  728. }
  729. // Have Viper check ENV variables for all
  730. // keys set in config, default & flags
  731. func AutomaticEnv() { v.AutomaticEnv() }
  732. func (v *Viper) AutomaticEnv() {
  733. v.automaticEnvApplied = true
  734. }
  735. // SetEnvKeyReplacer sets the strings.Replacer on the viper object
  736. // Useful for mapping an environmental variable to a key that does
  737. // not match it.
  738. func SetEnvKeyReplacer(r *strings.Replacer) { v.SetEnvKeyReplacer(r) }
  739. func (v *Viper) SetEnvKeyReplacer(r *strings.Replacer) {
  740. v.envKeyReplacer = r
  741. }
  742. // Aliases provide another accessor for the same key.
  743. // This enables one to change a name without breaking the application
  744. func RegisterAlias(alias string, key string) { v.RegisterAlias(alias, key) }
  745. func (v *Viper) RegisterAlias(alias string, key string) {
  746. v.registerAlias(alias, strings.ToLower(key))
  747. }
  748. func (v *Viper) registerAlias(alias string, key string) {
  749. alias = strings.ToLower(alias)
  750. if alias != key && alias != v.realKey(key) {
  751. _, exists := v.aliases[alias]
  752. if !exists {
  753. // if we alias something that exists in one of the maps to another
  754. // name, we'll never be able to get that value using the original
  755. // name, so move the config value to the new realkey.
  756. if val, ok := v.config[alias]; ok {
  757. delete(v.config, alias)
  758. v.config[key] = val
  759. }
  760. if val, ok := v.kvstore[alias]; ok {
  761. delete(v.kvstore, alias)
  762. v.kvstore[key] = val
  763. }
  764. if val, ok := v.defaults[alias]; ok {
  765. delete(v.defaults, alias)
  766. v.defaults[key] = val
  767. }
  768. if val, ok := v.override[alias]; ok {
  769. delete(v.override, alias)
  770. v.override[key] = val
  771. }
  772. v.aliases[alias] = key
  773. }
  774. } else {
  775. jww.WARN.Println("Creating circular reference alias", alias, key, v.realKey(key))
  776. }
  777. }
  778. func (v *Viper) realKey(key string) string {
  779. newkey, exists := v.aliases[key]
  780. if exists {
  781. jww.DEBUG.Println("Alias", key, "to", newkey)
  782. return v.realKey(newkey)
  783. } else {
  784. return key
  785. }
  786. }
  787. // Check to see if the given key (or an alias) is in the config file
  788. func InConfig(key string) bool { return v.InConfig(key) }
  789. func (v *Viper) InConfig(key string) bool {
  790. // if the requested key is an alias, then return the proper key
  791. key = v.realKey(key)
  792. _, exists := v.config[key]
  793. return exists
  794. }
  795. // Set the default value for this key.
  796. // Default only used when no value is provided by the user via flag, config or ENV.
  797. func SetDefault(key string, value interface{}) { v.SetDefault(key, value) }
  798. func (v *Viper) SetDefault(key string, value interface{}) {
  799. // If alias passed in, then set the proper default
  800. key = v.realKey(strings.ToLower(key))
  801. v.defaults[key] = value
  802. }
  803. // Sets the value for the key in the override regiser.
  804. // Will be used instead of values obtained via
  805. // flags, config file, ENV, default, or key/value store
  806. func Set(key string, value interface{}) { v.Set(key, value) }
  807. func (v *Viper) Set(key string, value interface{}) {
  808. // If alias passed in, then set the proper override
  809. key = v.realKey(strings.ToLower(key))
  810. v.override[key] = value
  811. }
  812. // Viper will discover and load the configuration file from disk
  813. // and key/value stores, searching in one of the defined paths.
  814. func ReadInConfig() error { return v.ReadInConfig() }
  815. func (v *Viper) ReadInConfig() error {
  816. jww.INFO.Println("Attempting to read in config file")
  817. if !stringInSlice(v.getConfigType(), SupportedExts) {
  818. return UnsupportedConfigError(v.getConfigType())
  819. }
  820. file, err := afero.ReadFile(v.fs, v.getConfigFile())
  821. if err != nil {
  822. return err
  823. }
  824. v.config = make(map[string]interface{})
  825. return v.unmarshalReader(bytes.NewReader(file), v.config)
  826. }
  827. // MergeInConfig merges a new configuration with an existing config.
  828. func MergeInConfig() error { return v.MergeInConfig() }
  829. func (v *Viper) MergeInConfig() error {
  830. jww.INFO.Println("Attempting to merge in config file")
  831. if !stringInSlice(v.getConfigType(), SupportedExts) {
  832. return UnsupportedConfigError(v.getConfigType())
  833. }
  834. file, err := afero.ReadFile(v.fs, v.getConfigFile())
  835. if err != nil {
  836. return err
  837. }
  838. return v.MergeConfig(bytes.NewReader(file))
  839. }
  840. // Viper will read a configuration file, setting existing keys to nil if the
  841. // key does not exist in the file.
  842. func ReadConfig(in io.Reader) error { return v.ReadConfig(in) }
  843. func (v *Viper) ReadConfig(in io.Reader) error {
  844. v.config = make(map[string]interface{})
  845. return v.unmarshalReader(in, v.config)
  846. }
  847. // MergeConfig merges a new configuration with an existing config.
  848. func MergeConfig(in io.Reader) error { return v.MergeConfig(in) }
  849. func (v *Viper) MergeConfig(in io.Reader) error {
  850. if v.config == nil {
  851. v.config = make(map[string]interface{})
  852. }
  853. cfg := make(map[string]interface{})
  854. if err := v.unmarshalReader(in, cfg); err != nil {
  855. return err
  856. }
  857. mergeMaps(cfg, v.config, nil)
  858. return nil
  859. }
  860. func keyExists(k string, m map[string]interface{}) string {
  861. lk := strings.ToLower(k)
  862. for mk := range m {
  863. lmk := strings.ToLower(mk)
  864. if lmk == lk {
  865. return mk
  866. }
  867. }
  868. return ""
  869. }
  870. func castToMapStringInterface(
  871. src map[interface{}]interface{}) map[string]interface{} {
  872. tgt := map[string]interface{}{}
  873. for k, v := range src {
  874. tgt[fmt.Sprintf("%v", k)] = v
  875. }
  876. return tgt
  877. }
  878. // mergeMaps merges two maps. The `itgt` parameter is for handling go-yaml's
  879. // insistence on parsing nested structures as `map[interface{}]interface{}`
  880. // instead of using a `string` as the key for nest structures beyond one level
  881. // deep. Both map types are supported as there is a go-yaml fork that uses
  882. // `map[string]interface{}` instead.
  883. func mergeMaps(
  884. src, tgt map[string]interface{}, itgt map[interface{}]interface{}) {
  885. for sk, sv := range src {
  886. tk := keyExists(sk, tgt)
  887. if tk == "" {
  888. jww.TRACE.Printf("tk=\"\", tgt[%s]=%v", sk, sv)
  889. tgt[sk] = sv
  890. if itgt != nil {
  891. itgt[sk] = sv
  892. }
  893. continue
  894. }
  895. tv, ok := tgt[tk]
  896. if !ok {
  897. jww.TRACE.Printf("tgt[%s] != ok, tgt[%s]=%v", tk, sk, sv)
  898. tgt[sk] = sv
  899. if itgt != nil {
  900. itgt[sk] = sv
  901. }
  902. continue
  903. }
  904. svType := reflect.TypeOf(sv)
  905. tvType := reflect.TypeOf(tv)
  906. if svType != tvType {
  907. jww.ERROR.Printf(
  908. "svType != tvType; key=%s, st=%v, tt=%v, sv=%v, tv=%v",
  909. sk, svType, tvType, sv, tv)
  910. continue
  911. }
  912. jww.TRACE.Printf("processing key=%s, st=%v, tt=%v, sv=%v, tv=%v",
  913. sk, svType, tvType, sv, tv)
  914. switch ttv := tv.(type) {
  915. case map[interface{}]interface{}:
  916. jww.TRACE.Printf("merging maps (must convert)")
  917. tsv := sv.(map[interface{}]interface{})
  918. ssv := castToMapStringInterface(tsv)
  919. stv := castToMapStringInterface(ttv)
  920. mergeMaps(ssv, stv, ttv)
  921. case map[string]interface{}:
  922. jww.TRACE.Printf("merging maps")
  923. mergeMaps(sv.(map[string]interface{}), ttv, nil)
  924. default:
  925. jww.TRACE.Printf("setting value")
  926. tgt[tk] = sv
  927. if itgt != nil {
  928. itgt[tk] = sv
  929. }
  930. }
  931. }
  932. }
  933. // func ReadBufConfig(buf *bytes.Buffer) error { return v.ReadBufConfig(buf) }
  934. // func (v *Viper) ReadBufConfig(buf *bytes.Buffer) error {
  935. // v.config = make(map[string]interface{})
  936. // return v.unmarshalReader(buf, v.config)
  937. // }
  938. // Attempts to get configuration from a remote source
  939. // and read it in the remote configuration registry.
  940. func ReadRemoteConfig() error { return v.ReadRemoteConfig() }
  941. func (v *Viper) ReadRemoteConfig() error {
  942. err := v.getKeyValueConfig()
  943. if err != nil {
  944. return err
  945. }
  946. return nil
  947. }
  948. func WatchRemoteConfig() error { return v.WatchRemoteConfig() }
  949. func (v *Viper) WatchRemoteConfig() error {
  950. err := v.watchKeyValueConfig()
  951. if err != nil {
  952. return err
  953. }
  954. return nil
  955. }
  956. // Unmarshall a Reader into a map
  957. // Should probably be an unexported function
  958. func unmarshalReader(in io.Reader, c map[string]interface{}) error {
  959. return v.unmarshalReader(in, c)
  960. }
  961. func (v *Viper) unmarshalReader(in io.Reader, c map[string]interface{}) error {
  962. return unmarshallConfigReader(in, c, v.getConfigType())
  963. }
  964. func (v *Viper) insensitiviseMaps() {
  965. insensitiviseMap(v.config)
  966. insensitiviseMap(v.defaults)
  967. insensitiviseMap(v.override)
  968. insensitiviseMap(v.kvstore)
  969. }
  970. // retrieve the first found remote configuration
  971. func (v *Viper) getKeyValueConfig() error {
  972. if RemoteConfig == nil {
  973. return RemoteConfigError("Enable the remote features by doing a blank import of the viper/remote package: '_ github.com/spf13/viper/remote'")
  974. }
  975. for _, rp := range v.remoteProviders {
  976. val, err := v.getRemoteConfig(rp)
  977. if err != nil {
  978. continue
  979. }
  980. v.kvstore = val
  981. return nil
  982. }
  983. return RemoteConfigError("No Files Found")
  984. }
  985. func (v *Viper) getRemoteConfig(provider *defaultRemoteProvider) (map[string]interface{}, error) {
  986. reader, err := RemoteConfig.Get(provider)
  987. if err != nil {
  988. return nil, err
  989. }
  990. err = v.unmarshalReader(reader, v.kvstore)
  991. return v.kvstore, err
  992. }
  993. // retrieve the first found remote configuration
  994. func (v *Viper) watchKeyValueConfig() error {
  995. for _, rp := range v.remoteProviders {
  996. val, err := v.watchRemoteConfig(rp)
  997. if err != nil {
  998. continue
  999. }
  1000. v.kvstore = val
  1001. return nil
  1002. }
  1003. return RemoteConfigError("No Files Found")
  1004. }
  1005. func (v *Viper) watchRemoteConfig(provider *defaultRemoteProvider) (map[string]interface{}, error) {
  1006. reader, err := RemoteConfig.Watch(provider)
  1007. if err != nil {
  1008. return nil, err
  1009. }
  1010. err = v.unmarshalReader(reader, v.kvstore)
  1011. return v.kvstore, err
  1012. }
  1013. // Return all keys regardless where they are set
  1014. func AllKeys() []string { return v.AllKeys() }
  1015. func (v *Viper) AllKeys() []string {
  1016. m := map[string]struct{}{}
  1017. for key, _ := range v.defaults {
  1018. m[strings.ToLower(key)] = struct{}{}
  1019. }
  1020. for key, _ := range v.pflags {
  1021. m[strings.ToLower(key)] = struct{}{}
  1022. }
  1023. for key, _ := range v.env {
  1024. m[strings.ToLower(key)] = struct{}{}
  1025. }
  1026. for key, _ := range v.config {
  1027. m[strings.ToLower(key)] = struct{}{}
  1028. }
  1029. for key, _ := range v.kvstore {
  1030. m[strings.ToLower(key)] = struct{}{}
  1031. }
  1032. for key, _ := range v.override {
  1033. m[strings.ToLower(key)] = struct{}{}
  1034. }
  1035. for key, _ := range v.aliases {
  1036. m[strings.ToLower(key)] = struct{}{}
  1037. }
  1038. a := []string{}
  1039. for x, _ := range m {
  1040. a = append(a, x)
  1041. }
  1042. return a
  1043. }
  1044. // Return all settings as a map[string]interface{}
  1045. func AllSettings() map[string]interface{} { return v.AllSettings() }
  1046. func (v *Viper) AllSettings() map[string]interface{} {
  1047. m := map[string]interface{}{}
  1048. for _, x := range v.AllKeys() {
  1049. m[x] = v.Get(x)
  1050. }
  1051. return m
  1052. }
  1053. // Se the filesystem to use to read configuration.
  1054. func SetFs(fs afero.Fs) { v.SetFs(fs) }
  1055. func (v *Viper) SetFs(fs afero.Fs) {
  1056. v.fs = fs
  1057. }
  1058. // Name for the config file.
  1059. // Does not include extension.
  1060. func SetConfigName(in string) { v.SetConfigName(in) }
  1061. func (v *Viper) SetConfigName(in string) {
  1062. if in != "" {
  1063. v.configName = in
  1064. v.configFile = ""
  1065. }
  1066. }
  1067. // Sets the type of the configuration returned by the
  1068. // remote source, e.g. "json".
  1069. func SetConfigType(in string) { v.SetConfigType(in) }
  1070. func (v *Viper) SetConfigType(in string) {
  1071. if in != "" {
  1072. v.configType = in
  1073. }
  1074. }
  1075. func (v *Viper) getConfigType() string {
  1076. if v.configType != "" {
  1077. return v.configType
  1078. }
  1079. cf := v.getConfigFile()
  1080. ext := filepath.Ext(cf)
  1081. if len(ext) > 1 {
  1082. return ext[1:]
  1083. } else {
  1084. return ""
  1085. }
  1086. }
  1087. func (v *Viper) getConfigFile() string {
  1088. // if explicitly set, then use it
  1089. if v.configFile != "" {
  1090. return v.configFile
  1091. }
  1092. cf, err := v.findConfigFile()
  1093. if err != nil {
  1094. return ""
  1095. }
  1096. v.configFile = cf
  1097. return v.getConfigFile()
  1098. }
  1099. func (v *Viper) searchInPath(in string) (filename string) {
  1100. jww.DEBUG.Println("Searching for config in ", in)
  1101. for _, ext := range SupportedExts {
  1102. jww.DEBUG.Println("Checking for", filepath.Join(in, v.configName+"."+ext))
  1103. if b, _ := exists(filepath.Join(in, v.configName+"."+ext)); b {
  1104. jww.DEBUG.Println("Found: ", filepath.Join(in, v.configName+"."+ext))
  1105. return filepath.Join(in, v.configName+"."+ext)
  1106. }
  1107. }
  1108. return ""
  1109. }
  1110. // search all configPaths for any config file.
  1111. // Returns the first path that exists (and is a config file)
  1112. func (v *Viper) findConfigFile() (string, error) {
  1113. jww.INFO.Println("Searching for config in ", v.configPaths)
  1114. for _, cp := range v.configPaths {
  1115. file := v.searchInPath(cp)
  1116. if file != "" {
  1117. return file, nil
  1118. }
  1119. }
  1120. return "", ConfigFileNotFoundError{v.configName, fmt.Sprintf("%s", v.configPaths)}
  1121. }
  1122. // Prints all configuration registries for debugging
  1123. // purposes.
  1124. func Debug() { v.Debug() }
  1125. func (v *Viper) Debug() {
  1126. fmt.Println("Aliases:")
  1127. fmt.Printf("Aliases:\n%#v\n", v.aliases)
  1128. fmt.Printf("Override:\n%#v\n", v.override)
  1129. fmt.Printf("PFlags:\n%#v\n", v.pflags)
  1130. fmt.Printf("Env:\n%#v\n", v.env)
  1131. fmt.Printf("Key/Value Store:\n%#v\n", v.kvstore)
  1132. fmt.Printf("Config:\n%#v\n", v.config)
  1133. fmt.Printf("Defaults:\n%#v\n", v.defaults)
  1134. }