Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Correct displaying deprecated flag warnings #3880

Merged
merged 6 commits into from
Feb 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 3 additions & 122 deletions cli/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,60 +8,35 @@ import (
"path/filepath"
"strings"

"github.com/gruntwork-io/terragrunt/cli/commands/info"
"github.com/gruntwork-io/terragrunt/cli/commands/stack"
"github.com/gruntwork-io/terragrunt/cli/flags"
"slices"

"github.com/gruntwork-io/terragrunt/engine"
"github.com/gruntwork-io/terragrunt/internal/os/exec"
"github.com/gruntwork-io/terragrunt/internal/os/signal"
"github.com/gruntwork-io/terragrunt/telemetry"
"github.com/gruntwork-io/terragrunt/tf"
"golang.org/x/exp/slices"
"golang.org/x/sync/errgroup"
"golang.org/x/text/cases"
"golang.org/x/text/language"

"github.com/gruntwork-io/terragrunt/cli/commands"
"github.com/gruntwork-io/terragrunt/cli/commands/graph"
"github.com/gruntwork-io/terragrunt/cli/commands/hclvalidate"
helpCmd "github.com/gruntwork-io/terragrunt/cli/commands/help"
versionCmd "github.com/gruntwork-io/terragrunt/cli/commands/version"
"github.com/gruntwork-io/terragrunt/cli/flags/global"

"github.com/gruntwork-io/terragrunt/cli/commands/scaffold"

"github.com/gruntwork-io/go-commons/version"
"github.com/gruntwork-io/terragrunt/config"
"github.com/gruntwork-io/terragrunt/internal/errors"
"github.com/gruntwork-io/terragrunt/util"
hashicorpversion "github.com/hashicorp/go-version"

"github.com/gruntwork-io/go-commons/env"
awsproviderpatch "github.com/gruntwork-io/terragrunt/cli/commands/aws-provider-patch"
"github.com/gruntwork-io/terragrunt/cli/commands/catalog"
execCmd "github.com/gruntwork-io/terragrunt/cli/commands/exec"
graphdependencies "github.com/gruntwork-io/terragrunt/cli/commands/graph-dependencies"
"github.com/gruntwork-io/terragrunt/cli/commands/hclfmt"
outputmodulegroups "github.com/gruntwork-io/terragrunt/cli/commands/output-module-groups"
renderjson "github.com/gruntwork-io/terragrunt/cli/commands/render-json"
runCmd "github.com/gruntwork-io/terragrunt/cli/commands/run"
runall "github.com/gruntwork-io/terragrunt/cli/commands/run-all"
terragruntinfo "github.com/gruntwork-io/terragrunt/cli/commands/terragrunt-info"
validateinputs "github.com/gruntwork-io/terragrunt/cli/commands/validate-inputs"
"github.com/gruntwork-io/terragrunt/internal/cli"
"github.com/gruntwork-io/terragrunt/options"
"github.com/gruntwork-io/terragrunt/pkg/log/format/placeholders"
)

// Command category names.
const (
MainCommandsCategoryName = "Main commands"
CatalogCommandsCategoryName = "Catalog commands"
ConfigurationCommandsCategoryName = "Configuration commands"
ShortcutsCommandsCategoryName = "OpenTofu shortcuts"
)

