hashicorp / packer

Packer is a tool for creating identical machine images for multiple platforms from a single source configuration.
http://www.packer.io
Other
15.11k stars 3.33k forks source link

HCL2: Add "strcontains" function support #13191

Open jaredledvina opened 2 weeks ago

jaredledvina commented 2 weeks ago

Community Note

Please search the existing issues for relevant feature requests, and use the reaction feature (https://blog.github.com/2016-03-10-add-reactions-to-pull-requests-issues-and-comments/) to add upvotes to pre-existing requests.

Description

I would love to see the Packer HCL2 syntax support the strcontains function similar to what is currently supported over in Terraform. This function is a really nice quick helper to do a contains comparison.

Use Case(s)

The primary use case is writing conditionals that depend on if a string is within another. For example if the source_ami name contains "test" or not.

Potential configuration

strcontains("hello world", "wor")

Potential References

https://developer.hashicorp.com/terraform/language/functions/strcontains

jaredledvina commented 2 weeks ago

I attempted to start adding support for this via https://github.com/zclconf/go-cty/pull/193 but completely overlooked the licensing issue. Looking at https://github.com/hashicorp/go-cty-funcs there's not an existing place that seems obvious to add support. I could just add it to https://github.com/hashicorp/packer/tree/main/hcl2template/function.

Do the maintainers have any preference for how to go about adding this?

lbajolet-hashicorp commented 1 week ago

Hey @jaredledvina,

This is a good idea indeed!

I see you had a chat with @apparentlymart over on CTY, he pretty much nailed it; I don't think we want to deal with potential legal issues between licenses, so do feel free to adapt Terraform's code for the function in Packer itself, no need to upstream that in a MIT project, even if it seems the better option from a technical standpoint, I don't want us to get dragged into a legal argument. I can probably do it also later this week, your call ultimately, but if you do implement it, I'll make sure to review the PR ASAP, thanks!