Documentation
¶
Overview ¶
Package flags provides tools that are used by all commands to create deprecation flags with strict controls.
Index ¶
- Constants
- func ErrorHandler(commands clihelper.Commands) clihelper.FlagErrHandlerFunc
- type CommandFlagHintError
- type DeprecatedFlag
- func (flag *DeprecatedFlag) AllowedSubcommandScope() bool
- func (flag *DeprecatedFlag) Evaluate(ctx context.Context) error
- func (flag *DeprecatedFlag) GetEnvVars() []string
- func (flag *DeprecatedFlag) GetHidden() bool
- func (flag *DeprecatedFlag) Names() []string
- func (flag *DeprecatedFlag) SetStrictControls(mainFlag *Flag, regControlsFn RegisterStrictControlsFunc)
- type DeprecatedFlags
- type EvaluateWrapperFunc
- type Flag
- func (newFlag *Flag) Apply(set *flag.FlagSet) error
- func (newFlag *Flag) DeprecatedNames() []string
- func (newFlag *Flag) Parse(args clihelper.Args) error
- func (newFlag *Flag) RunAction(ctx context.Context, cliCtx *clihelper.Context) error
- func (newFlag *Flag) TakesValue() bool
- func (newFlag *Flag) Value() clihelper.FlagValue
- type GlobalFlagHintError
- type NewValueFunc
- type Option
- func WithDeprecatedEnvVars(envVars []string, regControlsFn RegisterStrictControlsFunc) Option
- func WithDeprecatedFlag(deprecatedFlag clihelper.Flag, newValueFn NewValueFunc, ...) Option
- func WithDeprecatedFlagName(flagName string, regControlsFn RegisterStrictControlsFunc) Option
- func WithDeprecatedFlagNames(flagNames []string, regControlsFn RegisterStrictControlsFunc) Option
- func WithDeprecatedName(flagName string, regControlsFn RegisterStrictControlsFunc) Option
- func WithDeprecatedNames(flagNames []string, regControlsFn RegisterStrictControlsFunc) Option
- func WithDeprecatedNamesEnvVars(flagNames, envVars []string, regControlsFn RegisterStrictControlsFunc) Option
- func WithDeprecatedPrefix(prefix Prefix, regControlsFn RegisterStrictControlsFunc) Option
- func WithEvaluateWrapper(fn EvaluateWrapperFunc) Option
- type Prefix
- func (prefix Prefix) Append(val string) Prefix
- func (prefix Prefix) EnvVar(name string) string
- func (prefix Prefix) EnvVars(names ...string) []string
- func (prefix Prefix) FlagName(name string) string
- func (prefix Prefix) FlagNames(names ...string) []string
- func (prefix Prefix) Prepend(val string) Prefix
- type RegisterStrictControlsFunc
Constants ¶
const ( // TgPrefix is an environment variable prefix. TgPrefix = "TG" // TerragruntPrefix is an environment variable deprecated prefix. TerragruntPrefix = "TERRAGRUNT" )
Variables ¶
This section is empty.
Functions ¶
func ErrorHandler ¶
func ErrorHandler(commands clihelper.Commands) clihelper.FlagErrHandlerFunc
ErrorHandler returns `FlagErrHandlerFunc` which takes a flag parsing error and tries to suggest the correct command to use with this flag. Otherwise returns the error as is.
Types ¶
type CommandFlagHintError ¶
type CommandFlagHintError struct {
// contains filtered or unexported fields
}
func NewCommandFlagHintError ¶
func NewCommandFlagHintError(wrongCmd, undefFlag, cmdHint, flagHint string) *CommandFlagHintError
func (CommandFlagHintError) Error ¶
func (err CommandFlagHintError) Error() string
type DeprecatedFlag ¶
DeprecatedFlag represents a deprecated flag that is not shown in the CLI help, but its names, envVars, are registered.
func (*DeprecatedFlag) AllowedSubcommandScope ¶
func (flag *DeprecatedFlag) AllowedSubcommandScope() bool
AllowedSubcommandScope implements `clihelper.Flag` interface.
func (*DeprecatedFlag) Evaluate ¶
func (flag *DeprecatedFlag) Evaluate(ctx context.Context) error
Evaluate returns an error if the one of the controls is enabled otherwise logs warning messages and returns nil.
func (*DeprecatedFlag) GetEnvVars ¶
func (flag *DeprecatedFlag) GetEnvVars() []string
GetEnvVars implements `clihelper.Flag` interface.
func (*DeprecatedFlag) GetHidden ¶
func (flag *DeprecatedFlag) GetHidden() bool
GetHidden implements `clihelper.Flag` interface.
func (*DeprecatedFlag) Names ¶
func (flag *DeprecatedFlag) Names() []string
Names implements `clihelper.Flag` interface.
func (*DeprecatedFlag) SetStrictControls ¶
func (flag *DeprecatedFlag) SetStrictControls(mainFlag *Flag, regControlsFn RegisterStrictControlsFunc)
SetStrictControls creates a strict control for the flag and registers it.
type DeprecatedFlags ¶
type DeprecatedFlags []*DeprecatedFlag
DeprecatedFlags are multiple of DeprecatedFlag flags.
type EvaluateWrapperFunc ¶
EvaluateWrapperFunc represents a function that is used to wrap the `Evaluate(ctx context.Context) error` strict control method. Which can be passed as an option `WithEvaluateWrapper` to `NewFlag(...)` to control the behavior of strict control evaluation.
type Flag ¶
Flag is a wrapper for `clihelper.Flag` that avoids displaying deprecated flags in help, but registers their flag names and environment variables.
func (*Flag) DeprecatedNames ¶
DeprecatedNames returns all deprecated names for this flag.
func (*Flag) Parse ¶
Parse parses the given `args` for the flag value and env vars values specified in the flag. The value will be assigned to the `Destination` field. The value can also be retrieved using `flag.Value().Get()`.
func (*Flag) TakesValue ¶
TakesValue implements `github.com/urfave/clihelper.DocGenerationFlag` required to generate help. TakesValue returns `true` for all flags except boolean ones that are `false` or `true` inverted.
type GlobalFlagHintError ¶
type GlobalFlagHintError struct {
// contains filtered or unexported fields
}
func NewGlobalFlagHintError ¶
func NewGlobalFlagHintError(undefFlag, cmdHint, flagHint string) *GlobalFlagHintError
func (GlobalFlagHintError) Error ¶
func (err GlobalFlagHintError) Error() string
type NewValueFunc ¶
NewValueFunc represents a function that returns a new value for the current flag if a deprecated flag is called. Used when the current flag and the deprecated flag are of different types. For example, the string `log-format` flag must be set to `json` when deprecated bool `terragrunt-json-log` flag is used. More examples:
terragrunt-disable-log-formatting replaced with: log-format=key-value terragrunt-json-log replaced with: log-format=json terragrunt-tf-logs-to-json replaced with: log-format=json
func NewValue ¶
func NewValue(val string) NewValueFunc
NewValue returns a callback function that is used to get a new value for the current flag.
type Option ¶
type Option func(*Flag)
Option is used to set options to the `Flag`.
func WithDeprecatedEnvVars ¶
func WithDeprecatedEnvVars(envVars []string, regControlsFn RegisterStrictControlsFunc) Option
WithDeprecatedEnvVars returns an `Option` that will create a flag with the given deprecated env vars.
func WithDeprecatedFlag ¶
func WithDeprecatedFlag(deprecatedFlag clihelper.Flag, newValueFn NewValueFunc, regControlsFn RegisterStrictControlsFunc) Option
WithDeprecatedFlag returns an `Option` that will register the given `deprecatedFlag` as a deprecated flag. `newValueFn` is called to get a value for the new flag when this deprecated flag triggers. For example:
NewFlag(&clihelper.GenericFlag[string]{
Name: "log-format",
}, WithDeprecatedFlag(&clihelper.BoolFlag{
Name: "terragrunt-json-log",
}, flags.NewValue("json"), nil))
func WithDeprecatedFlagName ¶
func WithDeprecatedFlagName(flagName string, regControlsFn RegisterStrictControlsFunc) Option
WithDeprecatedFlagName does the same as `WithDeprecatedFlagNames`, but with a single name.
func WithDeprecatedFlagNames ¶
func WithDeprecatedFlagNames(flagNames []string, regControlsFn RegisterStrictControlsFunc) Option
WithDeprecatedFlagNames returns an `Option` that will create a flag with the given deprecated flag names.
func WithDeprecatedName ¶
func WithDeprecatedName(flagName string, regControlsFn RegisterStrictControlsFunc) Option
WithDeprecatedName does the same as `WithDeprecatedNames`, but with a single name.
func WithDeprecatedNames ¶
func WithDeprecatedNames(flagNames []string, regControlsFn RegisterStrictControlsFunc) Option
WithDeprecatedNames returns an `Option` that will create a deprecated flag. The given `flagNames` names will assign both names (converting to lowercase,dash) and env vars (converting to uppercase,underscore). For example:
WithDeprecatedNames([]string{"NO_COLOR", "working-dir"}, nil)
The deprecated flag will have "no-color","working-dir" names and "NO_COLOR","WORKING_DIR" env vars.
func WithDeprecatedNamesEnvVars ¶
func WithDeprecatedNamesEnvVars(flagNames, envVars []string, regControlsFn RegisterStrictControlsFunc) Option
WithDeprecatedNamesEnvVars returns an `Option` that will create a deprecated flag, with the given `flagNames`, `envVars` assigned to the flag names and environment variables as is.
func WithDeprecatedPrefix ¶
func WithDeprecatedPrefix(prefix Prefix, regControlsFn RegisterStrictControlsFunc) Option
WithDeprecatedPrefix returns an `Option` that will create a deprecated flag with the same name as the new flag, but with the specified `prefix` prepended to the names and environment variables. Should be used with caution, as changing the name of the new flag will change the name of the deprecated flag. For example:
NewFlag(&clihelper.GenericFlag[string]{
Name: "no-color",
Aliases: []string{"disable-color"},
EnvVars: []string{"NO_COLOR","DISABLE_COLOR"},
}, WithDeprecatedPrefix(Prefix{"terragrunt"}, nil))
The deprecated flag will have "terragrunt-no-color","terragrunt-disable-color" names and "TERRAGRUNT_NO_COLOR","TERRAGRUNT_DISABLE_COLOR" env vars. NOTE: This function is currently unused but retained for future flag deprecation needs.
func WithEvaluateWrapper ¶
func WithEvaluateWrapper(fn EvaluateWrapperFunc) Option
WithEvaluateWrapper returns an Option that wraps the strict control `Evaluate(ctx context.Context)` function.
type Prefix ¶
type Prefix []string
Prefix helps to combine strings into flag names or environment variables in a convenient way. Can be passed to subcommands and contain the names of parent commands, thus creating env vars as a chain of "TG prefix, parent command names, command name, flag name". For example: `TG_HLC_FMT_FILE`, where `hcl` is the parent command, `fmt` is the command and `file` is a flag. Example of use:
func main () {
ParentCommand(Prefix{TgPrefix})
}
func ParentCommand(prefix Prefix) {
Command(prefix.Append("hcl"))
}
func Command(prefix Prefix) {
Flag(prefix.Append("fmt"))
}
func Flag(prefix Prefix) {
envName := prefix.EnvVar("file") // TG_HCL_FMT_FILE
}
func (Prefix) EnvVar ¶
EnvVar returns a string that is the concatenation of the slice values with the given `name`, using underscores as separators, replacing dashes with underscores, converting to uppercase.
func (Prefix) FlagName ¶
FlagName returns a string that is the concatenation of the slice values with the given `name`, using dashes as separators, replacing dashes with underscores, converting to lowercase.
type RegisterStrictControlsFunc ¶
RegisterStrictControlsFunc represents a callback func that registers the given controls in the `opts.StrictControls` stict control tree .
func StrictControlsByCommand ¶
func StrictControlsByCommand(strictControls strict.Controls, commandName string, controlNames ...string) RegisterStrictControlsFunc
StrictControlsByCommand returns a callback function that adds the taken controls as subcontrols for the given `controlNames`. Using the given `commandName` as categories.
func StrictControlsByGlobalFlags ¶
func StrictControlsByGlobalFlags(strictControls strict.Controls, controlNames ...string) RegisterStrictControlsFunc
StrictControlsByGlobalFlags returns a callback function that adds the taken controls as subcontrols for the given `controlNames`. And assigns the "Global Flag" category to these controls.