config

package
v2.2.0+incompatible Latest Latest
Warning

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

Go to latest
Published: Aug 23, 2019 License: Apache-2.0 Imports: 9 Imported by: 184

Documentation

Overview

Package config contains helper functions for parsing of configuration files.

Index

Constants

View Source
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"
)
View Source
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

func Dir() (dir string, err error)

Dir returns config directory by evaluating the flag DirFlag. It interprets "." as current working directory.

func EnvVar added in v1.5.0

func EnvVar(name string) string

EnvVar returns config env variable for the name, usually plugin.

func Filename added in v1.5.0

func Filename(name string) string

Filename returns config filename for the name, usually plugin.

func FlagName added in v1.5.0

func FlagName(name string) string

FlagName returns config flag name for the name, usually plugin.

func ParseConfigFromYamlFile

func ParseConfigFromYamlFile(path string, cfg interface{}) error

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

func SaveConfigToYamlFile(cfg interface{}, path string, perm os.FileMode, comment string) error

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 FlagSet added in v1.5.0

type FlagSet = flag.FlagSet

FlagSet is a type alias for flag.FlagSet.

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

func WithCustomizedFlag(s ...string) Option

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

func WithExtraFlags(f func(flags *FlagSet)) Option

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.

Jump to

Keyboard shortcuts

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