-
-
Notifications
You must be signed in to change notification settings - Fork 582
Expand file tree
/
Copy pathterraform_docs_replace.py
More file actions
116 lines (102 loc) · 3.98 KB
/
terraform_docs_replace.py
File metadata and controls
116 lines (102 loc) · 3.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
"""Terraform Docs Replace Hook.
This hook is deprecated and will be removed in the future.
Please, use 'terraform_docs' hook instead.
"""
import os
# S404 - Allow importing 'subprocess' module to call external tools
# needed by these hooks. FIXME - should be moved to separate module
# when more hooks will be introduced
import subprocess # noqa: S404
import warnings
from argparse import ArgumentParser, Namespace
from typing import Final
from typing import cast as cast_to
from ._structs import ReturnCode
from ._types import ReturnCodeType
CLI_SUBCOMMAND_NAME: Final[str] = 'replace-docs'
def populate_argument_parser(subcommand_parser: ArgumentParser) -> None:
"""Populate the parser for the subcommand."""
subcommand_parser.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.'
)
subcommand_parser.add_argument(
'--dest',
dest='dest',
default='README.md',
)
subcommand_parser.add_argument(
'--sort-inputs-by-required',
dest='sort',
action='store_true',
help='[deprecated] use --sort-by-required instead',
)
subcommand_parser.add_argument(
'--sort-by-required',
dest='sort',
action='store_true',
)
subcommand_parser.add_argument(
'--with-aggregate-type-defaults',
dest='aggregate',
action='store_true',
help='[deprecated]',
)
subcommand_parser.add_argument(
'filenames',
nargs='*',
help='Filenames to check.',
)
# WPS231 - Found function with too much cognitive complexity
# We will not spend time on fixing complexity in deprecated hook
def invoke_cli_app(parsed_cli_args: Namespace) -> ReturnCodeType: # noqa: WPS231
"""Run the entry-point of the CLI app.
Returns:
ReturnCodeType: The return code of the app.
"""
warnings.warn(
'`terraform_docs_replace` hook is DEPRECATED.'
'For migration instructions see '
'https://github.com/antonbabenko/pre-commit-terraform/issues/248'
'#issuecomment-1290829226',
category=UserWarning,
stacklevel=1, # It's should be 2, but tests are failing w/ values >1.
# As it's deprecated hook, it's safe to leave it as is w/o fixing it.
)
dirs: list[str] = []
for filename in cast_to('list[str]', parsed_cli_args.filenames):
if os.path.realpath(filename) not in dirs and (
filename.endswith(('.tf', '.tfvars'))
):
# PTH120 - It should use 'pathlib', but this hook is deprecated and
# we don't want to spent time on testing fixes for it
dirs.append(os.path.dirname(filename)) # noqa: PTH120
retval = ReturnCode.OK
for directory in dirs:
try: # noqa: WPS229 - ignore as it's deprecated hook
proc_args = []
proc_args.append('terraform-docs')
if cast_to('bool', parsed_cli_args.sort):
proc_args.append('--sort-by-required')
proc_args.extend(
(
'md',
f'./{directory}',
'>',
'./{dir}/{dest}'.format(
dir=directory,
dest=cast_to('str', parsed_cli_args.dest),
),
),
)
# S602 - 'shell=True' is insecure, but this hook is deprecated and
# we don't want to spent time on testing fixes for it
subprocess.check_call(' '.join(proc_args), shell=True) # noqa: S602
# PERF203 - try-except shouldn't be in a loop, but it's deprecated
# hook, so leave as is
except subprocess.CalledProcessError as err: # noqa: PERF203
# T201 - Leave print statement as is, as this is deprecated hook
print(err) # noqa: T201,WPS421
retval = ReturnCode.ERROR
return retval