Documentation
¶
Overview ¶
Package profile provides named configurations. Each non-default profile reads its values from its own file, so profiles do not share values. A Registry finds profiles by name.
Index ¶
Constants ¶
const ConfigKey = "config_profile"
ConfigKey is the configuration key that holds the name of the profile to use. An empty value, or "default", selects the default profile. The key is read from any source that the configuration of the application reads. For example, a configuration with automatic environment variables enabled, as configuration.New creates, reads the unprefixed environment variable CONFIG_PROFILE.
const DefaultName = "default"
DefaultName is the name of the default profile, whose configuration is the base configuration of the registry.
const DirKey = "config_profile_dir"
DirKey is the configuration key of the directory that directly holds the profile files, one <name>.json file per profile. The directory must be absolute. An application sets a default for it. Without a value, selecting a profile other than the default one fails with an error, see workflow.GetProfileRegistry. Like ConfigKey, the key is read from any source that the configuration reads, for example the unprefixed environment variable CONFIG_PROFILE_DIR with automatic environment variables enabled.
Variables ¶
var ErrCorrupt = errors.New("profile file is not valid")
ErrCorrupt is returned when a profile file exists but is not a valid configuration file.
var ErrInvalidName = errors.New("invalid profile name")
ErrInvalidName is returned when a profile name is not allowed.
var ErrNotFound = errors.New("profile not found")
ErrNotFound is returned when a profile does not exist.
Deprecated: a profile without a file is an empty profile, nothing returns this error any more.
Functions ¶
This section is empty.
Types ¶
type FileRegistryOption ¶ added in v0.41.0
type FileRegistryOption func(*fileRegistry)
FileRegistryOption configures a registry that NewFileRegistry returns.
func WithLogger ¶ added in v0.41.0
func WithLogger(logger *zerolog.Logger) FileRegistryOption
WithLogger makes the registry log, at debug level, each profile that has no file, once per profile name. A nil logger logs nothing.
type Profile ¶
type Profile interface {
// Name is the profile's name.
Name() string
// Configuration holds the profile's values. The same instance is returned on every call.
Configuration() configuration.Configuration
}
Profile is one named profile.
type Registry ¶
type Registry interface {
// Profile returns the profile with the given name. A profile without a file is empty, and reading it creates no
// file: the file is created by the first persisted write. It returns ErrInvalidName for a name that is not allowed,
// and ErrCorrupt for an entry that is not a regular file holding a configuration.
Profile(name string) (Profile, error)
}
Registry finds profiles by name.
func NewFileRegistry ¶
func NewFileRegistry(dir string, base configuration.Configuration, opts ...FileRegistryOption) (Registry, error)
NewFileRegistry returns a registry that reads each profile from dir/<name>.json, where dir is the profile directory itself. The default profile is base itself, and every other profile is derived from base. dir must be absolute, and it is not created: a profile file is created by the first persisted write of its profile.