Syntax highlighting

This commit is contained in:
Maxime Horcholle 2015-07-16 14:16:31 +02:00 committed by spf13
parent db7ff930a1
commit 1e6a237e05

View file

@ -63,9 +63,11 @@ remote configuration or flag.
Examples: Examples:
```go
viper.SetDefault("ContentDir", "content") viper.SetDefault("ContentDir", "content")
viper.SetDefault("LayoutDir", "layouts") viper.SetDefault("LayoutDir", "layouts")
viper.SetDefault("Taxonomies", map[string]string{"tag": "tags", "category": "categories"}) viper.SetDefault("Taxonomies", map[string]string{"tag": "tags", "category": "categories"})
```
### Reading Config Files ### Reading Config Files
@ -74,6 +76,7 @@ configuration so it knows where to look for the config file. Viper
supports json, toml and yaml files. Viper can search multiple paths, but supports json, toml and yaml files. Viper can search multiple paths, but
currently a single viper only supports a single config file. currently a single viper only supports a single config file.
```go
viper.SetConfigName("config") // name of config file (without extension) viper.SetConfigName("config") // name of config file (without extension)
viper.AddConfigPath("/etc/appname/") // path to look for the config file in viper.AddConfigPath("/etc/appname/") // path to look for the config file in
viper.AddConfigPath("$HOME/.appname") // call multiple times to add many search paths viper.AddConfigPath("$HOME/.appname") // call multiple times to add many search paths
@ -81,6 +84,7 @@ currently a single viper only supports a single config file.
if err != nil { // Handle errors reading the config file if err != nil { // Handle errors reading the config file
panic(fmt.Errorf("Fatal error config file: %s \n", err)) panic(fmt.Errorf("Fatal error config file: %s \n", err))
} }
```
### Reading Config from io.Reader ### Reading Config from io.Reader
@ -88,7 +92,7 @@ Viper predefined many configuration sources, such as files, environment variable
remote K/V store. But you are not bound to them. You can also implement your own way to remote K/V store. But you are not bound to them. You can also implement your own way to
require configuration and feed it to viper. require configuration and feed it to viper.
````go ```go
viper.SetConfigType("yaml") // or viper.SetConfigType("YAML") viper.SetConfigType("yaml") // or viper.SetConfigType("YAML")
// any approach to require this configuration into your program. // any approach to require this configuration into your program.
@ -110,19 +114,22 @@ beard: true
viper.ReadConfig(bytes.NewBuffer(yamlExample)) viper.ReadConfig(bytes.NewBuffer(yamlExample))
viper.Get("name") // this would be "steve" viper.Get("name") // this would be "steve"
```` ```
### Setting Overrides ### Setting Overrides
These could be from a command line flag, or from your own application logic. These could be from a command line flag, or from your own application logic.
```go
viper.Set("Verbose", true) viper.Set("Verbose", true)
viper.Set("LogFile", LogFile) viper.Set("LogFile", LogFile)
```
### Registering and Using Aliases ### Registering and Using Aliases
Aliases permit a single value to be referenced by multiple keys Aliases permit a single value to be referenced by multiple keys
```go
viper.RegisterAlias("loud", "Verbose") viper.RegisterAlias("loud", "Verbose")
viper.Set("verbose", true) // same result as next line viper.Set("verbose", true) // same result as next line
@ -130,6 +137,7 @@ Aliases permit a single value to be referenced by multiple keys
viper.GetBool("loud") // true viper.GetBool("loud") // true
viper.GetBool("verbose") // true viper.GetBool("verbose") // true
```
### Working with Environment Variables ### Working with Environment Variables
@ -175,13 +183,14 @@ of using it can be found in `viper_test.go`.
#### Env example #### Env example
```go
SetEnvPrefix("spf") // will be uppercased automatically SetEnvPrefix("spf") // will be uppercased automatically
BindEnv("id") BindEnv("id")
os.Setenv("SPF_ID", "13") // typically done outside of the app os.Setenv("SPF_ID", "13") // typically done outside of the app
id := Get("id") // 13 id := Get("id") // 13
```
### Working with Flags ### Working with Flags
@ -196,9 +205,10 @@ The BindPFlag() method provides this functionality.
Example: Example:
```go
serverCmd.Flags().Int("port", 1138, "Port to run Application server on") serverCmd.Flags().Int("port", 1138, "Port to run Application server on")
viper.BindPFlag("port", serverCmd.Flags().Lookup("port")) viper.BindPFlag("port", serverCmd.Flags().Lookup("port"))
```
### Remote Key/Value Store Support ### Remote Key/Value Store Support
@ -222,30 +232,39 @@ independently of it.
`crypt` has a command-line helper that you can use to put configurations `crypt` has a command-line helper that you can use to put configurations
in your K/V store. `crypt` defaults to etcd on http://127.0.0.1:4001. in your K/V store. `crypt` defaults to etcd on http://127.0.0.1:4001.
go get github.com/xordataexchange/crypt/bin/crypt ```bash
crypt set -plaintext /config/hugo.json /Users/hugo/settings/config.json $ go get github.com/xordataexchange/crypt/bin/crypt
$ crypt set -plaintext /config/hugo.json /Users/hugo/settings/config.json
```
Confirm that your value was set: Confirm that your value was set:
crypt get -plaintext /config/hugo.json ```bash
$ crypt get -plaintext /config/hugo.json
```
See the `crypt` documentation for examples of how to set encrypted values, or how See the `crypt` documentation for examples of how to set encrypted values, or how
to use Consul. to use Consul.
### Remote Key/Value Store Example - Unencrypted ### Remote Key/Value Store Example - Unencrypted
```go
viper.AddRemoteProvider("etcd", "http://127.0.0.1:4001","/config/hugo.json") viper.AddRemoteProvider("etcd", "http://127.0.0.1:4001","/config/hugo.json")
viper.SetConfigType("json") // because there is no file extension in a stream of bytes viper.SetConfigType("json") // because there is no file extension in a stream of bytes
err := viper.ReadRemoteConfig() err := viper.ReadRemoteConfig()
```
### Remote Key/Value Store Example - Encrypted ### Remote Key/Value Store Example - Encrypted
```go
viper.AddSecureRemoteProvider("etcd","http://127.0.0.1:4001","/config/hugo.json","/etc/secrets/mykeyring.gpg") viper.AddSecureRemoteProvider("etcd","http://127.0.0.1:4001","/config/hugo.json","/etc/secrets/mykeyring.gpg")
viper.SetConfigType("json") // because there is no file extension in a stream of bytes viper.SetConfigType("json") // because there is no file extension in a stream of bytes
err := viper.ReadRemoteConfig() err := viper.ReadRemoteConfig()
```
### Watching Changes in Etcd - Unencrypted ### Watching Changes in Etcd - Unencrypted
```go
// alternatively, you can create a new viper instance. // alternatively, you can create a new viper instance.
var runtime_viper = viper.New() var runtime_viper = viper.New()
@ -275,7 +294,7 @@ to use Consul.
runtime_viper.Marshal(&runtime_conf) runtime_viper.Marshal(&runtime_conf)
} }
}() }()
```
## Getting Values From Viper ## Getting Values From Viper
@ -299,18 +318,18 @@ its zero value if its not found. To check if a given key exists, the IsSet()
method has been provided. method has been provided.
Example: Example:
```go
viper.GetString("logfile") // case-insensitive Setting & Getting viper.GetString("logfile") // case-insensitive Setting & Getting
if viper.GetBool("verbose") { if viper.GetBool("verbose") {
fmt.Println("verbose enabled") fmt.Println("verbose enabled")
} }
```
### Accessing nested keys ### Accessing nested keys
The accessor methods also accept formatted paths to deeply nested keys. The accessor methods also accept formatted paths to deeply nested keys.
For example, if the following JSON file is loaded: For example, if the following JSON file is loaded:
``` ```json
{ {
"host": { "host": {
"address": "localhost", "address": "localhost",
@ -331,7 +350,8 @@ For example, if the following JSON file is loaded:
``` ```
Viper can access a nested field by passing a `.` delimited path of keys: Viper can access a nested field by passing a `.` delimited path of keys:
```
```go
GetString("datastore.metric.host") // (returns "127.0.0.1") GetString("datastore.metric.host") // (returns "127.0.0.1")
``` ```
@ -348,7 +368,7 @@ remaining registries looking for it.
Lastly, if there exists a key that matches the delimited key path, its value will Lastly, if there exists a key that matches the delimited key path, its value will
be returned instead. E.g. be returned instead. E.g.
``` ```json
{ {
"datastore.metric.host": "0.0.0.0", "datastore.metric.host": "0.0.0.0",
"host": { "host": {
@ -381,6 +401,7 @@ There are two methods to do this:
Example: Example:
```go
type config struct { type config struct {
Port int Port int
Name string Name string
@ -392,7 +413,7 @@ Example:
if err != nil { if err != nil {
t.Fatalf("unable to decode into struct, %v", err) t.Fatalf("unable to decode into struct, %v", err)
} }
```
## Viper or Vipers? ## Viper or Vipers?
@ -413,13 +434,15 @@ functions that viper package supports are mirrored as methods on a viper.
Example: Example:
```go
x := viper.New() x := viper.New()
y := viper.New() y := viper.New()
x.SetDefault("ContentDir", "content") x.SetDefault("ContentDir", "content")
y.SetDefault("ContentDir", "foobar") y.SetDefault("ContentDir", "foobar")
... //...
```
When working with multiple vipers, it is up to the user to keep track of When working with multiple vipers, it is up to the user to keep track of
the different vipers. the different vipers.