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
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 10 additions & 19 deletions terraform_docs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ set -eo pipefail
main() {
initialize_
parse_cmdline_ "$@"
terraform_docs_ "${ARGS[*]}" "${FILES[@]}"
terraform_docs_
}

initialize_() {
Expand Down Expand Up @@ -47,10 +47,6 @@ parse_cmdline_() {
}

terraform_docs_() {
local -r args="$1"
shift
local -a -r files=("$@")

local hack_terraform_docs
hack_terraform_docs=$(terraform version | head -1 | grep -c 0.12) || true

Expand All @@ -64,7 +60,7 @@ terraform_docs_() {

if [[ -z "$is_old_terraform_docs" ]]; then # Using terraform-docs 0.8+ (preferred)

terraform_docs "0" "$args" "${files[@]}"
terraform_docs "0"

elif [[ "$hack_terraform_docs" == "1" ]]; then # Using awk script because terraform-docs is older than 0.8 and terraform 0.12 is used

Expand All @@ -76,28 +72,25 @@ terraform_docs_() {
local tmp_file_awk
tmp_file_awk=$(mktemp "${TMPDIR:-/tmp}/terraform-docs-XXXXXXXXXX")
terraform_docs_awk "$tmp_file_awk"
terraform_docs "$tmp_file_awk" "$args" "${files[@]}"
terraform_docs "$tmp_file_awk"
rm -f "$tmp_file_awk"

else # Using terraform 0.11 and no awk script is needed for that

terraform_docs "0" "$args" "${files[@]}"
terraform_docs "0"

fi
}

terraform_docs() {
local -r terraform_docs_awk_file="$1"
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.


declare -a paths
declare -a tfvars_files

local index=0
local file_with_path
for file_with_path in "${files[@]}"; do
for file_with_path in "${FILES[@]}"; do
file_with_path="${file_with_path// /__REPLACED__SPACE__}"

paths[index]=$(dirname "$file_with_path")
Expand All @@ -124,19 +117,17 @@ terraform_docs() {
fi

if [[ "$terraform_docs_awk_file" == "0" ]]; then
# shellcheck disable=SC2086
terraform-docs md $args ./ > "$tmp_file"
terraform-docs md "${ARGS[@]}" ./ > "$tmp_file"
else
# Can't append extension for mktemp, so renaming instead
local tmp_file_docs
tmp_file_docs=$(mktemp "${TMPDIR:-/tmp}/terraform-docs-XXXXXXXXXX")
mv "$tmp_file_docs" "$tmp_file_docs.tf"
local tmp_file_docs_tf
tmp_file_docs=$(mktemp "${TMPDIR:-/tmp}/terraform-docs-XXXXXXXXXX")
tmp_file_docs_tf="$tmp_file_docs.tf"
mv "$tmp_file_docs" "$tmp_file_docs_tf"

awk -f "$terraform_docs_awk_file" ./*.tf > "$tmp_file_docs_tf"
# shellcheck disable=SC2086
terraform-docs md $args "$tmp_file_docs_tf" > "$tmp_file"
terraform-docs md "${ARGS[@]}" "$tmp_file_docs_tf" > "$tmp_file"
rm -f "$tmp_file_docs_tf"
fi

Expand Down
11 changes: 4 additions & 7 deletions terraform_tfsec.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,22 @@ set -eo pipefail
main() {
initialize_
parse_cmdline_ "$@"

# propagate $FILES to custom function
tfsec_ "$ARGS" "$FILES"
tfsec_
}

tfsec_() {
# consume modified files passed from pre-commit so that
# tfsec runs against only those relevant directories
for file_with_path in $FILES; do
for file_with_path in "${FILES[@]}"; do
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++ ))

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.

popd > /dev/null
done
}
Expand Down