-
-
Notifications
You must be signed in to change notification settings - Fork 562
/
Copy pathterraform_fmt.py
46 lines (30 loc) · 1 KB
/
terraform_fmt.py
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
"""
Pre-commit hook for terraform fmt.
"""
from __future__ import annotations
import logging
import os
import shlex
import sys
from subprocess import PIPE
from subprocess import run
from typing import Sequence
from .common import parse_cmdline
from .common import setup_logging
logger = logging.getLogger(__name__)
def main(argv: Sequence[str] | None = None) -> int:
setup_logging()
logger.debug(sys.version_info)
args, hook_config, files, tf_init_args, env_vars = parse_cmdline(argv)
if os.environ.get('PRE_COMMIT_COLOR') == 'never':
args.append('-no-color')
cmd = ['terraform', 'fmt', *args, *files]
logger.info('calling %s', shlex.join(cmd))
logger.debug('env_vars: %r', env_vars)
logger.debug('args: %r', args)
completed_process = run(cmd, env={**os.environ, **env_vars}, text=True, stdout=PIPE)
if completed_process.stdout:
print(completed_process.stdout)
return completed_process.returncode
if __name__ == '__main__':
raise SystemExit(main())