本站源代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1869 lines
53KB

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