changeset 262 | 8d3354485fc3 |
parent 260 | 445e01aede7e |
child 265 | 05c40b36d3b2 |
261:270cc4dda0c5 | 262:8d3354485fc3 |
---|---|
117 viper.AddConfigPath("/etc/appname/") // path to look for the config file in |
117 viper.AddConfigPath("/etc/appname/") // path to look for the config file in |
118 viper.AddConfigPath("$HOME/.appname") // call multiple times to add many search paths |
118 viper.AddConfigPath("$HOME/.appname") // call multiple times to add many search paths |
119 viper.AddConfigPath(".") // optionally look for config in the working directory |
119 viper.AddConfigPath(".") // optionally look for config in the working directory |
120 err := viper.ReadInConfig() // Find and read the config file |
120 err := viper.ReadInConfig() // Find and read the config file |
121 if err != nil { // Handle errors reading the config file |
121 if err != nil { // Handle errors reading the config file |
122 panic(fmt.Errorf("Fatal error config file: %w \n", err)) |
122 panic(fmt.Errorf("fatal error config file: %w", err)) |
123 } |
123 } |
124 ``` |
124 ``` |
125 |
125 |
126 You can handle the specific case where no config file is found like this: |
126 You can handle the specific case where no config file is found like this: |
127 |
127 |
441 ### Remote Key/Value Store Example - Unencrypted |
441 ### Remote Key/Value Store Example - Unencrypted |
442 |
442 |
443 #### etcd |
443 #### etcd |
444 ```go |
444 ```go |
445 viper.AddRemoteProvider("etcd", "http://127.0.0.1:4001","/config/hugo.json") |
445 viper.AddRemoteProvider("etcd", "http://127.0.0.1:4001","/config/hugo.json") |
446 viper.SetConfigType("json") // because there is no file extension in a stream of bytes, supported extensions are "json", "toml", "yaml", "yml", "properties", "props", "prop", "env", "dotenv" |
|
447 err := viper.ReadRemoteConfig() |
|
448 ``` |
|
449 |
|
450 #### etcd3 |
|
451 ```go |
|
452 viper.AddRemoteProvider("etcd3", "http://127.0.0.1:4001","/config/hugo.json") |
|
446 viper.SetConfigType("json") // because there is no file extension in a stream of bytes, supported extensions are "json", "toml", "yaml", "yml", "properties", "props", "prop", "env", "dotenv" |
453 viper.SetConfigType("json") // because there is no file extension in a stream of bytes, supported extensions are "json", "toml", "yaml", "yml", "properties", "props", "prop", "env", "dotenv" |
447 err := viper.ReadRemoteConfig() |
454 err := viper.ReadRemoteConfig() |
448 ``` |
455 ``` |
449 |
456 |
450 #### Consul |
457 #### Consul |
592 `datastore.metric` become undefined, they are “shadowed” by the higher-priority |
599 `datastore.metric` become undefined, they are “shadowed” by the higher-priority |
593 configuration level. |
600 configuration level. |
594 |
601 |
595 Viper can access array indices by using numbers in the path. For example: |
602 Viper can access array indices by using numbers in the path. For example: |
596 |
603 |
597 ```json |
604 ```jsonc |
598 { |
605 { |
599 "host": { |
606 "host": { |
600 "address": "localhost", |
607 "address": "localhost", |
601 "ports": [ |
608 "ports": [ |
602 5799, |
609 5799, |
620 ``` |
627 ``` |
621 |
628 |
622 Lastly, if there exists a key that matches the delimited key path, its value |
629 Lastly, if there exists a key that matches the delimited key path, its value |
623 will be returned instead. E.g. |
630 will be returned instead. E.g. |
624 |
631 |
625 ```json |
632 ```jsonc |
626 { |
633 { |
627 "datastore.metric.host": "0.0.0.0", |
634 "datastore.metric.host": "0.0.0.0", |
628 "host": { |
635 "host": { |
629 "address": "localhost", |
636 "address": "localhost", |
630 "port": 5799 |
637 "port": 5799 |