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: Cleanup internal argument handling #142

Closed
wants to merge 2 commits into from

Conversation

robinbowes
Copy link
Contributor

ARGS and FILES are global arrays - no need to pass them as function arguments

@robinbowes robinbowes marked this pull request as draft September 8, 2020 11:26
@robinbowes robinbowes changed the title Fix: Cleanup internal argument handling fix: Cleanup internal argument handling Sep 8, 2020
ARGS and FILES are global arrays - no need to pass them as function arguments
@robinbowes
Copy link
Contributor Author

@sergei-ivanov Can you please take a look at this. I've cleaned up the argument handling in terraform_docs so we simply use the global arrays ARGS and FILES rather than trying to pass them around internally.

terraform_docs.sh Outdated Show resolved Hide resolved
local -r args="$2"
shift 2
local -a -r files=("$@")
local -r terraform_docs_awk_file="$1" ; shift
Copy link
Contributor

Choose a reason for hiding this comment

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

There's not really much point in shift anymore, now that we are using the globals.

Suggested change
local -r terraform_docs_awk_file="$1" ; shift
local -r terraform_docs_awk_file="$1"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There's not really much point in shift anymore, now that we are using the globals.

This is more of a safety net - it will throw an error if there is no arg.

terraform_docs.sh Outdated Show resolved Hide resolved
done

for path_uniq in $(echo "${paths[*]}" | tr ' ' '\n' | sort -u); do
path_uniq="${path_uniq//__REPLACED__SPACE__/ }"
pushd "$path_uniq" > /dev/null
tfsec $ARGS
tfsec "${ARGS[@]}"
Copy link
Contributor

Choose a reason for hiding this comment

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

Same here probably:

Suggested change
tfsec "${ARGS[@]}"
tfsec ${ARGS[*]}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually, I think using the quoted @ form is the correct approach here, and in terraform_docs.

Can you re-check with 3a68667 and let me know what you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

FYI, it seems work OK for me:

 $ pre-commit try-repo -a ~/code/pre-commit-terraform terraform_docs
===============================================================================
Using config:
===============================================================================
repos:
-   repo: ../../../../pre-commit-terraform
    rev: 3a68667efc0f95f9c01db4a79aff33075697fc2e
    hooks:
    -   id: terraform_docs
===============================================================================
[INFO] Initializing environment for ../../../../pre-commit-terraform.
Terraform docs...........................................................Failed
- hook id: terraform_docs
- files were modified by this hook

Copy link
Contributor

Choose a reason for hiding this comment

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

It does not work with args specified like this:

    hooks:
      - id: terraform_docs
        args:
          - '--args=document --indent 3'

...which is expected. But it does work when args are split into individual tokens:

    hooks:
      - id: terraform_docs
        args:
          - '--args=document'
          - '--args=--indent'
          - '--args=3'

I am not sure whether we would want to enforce passing the args one by one, or be more lenient like in the 1st example.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think that's a cleaner solution than trying to cope with eg. '--args=document --indent 3'

Copy link
Contributor

Choose a reason for hiding this comment

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

Both options are fine by me. If we settle on option 2, I think we need a section in the documentation/readme with a usage example for additional arguments (feel free to use the example above). Simply because it is not obvious at all at the moment.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agreed - the docs need updating.

Is your example correct, though? Wouldn't it be this:

hooks:
      - id: terraform_docs
        args:
          - 'document'
          - '--indent=3'

I think the other examples in the README may be wrong too.

Copy link
Contributor

Choose a reason for hiding this comment

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

The one above is correct and tested. One needs to prepend each argument with --args= prefix. I guess that is because we want to distinguish the arguments to terraform-docs from the list of files passed in by pre-commit.

So the arguments should be passed like this:

      - id: terraform_docs
        args:
          - '--args=document'
          - '--args=--indent'
          - '--args=3'

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK, so I think we're saying we should use one arg per line ie. option 2 but that we should make sure we update the documentation to be clearer.

@jpatallah
Copy link

When is the slated to be merged in? Our tfsec exclusion parameter isn't being read properly and this fix is very important!

@MaxymVlasov MaxymVlasov added estimate/2h Need 2 hours to be done help wanted Extra attention is needed labels Sep 9, 2021
@github-actions
Copy link

This PR has been automatically marked as stale because it has been open 30 days
with no activity. Remove stale label or comment or this PR will be closed in 10 days

@github-actions github-actions bot added the stale Denotes an issue or PR has remained open with no activity and has become stale. label Oct 10, 2021
@MaxymVlasov MaxymVlasov removed the stale Denotes an issue or PR has remained open with no activity and has become stale. label Oct 14, 2021
file_with_path="${file_with_path// /__REPLACED__SPACE__}"
paths[index]=$(dirname "$file_with_path")

let "index+=1"
(( index+=1 ))
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
(( index+=1 ))
(( index++ ))

@MaxymVlasov
Copy link
Collaborator

Nope, we should pass them to functions, because it explicitly mentions which vars go to function.

@robinbowes robinbowes deleted the fix_array_handling branch September 9, 2024 12:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
estimate/2h Need 2 hours to be done help wanted Extra attention is needed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants