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

feat: Support set custom TF/OpenTofu binary. | If you use a custom Docker image build, please note that TERRAFORM_VERSION now must be provided #670

Merged
merged 36 commits into from May 23, 2024

Conversation

den-is
Copy link
Contributor

@den-is den-is commented May 4, 2024

Put an x into the box if that apply:

  • This PR introduces breaking change.
  • This PR fixes a bug.
  • This PR adds new functionality.
  • This PR enhances existing functionality.

Description of your changes

Allow setting custom TF binary.

  • provide per hook setting --hook-config=--tf-path=<path_to_binary_or_binary_name>
  • export ENV variable PCT_TFPATH=<path_to_binary_or_binary_name>
  • export ENV variable TERRAGRUNT_TFPATH=<path_to_binary_or_binary_name>
  • if none of above are set defaults to terraform binary found in the user's $PATH
  • if none of above are set/available defaults to tofu binary found in the user's $PATH
  • in the end, if none of above is set/available - exit 1 with error message

Side feature. Fixes bad README.md

I'm open to any suggestion and contributions.

Fixes #570

How can we test changes

Create dummy repository with a dummy terraform module test

mkdir test
cd test
git init

Add files to test/:
.pre-commit-config.yaml

repos:
- repo: local
  hooks:
  - id: terraform_fmt
    name: terraform_fmt
    entry: /path/to/pre-commit-terraform/hooks/terraform_fmt.sh
    language: script
    files: \.tf$
    # Uncomment these lines. Or set global ENV variables PCT_TFPATH or TERRAGRUNT_TFPATH
    #args:
    #- --hook-config=--tf-path=tofu

main.tf

# dummy terraform file with ugly formatting
resource "null_resource" "print_time" {
triggers = {
current_time = timestamp()
}

provisioner "local-exec" {
command = "echo Current time is ${triggers.current_time}"
}
}

Run in the test module directory:

pre-commit install
pre-commit run --verbose --files main.tf

@den-is den-is changed the title Allowing setting custom TF binary feat: Allow setting custom TF binary May 4, 2024
yermulnik

This comment was marked as resolved.

@den-is

This comment was marked as resolved.

@yermulnik

This comment was marked as outdated.

@yermulnik

This comment was marked as resolved.

@yermulnik

This comment was marked as resolved.

@den-is

This comment was marked as resolved.

@yermulnik

This comment was marked as resolved.

@MaxymVlasov
Copy link
Collaborator

Ah, it discared full readme, not just this commit, meh, 1 min

hooks/_common.sh Outdated Show resolved Hide resolved
@den-is
Copy link
Contributor Author

den-is commented May 21, 2024

Pushed final README fixes. Missed the broken README part the last time when I was reverting back to */

hooks/_common.sh Outdated
Comment on lines 481 to 484
# Maybe there is a similar setting for Terragrunt already
elif [[ $TERRAGRUNT_TFPATH ]]; then
echo "${TERRAGRUNT_TFPATH}"
return
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't get it

According to https://terragrunt.gruntwork.io/docs/reference/cli-options/#terragrunt-tfpath it will be automatically specified for terragrunt => for terragrunt hooks too.

Why do we need to set Terragrunt path for vanilla TF hooks?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Imagine you are working in some environment that already uses TERRAGRUNT_TFPATH env variable (global env, using direnv, or any other method)
So if a variable already exists and serves exactly the same purposes, why not to check it and reuse it, why to have other args/vars/etc?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Not sure I'm following the gist of the question, though probably (I'm purely guesstimating) this way it would be easier for people using both TF and TG hooks to specify custom binary only once using TG's method but covering also TF? 🤔 And from this point of view it makes sense to me.

