Skip to content

Commit 8627f75

Browse files
authored
chore: Avoiding contextcheck suppression (#5320)
1 parent ef7c519 commit 8627f75

76 files changed

Lines changed: 1183 additions & 1057 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

cli/app.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -152,11 +152,11 @@ func removeNoColorFlagDuplicates(args []string) []string {
152152
}
153153

154154
func beforeAction(_ *options.TerragruntOptions) cli.ActionFunc {
155-
return func(ctx *cli.Context) error {
155+
return func(ctx context.Context, cliCtx *cli.Context) error {
156156
// setting current context to the options
157157
// show help if the args are not specified.
158-
if !ctx.Args().Present() {
159-
err := cli.ShowAppHelp(ctx)
158+
if !cliCtx.Args().Present() {
159+
err := cli.ShowAppHelp(ctx, cliCtx)
160160
// exit the app
161161
return cli.NewExitError(err, 0)
162162
}
@@ -165,9 +165,9 @@ func beforeAction(_ *options.TerragruntOptions) cli.ActionFunc {
165165
// top-level command, fail fast with guidance to use `run --`.
166166
// This removes the legacy behavior of implicitly forwarding unknown
167167
// commands to OpenTofu/Terraform.
168-
cmdName := ctx.Args().CommandName()
168+
cmdName := cliCtx.Args().CommandName()
169169
if cmdName != "" {
170-
if ctx.Command == nil || ctx.Command.Subcommand(cmdName) == nil {
170+
if cliCtx.Command == nil || cliCtx.Command.Subcommand(cmdName) == nil {
171171
// Show a clear error pointing users to the explicit run form.
172172
// Example: `terragrunt workspace ls` -> suggest `terragrunt run -- workspace ls`.
173173
return cli.NewExitError(

cli/app_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package cli_test
22

33
import (
44
"bytes"
5+
"context"
56
"fmt"
67
"os"
78
"path/filepath"
@@ -859,7 +860,7 @@ func setCommandAction(action clipkg.ActionFunc, cmds ...*clipkg.Command) {
859860
}
860861

861862
func runAppTest(l log.Logger, args []string, opts *options.TerragruntOptions) (*options.TerragruntOptions, error) {
862-
emptyAction := func(ctx *clipkg.Context) error { return nil }
863+
emptyAction := func(ctx context.Context, cliCtx *clipkg.Context) error { return nil }
863864

864865
terragruntCommands := commands.New(l, opts)
865866
setCommandAction(emptyAction, terragruntCommands...)
@@ -871,8 +872,8 @@ func runAppTest(l log.Logger, args []string, opts *options.TerragruntOptions) (*
871872
app.Flags = append(global.NewFlags(l, opts, nil), run.NewFlags(l, opts, nil)...)
872873
app.Commands = terragruntCommands.WrapAction(commands.WrapWithTelemetry(l, opts))
873874
app.OsExiter = cli.OSExiter
874-
app.Action = func(ctx *clipkg.Context) error {
875-
opts.TerraformCliArgs = append(opts.TerraformCliArgs, ctx.Args()...)
875+
app.Action = func(ctx context.Context, cliCtx *clipkg.Context) error {
876+
opts.TerraformCliArgs = append(opts.TerraformCliArgs, cliCtx.Args()...)
876877
return nil
877878
}
878879
app.ExitErrHandler = cli.ExitErrHandler

cli/commands/aws-provider-patch/cli.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
package awsproviderpatch
3131

3232
import (
33+
"context"
34+
3335
"github.com/gruntwork-io/terragrunt/cli/commands/common/graph"
3436
"github.com/gruntwork-io/terragrunt/cli/commands/common/runall"
3537
runcmd "github.com/gruntwork-io/terragrunt/cli/commands/run"
@@ -72,14 +74,14 @@ func NewCommand(l log.Logger, opts *options.TerragruntOptions) *cli.Command {
7274
Usage: "Overwrite settings on nested AWS providers to work around a Terraform bug (issue #13018).",
7375
Hidden: true,
7476
Flags: append(runcmd.NewFlags(l, opts, nil), NewFlags(l, opts, nil)...),
75-
Before: func(ctx *cli.Context) error {
77+
Before: func(ctx context.Context, _ *cli.Context) error {
7678
if err := control.Evaluate(ctx); err != nil {
7779
return cli.NewExitError(err, cli.ExitCodeGeneralError)
7880
}
7981

8082
return nil
8183
},
82-
Action: func(ctx *cli.Context) error {
84+
Action: func(ctx context.Context, _ *cli.Context) error {
8385
return Run(ctx, l, opts.OptionsFromContext(ctx))
8486
},
8587
DisabledErrorOnUndefinedFlag: true,

cli/commands/backend/bootstrap/cli.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package bootstrap
22

33
import (
4+
"context"
5+
46
"github.com/gruntwork-io/terragrunt/cli/commands/common/runall"
57
"github.com/gruntwork-io/terragrunt/cli/flags"
68
"github.com/gruntwork-io/terragrunt/cli/flags/shared"
@@ -12,7 +14,9 @@ import (
1214

1315
const CommandName = "bootstrap"
1416

15-
func NewFlags(l log.Logger, opts *options.TerragruntOptions, prefix flags.Prefix) cli.Flags {
17+
func NewFlags(opts *options.TerragruntOptions) cli.Flags {
18+
prefix := flags.Prefix{flags.TgPrefix}
19+
1620
sharedFlags := cli.Flags{
1721
shared.NewConfigFlag(opts, prefix, CommandName),
1822
shared.NewDownloadDirFlag(opts, prefix, CommandName),
@@ -27,8 +31,8 @@ func NewCommand(l log.Logger, opts *options.TerragruntOptions) *cli.Command {
2731
cmd := &cli.Command{
2832
Name: CommandName,
2933
Usage: "Bootstrap OpenTofu/Terraform backend infrastructure.",
30-
Flags: NewFlags(l, opts, nil),
31-
Action: func(ctx *cli.Context) error {
34+
Flags: NewFlags(opts),
35+
Action: func(ctx context.Context, _ *cli.Context) error {
3236
return Run(ctx, l, opts.OptionsFromContext(ctx))
3337
},
3438
}

cli/commands/backend/delete/cli.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package delete
22

33
import (
4+
"context"
5+
46
"github.com/gruntwork-io/terragrunt/cli/commands/common/runall"
57
"github.com/gruntwork-io/terragrunt/cli/flags"
68
"github.com/gruntwork-io/terragrunt/cli/flags/shared"
@@ -49,7 +51,7 @@ func NewCommand(l log.Logger, opts *options.TerragruntOptions) *cli.Command {
4951
Name: CommandName,
5052
Usage: "Delete OpenTofu/Terraform state.",
5153
Flags: NewFlags(l, opts, nil),
52-
Action: func(ctx *cli.Context) error {
54+
Action: func(ctx context.Context, _ *cli.Context) error {
5355
return Run(ctx, l, opts.OptionsFromContext(ctx))
5456
},
5557
}

cli/commands/backend/migrate/cli.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package migrate
22

33
import (
4+
"context"
5+
46
"github.com/gruntwork-io/terragrunt/cli/flags"
57
"github.com/gruntwork-io/terragrunt/cli/flags/shared"
68
"github.com/gruntwork-io/terragrunt/internal/cli"
@@ -43,13 +45,13 @@ func NewCommand(l log.Logger, opts *options.TerragruntOptions) *cli.Command {
4345
Usage: "Migrate OpenTofu/Terraform state from one location to another.",
4446
UsageText: usageText,
4547
Flags: NewFlags(l, opts, nil),
46-
Action: func(ctx *cli.Context) error {
47-
srcPath := ctx.Args().First()
48+
Action: func(ctx context.Context, cliCtx *cli.Context) error {
49+
srcPath := cliCtx.Args().First()
4850
if srcPath == "" {
4951
return errors.New(usageText)
5052
}
5153

52-
dstPath := ctx.Args().Second()
54+
dstPath := cliCtx.Args().Second()
5355
if dstPath == "" {
5456
return errors.New(usageText)
5557
}

cli/commands/catalog/cli.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
package catalog
44

55
import (
6+
"context"
7+
68
"github.com/gruntwork-io/terragrunt/cli/commands/scaffold"
79
"github.com/gruntwork-io/terragrunt/cli/flags"
810
"github.com/gruntwork-io/terragrunt/cli/flags/shared"
@@ -24,10 +26,10 @@ func NewCommand(l log.Logger, opts *options.TerragruntOptions) *cli.Command {
2426
Name: CommandName,
2527
Usage: "Launch the user interface for searching and managing your module catalog.",
2628
Flags: NewFlags(opts, nil),
27-
Action: func(ctx *cli.Context) error {
29+
Action: func(ctx context.Context, cliCtx *cli.Context) error {
2830
var repoPath string
2931

30-
if val := ctx.Args().Get(0); val != "" {
32+
if val := cliCtx.Args().Get(0); val != "" {
3133
repoPath = val
3234
}
3335

cli/commands/commands.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ func New(l log.Logger, opts *options.TerragruntOptions) cli.Commands {
9797
dag.NewCommand(l, opts), // dag
9898
render.NewCommand(l, opts), // render
9999
helpcmd.NewCommand(l, opts), // help (hidden)
100-
versioncmd.NewCommand(opts), // version (hidden)
100+
versioncmd.NewCommand(), // version (hidden)
101101
awsproviderpatch.NewCommand(l, opts), // aws-provider-patch (hidden)
102102
}.SetCategory(
103103
&cli.Category{
@@ -123,26 +123,24 @@ func New(l log.Logger, opts *options.TerragruntOptions) cli.Commands {
123123
}
124124

125125
// WrapWithTelemetry wraps CLI command execution with setting of telemetry context and labels, if telemetry is disabled, just runAction the command.
126-
func WrapWithTelemetry(l log.Logger, opts *options.TerragruntOptions) func(ctx *cli.Context, action cli.ActionFunc) error {
127-
return func(ctx *cli.Context, action cli.ActionFunc) error {
128-
return telemetry.TelemeterFromContext(ctx).Collect(ctx.Context, fmt.Sprintf("%s %s", ctx.Command.Name, opts.TerraformCommand), map[string]any{
126+
func WrapWithTelemetry(l log.Logger, opts *options.TerragruntOptions) func(ctx context.Context, cliCtx *cli.Context, action cli.ActionFunc) error {
127+
return func(ctx context.Context, cliCtx *cli.Context, action cli.ActionFunc) error {
128+
return telemetry.TelemeterFromContext(ctx).Collect(ctx, fmt.Sprintf("%s %s", cliCtx.Command.Name, opts.TerraformCommand), map[string]any{
129129
"terraformCommand": opts.TerraformCommand,
130130
"args": opts.TerraformCliArgs,
131131
"dir": opts.WorkingDir,
132132
}, func(childCtx context.Context) error {
133-
ctx.Context = childCtx //nolint:fatcontext
134-
if err := initialSetup(ctx, l, opts); err != nil {
133+
if err := initialSetup(cliCtx, l, opts); err != nil {
135134
return err
136135
}
137136

138-
// TODO: See if this lint should be ignored
139-
return runAction(ctx, l, opts, action) //nolint:contextcheck
137+
return runAction(childCtx, cliCtx, l, opts, action)
140138
})
141139
}
142140
}
143141

144-
func runAction(cliCtx *cli.Context, l log.Logger, opts *options.TerragruntOptions, action cli.ActionFunc) error {
145-
ctx, cancel := context.WithCancel(cliCtx.Context)
142+
func runAction(ctx context.Context, cliCtx *cli.Context, l log.Logger, opts *options.TerragruntOptions, action cli.ActionFunc) error {
143+
ctx, cancel := context.WithCancel(ctx)
146144
defer cancel()
147145

148146
errGroup, ctx := errgroup.WithContext(ctx)
@@ -154,6 +152,9 @@ func runAction(cliCtx *cli.Context, l log.Logger, opts *options.TerragruntOption
154152
}
155153
}
156154

155+
// actionCtx is the context passed to the action, which may be wrapped with hooks
156+
actionCtx := ctx
157+
157158
// Run provider cache server
158159
if opts.ProviderCache {
159160
server, err := providercache.InitServer(l, opts)
@@ -167,7 +168,7 @@ func runAction(cliCtx *cli.Context, l log.Logger, opts *options.TerragruntOption
167168
}
168169
defer ln.Close() //nolint:errcheck
169170

170-
cliCtx.Context = tf.ContextWithTerraformCommandHook(ctx, server.TerraformCommandHook)
171+
actionCtx = tf.ContextWithTerraformCommandHook(ctx, server.TerraformCommandHook)
171172

172173
errGroup.Go(func() error {
173174
return server.Run(ctx, ln)
@@ -179,7 +180,7 @@ func runAction(cliCtx *cli.Context, l log.Logger, opts *options.TerragruntOption
179180
defer cancel()
180181

181182
if action != nil {
182-
return action(cliCtx)
183+
return action(actionCtx, cliCtx)
183184
}
184185

185186
return nil

cli/commands/common/graph/cli.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515

1616
const GraphFlagName = "graph"
1717

18-
func NewFlags(opts *options.TerragruntOptions, commandName string, prefix flags.Prefix) cli.Flags {
18+
func NewFlags(opts *options.TerragruntOptions, prefix flags.Prefix) cli.Flags {
1919
tgPrefix := prefix.Prepend(flags.TgPrefix)
2020

2121
return cli.Flags{
@@ -24,7 +24,7 @@ func NewFlags(opts *options.TerragruntOptions, commandName string, prefix flags.
2424
EnvVars: tgPrefix.EnvVars(GraphFlagName),
2525
Destination: &opts.Graph,
2626
Usage: "Run the specified OpenTofu/Terraform command following the Directed Acyclic Graph (DAG) of dependencies.",
27-
Action: func(_ *cli.Context, _ bool) error {
27+
Action: func(_ context.Context, _ *cli.Context, _ bool) error {
2828
if opts.RunAll {
2929
return errors.New(new(common.AllGraphFlagsError))
3030
}
@@ -43,29 +43,29 @@ func WrapCommand(
4343
runFn func(ctx context.Context, l log.Logger, opts *options.TerragruntOptions, r *report.Report) error,
4444
alwaysDisableSummary bool,
4545
) *cli.Command {
46-
cmd = cmd.WrapAction(func(cliCtx *cli.Context, action cli.ActionFunc) error {
46+
cmd = cmd.WrapAction(func(ctx context.Context, cliCtx *cli.Context, action cli.ActionFunc) error {
4747
if alwaysDisableSummary {
4848
opts.SummaryDisable = true
4949
}
5050

5151
if !opts.Graph {
52-
return action(cliCtx)
52+
return action(ctx, cliCtx)
5353
}
5454

55-
opts.RunTerragrunt = func(ctx context.Context, l log.Logger, opts *options.TerragruntOptions, r *report.Report) error {
55+
opts.RunTerragrunt = func(innerCtx context.Context, l log.Logger, opts *options.TerragruntOptions, r *report.Report) error {
5656
if opts.TerraformCommand == cmd.Name {
57-
cliCtx := cliCtx.WithValue(options.ContextKey, opts)
57+
innerCtx = context.WithValue(innerCtx, options.ContextKey, opts)
5858

59-
return action(cliCtx)
59+
return action(innerCtx, cliCtx)
6060
}
6161

62-
return runFn(ctx, l, opts, r)
62+
return runFn(innerCtx, l, opts, r)
6363
}
6464

65-
return Run(cliCtx, l, opts.OptionsFromContext(cliCtx))
65+
return Run(ctx, l, opts.OptionsFromContext(ctx))
6666
})
6767

68-
cmd.Flags = append(cmd.Flags, NewFlags(opts, cmd.Name, nil)...)
68+
cmd.Flags = append(cmd.Flags, NewFlags(opts, nil)...)
6969

7070
return cmd
7171
}

cli/commands/common/runall/cli.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const (
1919
AllFlagAlias = "a"
2020
)
2121

22-
func NewFlags(opts *options.TerragruntOptions, commandName string, prefix flags.Prefix) cli.Flags {
22+
func NewFlags(opts *options.TerragruntOptions, prefix flags.Prefix) cli.Flags {
2323
tgPrefix := prefix.Prepend(flags.TgPrefix)
2424

2525
return cli.Flags{
@@ -29,7 +29,7 @@ func NewFlags(opts *options.TerragruntOptions, commandName string, prefix flags.
2929
EnvVars: tgPrefix.EnvVars(AllFlagName),
3030
Destination: &opts.RunAll,
3131
Usage: `Run the specified command on the stack of units in the current directory.`,
32-
Action: func(_ *cli.Context, _ bool) error {
32+
Action: func(_ context.Context, _ *cli.Context, _ bool) error {
3333
if opts.Graph {
3434
return errors.New(new(common.AllGraphFlagsError))
3535
}
@@ -48,29 +48,29 @@ func WrapCommand(
4848
runFn func(ctx context.Context, l log.Logger, opts *options.TerragruntOptions, r *report.Report) error,
4949
alwaysDisableSummary bool,
5050
) *cli.Command {
51-
cmd = cmd.WrapAction(func(cliCtx *cli.Context, action cli.ActionFunc) error {
51+
cmd = cmd.WrapAction(func(ctx context.Context, cliCtx *cli.Context, action cli.ActionFunc) error {
5252
if alwaysDisableSummary {
5353
opts.SummaryDisable = true
5454
}
5555

5656
if !opts.RunAll {
57-
return action(cliCtx)
57+
return action(ctx, cliCtx)
5858
}
5959

60-
opts.RunTerragrunt = func(ctx context.Context, l log.Logger, opts *options.TerragruntOptions, r *report.Report) error {
60+
opts.RunTerragrunt = func(innerCtx context.Context, l log.Logger, opts *options.TerragruntOptions, r *report.Report) error {
6161
if opts.TerraformCommand == cmd.Name {
62-
cliCtx := cliCtx.WithValue(options.ContextKey, opts)
62+
innerCtx = context.WithValue(innerCtx, options.ContextKey, opts)
6363

64-
return action(cliCtx)
64+
return action(innerCtx, cliCtx)
6565
}
6666

67-
return runFn(ctx, l, opts, r)
67+
return runFn(innerCtx, l, opts, r)
6868
}
6969

70-
return Run(cliCtx, l, opts.OptionsFromContext(cliCtx))
70+
return Run(ctx, l, opts.OptionsFromContext(ctx))
7171
})
7272

73-
cmd.Flags = append(cmd.Flags, NewFlags(opts, cmd.Name, nil)...)
73+
cmd.Flags = append(cmd.Flags, NewFlags(opts, nil)...)
7474

7575
return cmd
7676
}

0 commit comments

Comments
 (0)