Documentation ¶
Overview ¶
Package config implements a file/command line based configuration.
Configuration values are defined using a struct, which can be tagged to include default values and help strings. Then, values can be read from a config file or specified in the command line.
Index ¶
- Variables
- func BoolValue(obj interface{}, name string, def bool) bool
- func Filename() string
- func IntValue(obj interface{}, name string, def int) int
- func MustParse()
- func Parse() error
- func ParseFile(filename string, config interface{}) error
- func ParseReader(r io.Reader, config interface{}) error
- func PointerValue(obj interface{}, name string, out interface{}) bool
- func Register(value interface{})
- func RegisterFunc(value interface{}, f func())
- func StringValue(obj interface{}, name string, def string) string
- type Map
- type URL
Constants ¶
This section is empty.
Variables ¶
var (
DefaultFilename = pathutil.Relative("app.conf")
)
Functions ¶
func BoolValue ¶
BoolValue returns the boolean value of the field named by name, of def if the field does not exists or does not have the correct type.
func Filename ¶
func Filename() string
Filename returns the filename used by Parse(). If the -config command line flag was provided, it returns its value. Otherwise, it returns DefaultFilename().
func IntValue ¶
IntValue returns the integer value of the field named by name, of def if the field does not exists or does not have the correct type.
func Parse ¶
func Parse() error
Parse parses all configurations previously registered using Register or RegisterFunc. See those functions for information about adding your own configuration parameters.
func ParseFile ¶
ParseFile parses the given config file into the given config struct. No signal is emitted. Look at the documentation of Parse() for information on the supported types as well as the name mangling performed in the struct fields to convert them to config file keys.
func ParseReader ¶
ParseReader parses the config from the given io.Reader into the given config struct. No signal is emitted. Look at the documentation of Parse() for information on the supported types as well as the name mangling performed in the struct fields to convert them to config file keys.
func PointerValue ¶
PointerValue sets the out parameter, which must be a pointer to a pointer of the same type as the field, to point to the same object as the field named by name. The return value indicates if the assignment could be made.
func Register ¶
func Register(value interface{})
Register is a shorthand for RegisterFunc(value, nil).
func RegisterFunc ¶
func RegisterFunc(value interface{}, f func())
Register adds a struct pointer to be parsed by the application configuration.
Supported field types include bool, string, u?int(|8|6|32|62) and float(32|64). If any config field type is not supported, Register will panic. Additionally, two struct tags are taken into account. The "help" tag is used when to provide a help string to the user when defining command like flags, while the "default" tag is used to provide a default value for the field in case it hasn't been provided as a config key nor a command line flag.
The parsing process starts by reading the config file returned by Filename() (which might be overriden by the -config command line flag), and then parses any flags provided in the command line. This means any value in the config file might be overriden by a command line flag.
Go's idiomatic camel-cased struct field names are mangled into lowercase words to produce the flag names and config fields. e.g. a field named "FooBar" will produce a "-foo-bar" flag and a "foo-bar" config key. Embedded struct are flattened, as if their fields were part of the container struct.
var MyConfig struct { MyStringValue string MyINTValue int `help:"Some int used for something" default:"42"` } func init() { config.Register(&MyConfig) config.MustParse() } // This config would define the flags -my-string-value and -my-int-value // as well as the config file keys my-string-value and my-int-value.
Note that registering several structs with the same field names will cause Parse to return an error. Gondola itself registers a few flags. To see them all, start your app with the -h flag (e.g. ./myapp -h on Unix or myapp.exe -h on Windows).
func StringValue ¶
StringValue returns the string value of the field named by name, of def if the field does not exists or does not have the correct type.
Types ¶
type Map ¶
Map is a conveniency type for representing the query and fragment specified in a configuration pseudo-url.
func (Map) Get ¶
Get returns the value for the given option, or the empty string if no such option was specified.
type URL ¶
URL represents a pseudo URL which is used to specify a configuration e.g. postgres://dbname=foo user=bar password=baz. Systems in Gondola using this configuration type include the cache, the ORM and the blobstore. Config URLs are parsed using the following algorithm:
- Anything before the :// is parsed as Scheme
- The part from the :// until the end or the first ? is parsed as Value
- The part from the ? until the first # is parsed as Query.
- The remaining, if any, is parsed as Fragment
- Anything after the ? is parsed as a query string and stored in Options, with the difference that multiple values for the same parameter are not supported. Only the last one is taken into account.
func MustParseURL ¶
MustParseURL works like ParseURL, but panics if there's an error.
func (*URL) ValueAndQuery ¶
ValueAndQuery returns the Value with the Query, using a ? as a separator when the latter is nonempty.