Documentation ¶
Overview ¶
Package config contains helper functions for parsing of configuration files.
Index ¶
- Constants
- func DefineDirFlag()
- func DefineFlagsFor(name string)
- func Dir() (dir string, err error)
- func EnvVar(name string) string
- func Filename(name string) string
- func FlagName(name string) string
- func ParseConfigFromYamlFile(path string, cfg interface{}) error
- func SaveConfigToYamlFile(cfg interface{}, path string, perm os.FileMode, comment string) error
- type FlagSet
- type Option
- type PluginConfig
Constants ¶
const ( // FlagSuffix is added to plugin name while loading plugins configuration. FlagSuffix = "-config" // EnvSuffix is added to plugin name while loading plugins configuration from ENV variable. EnvSuffix = "_CONFIG" // FileExtension is used as a default extension for config files in flags. FileExtension = ".conf" )
const ( // DirFlag as flag name (see implementation in declareFlags()) // is used to define default directory where config files reside. // This flag name is derived from the name of the plugin. DirFlag = "config-dir" // DirDefault holds a default value "." for flag, which represents current working directory. DirDefault = "." // DirUsage used as a flag (see implementation in declareFlags()). DirUsage = "Location of the config files; can also be set via 'CONFIG_DIR' env variable." )
Variables ¶
This section is empty.
Functions ¶
func DefineDirFlag ¶ added in v1.5.0
func DefineDirFlag()
DefineDirFlag defines flag for configuration directory.
func DefineFlagsFor ¶ added in v1.5.0
func DefineFlagsFor(name string)
DefineFlagsFor registers defined flags for plugin with given name.
func Dir ¶ added in v1.0.4
Dir returns config directory by evaluating the flag DirFlag. It interprets "." as current working directory.
func ParseConfigFromYamlFile ¶
ParseConfigFromYamlFile parses a configuration from a file in YAML format. The file's location is specified by the <path> parameter and the resulting config is stored into the structure referenced by the <cfg> parameter. If the file doesn't exist or cannot be read, the returned error will be of type os.PathError. An untyped error is returned in case the file doesn't contain a valid YAML configuration.
func SaveConfigToYamlFile ¶
SaveConfigToYamlFile saves the configuration <cfg> into a YAML-formatted file at the location <path> with permissions defined by <perm>. <comment>, if non-empty, is printed at the beginning of the file before the configuration is printed (with a line break in between). Each line in <comment> should thus begin with the number sign ( # ). If the file cannot be created af the location, os.PathError is returned. An untyped error is returned if the configuration couldn't be marshaled into the YAML format.
Types ¶
type Option ¶ added in v1.5.0
type Option func(*options)
Option is an option used in ForPlugin
func WithCustomizedFlag ¶ added in v1.5.0
WithCustomizedFlag is an option to customize config flag for plugin in ForPlugin. The parameters are used to replace defaults in this order: flag name, default, usage.
func WithExtraFlags ¶ added in v1.5.0
WithExtraFlags is an option to define additional flags for plugin in ForPlugin.
type PluginConfig ¶
type PluginConfig interface { // LoadValue parses configuration for a plugin and stores the results in data. // The argument data is a pointer to an instance of a go structure. LoadValue(data interface{}) (found bool, err error) // GetConfigName returns config name derived from plugin name: // flag = PluginName + FlagSuffix (evaluated most often as absolute path to a config file) GetConfigName() string }
PluginConfig is API for plugins to access configuration.
Aim of this API is to let a particular plugin to bind it's configuration without knowing a particular key name. The key name is injected into Plugin.
func ForPlugin ¶
func ForPlugin(name string, opts ...Option) PluginConfig
ForPlugin returns API that is injectable to a particular Plugin and is used to read it's configuration.
By default it tries to lookup `<plugin-name> + "-config"`in flags and declare the flag if it's not defined yet. There are options that can be used to customize the config flag for plugin and/or define additional flags for the plugin.