1+ """`terraform_docs_replace` hook. Deprecated."""
2+
13import os
2- import subprocess
4+ import subprocess # noqa: S404. We invoke cli tools
35import warnings
4- from argparse import ArgumentParser , Namespace
6+ from argparse import ArgumentParser
7+ from argparse import Namespace
58from typing import cast as cast_to
69
710from ._structs import ReturnCode
811from ._types import ReturnCodeType
912
10-
1113CLI_SUBCOMMAND_NAME : str = 'replace-docs'
1214
1315
1416def populate_argument_parser (subcommand_parser : ArgumentParser ) -> None :
17+ """Populate the parser for the subcommand."""
1518 subcommand_parser .description = (
1619 'Run terraform-docs on a set of files. Follows the standard '
1720 'convention of pulling the documentation from main.tf in order to '
1821 'replace the entire README.md file each time.'
1922 )
2023 subcommand_parser .add_argument (
21- '--dest' , dest = 'dest' , default = 'README.md' ,
24+ '--dest' ,
25+ dest = 'dest' ,
26+ default = 'README.md' ,
2227 )
2328 subcommand_parser .add_argument (
24- '--sort-inputs-by-required' , dest = 'sort' , action = 'store_true' ,
29+ '--sort-inputs-by-required' ,
30+ dest = 'sort' ,
31+ action = 'store_true' ,
2532 help = '[deprecated] use --sort-by-required instead' ,
2633 )
2734 subcommand_parser .add_argument (
28- '--sort-by-required' , dest = 'sort' , action = 'store_true' ,
35+ '--sort-by-required' ,
36+ dest = 'sort' ,
37+ action = 'store_true' ,
2938 )
3039 subcommand_parser .add_argument (
3140 '--with-aggregate-type-defaults' ,
@@ -41,7 +50,13 @@ def populate_argument_parser(subcommand_parser: ArgumentParser) -> None:
4150
4251
4352def invoke_cli_app (parsed_cli_args : Namespace ) -> ReturnCodeType :
44- warnings .warn (
53+ """Run the entry-point of the CLI app.
54+
55+ Returns:
56+ ReturnCodeType: The return code of the app.
57+
58+ """
59+ warnings .warn ( # noqa: B028. that's user warning, no need to show stacktrace etc.
4560 '`terraform_docs_replace` hook is DEPRECATED.'
4661 'For migration instructions see '
4762 'https://github.com/antonbabenko/pre-commit-terraform/issues/248'
@@ -50,28 +65,32 @@ def invoke_cli_app(parsed_cli_args: Namespace) -> ReturnCodeType:
5065 )
5166
5267 dirs : list [str ] = []
53- for filename in cast_to (list [str ], parsed_cli_args .filenames ):
54- if (os .path .realpath (filename ) not in dirs and
55- (filename .endswith (".tf" ) or filename .endswith (".tfvars" ))):
56- dirs .append (os .path .dirname (filename ))
68+ for filename in cast_to ('list[str]' , parsed_cli_args .filenames ):
69+ if os .path .realpath (filename ) not in dirs and (
70+ filename .endswith (('.tf' , '.tfvars' ))
71+ ):
72+ dirs .append (os .path .dirname (filename )) # noqa: PTH120. Legacy hook, no need to refactor
5773
5874 retval = ReturnCode .OK
5975
60- for dir in dirs :
76+ for directory in dirs :
6177 try :
62- procArgs = []
63- procArgs .append ('terraform-docs' )
64- if cast_to (bool , parsed_cli_args .sort ):
65- procArgs .append ('--sort-by-required' )
66- procArgs .append ('md' )
67- procArgs .append ("./{dir}" .format (dir = dir ))
68- procArgs .append ('>' )
69- procArgs .append (
70- './{dir}/{dest}' .
71- format (dir = dir , dest = cast_to (bool , parsed_cli_args .dest )),
78+ proc_args = []
79+ proc_args .append ('terraform-docs' )
80+ if cast_to ('bool' , parsed_cli_args .sort ):
81+ proc_args .append ('--sort-by-required' )
82+ proc_args .extend (
83+ (
84+ 'md' ,
85+ f'./{ directory } ' ,
86+ '>' ,
87+ f"./{ directory } /{ cast_to ('bool' , parsed_cli_args .dest )} " ,
88+ ),
7289 )
73- subprocess .check_call (" " .join (procArgs ), shell = True )
74- except subprocess .CalledProcessError as e :
75- print (e )
90+ # We call cli tools, of course we use shell=True
91+ subprocess .check_call (' ' .join (proc_args ), shell = True ) # noqa: S602
92+ # Legacy hook, no need to refactor
93+ except subprocess .CalledProcessError as e : # noqa: PERF203
94+ print (e ) # noqa: T201
7695 retval = ReturnCode .ERROR
7796 return retval
0 commit comments