• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

remote/H17-Apr-2017-

.gitignoreH A D17-Apr-2017266

.travis.ymlH A D17-Apr-2017361

LICENSEH A D17-Apr-20171.1 KiB

README.mdH A D17-Apr-201718.4 KiB

flags.goH A D17-Apr-20171.3 KiB

flags_test.goH A D17-Apr-20171.3 KiB

nohup.outH A D17-Apr-201744

overrides_test.goH A D17-Apr-20176.3 KiB

util.goH A D17-Apr-20176.6 KiB

util_test.goH A D17-Apr-20171.1 KiB

viper.goH A D17-Apr-201744.3 KiB

viper_test.goH A D17-Apr-201726.7 KiB

README.md

1![viper logo](https://cloud.githubusercontent.com/assets/173412/10886745/998df88a-8151-11e5-9448-4736db51020d.png)
2
3Go configuration with fangs!
4
5Many Go projects are built using Viper including:
6
7* [Hugo](http://gohugo.io)
8* [EMC RexRay](http://rexray.readthedocs.org/en/stable/)
9* [Imgur's Incus](https://github.com/Imgur/incus)
10* [Nanobox](https://github.com/nanobox-io/nanobox)/[Nanopack](https://github.com/nanopack)
11* [Docker Notary](https://github.com/docker/Notary)
12* [BloomApi](https://www.bloomapi.com/)
13* [doctl](https://github.com/digitalocean/doctl)
14
15[![Build Status](https://travis-ci.org/spf13/viper.svg)](https://travis-ci.org/spf13/viper) [![Join the chat at https://gitter.im/spf13/viper](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/spf13/viper?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) [![GoDoc](https://godoc.org/github.com/spf13/viper?status.svg)](https://godoc.org/github.com/spf13/viper)
16
17
18## What is Viper?
19
20Viper is a complete configuration solution for go applications including 12 factor apps. It is designed
21to work within an application, and can handle all types of configuration needs
22and formats. It supports:
23
24* setting defaults
25* reading from JSON, TOML, YAML, HCL, and Java properties config files
26* live watching and re-reading of config files (optional)
27* reading from environment variables
28* reading from remote config systems (etcd or Consul), and watching changes
29* reading from command line flags
30* reading from buffer
31* setting explicit values
32
33Viper can be thought of as a registry for all of your applications
34configuration needs.
35
36## Why Viper?
37
38When building a modern application, you don’t want to worry about
39configuration file formats; you want to focus on building awesome software.
40Viper is here to help with that.
41
42Viper does the following for you:
43
441. Find, load, and unmarshal a configuration file in JSON, TOML, YAML, HCL, or Java properties formats.
452. Provide a mechanism to set default values for your different
46   configuration options.
473. Provide a mechanism to set override values for options specified through
48   command line flags.
494. Provide an alias system to easily rename parameters without breaking existing
50   code.
515. Make it easy to tell the difference between when a user has provided a
52   command line or config file which is the same as the default.
53
54Viper uses the following precedence order. Each item takes precedence over the
55item below it:
56
57 * explicit call to Set
58 * flag
59 * env
60 * config
61 * key/value store
62 * default
63
64Viper configuration keys are case insensitive.
65
66## Putting Values into Viper
67
68### Establishing Defaults
69
70A good configuration system will support default values. A default value is not
71required for a key, but it's useful in the event that a key hasn’t been set via
72config file, environment variable, remote configuration or flag.
73
74Examples:
75
76```go
77viper.SetDefault("ContentDir", "content")
78viper.SetDefault("LayoutDir", "layouts")
79viper.SetDefault("Taxonomies", map[string]string{"tag": "tags", "category": "categories"})
80```
81
82### Reading Config Files
83
84Viper requires minimal configuration so it knows where to look for config files.
85Viper supports JSON, TOML, YAML, HCL, and Java Properties files. Viper can search multiple paths, but
86currently a single Viper instance only supports a single configuration file.
87Viper does not default to any configuration search paths leaving defaults decision
88to an application.
89
90Here is an example of how to use Viper to search for and read a configuration file.
91None of the specific paths are required, but at least one path should be provided
92where a configuration file is expected.
93
94```go
95viper.SetConfigName("config") // name of config file (without extension)
96viper.AddConfigPath("/etc/appname/")   // path to look for the config file in
97viper.AddConfigPath("$HOME/.appname")  // call multiple times to add many search paths
98viper.AddConfigPath(".")               // optionally look for config in the working directory
99err := viper.ReadInConfig() // Find and read the config file
100if err != nil { // Handle errors reading the config file
101	panic(fmt.Errorf("Fatal error config file: %s \n", err))
102}
103```
104
105### Watching and re-reading config files
106
107Viper supports the ability to have your application live read a config file while running.
108
109Gone are the days of needing to restart a server to have a config take effect,
110viper powered applications can read an update to a config file while running and
111not miss a beat.
112
113Simply tell the viper instance to watchConfig.
114Optionally you can provide a function for Viper to run each time a change occurs.
115
116**Make sure you add all of the configPaths prior to calling `WatchConfig()`**
117
118```go
119		viper.WatchConfig()
120		viper.OnConfigChange(func(e fsnotify.Event) {
121			fmt.Println("Config file changed:", e.Name)
122		})
123```
124
125### Reading Config from io.Reader
126
127Viper predefines many configuration sources such as files, environment
128variables, flags, and remote K/V store, but you are not bound to them. You can
129also implement your own required configuration source and feed it to viper.
130
131```go
132viper.SetConfigType("yaml") // or viper.SetConfigType("YAML")
133
134// any approach to require this configuration into your program.
135var yamlExample = []byte(`
136Hacker: true
137name: steve
138hobbies:
139- skateboarding
140- snowboarding
141- go
142clothing:
143  jacket: leather
144  trousers: denim
145age: 35
146eyes : brown
147beard: true
148`)
149
150viper.ReadConfig(bytes.NewBuffer(yamlExample))
151
152viper.Get("name") // this would be "steve"
153```
154
155### Setting Overrides
156
157These could be from a command line flag, or from your own application logic.
158
159```go
160viper.Set("Verbose", true)
161viper.Set("LogFile", LogFile)
162```
163
164### Registering and Using Aliases
165
166Aliases permit a single value to be referenced by multiple keys
167
168```go
169viper.RegisterAlias("loud", "Verbose")
170
171viper.Set("verbose", true) // same result as next line
172viper.Set("loud", true)   // same result as prior line
173
174viper.GetBool("loud") // true
175viper.GetBool("verbose") // true
176```
177
178### Working with Environment Variables
179
180Viper has full support for environment variables. This enables 12 factor
181applications out of the box. There are four methods that exist to aid working
182with ENV:
183
184 * `AutomaticEnv()`
185 * `BindEnv(string...) : error`
186 * `SetEnvPrefix(string)`
187 * `SetEnvReplacer(string...) *strings.Replacer`
188
189_When working with ENV variables, it’s important to recognize that Viper
190treats ENV variables as case sensitive._
191
192Viper provides a mechanism to try to ensure that ENV variables are unique. By
193using `SetEnvPrefix`, you can tell Viper to use add a prefix while reading from
194the environment variables. Both `BindEnv` and `AutomaticEnv` will use this
195prefix.
196
197`BindEnv` takes one or two parameters. The first parameter is the key name, the
198second is the name of the environment variable. The name of the environment
199variable is case sensitive. If the ENV variable name is not provided, then
200Viper will automatically assume that the key name matches the ENV variable name,
201but the ENV variable is IN ALL CAPS. When you explicitly provide the ENV
202variable name, it **does not** automatically add the prefix.
203
204One important thing to recognize when working with ENV variables is that the
205value will be read each time it is accessed. Viper does not fix the value when
206the `BindEnv` is called.
207
208`AutomaticEnv` is a powerful helper especially when combined with
209`SetEnvPrefix`. When called, Viper will check for an environment variable any
210time a `viper.Get` request is made. It will apply the following rules. It will
211check for a environment variable with a name matching the key uppercased and
212prefixed with the `EnvPrefix` if set.
213
214`SetEnvReplacer` allows you to use a `strings.Replacer` object to rewrite Env
215keys to an extent. This is useful if you want to use `-` or something in your
216`Get()` calls, but want your environmental variables to use `_` delimiters. An
217example of using it can be found in `viper_test.go`.
218
219#### Env example
220
221```go
222SetEnvPrefix("spf") // will be uppercased automatically
223BindEnv("id")
224
225os.Setenv("SPF_ID", "13") // typically done outside of the app
226
227id := Get("id") // 13
228```
229
230### Working with Flags
231
232Viper has the ability to bind to flags. Specifically, Viper supports `Pflags`
233as used in the [Cobra](https://github.com/spf13/cobra) library.
234
235Like `BindEnv`, the value is not set when the binding method is called, but when
236it is accessed. This means you can bind as early as you want, even in an
237`init()` function.
238
239The `BindPFlag()` method provides this functionality.
240
241Example:
242
243```go
244serverCmd.Flags().Int("port", 1138, "Port to run Application server on")
245viper.BindPFlag("port", serverCmd.Flags().Lookup("port"))
246```
247
248The use of [pflag](https://github.com/spf13/pflag/) in Viper does not preclude
249the use of other packages that use the [flag](https://golang.org/pkg/flag/)
250package from the standard library. The pflag package can handle the flags
251defined for the flag package by importing these flags. This is accomplished
252by a calling a convenience function provided by the pflag package called
253AddGoFlagSet().
254
255Example:
256
257```go
258package main
259
260import (
261	"flag"
262	"github.com/spf13/pflag"
263)
264
265func main() {
266	pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
267	pflag.Parse()
268    ...
269}
270```
271
272#### Flag interfaces
273
274Viper provides two Go interfaces to bind other flag systems if you don't use `Pflags`.
275
276`FlagValue` represents a single flag. This is a very simple example on how to implement this interface:
277
278```go
279type myFlag struct {}
280func (f myFlag) HasChanged() bool { return false }
281func (f myFlag) Name() string { return "my-flag-name" }
282func (f myFlag) ValueString() string { return "my-flag-value" }
283func (f myFlag) ValueType() string { return "string" }
284```
285
286Once your flag implements this interface, you can simply tell Viper to bind it:
287
288```go
289viper.BindFlagValue("my-flag-name", myFlag{})
290```
291
292`FlagValueSet` represents a group of flags. This is a very simple example on how to implement this interface:
293
294```go
295type myFlagSet struct {
296	flags []myFlag
297}
298
299func (f myFlagSet) VisitAll(fn func(FlagValue)) {
300	for _, flag := range flags {
301		fn(flag)
302	}
303}
304```
305
306Once your flag set implements this interface, you can simply tell Viper to bind it:
307
308```go
309fSet := myFlagSet{
310	flags: []myFlag{myFlag{}, myFlag{}},
311}
312viper.BindFlagValues("my-flags", fSet)
313```
314
315### Remote Key/Value Store Support
316
317To enable remote support in Viper, do a blank import of the `viper/remote`
318package:
319
320`import _ "github.com/spf13/viper/remote"`
321
322Viper will read a config string (as JSON, TOML, YAML or HCL) retrieved from a path
323in a Key/Value store such as etcd or Consul.  These values take precedence over
324default values, but are overridden by configuration values retrieved from disk,
325flags, or environment variables.
326
327Viper uses [crypt](https://github.com/xordataexchange/crypt) to retrieve
328configuration from the K/V store, which means that you can store your
329configuration values encrypted and have them automatically decrypted if you have
330the correct gpg keyring.  Encryption is optional.
331
332You can use remote configuration in conjunction with local configuration, or
333independently of it.
334
335`crypt` has a command-line helper that you can use to put configurations in your
336K/V store. `crypt` defaults to etcd on http://127.0.0.1:4001.
337
338```bash
339$ go get github.com/xordataexchange/crypt/bin/crypt
340$ crypt set -plaintext /config/hugo.json /Users/hugo/settings/config.json
341```
342
343Confirm that your value was set:
344
345```bash
346$ crypt get -plaintext /config/hugo.json
347```
348
349See the `crypt` documentation for examples of how to set encrypted values, or
350how to use Consul.
351
352### Remote Key/Value Store Example - Unencrypted
353
354```go
355viper.AddRemoteProvider("etcd", "http://127.0.0.1:4001","/config/hugo.json")
356viper.SetConfigType("json") // because there is no file extension in a stream of bytes, supported extensions are "json", "toml", "yaml", "yml", "properties", "props", "prop"
357err := viper.ReadRemoteConfig()
358```
359
360### Remote Key/Value Store Example - Encrypted
361
362```go
363viper.AddSecureRemoteProvider("etcd","http://127.0.0.1:4001","/config/hugo.json","/etc/secrets/mykeyring.gpg")
364viper.SetConfigType("json") // because there is no file extension in a stream of bytes,  supported extensions are "json", "toml", "yaml", "yml", "properties", "props", "prop"
365err := viper.ReadRemoteConfig()
366```
367
368### Watching Changes in etcd - Unencrypted
369
370```go
371// alternatively, you can create a new viper instance.
372var runtime_viper = viper.New()
373
374runtime_viper.AddRemoteProvider("etcd", "http://127.0.0.1:4001", "/config/hugo.yml")
375runtime_viper.SetConfigType("yaml") // because there is no file extension in a stream of bytes, supported extensions are "json", "toml", "yaml", "yml", "properties", "props", "prop"
376
377// read from remote config the first time.
378err := runtime_viper.ReadRemoteConfig()
379
380// unmarshal config
381runtime_viper.Unmarshal(&runtime_conf)
382
383// open a goroutine to watch remote changes forever
384go func(){
385	for {
386	    time.Sleep(time.Second * 5) // delay after each request
387
388	    // currently, only tested with etcd support
389	    err := runtime_viper.WatchRemoteConfig()
390	    if err != nil {
391	        log.Errorf("unable to read remote config: %v", err)
392	        continue
393	    }
394
395	    // unmarshal new config into our runtime config struct. you can also use channel
396	    // to implement a signal to notify the system of the changes
397	    runtime_viper.Unmarshal(&runtime_conf)
398	}
399}()
400```
401
402## Getting Values From Viper
403
404In Viper, there are a few ways to get a value depending on the value's type.
405The following functions and methods exist:
406
407 * `Get(key string) : interface{}`
408 * `GetBool(key string) : bool`
409 * `GetFloat64(key string) : float64`
410 * `GetInt(key string) : int`
411 * `GetString(key string) : string`
412 * `GetStringMap(key string) : map[string]interface{}`
413 * `GetStringMapString(key string) : map[string]string`
414 * `GetStringSlice(key string) : []string`
415 * `GetTime(key string) : time.Time`
416 * `GetDuration(key string) : time.Duration`
417 * `IsSet(key string) : bool`
418
419One important thing to recognize is that each Get function will return a zero
420value if it’s not found. To check if a given key exists, the `IsSet()` method
421has been provided.
422
423Example:
424```go
425viper.GetString("logfile") // case-insensitive Setting & Getting
426if viper.GetBool("verbose") {
427    fmt.Println("verbose enabled")
428}
429```
430### Accessing nested keys
431
432The accessor methods also accept formatted paths to deeply nested keys. For
433example, if the following JSON file is loaded:
434
435```json
436{
437    "host": {
438        "address": "localhost",
439        "port": 5799
440    },
441    "datastore": {
442        "metric": {
443            "host": "127.0.0.1",
444            "port": 3099
445        },
446        "warehouse": {
447            "host": "198.0.0.1",
448            "port": 2112
449        }
450    }
451}
452
453```
454
455Viper can access a nested field by passing a `.` delimited path of keys:
456
457```go
458GetString("datastore.metric.host") // (returns "127.0.0.1")
459```
460
461This obeys the precedence rules established above; the search for the path
462will cascade through the remaining configuration registries until found.
463
464For example, given this configuration file, both `datastore.metric.host` and
465`datastore.metric.port` are already defined (and may be overridden). If in addition
466`datastore.metric.protocol` was defined in the defaults, Viper would also find it.
467
468However, if `datastore.metric` was overridden (by a flag, an environment variable,
469the `Set()` method, …) with an immediate value, then all sub-keys of
470`datastore.metric` become undefined, they are “shadowed” by the higher-priority
471configuration level.
472
473Lastly, if there exists a key that matches the delimited key path, its value
474will be returned instead. E.g.
475
476```json
477{
478    "datastore.metric.host": "0.0.0.0",
479    "host": {
480        "address": "localhost",
481        "port": 5799
482    },
483    "datastore": {
484        "metric": {
485            "host": "127.0.0.1",
486            "port": 3099
487        },
488        "warehouse": {
489            "host": "198.0.0.1",
490            "port": 2112
491        }
492    }
493}
494
495GetString("datastore.metric.host") // returns "0.0.0.0"
496```
497
498### Extract sub-tree
499
500Extract sub-tree from Viper.
501
502For example, `viper` represents:
503
504```json
505app:
506  cache1:
507    max-items: 100
508    item-size: 64
509  cache2:
510    max-items: 200
511    item-size: 80
512```
513
514After executing:
515
516```go
517subv := viper.Sub("app.cache1")
518```
519
520`subv` represents:
521
522```json
523max-items: 100
524item-size: 64
525```
526
527Suppose we have:
528
529```go
530func NewCache(cfg *Viper) *Cache {...}
531```
532
533which creates a cache based on config information formatted as `subv`.
534Now it's easy to create these 2 caches separately as:
535
536```go
537cfg1 := viper.Sub("app.cache1")
538cache1 := NewCache(cfg1)
539
540cfg2 := viper.Sub("app.cache2")
541cache2 := NewCache(cfg2)
542```
543
544### Unmarshaling
545
546You also have the option of Unmarshaling all or a specific value to a struct, map,
547etc.
548
549There are two methods to do this:
550
551 * `Unmarshal(rawVal interface{}) : error`
552 * `UnmarshalKey(key string, rawVal interface{}) : error`
553
554Example:
555
556```go
557type config struct {
558	Port int
559	Name string
560	PathMap string `mapstructure:"path_map"`
561}
562
563var C config
564
565err := Unmarshal(&C)
566if err != nil {
567	t.Fatalf("unable to decode into struct, %v", err)
568}
569```
570
571## Viper or Vipers?
572
573Viper comes ready to use out of the box. There is no configuration or
574initialization needed to begin using Viper. Since most applications will want
575to use a single central repository for their configuration, the viper package
576provides this. It is similar to a singleton.
577
578In all of the examples above, they demonstrate using viper in it's singleton
579style approach.
580
581### Working with multiple vipers
582
583You can also create many different vipers for use in your application. Each will
584have it’s own unique set of configurations and values. Each can read from a
585different config file, key value store, etc. All of the functions that viper
586package supports are mirrored as methods on a viper.
587
588Example:
589
590```go
591x := viper.New()
592y := viper.New()
593
594x.SetDefault("ContentDir", "content")
595y.SetDefault("ContentDir", "foobar")
596
597//...
598```
599
600When working with multiple vipers, it is up to the user to keep track of the
601different vipers.
602
603## Q & A
604
605Q: Why not INI files?
606
607A: Ini files are pretty awful. There’s no standard format, and they are hard to
608validate. Viper is designed to work with JSON, TOML or YAML files. If someone
609really wants to add this feature, I’d be happy to merge it. It’s easy to specify
610which formats your application will permit.
611
612Q: Why is it called “Viper”?
613
614A: Viper is designed to be a [companion](http://en.wikipedia.org/wiki/Viper_(G.I._Joe))
615to [Cobra](https://github.com/spf13/cobra). While both can operate completely
616independently, together they make a powerful pair to handle much of your
617application foundation needs.
618
619Q: Why is it called “Cobra”?
620
621A: Is there a better name for a [commander](http://en.wikipedia.org/wiki/Cobra_Commander)?
622