-
-
Notifications
You must be signed in to change notification settings - Fork 541
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: new terraform_fmt_v2 with better Windows support
- Loading branch information
1 parent
1bdc82b
commit e12fae3
Showing
7 changed files
with
281 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,177 @@ | ||
tests/results/* | ||
# Created by https://www.toptal.com/developers/gitignore/api/python | ||
# Edit at https://www.toptal.com/developers/gitignore?templates=python | ||
|
||
### Python ### | ||
# Byte-compiled / optimized / DLL files | ||
__pycache__/ | ||
*.py[cod] | ||
*$py.class | ||
|
||
# C extensions | ||
*.so | ||
|
||
# Distribution / packaging | ||
.Python | ||
build/ | ||
develop-eggs/ | ||
dist/ | ||
downloads/ | ||
eggs/ | ||
.eggs/ | ||
lib/ | ||
lib64/ | ||
parts/ | ||
sdist/ | ||
var/ | ||
wheels/ | ||
share/python-wheels/ | ||
*.egg-info/ | ||
.installed.cfg | ||
*.egg | ||
MANIFEST | ||
|
||
# PyInstaller | ||
# Usually these files are written by a python script from a template | ||
# before PyInstaller builds the exe, so as to inject date/other infos into it. | ||
*.manifest | ||
*.spec | ||
|
||
# Installer logs | ||
pip-log.txt | ||
pip-delete-this-directory.txt | ||
|
||
# Unit test / coverage reports | ||
htmlcov/ | ||
.tox/ | ||
.nox/ | ||
.coverage | ||
.coverage.* | ||
.cache | ||
nosetests.xml | ||
coverage.xml | ||
*.cover | ||
*.py,cover | ||
.hypothesis/ | ||
.pytest_cache/ | ||
cover/ | ||
|
||
# Translations | ||
*.mo | ||
*.pot | ||
|
||
# Django stuff: | ||
*.log | ||
local_settings.py | ||
db.sqlite3 | ||
db.sqlite3-journal | ||
|
||
# Flask stuff: | ||
instance/ | ||
.webassets-cache | ||
|
||
# Scrapy stuff: | ||
.scrapy | ||
|
||
# Sphinx documentation | ||
docs/_build/ | ||
|
||
# PyBuilder | ||
.pybuilder/ | ||
target/ | ||
|
||
# Jupyter Notebook | ||
.ipynb_checkpoints | ||
|
||
# IPython | ||
profile_default/ | ||
ipython_config.py | ||
|
||
# pyenv | ||
# For a library or package, you might want to ignore these files since the code is | ||
# intended to run in multiple environments; otherwise, check them in: | ||
# .python-version | ||
|
||
# pipenv | ||
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. | ||
# However, in case of collaboration, if having platform-specific dependencies or dependencies | ||
# having no cross-platform support, pipenv may install dependencies that don't work, or not | ||
# install all needed dependencies. | ||
#Pipfile.lock | ||
|
||
# poetry | ||
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. | ||
# This is especially recommended for binary packages to ensure reproducibility, and is more | ||
# commonly ignored for libraries. | ||
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control | ||
#poetry.lock | ||
|
||
# pdm | ||
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. | ||
#pdm.lock | ||
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it | ||
# in version control. | ||
# https://pdm.fming.dev/#use-with-ide | ||
.pdm.toml | ||
|
||
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm | ||
__pypackages__/ | ||
|
||
# Celery stuff | ||
celerybeat-schedule | ||
celerybeat.pid | ||
|
||
# SageMath parsed files | ||
*.sage.py | ||
|
||
# Environments | ||
.env | ||
.venv | ||
env/ | ||
venv/ | ||
ENV/ | ||
env.bak/ | ||
venv.bak/ | ||
|
||
# Spyder project settings | ||
.spyderproject | ||
.spyproject | ||
|
||
# Rope project settings | ||
.ropeproject | ||
|
||
# mkdocs documentation | ||
/site | ||
|
||
# mypy | ||
.mypy_cache/ | ||
.dmypy.json | ||
dmypy.json | ||
|
||
# Pyre type checker | ||
.pyre/ | ||
|
||
# pytype static type analyzer | ||
.pytype/ | ||
|
||
# Cython debug symbols | ||
cython_debug/ | ||
|
||
# PyCharm | ||
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can | ||
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore | ||
# and can be added to the global gitignore or merged into this file. For a more nuclear | ||
# option (not recommended) you can uncomment the following to ignore the entire idea folder. | ||
#.idea/ | ||
|
||
### Python Patch ### | ||
# Poetry local configuration file - https://python-poetry.org/docs/configuration/#local-configuration | ||
poetry.toml | ||
|
||
# ruff | ||
.ruff_cache/ | ||
|
||
# LSP config files | ||
pyrightconfig.json | ||
|
||
# End of https://www.toptal.com/developers/gitignore/api/python |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +0,0 @@ | ||
print( | ||
'`terraform_docs_replace` hook is DEPRECATED.' | ||
'For migration instructions see https://github.com/antonbabenko/pre-commit-terraform/issues/248#issuecomment-1290829226' | ||
) | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
from __future__ import annotations | ||
|
||
import argparse | ||
import logging | ||
import os | ||
from collections.abc import Sequence | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def setup_logging(): | ||
logging.basicConfig( | ||
level={ | ||
"error": logging.ERROR, | ||
"warn": logging.WARNING, | ||
"warning": logging.WARNING, | ||
"info": logging.INFO, | ||
"debug": logging.DEBUG, | ||
}[os.environ.get("PRE_COMMIT_TERRAFORM_LOG_LEVEL", "warning").lower()] | ||
) | ||
|
||
|
||
def parse_env_vars(ev_strs: list[str]) -> dict[str, str]: | ||
ret = {} | ||
for ev_str in ev_strs: | ||
name, val = ev_str.split("=", 1) | ||
if val.startswith('"') and val.endswith('"'): | ||
val = val[1:-1] | ||
ret[name] = val | ||
return ret | ||
|
||
|
||
def parse_cmdline( | ||
argv: Sequence[str] | None = None, | ||
) -> tuple[list[str], list[str], list[str], list[str], dict[str, str]]: | ||
parser = argparse.ArgumentParser( | ||
add_help=False, # to allow us to use -h to be compatible with previous bash version | ||
) | ||
parser.add_argument("-a", "--args", action="append", help="Arguments") | ||
parser.add_argument("-h", "--hook-config", action="append", help="Hook Config") | ||
parser.add_argument("-i", "--init-args", "--tf-init-args", action="append", help="Init Args") | ||
parser.add_argument("-e", "--envs", "--env-vars", action="append", help="Environment Variables") | ||
parser.add_argument("FILES", nargs="*", help="Files") | ||
|
||
parsed_args = parser.parse_args(argv) | ||
|
||
args = parsed_args.args or [] | ||
hook_config = parsed_args.hook_config or [] | ||
files = parsed_args.FILES or [] | ||
tc_init_args = parsed_args.init_args or [] | ||
env_vars = parsed_args.envs or [] | ||
|
||
env_var_dict = parse_env_vars(env_vars) | ||
|
||
if hook_config: | ||
raise NotImplementedError("TODO: implement: hook_config") | ||
|
||
if tc_init_args: | ||
raise NotImplementedError("TODO: implement: tc_init_args") | ||
|
||
return args, hook_config, files, tc_init_args, env_var_dict |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
from __future__ import annotations | ||
|
||
import logging | ||
import os | ||
import shlex | ||
import sys | ||
from subprocess import PIPE, run | ||
from typing import Sequence | ||
|
||
from .common import parse_cmdline, 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) | ||
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()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters