1- """`terraform_docs_replace` hook. Deprecated."""
2-
31import os
4- import subprocess # noqa: S404. We invoke cli tools
2+ import subprocess
53import warnings
64from argparse import ArgumentParser , Namespace
75from typing import cast as cast_to
1412
1513
1614def populate_argument_parser (subcommand_parser : ArgumentParser ) -> None :
17- """Populate the parser for the subcommand."""
1815 subcommand_parser .description = (
1916 'Run terraform-docs on a set of files. Follows the standard '
2017 'convention of pulling the documentation from main.tf in order to '
2118 'replace the entire README.md file each time.'
2219 )
2320 subcommand_parser .add_argument (
24- '--dest' ,
25- dest = 'dest' ,
26- default = 'README.md' ,
21+ '--dest' , dest = 'dest' , default = 'README.md' ,
2722 )
2823 subcommand_parser .add_argument (
29- '--sort-inputs-by-required' ,
30- dest = 'sort' ,
31- action = 'store_true' ,
24+ '--sort-inputs-by-required' , dest = 'sort' , action = 'store_true' ,
3225 help = '[deprecated] use --sort-by-required instead' ,
3326 )
3427 subcommand_parser .add_argument (
35- '--sort-by-required' ,
36- dest = 'sort' ,
37- action = 'store_true' ,
28+ '--sort-by-required' , dest = 'sort' , action = 'store_true' ,
3829 )
3930 subcommand_parser .add_argument (
4031 '--with-aggregate-type-defaults' ,
@@ -50,13 +41,7 @@ def populate_argument_parser(subcommand_parser: ArgumentParser) -> None:
5041
5142
5243def invoke_cli_app (parsed_cli_args : Namespace ) -> ReturnCodeType :
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.
44+ warnings .warn (
6045 '`terraform_docs_replace` hook is DEPRECATED.'
6146 'For migration instructions see '
6247 'https://github.com/antonbabenko/pre-commit-terraform/issues/248'
@@ -65,32 +50,28 @@ def invoke_cli_app(parsed_cli_args: Namespace) -> ReturnCodeType:
6550 )
6651
6752 dirs : list [str ] = []
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
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 ))
7357
7458 retval = ReturnCode .OK
7559
76- for directory in dirs :
60+ for dir in dirs :
7761 try :
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- ),
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 )),
8972 )
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
73+ subprocess .check_call (" " .join (procArgs ), shell = True )
74+ except subprocess .CalledProcessError as e :
75+ print (e )
9576 retval = ReturnCode .ERROR
9677 return retval
0 commit comments