Skip to content

Commit b8931d6

Browse files
authored
fix: terraform_binary with run version command (#4095)
1 parent fe94ff7 commit b8931d6

7 files changed

Lines changed: 80 additions & 14 deletions

File tree

cli/commands/run/run.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,16 +85,16 @@ func Run(ctx context.Context, opts *options.TerragruntOptions) error {
8585
return errors.New(MissingCommand{})
8686
}
8787

88-
return runTerraform(ctx, opts, new(Target))
88+
return run(ctx, opts, new(Target))
8989
}
9090

9191
func RunWithTarget(ctx context.Context, opts *options.TerragruntOptions, target *Target) error {
92-
return runTerraform(ctx, opts, target)
92+
return run(ctx, opts, target)
9393
}
9494

95-
func runTerraform(ctx context.Context, terragruntOptions *options.TerragruntOptions, target *Target) error {
95+
func run(ctx context.Context, terragruntOptions *options.TerragruntOptions, target *Target) error {
9696
if terragruntOptions.TerraformCommand == tf.CommandNameVersion {
97-
return tf.RunCommand(ctx, terragruntOptions, tf.CommandNameVersion)
97+
return runVersionCommand(ctx, terragruntOptions)
9898
}
9999

100100
// We need to get the credentials from auth-provider-cmd at the very beginning, since the locals block may contain `get_aws_account_id()` func.
@@ -907,3 +907,15 @@ func setTerragruntNullValues(terragruntOptions *options.TerragruntOptions, terra
907907
func useLegacyNullValues() bool {
908908
return os.Getenv(useLegacyNullValuesEnvVar) == "1"
909909
}
910+
911+
func getTerragruntConfig(ctx context.Context, opts *options.TerragruntOptions) (*config.TerragruntConfig, error) {
912+
configCtx := config.NewParsingContext(ctx, opts).WithDecodeList(
913+
config.TerragruntVersionConstraints, config.FeatureFlagsBlock)
914+
915+
// TODO: See if we should be ignore this lint error
916+
return config.PartialParseConfigFile( //nolint: contextcheck
917+
configCtx,
918+
opts.TerragruntConfigPath,
919+
nil,
920+
)
921+
}

cli/commands/run/version_check.go

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"regexp"
88
"strings"
99

10-
"github.com/gruntwork-io/terragrunt/config"
1110
"github.com/gruntwork-io/terragrunt/internal/errors"
1211
"github.com/gruntwork-io/terragrunt/options"
1312
"github.com/gruntwork-io/terragrunt/tf"
@@ -35,15 +34,7 @@ const versionParts = 3
3534
// - FeatureFlags
3635
// TODO: Look into a way to refactor this function to avoid the side effect.
3736
func CheckVersionConstraints(ctx context.Context, terragruntOptions *options.TerragruntOptions) error {
38-
configContext := config.NewParsingContext(ctx, terragruntOptions).WithDecodeList(
39-
config.TerragruntVersionConstraints, config.FeatureFlagsBlock)
40-
41-
// TODO: See if we should be ignore this lint error
42-
partialTerragruntConfig, err := config.PartialParseConfigFile( //nolint: contextcheck
43-
configContext,
44-
terragruntOptions.TerragruntConfigPath,
45-
nil,
46-
)
37+
partialTerragruntConfig, err := getTerragruntConfig(ctx, terragruntOptions)
4738
if err != nil {
4839
return err
4940
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package run
2+
3+
import (
4+
"context"
5+
6+
"github.com/gruntwork-io/terragrunt/options"
7+
"github.com/gruntwork-io/terragrunt/tf"
8+
"github.com/gruntwork-io/terragrunt/util"
9+
)
10+
11+
func runVersionCommand(ctx context.Context, opts *options.TerragruntOptions) error {
12+
if tfPath, err := getTfPathFromConfig(ctx, opts); err != nil {
13+
return err
14+
} else if tfPath != "" {
15+
opts.TerraformPath = tfPath
16+
}
17+
18+
return tf.RunCommand(ctx, opts, tf.CommandNameVersion)
19+
}
20+
21+
func getTfPathFromConfig(ctx context.Context, opts *options.TerragruntOptions) (string, error) {
22+
if !util.FileExists(opts.TerragruntConfigPath) {
23+
opts.Logger.Debugf("Did not find the config file %s", opts.TerragruntConfigPath)
24+
25+
return "", nil
26+
}
27+
28+
cfg, err := getTerragruntConfig(ctx, opts)
29+
if err != nil {
30+
return "", err
31+
}
32+
33+
return cfg.TerraformBinary, nil
34+
}

test/fixtures/tf-path/main.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Intentionally empty
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# We can't explicitly specify `tofu` or `terraform` because a CircleCI job contains either `terraform` or `tofu` binary, but not both in the same job.
2+
terraform_binary = "./tf.sh"

test/fixtures/tf-path/tf.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/bin/sh
2+
3+
tfpath="${TG_TF_PATH:-tofu}"
4+
5+
$tfpath "$@"

test/integration_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ const (
111111
testFixtureLogStreaming = "fixtures/streaming"
112112
testFixtureCLIFlagHints = "fixtures/cli-flag-hints"
113113
testFixtureEphemeralInputs = "fixtures/ephemeral-inputs"
114+
testFixtureTfPath = "fixtures/tf-path"
114115

115116
terraformFolder = ".terraform"
116117

@@ -1067,6 +1068,10 @@ func TestTerraformCommandCliArgs(t *testing.T) {
10671068
expected string
10681069
command []string
10691070
}{
1071+
{
1072+
command: []string{"version"},
1073+
expected: wrappedBinary() + " version",
1074+
},
10701075
{
10711076
command: []string{"--", "version"},
10721077
expected: wrappedBinary() + " version",
@@ -4173,3 +4178,19 @@ func TestTF110EphemeralVars(t *testing.T) {
41734178
require.NoError(t, err)
41744179
assert.Contains(t, stdout, "Apply complete! Resources: 1 added, 0 changed, 0 destroyed")
41754180
}
4181+
4182+
func TestTfPath(t *testing.T) {
4183+
t.Parallel()
4184+
// Test that the terragrunt run version command correctly identifies and uses
4185+
// the terraform_binary path configuration if present
4186+
helpers.CleanupTerraformFolder(t, testFixtureTfPath)
4187+
rootPath := helpers.CopyEnvironment(t, testFixtureTfPath)
4188+
workingDir := util.JoinPath(rootPath, testFixtureTfPath)
4189+
workingDir, err := filepath.EvalSymlinks(workingDir)
4190+
require.NoError(t, err)
4191+
4192+
stdout, stderr, err := helpers.RunTerragruntCommandWithOutput(t, "terragrunt run version --experiment cli-redesign --working-dir "+workingDir)
4193+
require.NoError(t, err)
4194+
4195+
assert.Regexp(t, "(?i)(terraform|opentofu)", stdout+stderr)
4196+
}

0 commit comments

Comments
 (0)