From cbd26b20c7dcae9b15ccecc7bf76a5e1c0ffdc81 Mon Sep 17 00:00:00 2001 From: rothandrew Date: Fri, 14 Dec 2018 10:45:59 -0500 Subject: [PATCH] Add `--dest` argument --- README.md | 4 ++-- pre_commit_hooks/terraform_docs_replace.py | 9 +++++++-- setup.py | 2 +- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index ab1d44d..84ed481 100644 --- a/README.md +++ b/README.md @@ -62,13 +62,13 @@ Check the [source file](https://github.com/antonbabenko/pre-commit-terraform/blo 1. `terraform_docs` and `terraform_docs_without_aggregate_type_defaults` will insert/update documentation generated by [terraform-docs](https://github.com/segmentio/terraform-docs) between markers - `` and `` if they are present in `README.md`. Make sure that `terraform-docs` is installed. -1. `terraform_docs_replace` replaces the entire README.md rather than doing string replacement between markers. Put your additional documentation at the top of your `main.tf` for it to be pulled in. +1. `terraform_docs_replace` replaces the entire README.md rather than doing string replacement between markers. Put your additional documentation at the top of your `main.tf` for it to be pulled in. The optional `--dest` argument lets your change the name of the file that gets created/modified 1. Example: ```yaml hooks: - id: terraform_docs_replace - args: ['--with-aggregate-type-defaults', '--sort-inputs-by-required'] + args: ['--with-aggregate-type-defaults', '--sort-inputs-by-required', '--dest=TEST.md'] ``` 1. It is possible to pass additional arguments to shell scripts when using `terraform_docs` and `terraform_docs_without_aggregate_type_defaults`. Send pull-request with the new hook if there is something missing. diff --git a/pre_commit_hooks/terraform_docs_replace.py b/pre_commit_hooks/terraform_docs_replace.py index 31fbd1a..7e3bc2f 100644 --- a/pre_commit_hooks/terraform_docs_replace.py +++ b/pre_commit_hooks/terraform_docs_replace.py @@ -10,6 +10,9 @@ def main(argv=None): pulling the documentation from main.tf in order to replace the entire README.md file each time.""" ) + parser.add_argument( + '--dest', dest='dest', default='README.md', + ) parser.add_argument( '--sort-inputs-by-required', dest='sort', action='store_true', ) @@ -21,7 +24,9 @@ def main(argv=None): dirs = [] for filename in args.filenames: - if os.path.realpath(filename) not in dirs: + if (os.path.realpath(filename) not in dirs and \ + len(os.path.realpath(filename).strip()) > 0 and \ + (filename.endswith(".tf") or filename.endswith(".tfvars"))): dirs.append(os.path.dirname(filename)) retval = 0 @@ -38,7 +43,7 @@ def main(argv=None): procArgs.append(dir) procArgs.append("| sed -e '$ d' -e 'N;/^\\n$/D;P;D'") procArgs.append('>') - procArgs.append('{}/README.md'.format(dir)) + procArgs.append("./{dir}/{dest}".format(dir=dir,dest=args.dest)) subprocess.check_call(" ".join(procArgs), shell=True) except subprocess.CalledProcessError as e: print(e) diff --git a/setup.py b/setup.py index 44ae516..246de8c 100644 --- a/setup.py +++ b/setup.py @@ -4,7 +4,7 @@ from setuptools import setup setup( name='pre-commit-terraform', - description='Pre-commit hooks for terraform_docs_replace, + description='Pre-commit hooks for terraform_docs_replace', url='https://github.com/antonbabenko/pre-commit-terraform', version_format='{tag}+{gitsha}',