@den-is What's your idea?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not sure I'm following the gist of the question, though probably (I'm purely guesstimating) this way it would be easier for people using both TF and TG hooks to specify custom binary only once using TG's method but covering also TF? 🤔 And from this point of view it makes sense to me.

@yermulnik Erm... this was already replied to by me yesterday.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Folks that use terragrunt should use terragrunt hooks, not terraform hooks - terraform will just not work in most cases. It's hard to imagine that someone uses both terragrunt and vanilla terraform, but if so - why do we think that folks would be happy with interference between "prj1 with TG and OpenTofu" and "prj2 with vanilla TF"?

And another question: do we need to specify $tf_path for terragrunt hooks?

Like terragrunt validate --terragrunt-tfpath "$tf_path" "${args[@]}"

terragrunt validate "${args[@]}"

Copy link
Collaborator

Choose a reason for hiding this comment

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

Erm... this was already replied to by me yesterday.

@den-is My bad. I missed that comment (found it now). Thanks.

Copy link
Collaborator

Choose a reason for hiding this comment

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

It's hard to imagine that someone uses both terragrunt and vanilla terraform

I don't in particular, but a fellow squad in my project — does. And I'd imagine there are people in the wild who do the same and hence they may like using TERRAGRUNT_TFPATH to cover both use cases if they use pre-commit-terraform.

And another question: do we need to specify $tf_path for terragrunt hooks?

That's a good question. We probably should. But only when it is provided via TG hook config. In the rest of use cases TG should rely upon its own logic, including TERRAGRUNT_TFPATH and others described at https://terragrunt.gruntwork.io/docs/reference/cli-options/#terragrunt-tfpath

Copy link
Collaborator

Choose a reason for hiding this comment

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

Had a conversation with @den-is
TL;DR: we assume that it will no break anything in 99% of cases. And if such a thing happened - I'll mention Den is such an issue :D

Copy link
Collaborator

Choose a reason for hiding this comment

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

Works for me 👍🏻

I'll mention Den is such an issue :D

I feel like is should be in, but with is the sentence sounds fabulous 🤣

@MaxymVlasov MaxymVlasov changed the title feat: Allow setting custom TF binary feat: Support set custom TF/OpenTofu binary. | If you use a custom Docker image build, please note that TERRAFORM_VERSION now must be provided May 21, 2024
README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
hooks/_common.sh Outdated Show resolved Hide resolved
hooks/_common.sh Outdated Show resolved Hide resolved
hooks/_common.sh Outdated Show resolved Hide resolved
hooks/_common.sh Outdated
Comment on lines 481 to 484
# Maybe there is a similar setting for Terragrunt already
elif [[ $TERRAGRUNT_TFPATH ]]; then
echo "${TERRAGRUNT_TFPATH}"
return
Copy link
Collaborator

Choose a reason for hiding this comment

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

Not sure I'm following the gist of the question, though probably (I'm purely guesstimating) this way it would be easier for people using both TF and TG hooks to specify custom binary only once using TG's method but covering also TF? 🤔 And from this point of view it makes sense to me.

@den-is What's your idea?

hooks/_common.sh Show resolved Hide resolved
tools/install/opentofu.sh Outdated Show resolved Hide resolved
tools/install/opentofu.sh Outdated Show resolved Hide resolved
Copy link
Collaborator

@yermulnik yermulnik left a comment

Choose a reason for hiding this comment

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

LGTM apart from #670 (comment) that I didn't get why TF switched from latest to exact version.

