middlewares

package
v0.5.29 Latest Latest
Warning

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

Go to latest
Published: Feb 14, 2025 License: MIT Imports: 8 Imported by: 18

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ExecuteMiddlewares

func ExecuteMiddlewares(layers_ *layers.ParameterLayers, parsedLayers *layers.ParsedLayers, middlewares ...Middleware) error

ExecuteMiddlewares executes a chain of middlewares with the given parameters. It starts with an initial empty handler, then iteratively wraps it with each middleware. Finally, it calls the resulting handler with the provided layers and parsedLayers.

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)))(layers_, parsedLayers).

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 the parsedLayers, call `next` first. This means that parsedLayers will be modified in the order of the middlewares. For example, executeMiddlewares(SetFromArgs(), SetFromEnv(), SetFromDefaults()) will first set the defaults, then the environment value, and finally the command line arguments.
  • if you want to modify the layers before parsing, use the call `next` last. This means that the middlewares further down the list will get the newly updated ParameterLayers and thus potentially restrict which parameters they parse.

func Identity

func Identity(layers_ *layers.ParameterLayers, parsedLayers *layers.ParsedLayers) error

Types

type HandlerFunc

type HandlerFunc func(layers *layers.ParameterLayers, parsedLayers *layers.ParsedLayers) error

func BlacklistLayerParametersHandler

func BlacklistLayerParametersHandler(parameters_ map[string][]string) HandlerFunc

func BlacklistLayersHandler

func BlacklistLayersHandler(slugs []string) HandlerFunc

BlacklistLayersHandler removes the specified layers from the given ParameterLayers. It takes a slice of layer slugs, and deletes any layers in the ParameterLayers that match those slugs.

func WhitelistLayerParametersHandler

func WhitelistLayerParametersHandler(parameters_ map[string][]string) HandlerFunc

func WhitelistLayersHandler

func WhitelistLayersHandler(slugs []string) HandlerFunc

WhitelistLayersHandler only leaves the specified layers from the given ParameterLayers. It takes a slice of layer slugs, and deletes any layers in the ParameterLayers that don't match those slugs.

type Middleware

type Middleware func(HandlerFunc) HandlerFunc

func BlacklistLayerParameters

func BlacklistLayerParameters(parameters_ map[string][]string) Middleware

BlacklistLayerParameters is a middleware that removes the given parameters from ParameterLayers after running `next`.

func BlacklistLayerParametersFirst

func BlacklistLayerParametersFirst(parameters_ map[string][]string) Middleware

BlacklistLayerParametersFirst is a middleware that removes the given parameters from ParameterLayers before running `next`.

func BlacklistLayers

func BlacklistLayers(slugs []string) Middleware

BlacklistLayers is a middleware that removes the given layers from ParameterLayers after running `next`.

func BlacklistLayersFirst

func BlacklistLayersFirst(slugs []string) Middleware

BlacklistLayersFirst is a middleware that removes the given layers from ParameterLayers before running `next`.

func Chain

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 GatherArguments

func GatherArguments(args []string, options ...parameters.ParseStepOption) Middleware

GatherArguments creates a middleware that parses positional arguments for the default layer. This middleware is typically used in conjunction with ParseFromCobraCommand for CLI applications that accept positional arguments.

Usage:

middleware := middlewares.GatherArguments(args, parameters.WithParseStepSource("args"))

func GatherFlagsFromProfiles added in v0.5.10

func GatherFlagsFromProfiles(
	defaultProfileFile string,
	profileFile string,
	profile string,
	options ...parameters.ParseStepOption) Middleware

func GatherFlagsFromViper

func GatherFlagsFromViper(options ...parameters.ParseStepOption) Middleware

GatherFlagsFromViper creates a middleware that loads parameter values from Viper configuration. This middleware is useful for integrating Viper-based configuration management with Glazed commands.

It iterates through each layer, gathering flags from Viper for all parameters in that layer.

Usage:

middleware := middlewares.GatherFlagsFromViper(parameters.WithParseStepSource("viper"))

func GatherSpecificFlagsFromViper added in v0.5.19

func GatherSpecificFlagsFromViper(flags []string, options ...parameters.ParseStepOption) Middleware

GatherSpecificFlagsFromViper creates a middleware that loads specific parameter 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 parameters from Viper while leaving others untouched.

Usage:

middleware := middlewares.GatherSpecificFlagsFromViper(
    []string{"flag1", "flag2"},
    parameters.WithParseStepSource("viper"),
)

func LoadParametersFromFile

func LoadParametersFromFile(filename string, options ...parameters.ParseStepOption) Middleware

LoadParametersFromFile loads parameter definitions from a JSON file and applies them to the parameter layers.

func MergeParsedLayer added in v0.5.26

func MergeParsedLayer(layerSlug string, layerToMerge *layers.ParsedLayer) Middleware

MergeParsedLayer is a middleware that merges a parsed layer into an existing one. It first calls next, then merges the provided layer into the specified one. If the target layer doesn't exist, it will be created.

func MergeParsedLayers added in v0.5.26

func MergeParsedLayers(layersToMerge *layers.ParsedLayers) Middleware

MergeParsedLayers is a middleware that merges multiple parsed layers at once. It first calls next, then merges all provided layers into the existing ones. If a target layer doesn't exist, it will be created.

func MergeParsedLayersSelective added in v0.5.26

func MergeParsedLayersSelective(layersToMerge *layers.ParsedLayers, slugs []string) Middleware

MergeParsedLayersSelective is a middleware that merges only the specified layers from the provided ParsedLayers. It first calls next, then merges only the layers specified in slugs from layersToMerge into the existing layers. If a layer in slugs doesn't exist in layersToMerge, it is skipped. If a target layer doesn't exist in parsedLayers, it will be created.

