viper.go 52 KB

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