mirror of
https://github.com/tofuutils/pre-commit-opentofu.git
synced 2025-10-15 17:38:54 +02:00
Add new hook for running terraform-docs with replacing README.md from doc in main.tf
This commit is contained in:
parent
7acd99eeb7
commit
9aa971c069
6 changed files with 102 additions and 40 deletions
50
pre_commit_hooks/terraform_docs_replace.py
Normal file
50
pre_commit_hooks/terraform_docs_replace.py
Normal file
|
|
@ -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())
|
||||
Loading…
Add table
Add a link
Reference in a new issue