flags

package
v0.73.0-alpha2025020401 Latest Latest
Warning

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

Go to latest
Published: Feb 4, 2025 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package flags provides tools that are used by all commands to create deprecation flags with strict controls.

Index

Constants

View Source
const (
	// TgPrefix is an environment variable prefix.
	TgPrefix = "TG"

	// TerragruntPrefix is an environment variable deprecated prefix.
	TerragruntPrefix = "TERRAGRUNT"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type DeprecatedFlag

type DeprecatedFlag struct {
	cli.Flag
	// contains filtered or unexported fields
}

DeprecatedFlag represents a deprecated flag that is not shown in the CLI help, but its names, envVars, are registered.

func (*DeprecatedFlag) GetEnvVars

func (flag *DeprecatedFlag) GetEnvVars() []string

GetEnvVars implements `cli.Flag` interface.

func (*DeprecatedFlag) Names

func (flag *DeprecatedFlag) Names() []string

Names implements `cli.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 Flag

type Flag struct {
	cli.Flag
	// contains filtered or unexported fields
}

Flag is a wrapper for `cli.Flag` that avoids displaying deprecated flags in help, but registers their flag names and environment variables.

func NewFlag

func NewFlag(main cli.Flag, opts ...Option) *Flag

NewFlag returns a new Flag instance.

func (*Flag) Apply

func (main *Flag) Apply(set *flag.FlagSet) error

Apply implements `cli.Flag` interface.

func (*Flag) RunAction

func (main *Flag) RunAction(ctx *cli.Context) error

RunAction implements `cli.Flag` interface.

func (*Flag) TakesValue

func (main *Flag) TakesValue() bool

TakesValue implements `cli.Flag` interface.

func (*Flag) Value

func (main *Flag) Value() cli.FlagValue

Value implements `cli.Flag` interface.

type NewValueFunc

type NewValueFunc func(flagValue cli.FlagValue) string

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 WithDeprecatedFlag

func WithDeprecatedFlag(deprecatedFlag cli.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 main flag when this deprecated flag triggers. For example:

NewFlag(&cli.GenericFlag[string]{
  Name:    "log-format",
}, WithDeprecatedFlag(&cli.BoolFlag{
  Name:    "terragrunt-json-log",
}, flags.NewValue("json"), nil))

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 main flag, but with the specified `prefix` prepended to the names and environment variables. Should be used with caution, as changing the name of the main flag will change the name of the deprecated flag. For example:

NewFlag(&cli.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-disabe-color" names and "TERRAGRUNT_NO_COLOR","TERRAGRUNT_DISABLE_COLOR" env vars.

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) Append

func (prefix Prefix) Append(val string) Prefix

Append adds a value to the end of the slice.

func (Prefix) EnvVar

func (prefix Prefix) EnvVar(name string) string

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) EnvVars

func (prefix Prefix) EnvVars(names ...string) []string

EnvVars does the same `EnvVar`, except it takes and returns the slice.

func (Prefix) FlagName

func (prefix Prefix) FlagName(name string) string

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.

func (Prefix) FlagNames

func (prefix Prefix) FlagNames(names ...string) []string

FlagNames does the same `FlagName`, except it takes and returns the slice.

func (Prefix) Prepend

func (prefix Prefix) Prepend(val string) Prefix

Prepend adds a value to the beginning of the slice.

type RegisterStrictControlsFunc

type RegisterStrictControlsFunc func(flagNameControl, envVarControl strict.Control)

RegisterStrictControlsFunc represents a callback func that registers the given controls in the `opts.StrictControls` stict control tree .

func StrictControls

func StrictControls(strcitControls strict.Controls, controlNames ...string) RegisterStrictControlsFunc

StrictControls returns a callback function that adds the taken controls as subcontrols for the given `controlNames`. And assigns the "Global Flag" category to these controls.

func StrictControlsByGroup

func StrictControlsByGroup(strcitControls strict.Controls, commandName string, controlNames ...string) RegisterStrictControlsFunc

StrictControlsByGroup returns a callback function that adds the taken controls as subcontrols for the given `controlNames`. Using the given `commandName` as categories.

Directories

Path Synopsis
Package global provides CLI global flags.
Package global provides CLI global flags.

Jump to

Keyboard shortcuts

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