README.md Show resolved Hide resolved
README.md Outdated
@@ -103,7 +115,7 @@ All available tags [here](https://github.com/antonbabenko/pre-commit-terraform/p
**Build from scratch**:

> [!IMPORTANT]
> To build image you need to have [`docker buildx`](https://docs.docker.com/build/install-buildx/) enabled as default builder.
> To build image you need to have [`docker buildx`](https://docs.docker.com/build/install-buildx/) enabled as default builder.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was fixed by VSCode automatically.
Will check for workaround

hooks/_common.sh Outdated Show resolved Hide resolved
hooks/_common.sh Outdated Show resolved Hide resolved
hooks/_common.sh Outdated Show resolved Hide resolved
hooks/_common.sh Show resolved Hide resolved
README.md Show resolved Hide resolved
hooks/_common.sh Outdated
Comment on lines 481 to 484
# Maybe there is a similar setting for Terragrunt already
elif [[ $TERRAGRUNT_TFPATH ]]; then
echo "${TERRAGRUNT_TFPATH}"
return
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Imagine you are working in some environment that already uses TERRAGRUNT_TFPATH env variable (global env, using direnv, or any other method)
So if a variable already exists and serves exactly the same purposes, why not to check it and reuse it, why to have other args/vars/etc?

README.md Show resolved Hide resolved
hooks/_common.sh Outdated
Comment on lines 481 to 484
# Maybe there is a similar setting for Terragrunt already
elif [[ $TERRAGRUNT_TFPATH ]]; then
echo "${TERRAGRUNT_TFPATH}"
return
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not sure I'm following the gist of the question, though probably (I'm purely guesstimating) this way it would be easier for people using both TF and TG hooks to specify custom binary only once using TG's method but covering also TF? 🤔 And from this point of view it makes sense to me.

@yermulnik Erm... this was already replied to by me yesterday.

@yermulnik
Copy link
Collaborator

yermulnik commented May 22, 2024

This was fixed by VSCode automatically.
Will check for workaround

Add the below to config of VSCode of yours:

    "[markdown]": {
        "files.trimTrailingWhitespace": false
    }

Other common useful settings VSCode usually is missing from what I've found from experience of others using it:

    "files.trimTrailingWhitespace": true,
    "files.insertFinalNewline": true,
    "files.trimFinalNewlines": true,

@den-is
Copy link
Contributor Author

den-is commented May 22, 2024

This was fixed by VSCode automatically.
Will check for workaround

Add the below to config of VSCode of yours:

    "[markdown]": {
        "files.trimTrailingWhitespace": false
    }

Other common useful settings VSCode usually is missing from what I've found from experience of others using it:

    "files.trimTrailingWhitespace": true,
    "files.insertFinalNewline": true,
    "files.trimFinalNewlines": true,

lol dude... i always have this setting in all my editors... for years... you basically copy pasted my snippets which I add to different projects were noobs can't solve their whitespace issues.

Only thing I was not able to find correct settings for VSCode to fix * replacement with -. (Well I did not spend hours to find it in VSCode... just used vim to revert to your standard)

@MaxymVlasov
Copy link
Collaborator

MaxymVlasov commented May 22, 2024

Space trimming and so on should be done by .editorconfig in the first place, as is supported by everything, including vim.

TOC is not supported by built-in MD engine, for this I use https://marketplace.visualstudio.com/items?itemName=yzhang.markdown-all-in-one
image

With

    "markdownlint.config": {
        "MD029": false,
        "MD012": {
            "maximum": 5
        }
    },
    "markdown.extension.toc.unorderedList.marker": "*",
    "markdown.extension.toc.levels": "2..6",

I'll check vanilla VS Code settings and then will send PR which will add needed configs and/or rewrite MD file

@den-is
Copy link
Contributor Author

den-is commented May 22, 2024

In case if person uses VSCode, most of .editorconfig settings are supported natively. Otherwise you have to install another extension.

But yea, addined .editorconfig to you project makes it clear what formatting you require and ask to match. (from those who can't configure their IDEs with universal/common/common-sense/not-personal settings)

@yermulnik
Copy link
Collaborator

Only thing I was not able to find correct settings for VSCode to fix * replacement with -. (Well I did not spend hours to find it in VSCode...

It's probably not the VSCode per se, but an extension. Like some of these: https://github.com/yzhang-gh/vscode-markdown or https://github.com/prettier/prettier-vscode

BTW FWIW: yzhang-gh/vscode-markdown#145

just used vim to revert to your standard)

It's not ours. It's Markdown language specification. Unless you prove otherwise.

PS: I looked at markdownlint rules and they seem to prefer * over - for unordered lists in documentation — https://github.com/DavidAnson/markdownlint/blob/main/doc/Rules.md 🤔

@MaxymVlasov MaxymVlasov merged commit c7011c0 into antonbabenko:master May 23, 2024
6 checks passed
antonbabenko pushed a commit that referenced this pull request May 23, 2024
# [1.90.0](v1.89.1...v1.90.0) (2024-05-23)

### Features

* Support set custom TF/OpenTofu binary. | If you use a custom Docker image build, please note that `TERRAFORM_VERSION` now must be provided ([#670](#670)) ([c7011c0](c7011c0))
@antonbabenko
Copy link
Owner

This PR is included in version 1.90.0 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support for OpenTofu
4 participants