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

Add new hook for running terraform-docs which will replace content of README.md (using Python) #27

Merged
merged 5 commits into from
Dec 14, 2018
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 2 additions & 2 deletions .pre-commit-hooks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 - `<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->` and `<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->` 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.
Expand Down
9 changes: 7 additions & 2 deletions pre_commit_hooks/terraform_docs_replace.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
)
Expand All @@ -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
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}',

Expand Down