Documentation
¶
Overview ¶
Package config contains the program configuration. The configuration is parsed from the configuration file, environment variables, and command-line arguments.
Index ¶
- Constants
- Variables
- func DefaultDir() (fspath.Path, error)
- func DefaultLogOutput() (fspath.Path, error)
- func DefaultPluginsDir() (fspath.Path, error)
- func FlagName(s string) string
- func HasInvertedFlagName(s string) bool
- func InvertedFlagName(s string) string
- func Validate(cfg *Config, p []*plugins.Plugin) error
- type Config
- type ValueParser
Constants ¶
const EnvPrefix = "REGINALD" // prefix used for the environment variables.
EnvPrefix is the prefix added to the names of the config values when reading them from environment variables.
Variables ¶
var (
ErrInvalidConfig = errors.New("invalid config")
)
Errors returned from the configuration parser.
Functions ¶
func DefaultDir ¶
DefaultDir returns the default working directory for Reginald.
func DefaultLogOutput ¶
DefaultLogOutput returns the default logging output file to use.
func DefaultPluginsDir ¶
DefaultPluginsDir returns the default plugins directory to use.
func FlagName ¶
FlagName returns the command-line flag name for the given Config field s. The field name should be given as you would write it in Go syntax, for example "Logging.Output".
Flag name is primarily resolved from the "flag" tag in the struct tags for the field. The tag should be written as `flag:"<regular>,<inverted>"`where regular is the normal name of the flag that is used to either give the value or set the value as true. The inverted is available only for boolean types and it is used for getting the name of a flag that explicitly set the value of the field to false. The inverted name and the comma before it may be omitted.
If the field has no "flag" tag, the flag name will be calculated from the field name. The function converts the field's name to lower case (and to "kebab-case") and adds the names of the parent fields before the field name separated with hyphen.
func HasInvertedFlagName ¶
HasInvertedFlagName reports whether the given config value has an inverted flag name tag.
func InvertedFlagName ¶
InvertedFlagName returns the command-line flag for the given Config field for a flag that explicitly sets the value of the boolean to false. The field name should be given as you would write it in Go syntax, for example "Logging.Output".
Flag name is primarily resolved from the "flag" tag in the struct tags for the field. The tag should be written as `flag:"<regular>,<inverted>"`where regular is the normal name of the flag that is used to either give the value or set the value as true. The inverted is available only for boolean types and it is used for getting the name of a flag that explicitly set the value of the field to false. The inverted name and the comma before it may be omitted.
If the field has no inverted flag name in the "flag" tag, this function will panic.
Types ¶
type Config ¶
type Config struct {
// Directory is the "dotfiles" directory option. If it is set, Reginald
// looks for all of the relative filenames from this directory. Most
// absolute paths are still resolved relative to actual current working
// directory of the program.
Directory fspath.Path `mapstructure:"directory"`
// PluginDir is the directory where Reginald looks for the plugins.
PluginDir fspath.Path `mapstructure:"plugin-dir"`
// Defaults contains the default options set for tasks.
Defaults taskcfg.Defaults `mapstructure:"defaults"`
// Plugins contains the rest of the config options which should only be
// plugin-defined options.
Plugins map[string]any `mapstructure:",remain"` //nolint:tagliatelle // linter doesn't know about "remain"
// Tasks contains tasks and the configs for them as given in the config
// file.
Tasks []taskcfg.Config `mapstructure:"tasks"`
// Logging contains the config values for logging.
Logging logging.Config `flag:"log" mapstructure:"logging"`
// Color tells whether colors should be enabled in the user output.
Color iostreams.ColorMode `mapstructure:"color"`
// Quiet tells the program to suppress all other output than errors.
Quiet bool `mapstructure:"quiet"`
// Verbose tells the program to print more verbose output.
Verbose bool `mapstructure:"verbose"`
}
Config is the parsed configuration of the program run. There should be only one effective Config per run.
Config has a lock for locking it when it is being parsed and written to. After the parsing, Config should not be written to and, thus, the lock should no longer be used.
func DefaultConfig ¶
func DefaultConfig() *Config
DefaultConfig returns the default values for configuration. The function panics on errors.
func Parse ¶
Parse parses the configuration according to the configuration given with flagSet. The flag set should contain all of the flags for the program as the function uses the flags to override values from the configuration file. The function returns a pointer to the parsed configuration and any errors it encounters.
The function also resolves the configuration file according to the standard paths for the file or according the flags. The relevant flags are `--directory` and `--config`.
type ValueParser ¶
type ValueParser struct {
// FullName is the name of the field including the names of the parent
// fields before it separated by dots.
FullName string
// EnvName is the name of the environment variable for checking the value
// for the current field.
EnvName string
// EnvValue is the value of the environment variable for the current field.
EnvValue string
// FlagName is the name of the command-line flag for checking the value for
// the current field.
FlagName string
// Cfg is the Config that is currently being parsed.
Cfg *Config
// FlagSet is the flag set used for checking the values.
FlagSet *flags.FlagSet
// Plugin is the plugin currently being parsed if the parser has moved to
// parsing plugin configs. Otherwise, it should be nil.
Plugin *plugins.Plugin
// Plugins are the loaded Plugins.
Plugins []*plugins.Plugin
// Value is the Value of the currently parsed field.
Value reflect.Value
// Field is the currently parsed struct Field.
Field reflect.StructField
}
A ValueParser is a helper type that holds the current values for the config value that is currently being parsed.
func (*ValueParser) ApplyOverrides ¶
func (p *ValueParser) ApplyOverrides(ctx context.Context) error
ApplyOverrides applies the overrides of the config values from environment variables and command-line flags to cfg. It modifies the pointed cfg.
func (*ValueParser) LogValue ¶
func (p *ValueParser) LogValue() slog.Value
LogValue implements slog.LogValuer for [valueParser]. It returns a group containing the fields of the parser that are relevant for logging.