Conditional replace file
javier-torres opened this issue · 5 comments
Hi,
I´m, using this fantatic plugin to generate automated version from my Terraform modules and change automatically the version in locals.tf file with the current version Release.
All this is working perfectly, but I have a problem and I need to implement the change of 2 values in the same file, but there are times that the search for the pattern 2 may or may not be there.
Terraform locals.tf file
module_tags = {
"tf:Used" = "True"
"tf:Module" = "module-base-iam-policy"
"tf:ModuleVersion" = "2.0.3"
}
submodule_tags = {
"tf:Used" = "True"
"tf:ParentModule" = "module-base-iam-policy"
"tf:ParentModuleVersion" = "2.0.3"
}
}
@google/semantic-release-replace-plugin
["@google/semantic-release-replace-plugin", {
"replacements": [
{
"files": ["locals.tf"],
"from": "\"tf:ModuleVersion\" = \".*\"",
"to": "\"tf:ModuleVersion\" = \"${nextRelease.version}\"",
"results": [{
"file": "locals.tf",
"hasChanged": true,
"numMatches": 1,
"numReplacements": 1
}],
"countMatches": true
},
{
"files": ["locals.tf"],
"from": "\"tf:ParentModuleVersion\" = \".*\"",
"to": "\"tf:ParentModuleVersion\" = \"${nextRelease.version}\"",
"results": [{
"file": "locals.tf",
"hasChanged": true,
"numMatches": 1,
"numReplacements": 1
}],
"countMatches": true
}
]
}]
The plugin works correctly and makes the change in the 2 patterns provided, but the problem comes in that not all terraform modules carry the local variable submodule_tag.local. So if I have configured to look for the 2 options, if the module has the 2 options it works correctly, but if the module has only one option it fails `Error: Expected match not found!`
Terraform locals.tf file
locals {
module_tags = {
"tf:Used" = "True"
"tf:Module" = "module-base-iam-policy"
"tf:ModuleVersion" = "2.0.3"
}
}
It would be nice to implement a condition that if you pass a value for example `"optionalFile": true` that if it does not find the parameter to make the replacement, it does not give an error and passes successfully.
@google/semantic-release-replace-plugin
["@google/semantic-release-replace-plugin", {
"replacements": [
{
"files": ["locals.tf"],
"from": "\"tf:ModuleVersion\" = \".*\"",
"to": "\"tf:ModuleVersion\" = \"${nextRelease.version}\"",
"results": [{
"file": "locals.tf",
"hasChanged": true,
"numMatches": 1,
"numReplacements": 1
}],
"countMatches": true
},
{
"files": ["locals.tf"],
"optionalFile": true,
"from": "\"tf:ParentModuleVersion\" = \".*\"",
"to": "\"tf:ParentModuleVersion\" = \"${nextRelease.version}\"",
"results": [{
"file": "locals.tf",
"hasChanged": true,
"numMatches": 1,
"numReplacements": 1
}],
"countMatches": true
}
]
}]
This will allow to configure the replacement of several parameters, and if one does not exist, to specify it so that it does not give an error.
Thank you very much, I hope you can help me with this implementation that I think will be of great value to the community.
Javier Torres
Try leaving out the results
and countMatches
. I believe that should turn of validation.
Can you explain me better how would be the result of the code according to what I have defined in my code and give me an example? I would appreciate it very much.
Thank you very much
Would it be like this?
{
"files": ["locals.tf"],
"from": "\"tf:ParentModuleVersion\" = \".*\"",
"to": "\"tf:ParentModuleVersion\" = \"${nextRelease.version}\"",
"countMatches": false
}
Yes. The presence of the results triggers the validation.
Hi,
Thank you very much! As you say, removing the result has been working correctly.
It's nice to meet with people as compententent and helpful as you!!!!
Javier Torres