-
-
Notifications
You must be signed in to change notification settings - Fork 540
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: When a config file is given, do not specify formatter on cli (terraform_docs) #386
feat: When a config file is given, do not specify formatter on cli (terraform_docs) #386
Conversation
hooks/terraform_docs.sh
Outdated
# Decide formatter to use | ||
# | ||
local tf_docs_formatter="md" | ||
if [[ "$args" == *"--config"* ]]; then |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the contribution. Good point.
Can this if
conditional be re-worked by adding check for --config
arg into case
block on lines 125-135 and setting some var conditionally, e.g. config_file="true"
, and here instead of if
statement do something like [[ $config_file != "true" ]] && local tf_docs_formatter="md"
? Thanks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So I had a look at the case block which appears to be looking at the values in the $hook_config
variable. This doesn't actually contain details passed in by the args property, those are contained in the $args
variable. I'm happy to alter the PR, but this doesn't appear to be the best way.
Would something like this be ok?
#
# Override formatter if no config file set
#
[[ "$args" != *"--config"* ]] && local tf_docs_formatter="md"
I can of course break this up a little and do something like:
#
# Override formatter if no config file set
#
[[ "$args" == *"--config"* ]] && local has_config_file="true"
[[ "$has_config_file" != "true" ]] && local tf_docs_formatter="md"
Let me know what you think.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, my bad. I indeed misread that part of code 🤦🏻
Yep [[ "$args" != *"--config"* ]] && local tf_docs_formatter="md"
looks good to me. Thanks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's no worries, that was actually my first solution this morning.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The conditional statement may probably be improved a bit like this to cover short opt and to ensure the match is not a part of some other opt/value and to ensure this opt has a value (not sure whether one can put =
instead of
between opt and its value though):
[[ ! $args =~ (^|[[:space:]])(-c|--config)[[:space:]]+[^[:space:]]+ ]]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On the short code I agree, however this currently isn't supported in any case. See line 19:
ARGS=${ARGS[*]/--config=/--config=$(pwd)\/}
The absolute path resolution only kicks in if the full option --config
is set. I can include this fix in my PR, but it feels out of scope. Happy to alter as required though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, so =
sign is allowed. Looks like we need to improve code on line 19 since it doesn't cover short options and space instead of equal sign 🤔 Though this indeed is out of scope of your PR. Let's pitch upon [[ ! $args =~ (^|[[:space:]])--config=[^[:space:]]+ ]] && local tf_docs_formatter="md"
so that these two are aligned?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[[ "$args" != *"--config="* ]] && local tf_docs_formatter="md"
looks good too if you prefer it more =) (the only thing I'd like to ask to put is equal sign so that — again — the two bits of code aligned with each other)
Thank you.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've swapped out for the regex approach so hopefully good to go.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Anyone has capacity to test this with Bash v3 please?
Maybe we can just pin the bash version used in Mac here pre-commit-terraform/Dockerfile Line 177 in 51147f1
In Mac used bash 3.2 |
So the "(..|..)" thing doesn't work in Bash v3. @acodeninja Looks like |
No worries, pushed an update and updated the PR heading to include the correct hash for testing. |
I just built bash3 locally which appeared to be quickest way for me =)
And I'm having an objection on pinning Docker to one version of Bash, while allowing and supporting another version of Bash outside Docker. |
Well, not possible to install bash 3 via apk add. The oldest bash in Alpine is 4.3 |
Yep, the only lucky folks to use ancient Bash are Mac addicts 🤣 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tested, works fine
# [1.72.0](v1.71.0...v1.72.0) (2022-05-25) ### Features * When a config file is given, do not specify formatter on cli (terraform_docs) ([#386](#386)) ([962054b](962054b))
This PR is included in version 1.72.0 🎉 |
Description of your changes
The change is to the
terraform_docs
hook. Currently this hook will ignore the formatter set in any specified config file, defaulting to themd
formatter. On our project we use themarkdown document
formatter but this is ignored by the hook. I consider this a bug but thought a PR would be better than a new issue for you.How can we test changes
Run:
Create the following files.
.terraform-docs.yml
.pre-commit-config.yaml
main.tf
README.md
Then run
pre-commit run --all-files
.You will see the output in the readme after running will be:
This is not the configured and desired format according to the
.terraform-docs.yml
configuration.Now, change the
.pre-commit-config.yaml
file to the following:And run
pre-commit run --all-files
again.You will now see the readme file contents is as expected from the configuration of terraform docs.