func ParseFromCobraCommand

func ParseFromCobraCommand(cmd *cobra.Command, options ...parameters.ParseStepOption) Middleware

ParseFromCobraCommand creates a middleware that parses parameter 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 layer, and if the layer implements the CobraParameterLayer interface, it parses the layer's parameters from the Cobra command.

Usage:

middleware := middlewares.ParseFromCobraCommand(cmd, parameters.WithParseStepSource("flags"))

func ReplaceParsedLayer added in v0.5.26

func ReplaceParsedLayer(layerSlug string, newLayer *layers.ParsedLayer) Middleware

ReplaceParsedLayer is a middleware that replaces a parsed layer with a new one. It first calls next, then replaces the specified layer with a clone of the provided one. If the layer doesn't exist in the original parsedLayers, it will be added.

func ReplaceParsedLayers added in v0.5.26

func ReplaceParsedLayers(newLayers *layers.ParsedLayers) Middleware

ReplaceParsedLayers is a middleware that replaces multiple parsed layers at once. It first calls next, then replaces all specified layers with clones of the provided ones. If a layer doesn't exist in the original parsedLayers, it will be added.

func ReplaceParsedLayersSelective added in v0.5.26

func ReplaceParsedLayersSelective(newLayers *layers.ParsedLayers, slugs []string) Middleware

ReplaceParsedLayersSelective is a middleware that replaces only the specified layers from the provided ParsedLayers. It first calls next, then replaces only the layers specified in slugs with clones from newLayers. If a layer in slugs doesn't exist in newLayers, it is skipped.

func SetFromDefaults

func SetFromDefaults(options ...parameters.ParseStepOption) Middleware

SetFromDefaults is a middleware that sets default values from parameter definitions. It calls the next handler, and then iterates through each layer and parameter definition. If a default is defined, it sets that as the parameter value in the parsed layer.

func UpdateFromEnv

func UpdateFromEnv(prefix string, options ...parameters.ParseStepOption) Middleware

func UpdateFromMap

func UpdateFromMap(m map[string]map[string]interface{}, options ...parameters.ParseStepOption) Middleware

UpdateFromMap takes a map where the keys are layer slugs and the values are maps of parameter name -> value. It calls next, and then merges the provided values into the parsed layers, skipping any layers not present in layers_.

func UpdateFromMapAsDefault

func UpdateFromMapAsDefault(m map[string]map[string]interface{}, options ...parameters.ParseStepOption) Middleware

UpdateFromMapAsDefault takes a map where the keys are layer slugs and the values are maps of parameter name -> value. It calls next, and then merges the provided values into the parsed layers if the parameter hasn't already been set, skipping any layers not present in layers_.

func UpdateFromMapAsDefaultFirst

func UpdateFromMapAsDefaultFirst(m map[string]map[string]interface{}, options ...parameters.ParseStepOption) Middleware

UpdateFromMapAsDefaultFirst takes a map where the keys are layer slugs and the values are maps of parameter name -> value. It calls next, and then merges the provided values into the parsed layers if the parameter hasn't already been set, skipping any layers not present in layers_.

func UpdateFromMapFirst

func UpdateFromMapFirst(m map[string]map[string]interface{}, options ...parameters.ParseStepOption) Middleware

UpdateFromMapFirst takes a map where the keys are layer slugs and the values are maps of parameter name -> value. It calls next, and then merges the provided values into the parsed layers, skipping any layers not present in layers_.

func UpdateFromStringList added in v0.5.1

func UpdateFromStringList(prefix string, args []string, options ...parameters.ParseStepOption) Middleware

func WhitelistLayerParameters

func WhitelistLayerParameters(parameters_ map[string][]string) Middleware

func WhitelistLayerParametersFirst

func WhitelistLayerParametersFirst(parameters_ map[string][]string) Middleware

func WhitelistLayers

func WhitelistLayers(slugs []string) Middleware

func WhitelistLayersFirst

func WhitelistLayersFirst(slugs []string) Middleware

func WrapWithBlacklistedLayers

func WrapWithBlacklistedLayers(slugs []string, nextMiddlewares ...Middleware) Middleware

WrapWithBlacklistedLayers wraps a middleware that restricts layers to a specified set of slugs, with any additional middlewares. It makes it possible to apply a subset of middlewares to only certain restricted layers.

func WrapWithBlacklistedParameterLayers

func WrapWithBlacklistedParameterLayers(parameters_ map[string][]string, nextMiddlewares ...Middleware) Middleware

func WrapWithLayerModifyingHandler

func WrapWithLayerModifyingHandler(m HandlerFunc, nextMiddlewares ...Middleware) Middleware

WrapWithLayerModifyingHandler wraps a middleware that modifies the layers with additional middlewares. It clones the original layers, calls the layer modifying middleware, chains any additional middlewares, calls next with the original layers, and returns any errors.

This makes it possible to restrict a set of middlewares to only apply to a restricted subset of layers. However, the normal set of middlewares is allowed to continue as normal.

func WrapWithWhitelistedLayers

func WrapWithWhitelistedLayers(slugs []string, nextMiddlewares ...Middleware) Middleware

WrapWithWhitelistedLayers wraps a middleware that restricts layers to a specified set of slugs, with any additional middlewares. It makes it possible to apply a subset of middlewares to only certain restricted layers.

func WrapWithWhitelistedParameterLayers

func WrapWithWhitelistedParameterLayers(parameters_ map[string][]string, nextMiddlewares ...Middleware) Middleware

Jump to

Keyboard shortcuts

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