Documentation ¶
Overview ¶
Package configo provides flexible configurtion for go applications from any number of 'sources', each of which implement the Source interface also provided by this package.
Sources provided in this package:
- JSONSource (key/value pairs in JSON content)
- EnvironmentSource (key/value pairs from the environment)
- CLISource (key/value pairs via command line flags)
- DefaultSource (key/value pairs manually configured by the application)
- ConditionalSource (filters key/value retrieval based on a condition)
These basic sources have been composed into additional sources, made available via the following constructor methods:
- FromDevelopmentOnlyDefaults()
- FromRequiredInProductionJSONFile()
- FromDefaultCLIConfigFileSource()
- FromCLIConfigFileSource(path string)
- etc...
Any of these sources may be provided to a Reader which is then used to retrieve configuration values based on keys contained in the sources.
The reader can fetch values of various types:
func (*Reader) Strings(key string) []string func (*Reader) String(key string) string func (*Reader) Ints(key string) []int func (*Reader) Int(key string) int func (*Reader) Bool(key string) bool func (*Reader) URLs(key string) []net.url.URL func (*Reader) URL(key string) net.url.URL
For each of the types returned above there are different ways to handle the scenario when a key is not found. I'll illustrate this with the applicable Int functions (but similar methods are implemented for each returned type):
// returns zero value if key not found or values are malformed. func (*Reader) Int(key string) int // returns the value or the specified default if the key is not found. func (*Reader) IntDefault(key string, Default int) int // returns 0 and an error if key not found or values are malformed. func (*Reader) IntError(key string) (int, error) // returns the value or panics if the key is not found or the values are malformed. func (*Reader) IntPanic(key string) int // returns the value or calls log.Fatal() if the key is not found or the values are malformed. func (*Reader) IntFatal(key string) int
Here's a full example:
reader := configo.NewReader( configo.FromDefaultCLIConfigFileSource(), configo.FromCLI( configo.Flag("s3-storage-address", "The address of the s3 bucket"), ), configo.FromOptionalJSONFile("config-prod.json"), ) value := reader.URL("s3-storage-address")
Index ¶
- Constants
- Variables
- func Hostname() string
- type CLI
- type CLIConfigFileSource
- type CLISource
- type Client
- type ConditionalSource
- type DefaultPair
- type DefaultSource
- type DirectorySource
- type EnvironmentSource
- type JSONSource
- func FromConditionalJSONFile(filename string, condition func() bool) *JSONSource
- func FromConfigurableJSONFile() *JSONSource
- func FromJSONContent(raw []byte) *JSONSource
- func FromJSONFile(filename string) *JSONSource
- func FromJSONObject(values map[string]interface{}) *JSONSource
- func FromOptionalJSONFile(filename string) *JSONSource
- func FromTemplateJSON(item *Template) *JSONSource
- func FromTemplateJSONFileWithSources(templatePath, confJson string, directories ...string) *JSONSource
- func FromVaultDocument(token, address, documentName string) *JSONSource
- type MultiSource
- type Reader
- func (this *Reader) Bool(key string) bool
- func (this *Reader) BoolDefault(key string, Default bool) bool
- func (this *Reader) BoolError(key string) (bool, error)
- func (this *Reader) BoolFatal(key string) bool
- func (this *Reader) BoolPanic(key string) bool
- func (this *Reader) Duration(key string) time.Duration
- func (this *Reader) DurationDefault(key string, Default time.Duration) time.Duration
- func (this *Reader) DurationError(key string) (time.Duration, error)
- func (this *Reader) DurationFatal(key string) time.Duration
- func (this *Reader) DurationPanic(key string) time.Duration
- func (this *Reader) Int(key string) int
- func (this *Reader) IntDefault(key string, Default int) int
- func (this *Reader) IntError(key string) (int, error)
- func (this *Reader) IntFatal(key string) int
- func (this *Reader) IntPanic(key string) int
- func (this *Reader) Ints(key string) []int
- func (this *Reader) IntsDefault(key string, Default []int) []int
- func (this *Reader) IntsError(key string) ([]int, error)
- func (this *Reader) IntsFatal(key string) []int
- func (this *Reader) IntsPanic(key string) []int
- func (this *Reader) RegisterAlias(key, alias string)
- func (this *Reader) String(key string) string
- func (this *Reader) StringDefault(key string, Default string) string
- func (this *Reader) StringError(key string) (string, error)
- func (this *Reader) StringFatal(key string) string
- func (this *Reader) StringPanic(key string) string
- func (this *Reader) Strings(key string) []string
- func (this *Reader) StringsDefault(key string, Default []string) []string
- func (this *Reader) StringsError(key string) ([]string, error)
- func (this *Reader) StringsFatal(key string) []string
- func (this *Reader) StringsPanic(key string) []string
- func (this *Reader) Time(key string, format string) time.Time
- func (this *Reader) TimeDefault(key string, format string, Default time.Time) time.Time
- func (this *Reader) TimeError(key string, format string) (time.Time, error)
- func (this *Reader) TimeFatal(key string, format string) time.Time
- func (this *Reader) TimePanic(key string, format string) time.Time
- func (this *Reader) URL(key string) url.URL
- func (this *Reader) URLDefault(key string, Default url.URL) url.URL
- func (this *Reader) URLError(key string) (url.URL, error)
- func (this *Reader) URLFatal(key string) url.URL
- func (this *Reader) URLPanic(key string) url.URL
- func (this *Reader) URLs(key string) []url.URL
- func (this *Reader) URLsDefault(key string, Default []url.URL) []url.URL
- func (this *Reader) URLsError(key string) ([]url.URL, error)
- func (this *Reader) URLsFatal(key string) []url.URL
- func (this *Reader) URLsPanic(key string) []url.URL
- type RetryClient
- type Source
- type Template
- type VaultSource
Constants ¶
const ( DateFormat = "2006-01-02" TimeFormat = "15:04:05" DateTimeFormat = DateFormat + " " + TimeFormat )
A few useful Date/Time stamp formats:
Variables ¶
var ( KeyNotFoundError = errors.New("The specified key was not found.") MalformedValueError = errors.New("The specified value could not be parsed.") )
Functions ¶
Types ¶
type CLI ¶
type CLI func(*CLISource)
func BoolFlag ¶
BoolFlag registers a boolean flag and corresponding usage description with the CLISource. The advantage of this method over Flag for boolean values is that the user can merely supply the flag without a value to set the boolean flag to true. This doesn't work with Flag.
func ContinueOnError ¶
func ContinueOnError() CLI
ContinueOnError sets the flag.ErrorHandling mode of the internal *flag.FlagSet to flag.ContinueOnError. Must be called before Initialize is called.
func PanicOnError ¶
func PanicOnError() CLI
PanicOnError sets the flag.ErrorHandling mode of the internal *flag.FlagSet to flag.PanicOnError. Must be called before Initialize is called.
type CLIConfigFileSource ¶
type CLIConfigFileSource struct {
// contains filtered or unexported fields
}
CLIConfigFileSource registers a command line flag for specifying an optional json config file, beyond any other config file definitions that follow this one. It is intended to be used to provide an override to the regularly used config file(s), like when you might be debugging in production (admit it, you've been there too).
func FromCLIConfigFileSource ¶
func FromCLIConfigFileSource(flagName string) *CLIConfigFileSource
FromCLIConfigFileSource registers a command line flag with the given flagName for specifying an alternate JSON config file.
func FromDefaultCLIConfigFileSource ¶
func FromDefaultCLIConfigFileSource() *CLIConfigFileSource
FromDefaultCLIConfigFileSource registers a command line flag called "config" for specifying an alternate JSON config file.
func (*CLIConfigFileSource) Initialize ¶
func (this *CLIConfigFileSource) Initialize()
Initialize parses the command line flag and reads the altnerate JSON source.
type CLISource ¶
type CLISource struct {
// contains filtered or unexported fields
}
CLISource allows for registration of command line flags and stores their actual values, if supplied on the command line. It implements the Source interface so it can be used by a Reader.
func FromCLI ¶
FromCLI creates a new CLISource for use in a Reader. It uses a *flag.FlagSet internally to register and parse the flags. Be default the flag.ErrorHandling mode is set to flag.ExitOnError
func (*CLISource) Initialize ¶
func (this *CLISource) Initialize()
Parses the internal *flag.FlagSet. Call only after making all Flag calls.
type ConditionalSource ¶
type ConditionalSource struct {
// contains filtered or unexported fields
}
ConditionalSource resolves values based on a condition supplied as a callback.
func FromDevelopmentOnlyDefaults ¶
func FromDevelopmentOnlyDefaults(pairs ...DefaultPair) *ConditionalSource
A conditional source which determines if we are running in a development environment.
func NewConditionalSource ¶
func NewConditionalSource(condition func() bool, pairs ...DefaultPair) *ConditionalSource
NewConditionalSource creates a conditional source with the provided condition callback and key/value pairs.
func (*ConditionalSource) Initialize ¶
func (this *ConditionalSource) Initialize()
type DefaultPair ¶
type DefaultPair func(*DefaultSource)
func Default ¶
func Default(key string, values ...interface{}) DefaultPair
Default registers the provided values (which will be converted to strings) to the given key. It does NOT overwrite existing values, it appends.
type DefaultSource ¶
type DefaultSource struct {
// contains filtered or unexported fields
}
DefaultSource is allows registration of specified default values of various types.
func NewDefaultSource ¶
func NewDefaultSource(pairs ...DefaultPair) *DefaultSource
NewDefaultSource initializes a new DefaultSource.
func (*DefaultSource) Initialize ¶
func (this *DefaultSource) Initialize()
type DirectorySource ¶
type DirectorySource struct {
// contains filtered or unexported fields
}
DirectorySource makes a key-value mapping available of filename (key) to file contents (value).
func FromDirectory ¶
func FromDirectory(path string) *DirectorySource
FromDirectory reads the directory path provided. If the path does not exist, a panic will result.
func FromOptionalDirectory ¶
func FromOptionalDirectory(path string) *DirectorySource
FromOptionalDirectory reads the directory path provided, if it exists.
func (*DirectorySource) Initialize ¶
func (this *DirectorySource) Initialize()
type EnvironmentSource ¶
type EnvironmentSource struct {
// contains filtered or unexported fields
}
EnvironmentSource reads key-value pairs from the environment.
func FromEnvironment ¶
func FromEnvironment() *EnvironmentSource
FromEnvironment creates an envirnoment source capable of parsing values separated by the pipe character.
func FromEnvironmentCustomSeparator ¶
func FromEnvironmentCustomSeparator(prefix, separator string) *EnvironmentSource
FromEnvironmentCustomSeparator creates an envirnoment source capable of parsing values separated by the specified character.
func FromEnvironmentWithPrefix ¶
func FromEnvironmentWithPrefix(prefix string) *EnvironmentSource
FromEnvironmentWithPrefix creates an envirnoment source capable of: - reading values with keys all beginning with the provided prefix, - parsing values separated by the pipe character.
func (*EnvironmentSource) Initialize ¶
func (this *EnvironmentSource) Initialize()
type JSONSource ¶
type JSONSource struct {
// contains filtered or unexported fields
}
JSONSource houses key-value pairs unmarshaled from JSON data.
func FromConditionalJSONFile ¶
func FromConditionalJSONFile(filename string, condition func() bool) *JSONSource
If the provided condition returns true, the specified filename is required and must be found; otherwise loading the file is optional.
func FromConfigurableJSONFile ¶
func FromConfigurableJSONFile() *JSONSource
FromConfigurableJSONFile allows the user to configure the config file path via the -config command line flag.
func FromJSONContent ¶
func FromJSONContent(raw []byte) *JSONSource
FromJSONContent unmarshals the provided json content into a JSONSource. Any resulting error results in a panic.
func FromJSONFile ¶
func FromJSONFile(filename string) *JSONSource
FromJSONFile reads and unmarshals the file at the provided path into a JSONSource. Any resulting error results in a panic.
func FromJSONObject ¶
func FromJSONObject(values map[string]interface{}) *JSONSource
func FromOptionalJSONFile ¶
func FromOptionalJSONFile(filename string) *JSONSource
FromOptionalJSONFile is like FromJSONFile but it does not panic if the file is not found.
func FromTemplateJSON ¶
func FromTemplateJSON(item *Template) *JSONSource
Parses the provided go json template, substituting data from provided sources, and returning it as a JSON source. Panics if there is a template error.
func FromTemplateJSONFileWithSources ¶
func FromTemplateJSONFileWithSources(templatePath, confJson string, directories ...string) *JSONSource
func FromVaultDocument ¶
func FromVaultDocument(token, address, documentName string) *JSONSource
func (*JSONSource) Initialize ¶
func (this *JSONSource) Initialize()
type MultiSource ¶
type MultiSource []Source
func FromOptionalDirectories ¶
func FromOptionalDirectories(paths ...string) MultiSource
func (MultiSource) Initialize ¶
func (this MultiSource) Initialize()
type Reader ¶
type Reader struct {
// contains filtered or unexported fields
}
Reader retrieves values from the provided sources, handling conversions to the type identified by the method being called (Strings, Ints, etc...).
func NewReader ¶
NewReader initializes a new reader using the provided sources. It calls each non-nil source's Initialize() method.
func (*Reader) Bool ¶
Bool returns the boolean value associated with the given key or false if the key does not exist or the value could not be parsed as a bool.
func (*Reader) BoolDefault ¶
BoolDefault returns the boolean value associated with the given key or returns the provided default if the key does not exist or the value could not be parsed as a bool.
func (*Reader) BoolError ¶
BoolError returns the boolean value associated with the given key with an error if the key does not exist or the value could not be parsed as a bool (according to strconv.ParseBool).
func (*Reader) BoolFatal ¶
BoolFatal returns the boolean value associated with the given key or calls log.Fatal() if the key does not exist or the value could not be parsed as a bool.
func (*Reader) BoolPanic ¶
BoolPanic returns the boolean value associated with the given key or panics if the key does not exist or the value could not be parsed as a bool.
func (*Reader) Duration ¶
Duration returns the first Duration associated with the given key or returns the zero value if the key does not exist or the value could not be parsed as a Duration. For examples of duration strings see http://golang.org/pkg/time/#ParseDuration
func (*Reader) DurationDefault ¶
DurationDefault returns the first Duration associated with the given key or returns provided default if the key does not exist or the values could not be parsed as Durations.
func (*Reader) DurationError ¶
DurationError returns the first Duration associated with the given key with an error if the key does not exist or the values could not be parsed as Durations.
func (*Reader) DurationFatal ¶
DurationFatal returns the first Duration associated with the given key or calls log.Fatal() if the key does not exist or the values could not be parsed as Durations.
func (*Reader) DurationPanic ¶
DurationPanic returns the first Duration associated with the given key or panics if the key does not exist or the values could not be parsed as Durations.
func (*Reader) Int ¶
Int returns the first integer value associated with the given key or returns 0 if the key does not exist.
func (*Reader) IntDefault ¶
IntDefault returns the first integer values associated with the given key or returns the provided default if the key does not exist or the values could not be parsed as integers.
func (*Reader) IntError ¶
IntError returns the first integer value associated with the given key with an error if the key does not exist or the values could not be parsed as integers (according to strconv.Atoi).
func (*Reader) IntFatal ¶
IntFatal returns the first integer value associated with the given key or calls log.Fatal() if the key does not exist or the values could not be parsed as integers.
func (*Reader) IntPanic ¶
IntPanic returns the first integer value associated with the given key or panics if the key does not exist or the values could not be parsed as integers.
func (*Reader) Ints ¶
Ints returns all integer values associated with the given key or returns 0 if the key does not exist.
func (*Reader) IntsDefault ¶
IntsDefault returns all integer values associated with the given key or returns provided defaults if the key does not exist or the values could not be parsed as integers.
func (*Reader) IntsError ¶
IntsError returns all integer values associated with the given key with an error if the key does not exist or the values could not be parsed as integers.
func (*Reader) IntsFatal ¶
IntsFatal returns all integer values associated with the given key or calls log.Fatal() if the key does not exist or the values could not be parsed as integers.
func (*Reader) IntsPanic ¶
IntsPanic returns all integer values associated with the given key or panics if the key does not exist or the values could not be parsed as integers.
func (*Reader) RegisterAlias ¶
func (*Reader) String ¶
String returns the first value associated with the given key or an empty string if the key does not exist.
func (*Reader) StringDefault ¶
StringDefault returns the first value associated with the given key or the provided default if the key does not exist.
func (*Reader) StringError ¶
StringError returns the first value associated with the given key with an an error if the key does not exist.
func (*Reader) StringFatal ¶
StringFatal returns the first value associated with the given key or calls log.Fatal() if the key does not exist.
func (*Reader) StringPanic ¶
StringPanic returns the first value associated with the given key or panics if the key does not exist.
func (*Reader) Strings ¶
Strings returns all values associated with the given key or nil if the key does not exist.
func (*Reader) StringsDefault ¶
StringsDefault returns all values associated with the given key or the provided defaults if the key does not exist.
func (*Reader) StringsError ¶
StringsError returns all values associated with the given key with an error if the key does not exist. It does so by searching it sources, in the order they were provided, and returns the first non-error result or KeyNotFoundError.
func (*Reader) StringsFatal ¶
StringsFatal returns all values associated with the given key or log.Fatal() if the key does not exist.
func (*Reader) StringsPanic ¶
StringsPanic returns all values associated with the given key or panics if the key does not exist.
func (*Reader) Time ¶
Time returns the first Time associated with the given key or returns the zero value if the key does not exist or the value could not be parsed as a Time using the provided format. For examples of format strings see http://golang.org/pkg/time/#pkg-constants
func (*Reader) TimeDefault ¶
TimeDefault returns the first Time associated with the given key or returns provided default if the key does not exist or the values could not be parsed as Times using the provided format.
func (*Reader) TimeError ¶
TimeError returns the first Time associated with the given key with an error if the key does not exist or the values could not be parsed as Times using the provided format.
func (*Reader) TimeFatal ¶
TimeFatal returns the first Time associated with the given key or calls log.Fatal() if the key does not exist or the values could not be parsed as Times using the provided format.
func (*Reader) TimePanic ¶
TimePanic returns the first Time associated with the given key or panics if the key does not exist or the values could not be parsed as Times using the provided format.
func (*Reader) URL ¶
URL returns the first URL associated with the given key or returns the zero value if the key does not exist or the value could not be parsed as a URL.
func (*Reader) URLDefault ¶
URLDefault returns the first URL associated with the given key or returns provided defaults if the key does not exist or the values could not be parsed as URLs.
func (*Reader) URLError ¶
URLError returns the first URL associated with the given key with an error if the key does not exist or the values could not be parsed as URLs.
func (*Reader) URLFatal ¶
URLFatal returns the first URL associated with the given key or calls log.Fatal() if the key does not exist or the values could not be parsed as URLs.
func (*Reader) URLPanic ¶
URLPanic returns the first URL associated with the given key or panics if the key does not exist or the values could not be parsed as URLs.
func (*Reader) URLs ¶
URLs returns all URL values associated with the given key or returns the zero value if the key does not exist or the value could not be parsed as a URL.
func (*Reader) URLsDefault ¶
URLsDefault returns all URL values associated with the given key or returns provided defaults if the key does not exist or the values could not be parsed as URLs.
func (*Reader) URLsError ¶
URLsError returns all URL values associated with the given key with an error if the key does not exist or the values could not be parsed as URLs.
type RetryClient ¶
type RetryClient struct {
// contains filtered or unexported fields
}
func NewRetryClient ¶
func NewRetryClient(inner Client, retries int, timeout time.Duration) *RetryClient
type Source ¶
Source defines the methods required by a Reader. The Strings method returns all values associated with the given key with an error if the key does not exist.
func FirstOrNop ¶
func FromRequiredInProductionJSONFile ¶
If we are running in a production (non-development) environment the specified JSON filename is required; otherwise it's optional.
type Template ¶
type Template struct { Content string // contains filtered or unexported fields }
Template retrieves values from the provided sources, inserting data from the sources into the Template. Includes support for sprig template functions (@see http://masterminds.github.io/sprig/)
func NewTemplate ¶
NewTemplate initializes a template and the provided sources.
type VaultSource ¶
type VaultSource struct {
// contains filtered or unexported fields
}