@@ -10,25 +10,17 @@ import (
1010 "github.com/gruntwork-io/terratest/modules/testing"
1111)
1212
13- // runTerragruntStackCommandE is the unified function that executes tg stack commands
13+ // runTerragruntStackCommandE executes tg stack commands
1414// It handles argument construction, retry logic, and error handling for all stack commands
1515func runTerragruntStackCommandE (
1616 t testing.TestingT , opts * Options , subCommand string , additionalArgs ... string ) (string , error ) {
17- // Default behavior: use arg separator (for backward compatibility)
18- return runTerragruntStackCommandWithSeparatorE (t , opts , subCommand , true , additionalArgs ... )
19- }
20-
21- // runTerragruntStackCommandWithSeparatorE executes tg stack commands with control over the -- separator
22- // useArgSeparator controls whether the "--" separator is added before additional arguments
23- func runTerragruntStackCommandWithSeparatorE (t testing.TestingT , opts * Options ,
24- subCommand string , useArgSeparator bool , additionalArgs ... string ) (string , error ) {
2517 // Build the base command arguments starting with "stack"
2618 commandArgs := []string {"stack" }
2719 if subCommand != "" {
2820 commandArgs = append (commandArgs , subCommand )
2921 }
3022
31- return executeTerragruntCommand (t , opts , commandArgs , useArgSeparator , additionalArgs ... )
23+ return executeTerragruntCommand (t , opts , commandArgs , additionalArgs ... )
3224}
3325
3426// runTerragruntCommandE is the core function that executes regular tg commands
@@ -38,42 +30,30 @@ func runTerragruntCommandE(t testing.TestingT, opts *Options, command string,
3830 // Build the base command arguments starting with the command
3931 commandArgs := []string {command }
4032
41- // For non-stack commands, we typically don't use the separator
42- return executeTerragruntCommand (t , opts , commandArgs , false , additionalArgs ... )
33+ return executeTerragruntCommand (t , opts , commandArgs , additionalArgs ... )
4334}
4435
4536// executeTerragruntCommand is the common execution function for all tg commands
4637// It handles validation, argument construction, retry logic, and error handling
4738func executeTerragruntCommand (t testing.TestingT , opts * Options , baseCommandArgs []string ,
48- useArgSeparator bool , additionalArgs ... string ) (string , error ) {
49- // Validate required options
50- if err := validateOptions (opts ); err != nil {
39+ additionalArgs ... string ) (string , error ) {
40+ // Validate and prepare options
41+ if err := prepareOptions (opts ); err != nil {
5142 return "" , err
5243 }
5344
54- // Apply common tg options and get the final command arguments
55- terragruntOptions , finalArgs := GetCommonOptions (opts , baseCommandArgs ... )
56-
57- // Append arguments from options using the new separation logic
58- argsFromOptions := GetArgsForCommand (terragruntOptions , useArgSeparator )
59- finalArgs = append (finalArgs , argsFromOptions ... )
60-
61- // Append any additional arguments passed directly to this function
62- if len (additionalArgs ) > 0 {
63- finalArgs = append (finalArgs , additionalArgs ... )
64- }
65-
66- // Generate the final shell command
67- execCommand := generateCommand (terragruntOptions , finalArgs ... )
68- commandDescription := fmt .Sprintf ("%s %v" , terragruntOptions .TerragruntBinary , finalArgs )
45+ // Build args and generate command
46+ finalArgs := buildTerragruntArgs (opts , append (baseCommandArgs , additionalArgs ... )... )
47+ execCommand := generateCommand (opts , finalArgs ... )
48+ commandDescription := fmt .Sprintf ("%s %v" , opts .TerragruntBinary , finalArgs )
6949
7050 // Execute the command with retry logic and error handling
7151 return retry .DoWithRetryableErrorsE (
7252 t ,
7353 commandDescription ,
74- terragruntOptions .RetryableTerraformErrors ,
75- terragruntOptions .MaxRetries ,
76- terragruntOptions .TimeBetweenRetries ,
54+ opts .RetryableTerraformErrors ,
55+ opts .MaxRetries ,
56+ opts .TimeBetweenRetries ,
7757 func () (string , error ) {
7858 output , err := shell .RunCommandAndGetOutputE (t , execCommand )
7959 if err != nil {
@@ -113,6 +93,33 @@ func hasWarning(opts *Options, commandOutput string) error {
11393 return nil
11494}
11595
96+ // prepareOptions validates options and sets defaults
97+ func prepareOptions (opts * Options ) error {
98+ if err := validateOptions (opts ); err != nil {
99+ return err
100+ }
101+ if opts .TerragruntBinary == "" {
102+ opts .TerragruntBinary = DefaultTerragruntBinary
103+ }
104+ setTerragruntLogFormatting (opts )
105+ return nil
106+ }
107+
108+ // buildTerragruntArgs constructs the final argument list for a terragrunt command
109+ // Arguments are ordered as: TerragruntArgs → --non-interactive → commandArgs → TerraformArgs
110+ func buildTerragruntArgs (opts * Options , commandArgs ... string ) []string {
111+ var args []string
112+ args = append (args , opts .TerragruntArgs ... )
113+ args = append (args , NonInteractiveFlag )
114+ args = append (args , commandArgs ... )
115+
116+ if len (opts .TerraformArgs ) > 0 {
117+ args = append (args , opts .TerraformArgs ... )
118+ }
119+
120+ return args
121+ }
122+
116123// validateOptions validates that required options are provided
117124func validateOptions (opts * Options ) error {
118125 if opts == nil {
@@ -132,10 +139,15 @@ const defaultErrorExitCode = 1
132139
133140// getExitCodeForTerragruntCommandE runs terragrunt with the given arguments and options and returns exit code
134141func getExitCodeForTerragruntCommandE (t testing.TestingT , additionalOptions * Options , additionalArgs ... string ) (int , error ) {
135- options , args := GetCommonOptions (additionalOptions , additionalArgs ... )
142+ // Validate and prepare options
143+ if err := prepareOptions (additionalOptions ); err != nil {
144+ return defaultErrorExitCode , err
145+ }
136146
147+ // Build args and generate command
148+ args := buildTerragruntArgs (additionalOptions , additionalArgs ... )
137149 additionalOptions .Logger .Logf (t , "Running terragrunt with args %v" , args )
138- cmd := generateCommand (options , args ... )
150+ cmd := generateCommand (additionalOptions , args ... )
139151 _ , err := shell .RunCommandAndGetOutputE (t , cmd )
140152 if err == nil {
141153 return defaultSuccessExitCode , nil
0 commit comments