http urls monitor.

viper.go 51KB

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