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: Set Terraform plan file as last argument in apply-all #1429

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
20 changes: 19 additions & 1 deletion options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,9 +276,27 @@ func (terragruntOptions *TerragruntOptions) InsertTerraformCliArgs(argsToInsert
// Options must be inserted after command but before the other args
// command is either 1 word or 2 words
var args []string
var lastArg string

args = append(args, terragruntOptions.TerraformCliArgs[:commandLength]...)
args = append(args, argsToInsert...)
// We need to iterate over the argsToInsert to find an arg that is a file.
// If we found one, we treat it as the Terraform plan file and we then set it
// as lastArg to append it as last argument.
Comment on lines +282 to +284
Copy link
Member

Choose a reason for hiding this comment

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

Hm, I'm not sure this is a safe assumption to make. That is, it's possible to have a different arg passed to Terraform that is a file—or worse yet, happens to match the name of a valid file—but isn't a plan file. Here's an example:

terraform apply \ 
  -state foo.tfstate \
  -var foo=bar

If there's a foo.tfstate already, the code above will move that parameter to the end of the command, as if it's a plan file.

Copy link
Author

Choose a reason for hiding this comment

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

Thank you @brikis98, I haven't considered that use case. I will write a test as well as validating the current ones work fine and come back to you.

for _, arg := range argsToInsert {
if util.IsFile(arg) {
lastArg = arg
} else {
args = append(args, arg)
}
}

args = append(args, terragruntOptions.TerraformCliArgs[commandLength:]...)

// Append lastArg which is a Terraform plan file
if lastArg != "" {
args = append(args, lastArg)
}

terragruntOptions.TerraformCliArgs = args
}

Expand Down