commit | 2b24bea958e2d411fc25a82e44fbbcc3b6ed0441 | [log] [tgz] |
---|---|---|
author | Nate Finch <nate.finch@gmail.com> | Tue Aug 05 07:35:21 2014 -0400 |
committer | spf13 <steve.francia@gmail.com> | Wed Aug 06 00:27:39 2014 -0400 |
tree | 7a3376b59c3e0c443c533aa1fc5d7353576d2df3 | |
parent | 3cf05f93ef6e3f9947f9259c8fbc547da79033f3 [diff] |
fix issue #8 This fixes the aliases in config files bug. Whenever we register an alias, if there is a value in the config (or defaults or override) for the alias, we move that value to the new "real key". Added a test for the bug, which fails without the changes and passes with the changes. This also fixes a bug in Hugo, where specifying "Taxonomies" in your config file doesn't get recognized, because Hugo aliases "Taxonomies" to "Indexes" which means that when the code does a Get("Taxnomies") it got translated to Get("Indexes"), which didn't exist in the original config map.
Go configuration with fangs
Viper is a complete configuration solution. Designed to work within an application to handle file based configuration and seamlessly marry that with command line flags which can also be used to control application behavior.
When building a modern application you don’t want to have to worry about configuration file formats, you want to focus on building awesome software. Viper is here to help with that.
Viper does the following for you:
Viper believes that:
Config files often can be found in multiple locations. Viper allows you to set multiple paths to search for the config file in.
Viper configuration keys are case insensitive.
viper.SetConfigName("config") // name of config file (without extension) 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.ReadInConfig() // Find and read the config file
viper.SetDefault("ContentDir", "content") viper.SetDefault("LayoutDir", "layouts") viper.SetDefault("Indexes", map[string]string{"tag": "tags", "category": "categories"})
viper.Set("Verbose", true) viper.Set("LogFile", LogFile)
viper.RegisterAlias("loud", "Verbose") viper.Set("verbose", true) // same result as next line viper.Set("loud", true) // same result as prior line viper.GetBool("loud") // true viper.GetBool("verbose") // true
viper.GetString("logfile") // case insensitive Setting & Getting if viper.GetBool("verbose") { fmt.Println("verbose enabled") }
Q: Why not INI files?
A: Ini files are pretty awful. There’s no standard format and they are hard to validate. Viper is designed to work with YAML, TOML or JSON files. If someone really wants to add this feature, I’d be happy to merge it. It’s easy to specify which formats your application will permit.
Q: Why is it called “viper”?
A: Viper is designed to be a companion to Cobra. While both can operate completely independently, together they make a powerful pair to handle much of your application foundation needs.
Q: Why is it called “Cobra”?
A: Is there a better name for a commander?