Documentation
¶
Index ¶
- func Execute(schema_ *schema.Schema, parsedValues *values.Values, middlewares ...Middleware) error
- func Identity(schema_ *schema.Schema, parsedValues *values.Values) error
- type ConfigFileMapper
- type ConfigFileOption
- type ConfigFilesResolver
- type ConfigMapper
- type HandlerFunc
- type Middleware
- func BlacklistSectionFields(fieldsBySection map[string][]string) Middleware
- func BlacklistSectionFieldsFirst(fieldsBySection map[string][]string) Middleware
- func BlacklistSections(slugs []string) Middleware
- func BlacklistSectionsFirst(slugs []string) Middleware
- func Chain(ms ...Middleware) Middleware
- func FromArgs(args []string, options ...fields.ParseOption) Middleware
- func FromCobra(cmd *cobra.Command, options ...fields.ParseOption) Middleware
- func FromDefaults(options ...fields.ParseOption) Middleware
- func FromEnv(prefix string, options ...fields.ParseOption) Middleware
- func FromFile(filename string, options ...ConfigFileOption) Middleware
- func FromFiles(files []string, options ...ConfigFileOption) Middleware
- func FromMap(m map[string]map[string]interface{}, options ...fields.ParseOption) Middleware
- func FromMapAsDefault(m map[string]map[string]interface{}, options ...fields.ParseOption) Middleware
- func FromMapAsDefaultFirst(m map[string]map[string]interface{}, options ...fields.ParseOption) Middleware
- func FromMapFirst(m map[string]map[string]interface{}, options ...fields.ParseOption) Middleware
- func GatherFlagsFromCustomProfiles(profileName string, options ...ProfileOption) Middleware
- func GatherFlagsFromProfiles(defaultProfileFile string, profileFile string, profile string, ...) Middleware
- func GatherFlagsFromViper(options ...fields.ParseOption) Middlewaredeprecated
- func GatherSpecificFlagsFromViper(flags []string, options ...fields.ParseOption) Middlewaredeprecated
- func LoadFieldsFromResolvedFilesForCobra(cmd *cobra.Command, args []string, resolver ConfigFilesResolver, ...) Middleware
- func MergeSectionValues(sectionSlug string, sectionToMerge *values.SectionValues) Middleware
- func MergeValues(valuesToMerge *values.Values) Middleware
- func MergeValuesSelective(valuesToMerge *values.Values, slugs []string) Middleware
- func ReplaceSectionValues(sectionSlug string, newSection *values.SectionValues) Middleware
- func ReplaceValues(newValues *values.Values) Middleware
- func ReplaceValuesSelective(newValues *values.Values, slugs []string) Middleware
- func UpdateFromStringList(prefix string, args []string, options ...fields.ParseOption) Middleware
- func WhitelistSectionFields(fieldsBySection map[string][]string) Middleware
- func WhitelistSectionFieldsFirst(fieldsBySection map[string][]string) Middleware
- func WhitelistSections(slugs []string) Middleware
- func WhitelistSectionsFirst(slugs []string) Middleware
- func WrapWithBlacklistedSectionFields(fieldsBySection map[string][]string, nextMiddlewares ...Middleware) Middleware
- func WrapWithBlacklistedSections(slugs []string, nextMiddlewares ...Middleware) Middleware
- func WrapWithSectionModifyingHandler(m HandlerFunc, nextMiddlewares ...Middleware) Middleware
- func WrapWithWhitelistedSectionFields(fieldsBySection map[string][]string, nextMiddlewares ...Middleware) Middleware
- func WrapWithWhitelistedSections(slugs []string, nextMiddlewares ...Middleware) Middleware
- type ProfileConfig
- type ProfileOption
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Execute ¶
Execute executes a chain of middlewares with the given schema. It starts with an initial empty handler, then iteratively wraps it with each middleware. Finally, it calls the resulting handler with the provided schema and parsed values.
Middlewares basically get executed in the reverse order they are provided, which means the first given middleware's handler will be called first.
[f1, f2, f3] will be executed as f1(f2(f3(handler)))(schema_, parsedValues).
How they call the next handler is up to them, but they should always call it.
Usually, the following rules of thumbs work well
- if all you do is modify parsed values, call `next` first. This means parsed values will be modified in the order of the middlewares. For example, executeMiddlewares(SetFromArgs(), SetFromEnv(), FromDefaults()) will first set the defaults, then the environment value, and finally the command line arguments.
- if you want to modify the schema before parsing, call `next` last. This means that the middlewares further down the list will get the newly updated schema and thus potentially restrict which fields they parse.
Types ¶
type ConfigFileMapper ¶
ConfigFileMapper is a function that transforms an arbitrary config file structure into the standard section map format: map[sectionSlug]map[fieldName]value. The input is the raw unmarshaled config data (typically from JSON or YAML). The output should map section slugs to field name/value pairs.
Example: A flat config file like {"api-key": "secret", "threshold": 5} might be mapped to {"demo": {"api-key": "secret", "threshold": 5}}
type ConfigFileOption ¶
type ConfigFileOption func(*configFileOptions)
ConfigFileOption configures behavior for FromFile and FromFiles.
func WithConfigFileMapper ¶
func WithConfigFileMapper(mapper ConfigFileMapper) ConfigFileOption
WithConfigFileMapper provides a custom mapper function to transform arbitrary config file structures into the standard section map format. If not provided, the default behavior expects:
section-slug: field-name: value
func WithConfigMapper ¶
func WithConfigMapper(mapper ConfigMapper) ConfigFileOption
WithConfigMapper provides a ConfigMapper (pattern-based or function-based) to transform config file structures into the standard section map format. This is the same as WithConfigFileMapper but accepts the ConfigMapper interface directly.
func WithParseOptions ¶
func WithParseOptions(options ...fields.ParseOption) ConfigFileOption
WithParseOptions adds parse step options that will be applied when loading fields from the config file.
type ConfigFilesResolver ¶ added in v1.0.0
type ConfigFilesResolver func(parsedValues *values.Values, cmd *cobra.Command, args []string) ([]string, error)
ConfigFilesResolver is a callback used by Cobra-specific middleware to resolve the list of config files to load in low -> high precedence order.
type ConfigMapper ¶
type ConfigMapper interface {
Map(rawConfig interface{}) (map[string]map[string]interface{}, error)
}
ConfigMapper is an interface that can map raw config data to section maps. This allows both ConfigFileMapper (function) and pattern-based mappers to be used interchangeably.
type HandlerFunc ¶ added in v1.0.0
func BlacklistSectionFieldsHandler ¶ added in v1.0.0
func BlacklistSectionFieldsHandler(fieldsBySection map[string][]string) HandlerFunc
BlacklistSectionFieldsHandler removes the specified fields per section.
func BlacklistSectionsHandler ¶ added in v1.0.0
func BlacklistSectionsHandler(slugs []string) HandlerFunc
BlacklistSectionsHandler removes the specified sections from the given schema. It takes a slice of section slugs, and deletes any sections in the schema that match those slugs.
func WhitelistSectionFieldsHandler ¶ added in v1.0.0
func WhitelistSectionFieldsHandler(fieldsBySection map[string][]string) HandlerFunc
WhitelistSectionFieldsHandler restricts each section to the specified field names.
func WhitelistSectionsHandler ¶ added in v1.0.0
func WhitelistSectionsHandler(slugs []string) HandlerFunc
WhitelistSectionsHandler only leaves the specified sections from the given schema. It takes a slice of section slugs, and deletes any sections in the schema that don't match those slugs.
type Middleware ¶
type Middleware func(HandlerFunc) HandlerFunc
func BlacklistSectionFields ¶ added in v1.0.0
func BlacklistSectionFields(fieldsBySection map[string][]string) Middleware
BlacklistSectionFields removes the given fields after running next.
func BlacklistSectionFieldsFirst ¶ added in v1.0.0
func BlacklistSectionFieldsFirst(fieldsBySection map[string][]string) Middleware
BlacklistSectionFieldsFirst removes the given fields before running next.
func BlacklistSections ¶ added in v1.0.0
func BlacklistSections(slugs []string) Middleware
BlacklistSections removes the given sections after running next.
func BlacklistSectionsFirst ¶ added in v1.0.0
func BlacklistSectionsFirst(slugs []string) Middleware
BlacklistSectionsFirst removes the given sections before running next.
func Chain ¶ added in v1.0.0
func Chain(ms ...Middleware) Middleware
Chain chains together a list of middlewares into a single middleware. It does this by iteratively wrapping each middleware around the next handler.
func FromArgs ¶
func FromArgs(args []string, options ...fields.ParseOption) Middleware
FromArgs creates a middleware that parses positional arguments for the default section. This middleware is typically used in conjunction with FromCobra for CLI applications that accept positional arguments.
Usage:
middleware := middlewares.FromArgs(args, fields.WithSource("args"))
func FromCobra ¶
func FromCobra(cmd *cobra.Command, options ...fields.ParseOption) Middleware
FromCobra creates a middleware that parses field values from a Cobra command. This middleware is typically used as the highest priority in the middleware chain for CLI applications.
It iterates through each section, and if the section implements the CobraSection interface, it parses the section's fields from the Cobra command.
Usage:
middleware := middlewares.FromCobra(cmd, fields.WithSource("flags"))
func FromDefaults ¶
func FromDefaults(options ...fields.ParseOption) Middleware
FromDefaults is a middleware that sets default values from field definitions. It calls the next handler, and then iterates through each section and field definition. If a default is defined, it sets that as the field value in the parsed values.
func FromEnv ¶
func FromEnv(prefix string, options ...fields.ParseOption) Middleware
func FromFile ¶
func FromFile(filename string, options ...ConfigFileOption) Middleware
FromFile loads field definitions from a JSON or YAML file and applies them to the schema. By default, it expects the config file to have the structure:
section-slug: field-name: value
To use a custom config file structure, provide a ConfigFileMapper via WithConfigFileMapper.
func FromFiles ¶
func FromFiles(files []string, options ...ConfigFileOption) Middleware
FromFiles applies a list of config files in order (low -> high precedence). Each file is applied as a separate step; callers may add metadata via options per-call. To use a custom config file structure, provide a ConfigFileMapper via WithConfigFileMapper.
func FromMap ¶
func FromMap(m map[string]map[string]interface{}, options ...fields.ParseOption) Middleware
FromMap takes a map where the keys are section slugs and the values are maps of field name -> value. It calls next, and then merges the provided values into the parsed values, skipping any sections not present in the schema.
func FromMapAsDefault ¶ added in v1.0.0
func FromMapAsDefault(m map[string]map[string]interface{}, options ...fields.ParseOption) Middleware
FromMapAsDefault takes a map where the keys are section slugs and the values are maps of field name -> value. It calls next, and then merges the provided values into the parsed values if the field hasn't already been set, skipping any sections not present in the schema.
func FromMapAsDefaultFirst ¶ added in v1.0.0
func FromMapAsDefaultFirst(m map[string]map[string]interface{}, options ...fields.ParseOption) Middleware
FromMapAsDefaultFirst takes a map where the keys are section slugs and the values are maps of field name -> value. It calls next, and then merges the provided values into the parsed values if the field hasn't already been set, skipping any sections not present in the schema.
func FromMapFirst ¶ added in v1.0.0
func FromMapFirst(m map[string]map[string]interface{}, options ...fields.ParseOption) Middleware
FromMapFirst takes a map where the keys are section slugs and the values are maps of field name -> value. It calls next, and then merges the provided values into the parsed values, skipping any sections not present in the schema.
func GatherFlagsFromCustomProfiles ¶ added in v1.0.0
func GatherFlagsFromCustomProfiles(profileName string, options ...ProfileOption) Middleware
GatherFlagsFromCustomProfiles creates a middleware that loads profile configuration from custom sources. This middleware allows loading profiles from: 1. A specific profile file path 2. Another app's default profile locations
This is useful when you want to load profile configuration from a different source than the default application profiles.
Usage:
// Load from specific profile file
middleware := middlewares.GatherFlagsFromCustomProfiles(
"production",
middlewares.WithProfileFile("/path/to/custom-profiles.yaml"),
middlewares.WithProfileParseOptions(fields.WithSource("custom-profiles")),
)
// Load from another app's profiles
middleware := middlewares.GatherFlagsFromCustomProfiles(
"shared-profile",
middlewares.WithProfileAppName("other-app"),
middlewares.WithProfileParseOptions(fields.WithSource("other-app-profiles")),
)
func GatherFlagsFromProfiles ¶ added in v1.0.0
func GatherFlagsFromProfiles( defaultProfileFile string, profileFile string, profile string, defaultProfileName string, options ...fields.ParseOption) Middleware
func GatherFlagsFromViper
deprecated
added in
v1.0.0
func GatherFlagsFromViper(options ...fields.ParseOption) Middleware
GatherFlagsFromViper creates a middleware that loads field values from Viper configuration. This middleware is useful for integrating Viper-based configuration management with Glazed commands.
It iterates through each section, gathering flags from Viper for all fields in that section.
Usage:
middleware := middlewares.GatherFlagsFromViper(fields.WithSource("viper"))
Deprecated: Use FromFiles and FromEnv instead.
func GatherSpecificFlagsFromViper
deprecated
added in
v1.0.0
func GatherSpecificFlagsFromViper(flags []string, options ...fields.ParseOption) Middleware
GatherSpecificFlagsFromViper creates a middleware that loads specific field values from Viper configuration. This middleware is similar to GatherFlagsFromViper, but it only loads values for the specified flags.
It's useful when you want to selectively load certain fields from Viper while leaving others untouched.
Usage:
middleware := middlewares.GatherSpecificFlagsFromViper(
[]string{"flag1", "flag2"},
fields.WithSource("viper"),
)
Deprecated: Use FromFiles and FromEnv instead.
func LoadFieldsFromResolvedFilesForCobra ¶ added in v1.0.0
func LoadFieldsFromResolvedFilesForCobra( cmd *cobra.Command, args []string, resolver ConfigFilesResolver, options ...fields.ParseOption, ) Middleware
LoadFieldsFromResolvedFilesForCobra loads fields from a resolver-provided list of files (low -> high precedence). Each file is tracked as a separate parse step with metadata.
func MergeSectionValues ¶ added in v1.0.0
func MergeSectionValues(sectionSlug string, sectionToMerge *values.SectionValues) Middleware
MergeSectionValues is a middleware that merges parsed section values into an existing one. It first calls next, then merges the provided values into the specified section. If the target section doesn't exist, it will be created.
func MergeValues ¶ added in v1.0.0
func MergeValues(valuesToMerge *values.Values) Middleware
MergeValues is a middleware that merges multiple parsed sections at once. It first calls next, then merges all provided values into the existing ones. If a target section doesn't exist, it will be created.
func MergeValuesSelective ¶ added in v1.0.0
func MergeValuesSelective(valuesToMerge *values.Values, slugs []string) Middleware
MergeValuesSelective is a middleware that merges only the specified sections from the provided Values. It first calls next, then merges only the sections specified in slugs from valuesToMerge into the existing values. If a section in slugs doesn't exist in valuesToMerge, it is skipped. If a target section doesn't exist in values, it will be created.
func ReplaceSectionValues ¶ added in v1.0.0
func ReplaceSectionValues(sectionSlug string, newSection *values.SectionValues) Middleware
ReplaceSectionValues is a middleware that replaces parsed section values with a new one. It first calls next, then replaces the specified section with a clone of the provided one. If the section doesn't exist in the original values, it will be added.
func ReplaceValues ¶ added in v1.0.0
func ReplaceValues(newValues *values.Values) Middleware
ReplaceValues is a middleware that replaces multiple parsed sections at once. It first calls next, then replaces all specified sections with clones of the provided ones. If a section doesn't exist in the original values, it will be added.
func ReplaceValuesSelective ¶ added in v1.0.0
func ReplaceValuesSelective(newValues *values.Values, slugs []string) Middleware
ReplaceValuesSelective is a middleware that replaces only the specified sections from the provided Values. It first calls next, then replaces only the sections specified in slugs with clones from newValues. If a section in slugs doesn't exist in newValues, it is skipped.
func UpdateFromStringList ¶ added in v1.0.0
func UpdateFromStringList(prefix string, args []string, options ...fields.ParseOption) Middleware
func WhitelistSectionFields ¶ added in v1.0.0
func WhitelistSectionFields(fieldsBySection map[string][]string) Middleware
WhitelistSectionFields applies a field whitelist per section after running next.
func WhitelistSectionFieldsFirst ¶ added in v1.0.0
func WhitelistSectionFieldsFirst(fieldsBySection map[string][]string) Middleware
WhitelistSectionFieldsFirst applies a field whitelist per section before running next.
func WhitelistSections ¶ added in v1.0.0
func WhitelistSections(slugs []string) Middleware
WhitelistSections applies a section whitelist after running next.
func WhitelistSectionsFirst ¶ added in v1.0.0
func WhitelistSectionsFirst(slugs []string) Middleware
WhitelistSectionsFirst applies a section whitelist before running next.
func WrapWithBlacklistedSectionFields ¶ added in v1.0.0
func WrapWithBlacklistedSectionFields(fieldsBySection map[string][]string, nextMiddlewares ...Middleware) Middleware
WrapWithBlacklistedSectionFields removes fields within a subset of sections.
func WrapWithBlacklistedSections ¶ added in v1.0.0
func WrapWithBlacklistedSections(slugs []string, nextMiddlewares ...Middleware) Middleware
WrapWithBlacklistedSections wraps a middleware that restricts sections to a specified set of slugs, with any additional middlewares. It makes it possible to apply a subset of middlewares to only certain restricted sections.
func WrapWithSectionModifyingHandler ¶ added in v1.0.0
func WrapWithSectionModifyingHandler(m HandlerFunc, nextMiddlewares ...Middleware) Middleware
WrapWithSectionModifyingHandler wraps a middleware that modifies the schema with additional middlewares. It clones the original schema, calls the section-modifying middleware, chains any additional middlewares, calls next with the original schema, and returns any errors.
This makes it possible to restrict a set of middlewares to only apply to a restricted subset of sections. However, the normal set of middlewares is allowed to continue as normal.
func WrapWithWhitelistedSectionFields ¶ added in v1.0.0
func WrapWithWhitelistedSectionFields(fieldsBySection map[string][]string, nextMiddlewares ...Middleware) Middleware
WrapWithWhitelistedSectionFields restricts fields within a subset of sections.
func WrapWithWhitelistedSections ¶ added in v1.0.0
func WrapWithWhitelistedSections(slugs []string, nextMiddlewares ...Middleware) Middleware
WrapWithWhitelistedSections wraps a middleware that restricts sections to a specified set of slugs, with any additional middlewares. It makes it possible to apply a subset of middlewares to only certain restricted sections.
type ProfileConfig ¶ added in v1.0.0
type ProfileConfig struct {
ProfileName string
ProfileFile string
AppName string
Required bool
ParseOptions []fields.ParseOption
}
ProfileConfig holds configuration for the custom profile middleware
type ProfileOption ¶ added in v1.0.0
type ProfileOption func(*ProfileConfig)
ProfileOption is a function that configures ProfileConfig
func WithProfileAppName ¶ added in v1.0.0
func WithProfileAppName(appName string) ProfileOption
WithProfileAppName sets the app name to use for loading profiles from standard locations
func WithProfileFile ¶ added in v1.0.0
func WithProfileFile(profileFile string) ProfileOption
WithProfileFile sets a specific profile file path to load
func WithProfileParseOptions ¶ added in v1.0.0
func WithProfileParseOptions(options ...fields.ParseOption) ProfileOption
WithProfileParseOptions adds parse step options to the middleware
func WithProfileRequired ¶ added in v1.0.0
func WithProfileRequired(required bool) ProfileOption
WithProfileRequired sets whether the profile file and profile must exist