Skip to content

Commit 0df7eb5

Browse files
authored
Add tf-path flag to exec command (#4343)
* Add tf-path flag to exec command * Use tf-path flag from run.
1 parent dc8d842 commit 0df7eb5

10 files changed

Lines changed: 171 additions & 43 deletions

File tree

cli/commands/exec/cli.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const (
1313
CommandName = "exec"
1414

1515
InDownloadDirFlagName = "in-download-dir"
16+
TFPathFlagName = "tf-path"
1617
)
1718

1819
func NewFlags(opts *options.TerragruntOptions, cmdOpts *Options, prefix flags.Prefix) cli.Flags {
@@ -27,6 +28,7 @@ func NewFlags(opts *options.TerragruntOptions, cmdOpts *Options, prefix flags.Pr
2728
run.IAMAssumeRoleDurationFlagName,
2829
run.IAMAssumeRoleSessionNameFlagName,
2930
run.IAMAssumeRoleWebIdentityTokenFlagName,
31+
run.TFPathFlagName,
3032
),
3133
flags.NewFlag(&cli.BoolFlag{
3234
Name: InDownloadDirFlagName,
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
variable "baz" {
2+
type = string
3+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
terraform {
2+
source = "."
3+
}
4+
5+
dependency "dep" {
6+
config_path = "../dep"
7+
}
8+
9+
inputs = {
10+
baz = dependency.dep.outputs.baz
11+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
output "baz" {
2+
value = "baz"
3+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Intentionally empty
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/env bash
2+
3+
echo "baz is ${TF_VAR_baz:-not set}"
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/usr/bin/env bash
2+
3+
# Handle -version
4+
if [ "$1" = "-version" ]; then
5+
echo "Terraform v1.0.0"
6+
exit 0
7+
fi
8+
9+
# Output variable
10+
cat << 'EOF'
11+
{
12+
"baz": {
13+
"sensitive": false,
14+
"type": "string",
15+
"value": "terraform"
16+
}
17+
}
18+
EOF
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/usr/bin/env bash
2+
3+
# Handle -version
4+
if [ "$1" = "-version" ]; then
5+
echo "OpenToFu v1.0.0"
6+
exit 0
7+
fi
8+
9+
# Output variable
10+
cat << 'EOF'
11+
{
12+
"baz": {
13+
"sensitive": false,
14+
"type": "string",
15+
"value": "tofu"
16+
}
17+
}
18+
EOF

test/integration_exec_test.go

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
package test_test
2+
3+
import (
4+
"bytes"
5+
"fmt"
6+
"os"
7+
"path/filepath"
8+
"strings"
9+
"testing"
10+
11+
"github.com/gruntwork-io/terragrunt/test/helpers"
12+
"github.com/gruntwork-io/terragrunt/util"
13+
"github.com/stretchr/testify/assert"
14+
"github.com/stretchr/testify/require"
15+
)
16+
17+
func TestExecCommand(t *testing.T) {
18+
t.Parallel()
19+
20+
testCases := []struct {
21+
scriptPath string
22+
runInDir string
23+
args []string
24+
}{
25+
{
26+
scriptPath: "./script.sh arg1 arg2",
27+
runInDir: "",
28+
},
29+
{
30+
args: []string{"--in-download-dir"},
31+
scriptPath: "./script.sh arg1 arg2",
32+
runInDir: ".terragrunt-cache",
33+
},
34+
}
35+
36+
for i, tc := range testCases {
37+
t.Run(fmt.Sprintf("testCase-%d", i), func(t *testing.T) {
38+
t.Parallel()
39+
40+
helpers.CleanupTerraformFolder(t, testFixtureExecCmd)
41+
tmpEnvPath := helpers.CopyEnvironment(t, testFixtureExecCmd)
42+
43+
rootPath := util.JoinPath(tmpEnvPath, testFixtureExecCmd, "app")
44+
rootPath, err := filepath.EvalSymlinks(rootPath)
45+
require.NoError(t, err)
46+
47+
downloadDirPath := util.JoinPath(rootPath, ".terragrunt-cache")
48+
scriptPath := util.JoinPath(tmpEnvPath, testFixtureExecCmd, tc.scriptPath)
49+
50+
err = os.Mkdir(downloadDirPath, os.ModePerm)
51+
require.NoError(t, err)
52+
53+
stdout, _, err := helpers.RunTerragruntCommandWithOutput(t, "terragrunt exec --working-dir "+rootPath+" "+strings.Join(tc.args, " ")+" -- "+scriptPath)
54+
require.NoError(t, err)
55+
assert.Contains(t, stdout, "The first arg is arg1. The second arg is arg2. The script is running in the directory "+util.JoinPath(rootPath, tc.runInDir))
56+
})
57+
}
58+
}
59+
60+
func TestExecCommandTfPath(t *testing.T) {
61+
t.Parallel()
62+
63+
testCases := []struct {
64+
expected string
65+
tfPath string
66+
}{
67+
{
68+
expected: "baz is baz",
69+
},
70+
{
71+
expected: "baz is terraform",
72+
tfPath: "terraform-output-json.sh",
73+
},
74+
{
75+
expected: "baz is tofu",
76+
tfPath: "tofu-output-json.sh",
77+
},
78+
}
79+
80+
for i, tc := range testCases {
81+
t.Run(fmt.Sprintf("testCase-%d", i), func(t *testing.T) {
82+
t.Parallel()
83+
84+
helpers.CleanupTerraformFolder(t, testFixtureExecCmdTfPath)
85+
tmpEnvPath := helpers.CopyEnvironment(t, testFixtureExecCmdTfPath)
86+
87+
rootPath := util.JoinPath(tmpEnvPath, testFixtureExecCmdTfPath, "app")
88+
rootPath, err := filepath.EvalSymlinks(rootPath)
89+
require.NoError(t, err)
90+
91+
downloadDirPath := util.JoinPath(rootPath, ".terragrunt-cache")
92+
scriptPath := util.JoinPath(tmpEnvPath, testFixtureExecCmdTfPath, "./script.sh")
93+
tfPath := ""
94+
if tc.tfPath != "" {
95+
tfPath = "--tf-path " + util.JoinPath(tmpEnvPath, testFixtureExecCmdTfPath, tc.tfPath)
96+
}
97+
98+
err = os.Mkdir(downloadDirPath, os.ModePerm)
99+
require.NoError(t, err)
100+
101+
depPath := util.JoinPath(tmpEnvPath, testFixtureExecCmdTfPath, "dep")
102+
depStdout := bytes.Buffer{}
103+
depStderr := bytes.Buffer{}
104+
require.NoError(t, helpers.RunTerragruntCommand(t, "terragrunt apply -auto-approve --non-interactive -no-color --no-color --log-format=pretty --working-dir "+depPath, &depStdout, &depStderr))
105+
106+
stdout, _, err := helpers.RunTerragruntCommandWithOutput(t, "terragrunt --log-level debug exec "+tfPath+" --working-dir "+rootPath+" -- "+scriptPath)
107+
require.NoError(t, err)
108+
assert.Contains(t, stdout, tc.expected)
109+
})
110+
}
111+
}

test/integration_test.go

Lines changed: 1 addition & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ const (
106106
testFixtureStdout = "fixtures/download/stdout-test"
107107
testFixtureTfTest = "fixtures/tftest/"
108108
testFixtureExecCmd = "fixtures/exec-cmd"
109+
testFixtureExecCmdTfPath = "fixtures/exec-cmd-tf-path"
109110
textFixtureDisjointSymlinks = "fixtures/stack/disjoint-symlinks"
110111
testFixtureLogStreaming = "fixtures/streaming"
111112
testFixtureCLIFlagHints = "fixtures/cli-flag-hints"
@@ -153,49 +154,6 @@ func TestCLIFlagHints(t *testing.T) {
153154
}
154155
}
155156

156-
func TestExecCommand(t *testing.T) {
157-
t.Parallel()
158-
159-
testCases := []struct {
160-
scriptPath string
161-
runInDir string
162-
args []string
163-
}{
164-
{
165-
scriptPath: "./script.sh arg1 arg2",
166-
runInDir: "",
167-
},
168-
{
169-
args: []string{"--in-download-dir"},
170-
scriptPath: "./script.sh arg1 arg2",
171-
runInDir: ".terragrunt-cache",
172-
},
173-
}
174-
175-
for i, tc := range testCases {
176-
t.Run(fmt.Sprintf("testCase-%d", i), func(t *testing.T) {
177-
t.Parallel()
178-
179-
helpers.CleanupTerraformFolder(t, testFixtureExecCmd)
180-
tmpEnvPath := helpers.CopyEnvironment(t, testFixtureExecCmd)
181-
182-
rootPath := util.JoinPath(tmpEnvPath, testFixtureExecCmd, "app")
183-
rootPath, err := filepath.EvalSymlinks(rootPath)
184-
require.NoError(t, err)
185-
186-
downloadDirPath := util.JoinPath(rootPath, ".terragrunt-cache")
187-
scriptPath := util.JoinPath(tmpEnvPath, testFixtureExecCmd, tc.scriptPath)
188-
189-
err = os.Mkdir(downloadDirPath, os.ModePerm)
190-
require.NoError(t, err)
191-
192-
stdout, _, err := helpers.RunTerragruntCommandWithOutput(t, "terragrunt exec --working-dir "+rootPath+" "+strings.Join(tc.args, " ")+" -- "+scriptPath)
193-
require.NoError(t, err)
194-
assert.Contains(t, stdout, "The first arg is arg1. The second arg is arg2. The script is running in the directory "+util.JoinPath(rootPath, tc.runInDir))
195-
})
196-
}
197-
}
198-
199157
func TestDetailedExitCodeError(t *testing.T) {
200158
t.Parallel()
201159

0 commit comments

Comments
 (0)