config

package
v0.0.0-...-ba7cdbd Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 10, 2014 License: MPL-2.0 Imports: 13 Imported by: 0

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

Constants

This section is empty.

Variables

View Source
var (
	DefaultFilename = pathutil.Relative("app.conf")
)

Functions

func BoolValue

func BoolValue(obj interface{}, name string, def bool) bool

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

func IntValue(obj interface{}, name string, def int) int

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 MustParse

func MustParse()

MustParse works like Parse, but panics if there's an error.

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

func ParseFile(filename string, config interface{}) error

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

func ParseReader(r io.Reader, config interface{}) error

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

func PointerValue(obj interface{}, name string, out interface{}) bool

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

func StringValue(obj interface{}, name string, def string) string

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

type Map map[string]string

Map is a conveniency type for representing the query and fragment specified in a configuration pseudo-url.

func (Map) Get

func (m Map) Get(key string) string

Get returns the value for the given option, or the empty string if no such option was specified.

func (Map) Int

func (m Map) Int(key string) (int, bool)

Int returns the int value for the given option. The second return value is true iff the key was present and it could be parsed as an int.

func (Map) String

func (m Map) String() string

String returns the options encoded as a query string.

type URL

type URL struct {
	Scheme   string
	Value    string
	Query    Map
	Fragment Map
}

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

func MustParseURL(s string) *URL

MustParseURL works like ParseURL, but panics if there's an error.

func ParseURL

func ParseURL(s string) (*URL, error)

ParseURL parses the given string into a *URL, if possible.

func (*URL) Parse

func (u *URL) Parse(s string) error

Parse parses the given string into a configuration URL.

func (*URL) String

func (u *URL) String() string

String returns the URL as a string.

func (*URL) ValueAndQuery

func (u *URL) ValueAndQuery() string

ValueAndQuery returns the Value with the Query, using a ? as a separator when the latter is nonempty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL