From 9aa971c069da7ef66f0a30075e6b68f21f243059 Mon Sep 17 00:00:00 2001 From: rothandrew Date: Thu, 13 Dec 2018 22:16:01 -0500 Subject: [PATCH 1/5] Add new hook for running terraform-docs with replacing README.md from doc in main.tf --- .pre-commit-hooks.yaml | 8 ++++ README.md | 10 +++++ hooks.yaml | 40 ----------------- pre_commit_hooks/__init__.py | 0 pre_commit_hooks/terraform_docs_replace.py | 50 ++++++++++++++++++++++ setup.py | 34 +++++++++++++++ 6 files changed, 102 insertions(+), 40 deletions(-) delete mode 100644 hooks.yaml create mode 100644 pre_commit_hooks/__init__.py create mode 100644 pre_commit_hooks/terraform_docs_replace.py create mode 100644 setup.py diff --git a/.pre-commit-hooks.yaml b/.pre-commit-hooks.yaml index 65436d109..64f2ad8ef 100644 --- a/.pre-commit-hooks.yaml +++ b/.pre-commit-hooks.yaml @@ -23,6 +23,14 @@ files: (\.tf)$ exclude: \.terraform\/.*$ +- id: terraform_docs_replace + name: Generate documentation for Terraform modules + language: python + entry: terraform_docs_replace + files: (\.tf)$ + exclude: \.terraform\/.*$ + description: Generates README.md files for Terraform modules + - id: terraform_validate_no_variables name: Terraform validate without variables description: Validates all Terraform configuration files without checking whether all required variables were set (basic check). diff --git a/README.md b/README.md index e3e7699ef..ab1d44df0 100644 --- a/README.md +++ b/README.md @@ -52,6 +52,7 @@ There are several [pre-commit](http://pre-commit.com/) hooks to keep Terraform c * `terraform_validate_with_variables` - Validates all Terraform configuration files and checks whether all required variables were specified. * `terraform_docs` - Inserts input and output documentation into `README.md`. Recommended. * `terraform_docs_without_aggregate_type_defaults` - Inserts input and output documentation into `README.md` without aggregate type defaults. +* `terraform_docs_replace` - Runs `terraform-docs` and pipes the output directly to README.md Check the [source file](https://github.com/antonbabenko/pre-commit-terraform/blob/master/.pre-commit-hooks.yaml) to know arguments used for each hook. @@ -61,6 +62,15 @@ 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. Example: + ```yaml + hooks: + - id: terraform_docs_replace + args: ['--with-aggregate-type-defaults', '--sort-inputs-by-required'] + ``` + 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. Enjoy the clean and documented code! diff --git a/hooks.yaml b/hooks.yaml deleted file mode 100644 index 65436d109..000000000 --- a/hooks.yaml +++ /dev/null @@ -1,40 +0,0 @@ -- id: terraform_fmt - name: Terraform fmt - description: Rewrites all Terraform configuration files to a canonical format. - entry: terraform_fmt.sh - language: script - files: (\.tf|\.tfvars)$ - exclude: \.terraform\/.*$ - -- id: terraform_docs - name: Terraform docs - description: Inserts input and output documentation into README.md (using terraform-docs). - entry: terraform_docs.sh - args: [--args=--with-aggregate-type-defaults] - language: script - files: (\.tf)$ - exclude: \.terraform\/.*$ - -- id: terraform_docs_without_aggregate_type_defaults - name: Terraform docs (without aggregate type defaults) - description: Inserts input and output documentation into README.md (using terraform-docs). - entry: terraform_docs.sh - language: script - files: (\.tf)$ - exclude: \.terraform\/.*$ - -- id: terraform_validate_no_variables - name: Terraform validate without variables - description: Validates all Terraform configuration files without checking whether all required variables were set (basic check). - entry: terraform_validate_no_variables.sh - language: script - files: (\.tf|\.tfvars)$ - exclude: \.terraform\/.*$ - -- id: terraform_validate_with_variables - name: Terraform validate with variables - description: Validates all Terraform configuration files and checks whether all required variables were specified. - entry: terraform_validate_with_variables.sh - language: script - files: (\.tf|\.tfvars)$ - exclude: \.terraform\/.*$ diff --git a/pre_commit_hooks/__init__.py b/pre_commit_hooks/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/pre_commit_hooks/terraform_docs_replace.py b/pre_commit_hooks/terraform_docs_replace.py new file mode 100644 index 000000000..31fbd1acd --- /dev/null +++ b/pre_commit_hooks/terraform_docs_replace.py @@ -0,0 +1,50 @@ +import argparse +import os +import subprocess +import sys + + +def main(argv=None): + parser = argparse.ArgumentParser( + description="""Run terraform-docs on a set of files. Follows the standard convention of + pulling the documentation from main.tf in order to replace the entire + README.md file each time.""" + ) + parser.add_argument( + '--sort-inputs-by-required', dest='sort', action='store_true', + ) + parser.add_argument( + '--with-aggregate-type-defaults', dest='aggregate', action='store_true', + ) + parser.add_argument('filenames', nargs='*', help='Filenames to check.') + args = parser.parse_args(argv) + + dirs = [] + for filename in args.filenames: + if os.path.realpath(filename) not in dirs: + dirs.append(os.path.dirname(filename)) + + retval = 0 + + for dir in dirs: + try: + procArgs = [] + procArgs.append('terraform-docs') + if args.sort: + procArgs.append('--sort-inputs-by-required') + if args.aggregate: + procArgs.append('--with-aggregate-type-defaults') + procArgs.append('md') + procArgs.append(dir) + procArgs.append("| sed -e '$ d' -e 'N;/^\\n$/D;P;D'") + procArgs.append('>') + procArgs.append('{}/README.md'.format(dir)) + subprocess.check_call(" ".join(procArgs), shell=True) + except subprocess.CalledProcessError as e: + print(e) + retval = 1 + return retval + + +if __name__ == '__main__': + sys.exit(main()) diff --git a/setup.py b/setup.py new file mode 100644 index 000000000..0b2d6e378 --- /dev/null +++ b/setup.py @@ -0,0 +1,34 @@ +from setuptools import find_packages +from setuptools import setup + + +setup( + name='pre-commit-terraform', + description='Pre-commit hooks for Terraform', + url='https://github.com/antonbabenko/pre-commit-terraform', + version_format='{tag}+{gitsha}', + + author='Andrew Roth', + author_email='roth.andy@gmail.com', + + classifiers=[ + 'License :: OSI Approved :: MIT License', + 'Programming Language :: Python :: 2', + 'Programming Language :: Python :: 2.7', + 'Programming Language :: Python :: 3', + 'Programming Language :: Python :: 3.6', + 'Programming Language :: Python :: 3.7', + 'Programming Language :: Python :: Implementation :: CPython', + 'Programming Language :: Python :: Implementation :: PyPy', + ], + + packages=find_packages(exclude=('tests*', 'testing*')), + install_requires=[ + 'setuptools-git-version', + ], + entry_points={ + 'console_scripts': [ + 'terraform_docs_replace = pre_commit_hooks.terraform_docs_replace:main', + ], + }, +) From debe93a82be27d9dab99858372a33cdde196a320 Mon Sep 17 00:00:00 2001 From: rothandrew Date: Fri, 14 Dec 2018 09:23:54 -0500 Subject: [PATCH 2/5] Address requested changes --- .pre-commit-hooks.yaml | 4 ++-- setup.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.pre-commit-hooks.yaml b/.pre-commit-hooks.yaml index 64f2ad8ef..e83763601 100644 --- a/.pre-commit-hooks.yaml +++ b/.pre-commit-hooks.yaml @@ -24,12 +24,12 @@ exclude: \.terraform\/.*$ - id: terraform_docs_replace - name: Generate documentation for Terraform modules + name: Terraform docs (overwrite README.md) language: python entry: terraform_docs_replace files: (\.tf)$ exclude: \.terraform\/.*$ - description: Generates README.md files for Terraform modules + description: Overwrite content of README.md with terraform-docs - id: terraform_validate_no_variables name: Terraform validate without variables diff --git a/setup.py b/setup.py index 0b2d6e378..44ae516ea 100644 --- a/setup.py +++ b/setup.py @@ -4,7 +4,7 @@ setup( name='pre-commit-terraform', - description='Pre-commit hooks for Terraform', + description='Pre-commit hooks for terraform_docs_replace, url='https://github.com/antonbabenko/pre-commit-terraform', version_format='{tag}+{gitsha}', From cbd26b20c7dcae9b15ccecc7bf76a5e1c0ffdc81 Mon Sep 17 00:00:00 2001 From: rothandrew Date: Fri, 14 Dec 2018 10:45:59 -0500 Subject: [PATCH 3/5] 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 ab1d44df0..84ed48114 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 31fbd1acd..7e3bc2fb8 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 44ae516ea..246de8c2b 100644 --- a/setup.py +++ b/setup.py @@ -4,7 +4,7 @@ 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}', From d3fe87daeac07704ae27e02e87df719dd7c9ac9f Mon Sep 17 00:00:00 2001 From: rothandrew Date: Fri, 14 Dec 2018 16:16:42 -0500 Subject: [PATCH 4/5] Address requested changes --- README.md | 8 +++++++- setup.py | 3 +-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 84ed48114..1bceb9517 100644 --- a/README.md +++ b/README.md @@ -62,7 +62,7 @@ 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. The optional `--dest` argument lets your change the name of the file that gets created/modified +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 @@ -73,6 +73,12 @@ Check the [source file](https://github.com/antonbabenko/pre-commit-terraform/blo 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. +## Notes for developers + +1. Python hooks are supported now too. All you have to do is: + 1. add a line to the `console_sripts` array in `entry_points` in `setup.py` + 1. Put your python script in the `pre_commit_hooks` folder + Enjoy the clean and documented code! ## Authors diff --git a/setup.py b/setup.py index 246de8c2b..4c2b47668 100644 --- a/setup.py +++ b/setup.py @@ -8,8 +8,7 @@ url='https://github.com/antonbabenko/pre-commit-terraform', version_format='{tag}+{gitsha}', - author='Andrew Roth', - author_email='roth.andy@gmail.com', + author='Contributors', classifiers=[ 'License :: OSI Approved :: MIT License', From fe3ba02d25e3c7baa7f0fd3f97aaf3f2c3db62c6 Mon Sep 17 00:00:00 2001 From: rothandrew Date: Fri, 14 Dec 2018 16:19:54 -0500 Subject: [PATCH 5/5] fix typo --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 1bceb9517..5e114729d 100644 --- a/README.md +++ b/README.md @@ -62,7 +62,7 @@ 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. The optional `--dest` argument lets your change the name of the file that gets created/modified. +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 you change the name of the file that gets created/modified. 1. Example: ```yaml