mirror of
https://github.com/taigrr/wtf
synced 2025-01-18 04:03:14 -08:00
Output from 'dep status': PROJECT CONSTRAINT VERSION REVISION LATEST PKGS USED cloud.google.com/go v0.23.0 v0.23.0 0fd7230 v0.23.0 1 github.com/briandowns/openweathermap ^0.11.0 0.11 1b87579 0.11 1 github.com/gdamore/encoding branch master branch master b23993c b23993c 1 github.com/gdamore/tcell ^1.0.0 v1.0.0 061d51a v1.0.0 2 github.com/go-test/deep ^1.0.1 v1.0.1 6592d9c v1.0.1 1 github.com/golang/protobuf v1.1.0 v1.1.0 b4deda0 v1.1.0 1 github.com/google/go-github branch master branch master 2ae5df7 2ae5df7 1 github.com/google/go-querystring branch master branch master 53e6ce1 53e6ce1 1 github.com/jessevdk/go-flags ^1.4.0 v1.4.0 c6ca198 v1.4.0 1 github.com/lucasb-eyer/go-colorful v1.0 v1.0 345fbb3 v1.0 1 github.com/mattn/go-runewidth v0.0.2 v0.0.2 9e777a8 v0.0.2 1 github.com/olebedev/config branch master branch master 9a10d05 9a10d05 1 github.com/radovskyb/watcher ^1.0.2 v1.0.2 6145e14 v1.0.2 1 github.com/rivo/tview branch master branch master 71ecf1f 71ecf1f 1 github.com/yfronto/newrelic branch master branch master f7fa0c6 f7fa0c6 1 golang.org/x/net branch master branch master 1e49130 1e49130 2 golang.org/x/oauth2 branch master branch master 1e0a3fa 1e0a3fa 5 golang.org/x/text v0.3.0 v0.3.0 f21a4df v0.3.0 5 google.golang.org/api branch master branch master 00e3bb8 00e3bb8 4 google.golang.org/appengine v1.0.0 v1.0.0 150dc57 v1.0.0 10 gopkg.in/yaml.v2 ^2.2.1 v2.2.1 5420a8b v2.2.1 1 See https://golang.github.io/dep/docs/daily-dep.html
142 lines
4.0 KiB
Go
142 lines
4.0 KiB
Go
// Copyright 2012 The Gorilla Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
/*
|
|
Package config provides convenient access methods to configuration stored as
|
|
JSON or YAML.
|
|
|
|
Let's start with a simple YAML example:
|
|
|
|
development:
|
|
database:
|
|
host: localhost
|
|
users:
|
|
- name: calvin
|
|
password: yukon
|
|
- name: hobbes
|
|
password: tuna
|
|
production:
|
|
database:
|
|
host: 192.168.1.1
|
|
|
|
We can parse it using ParseYaml(), which will return a *Config instance on
|
|
success:
|
|
|
|
cfg, err := config.ParseYaml(yamlString)
|
|
|
|
An equivalent JSON configuration could be built using ParseJson():
|
|
|
|
cfg, err := config.ParseJson(jsonString)
|
|
|
|
From now, we can retrieve configuration values using a path in dotted notation:
|
|
|
|
// "localhost"
|
|
host, err := cfg.String("development.database.host")
|
|
|
|
// or...
|
|
|
|
// "192.168.1.1"
|
|
host, err := cfg.String("production.database.host")
|
|
|
|
Besides String(), other types can be fetched directly: Bool(), Float64(),
|
|
Int(), Map() and List(). All these methods will return an error if the path
|
|
doesn't exist, or the value doesn't match or can't be converted to the
|
|
requested type.
|
|
|
|
A nested configuration can be fetched using Get(). Here we get a new *Config
|
|
instance with a subset of the configuration:
|
|
|
|
cfg, err := cfg.Get("development")
|
|
|
|
Then the inner values are fetched relatively to the subset:
|
|
|
|
// "localhost"
|
|
host, err := cfg.String("database.host")
|
|
|
|
For lists, the dotted path must use an index to refer to a specific value.
|
|
To retrieve the information from a user stored in the configuration above:
|
|
|
|
// map[string]interface{}{ ... }
|
|
user1, err := cfg.Map("development.users.0")
|
|
// map[string]interface{}{ ... }
|
|
user2, err := cfg.Map("development.users.1")
|
|
|
|
// or...
|
|
|
|
// "calvin"
|
|
name1, err := cfg.String("development.users.0.name")
|
|
// "hobbes"
|
|
name2, err := cfg.String("development.users.1.name")
|
|
|
|
JSON or YAML strings can be created calling the appropriate Render*()
|
|
functions. Here's how we render a configuration like the one used in these
|
|
examples:
|
|
|
|
cfg := map[string]interface{}{
|
|
"development": map[string]interface{}{
|
|
"database": map[string]interface{}{
|
|
"host": "localhost",
|
|
},
|
|
"users": []interface{}{
|
|
map[string]interface{}{
|
|
"name": "calvin",
|
|
"password": "yukon",
|
|
},
|
|
map[string]interface{}{
|
|
"name": "hobbes",
|
|
"password": "tuna",
|
|
},
|
|
},
|
|
},
|
|
"production": map[string]interface{}{
|
|
"database": map[string]interface{}{
|
|
"host": "192.168.1.1",
|
|
},
|
|
},
|
|
}
|
|
|
|
json, err := config.RenderJson(cfg)
|
|
|
|
// or...
|
|
|
|
yaml, err := config.RenderYaml(cfg)
|
|
|
|
This results in a configuration string to be stored in a file or database.
|
|
|
|
For more more convenience it can parse OS environment variables and command line arguments.
|
|
|
|
cfg, err := config.ParseYaml(yamlString)
|
|
cfg.Env()
|
|
|
|
// or
|
|
|
|
cfg.Flag()
|
|
|
|
We can also specify the order of parsing:
|
|
|
|
cfg.Env().Flag()
|
|
|
|
// or
|
|
|
|
cfg.Flag().Env()
|
|
|
|
In case of OS environment all existing at the moment of parsing keys will be scanned in OS environment,
|
|
but in uppercase and the separator will be `_` instead of a `.`. If EnvPrefix() is used the given prefix
|
|
will be used to lookup the environment variable, e.g PREFIX_FOO_BAR will set foo.bar.
|
|
In case of flags separator will be `-`.
|
|
In case of command line arguments possible to use regular dot notation syntax for all keys.
|
|
For see existing keys we can run application with `-h`.
|
|
|
|
We can use unsafe method to get value:
|
|
|
|
// ""
|
|
cfg.UString("undefined.key")
|
|
|
|
// or with default value
|
|
unsafeValue := cfg.UString("undefined.key", "default value")
|
|
|
|
There is unsafe methods, like regular, but wuth prefix `U`.
|
|
*/
|
|
package config
|