func init() {
cli.AppVersionTemplate = AppVersionTemplate
cli.AppHelpTemplate = AppHelpTemplate
Expand All @@ -82,8 +57,8 @@ func NewApp(opts *options.TerragruntOptions) *App {
app.Version = version.GetVersion()
app.Writer = opts.Writer
app.ErrWriter = opts.ErrWriter
app.Flags = GlobalFlags(opts)
app.Commands = TerragruntCommands(opts).WrapAction(WrapWithTelemetry(opts))
app.Flags = global.NewFlagsWithDeprecatedMovedFlags(opts)
app.Commands = commands.New(opts).WrapAction(WrapWithTelemetry(opts))
app.Before = beforeAction(opts)
app.OsExiter = OSExiter
app.ExitErrHandler = ExitErrHandler
Expand Down Expand Up @@ -150,40 +125,6 @@ func (app *App) RunContext(ctx context.Context, args []string) error {
return nil
}

// GlobalFlags returns global flags. For backward compatibility, the slice contains flags that have been moved to other commands and are hidden from the CLI help.
func GlobalFlags(opts *options.TerragruntOptions) cli.Flags {
globalFlags := global.NewFlags(opts, nil)

commands := cli.Commands{
runCmd.NewCommand(opts), // run
runall.NewCommand(opts), // runAction-all
terragruntinfo.NewCommand(opts), // terragrunt-info
validateinputs.NewCommand(opts), // validate-inputs
graphdependencies.NewCommand(opts), // graph-dependencies
renderjson.NewCommand(opts), // render-json
awsproviderpatch.NewCommand(opts), // aws-provider-patch
outputmodulegroups.NewCommand(opts), // output-module-groups
graph.NewCommand(opts), // graph
}

var seen []string

for _, cmd := range commands {
for _, flag := range cmd.Flags {
flagName := util.FirstElement(util.RemoveEmptyElements(flag.Names()))

if slices.Contains(seen, flagName) {
continue
}

seen = append(seen, flagName)
globalFlags = append(globalFlags, flags.NewMovedFlag(flag, cmd.Name, flags.StrictControlsByMovedGlobalFlags(opts.StrictControls, cmd.Name)))
}
}

return globalFlags
}

// removeNoColorFlagDuplicates removes one of the `--no-color` or `--terragrunt-no-color` arguments if both are present.
// We have to do this because `--terragrunt-no-color` is a deprecated alias for `--no-color`,
// therefore we end up specifying the same flag twice, which causes the `setting the flag multiple times` error.
Expand All @@ -208,66 +149,6 @@ func removeNoColorFlagDuplicates(args []string) []string {
return filteredArgs
}

// TerragruntCommands returns the set of Terragrunt commands.
func TerragruntCommands(opts *options.TerragruntOptions) cli.Commands {
mainCommands := cli.Commands{
runall.NewCommand(opts), // run-all
runCmd.NewCommand(opts), // run
stack.NewCommand(opts), // stack
graph.NewCommand(opts), // graph
execCmd.NewCommand(opts), // exec
}.SetCategory(
&cli.Category{
Name: MainCommandsCategoryName,
Order: 10, //nolint: mnd
},
)

catalogCommands := cli.Commands{
catalog.NewCommand(opts), // catalog
scaffold.NewCommand(opts), // scaffold
}.SetCategory(
&cli.Category{
Name: CatalogCommandsCategoryName,
Order: 20, //nolint: mnd
},
)

configurationCommands := cli.Commands{
graphdependencies.NewCommand(opts), // graph-dependencies
outputmodulegroups.NewCommand(opts), // output-module-groups
validateinputs.NewCommand(opts), // validate-inputs
hclvalidate.NewCommand(opts), // hclvalidate
hclfmt.NewCommand(opts), // hclfmt
info.NewCommand(opts), // info
terragruntinfo.NewCommand(opts), // terragrunt-info
renderjson.NewCommand(opts), // render-json
helpCmd.NewCommand(opts), // help (hidden)
versionCmd.NewCommand(opts), // version (hidden)
awsproviderpatch.NewCommand(opts), // aws-provider-patch (hidden)
}.SetCategory(
&cli.Category{
Name: ConfigurationCommandsCategoryName,
Order: 30, //nolint: mnd
},
)

shortcutsCommands := commands.NewShortcutsCommands(opts).SetCategory(
&cli.Category{
Name: ShortcutsCommandsCategoryName,
Order: 40, //nolint: mnd
},
)

allCommands := mainCommands.
Merge(catalogCommands...).
Merge(configurationCommands...).
Merge(shortcutsCommands...).
Merge(commands.NewDeprecatedCommands(opts)...)

return allCommands
}

// WrapWithTelemetry wraps CLI command execution with setting of telemetry context and labels, if telemetry is disabled, just runAction the command.
func WrapWithTelemetry(opts *options.TerragruntOptions) func(ctx *cli.Context, action cli.ActionFunc) error {
return func(ctx *cli.Context, action cli.ActionFunc) error {
Expand Down
2 changes: 1 addition & 1 deletion cli/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ func setCommandAction(action clipkg.ActionFunc, cmds ...*clipkg.Command) {
func runAppTest(args []string, opts *options.TerragruntOptions) (*options.TerragruntOptions, error) {
emptyAction := func(ctx *clipkg.Context) error { return nil }

terragruntCommands := cli.TerragruntCommands(opts)
terragruntCommands := commands.New(opts)
setCommandAction(emptyAction, terragruntCommands...)

app := clipkg.NewApp()
Expand Down
99 changes: 99 additions & 0 deletions cli/commands/commands.go
Original file line number Diff line number Diff line change
@@ -1,2 +1,101 @@
// Package commands represents CLI commands.
package commands

import (
"github.com/gruntwork-io/terragrunt/cli/commands/info"
"github.com/gruntwork-io/terragrunt/cli/commands/stack"
"github.com/gruntwork-io/terragrunt/options"

"github.com/gruntwork-io/terragrunt/cli/commands/graph"
"github.com/gruntwork-io/terragrunt/cli/commands/hclvalidate"
helpCmd "github.com/gruntwork-io/terragrunt/cli/commands/help"
versionCmd "github.com/gruntwork-io/terragrunt/cli/commands/version"

"github.com/gruntwork-io/terragrunt/cli/commands/scaffold"

awsproviderpatch "github.com/gruntwork-io/terragrunt/cli/commands/aws-provider-patch"
"github.com/gruntwork-io/terragrunt/cli/commands/catalog"
execCmd "github.com/gruntwork-io/terragrunt/cli/commands/exec"
graphdependencies "github.com/gruntwork-io/terragrunt/cli/commands/graph-dependencies"
"github.com/gruntwork-io/terragrunt/cli/commands/hclfmt"
outputmodulegroups "github.com/gruntwork-io/terragrunt/cli/commands/output-module-groups"
renderjson "github.com/gruntwork-io/terragrunt/cli/commands/render-json"
runCmd "github.com/gruntwork-io/terragrunt/cli/commands/run"
runall "github.com/gruntwork-io/terragrunt/cli/commands/run-all"
terragruntinfo "github.com/gruntwork-io/terragrunt/cli/commands/terragrunt-info"
validateinputs "github.com/gruntwork-io/terragrunt/cli/commands/validate-inputs"
"github.com/gruntwork-io/terragrunt/internal/cli"
)

// Command category names.
const (
// MainCommandsCategoryName represents primary Terragrunt operations like run, run-all.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// MainCommandsCategoryName represents primary Terragrunt operations like run, run-all.
// MainCommandsCategoryName represents primary Terragrunt operations like run, exec.

NIT: We'll forget to update this later.

MainCommandsCategoryName = "Main commands"
// CatalogCommandsCategoryName represents commands for managing Terragrunt catalogs.
CatalogCommandsCategoryName = "Catalog commands"
// ConfigurationCommandsCategoryName represents commands for managing Terragrunt configurations.
ConfigurationCommandsCategoryName = "Configuration commands"
// ShortcutsCommandsCategoryName represents OpenTofu-specific shortcut commands.
ShortcutsCommandsCategoryName = "OpenTofu shortcuts"
)

// New returns the set of Terragrunt commands, grouped into categories.
// Categories are ordered in increments of 10 for easy insertion of new categories.
func New(opts *options.TerragruntOptions) cli.Commands {
mainCommands := cli.Commands{
runCmd.NewCommand(opts), // run
runall.NewCommand(opts), // run-all
stack.NewCommand(opts), // stack
graph.NewCommand(opts), // graph
execCmd.NewCommand(opts), // exec
}.SetCategory(
&cli.Category{
Name: MainCommandsCategoryName,
Order: 10, //nolint: mnd
},
)

catalogCommands := cli.Commands{
catalog.NewCommand(opts), // catalog
scaffold.NewCommand(opts), // scaffold
}.SetCategory(
&cli.Category{
Name: CatalogCommandsCategoryName,
Order: 20, //nolint: mnd
},
)

configurationCommands := cli.Commands{
graphdependencies.NewCommand(opts), // graph-dependencies
outputmodulegroups.NewCommand(opts), // output-module-groups
validateinputs.NewCommand(opts), // validate-inputs
hclvalidate.NewCommand(opts), // hclvalidate
hclfmt.NewCommand(opts), // hclfmt
info.NewCommand(opts), // info
terragruntinfo.NewCommand(opts), // terragrunt-info
renderjson.NewCommand(opts), // render-json
helpCmd.NewCommand(opts), // help (hidden)
versionCmd.NewCommand(opts), // version (hidden)
awsproviderpatch.NewCommand(opts), // aws-provider-patch (hidden)
}.SetCategory(
&cli.Category{
Name: ConfigurationCommandsCategoryName,
Order: 30, //nolint: mnd
},
)

shortcutsCommands := NewShortcutsCommands(opts).SetCategory(
&cli.Category{
Name: ShortcutsCommandsCategoryName,
Order: 40, //nolint: mnd
},
)

allCommands := mainCommands.
Merge(catalogCommands...).
Merge(configurationCommands...).
Merge(shortcutsCommands...).
Merge(NewDeprecatedCommands(opts)...)

return allCommands
}
46 changes: 44 additions & 2 deletions cli/commands/help/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@
package help

import (
"github.com/gruntwork-io/terragrunt/cli/flags/global"
"os"

"github.com/gruntwork-io/terragrunt/internal/cli"
"github.com/gruntwork-io/terragrunt/internal/errors"
"github.com/gruntwork-io/terragrunt/options"
"github.com/gruntwork-io/terragrunt/pkg/log"
)

const (
Expand All @@ -17,7 +20,46 @@ func NewCommand(opts *options.TerragruntOptions) *cli.Command {
Usage: "Show help.",
Hidden: true,
Action: func(ctx *cli.Context) error {
return global.HelpAction(ctx, opts)
return Action(ctx, opts)
},
}
}

func Action(ctx *cli.Context, opts *options.TerragruntOptions) error {
var (
args = ctx.Args()
cmds = ctx.App.Commands
)

if opts.Logger.Level() >= log.DebugLevel {
// https: //github.com/urfave/cli/blob/f035ffaa3749afda2cd26fb824aa940747297ef1/help.go#L401
if err := os.Setenv("CLI_TEMPLATE_ERROR_DEBUG", "1"); err != nil {
return errors.Errorf("failed to set CLI_TEMPLATE_ERROR_DEBUG environment variable: %w", err)
}
}

if args.CommandName() == "" {
return cli.ShowAppHelp(ctx)
}

const maxCommandDepth = 1000 // Maximum depth of nested subcommands

for i := 0; i < maxCommandDepth && args.Len() > 0; i++ {
cmdName := args.CommandName()

cmd := cmds.Get(cmdName)
if cmd == nil {
break
}

args = args.Remove(cmdName)
cmds = cmd.Subcommands
ctx = ctx.NewCommandContext(cmd, args)
}

if ctx.Command != nil {
return cli.NewExitError(cli.ShowCommandHelp(ctx), cli.ExitCodeGeneralError)
}

return cli.NewExitError(errors.New(cli.InvalidCommandNameError(args.First())), cli.ExitCodeGeneralError)
}
1 change: 0 additions & 1 deletion cli/commands/run/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,6 @@ func NewFlags(opts *options.TerragruntOptions, prefix flags.Prefix) cli.Flags {
Name: terragruntPrefix.FlagName(DeprecatedIncludeModulePrefixFlagName),
EnvVars: terragruntPrefix.EnvVars(DeprecatedIncludeModulePrefixFlagName),
Usage: "When this flag is set output from Terraform sub-commands is prefixed with module path.",
Hidden: true,
Action: func(_ *cli.Context, _ bool) error {
opts.Logger.Warnf("The %q flag is deprecated. Use the functionality-inverted %q flag instead. By default, Terraform/OpenTofu output is integrated into the Terragrunt log, which prepends additional data, such as timestamps and prefixes, to log entries.", DeprecatedIncludeModulePrefixFlagName, TFForwardStdoutFlagName)

Expand Down
7 changes: 5 additions & 2 deletions cli/commands/version/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
package version

import (
"github.com/gruntwork-io/terragrunt/cli/flags/global"
"github.com/gruntwork-io/terragrunt/internal/cli"
"github.com/gruntwork-io/terragrunt/options"
)
Expand All @@ -17,7 +16,11 @@ func NewCommand(opts *options.TerragruntOptions) *cli.Command {
Usage: "Show terragrunt version.",
Hidden: true,
Action: func(ctx *cli.Context) error {
return cli.NewExitError(global.VersionAction(ctx, opts), 0)
return cli.NewExitError(Action(ctx), 0)
},
}
}

func Action(ctx *cli.Context) error {
return cli.ShowVersion(ctx)
}
Loading