Skip to content

Commit

Permalink
object orient wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
aerickson committed Aug 7, 2024
1 parent fd366ab commit bf374e1
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 36 deletions.
8 changes: 8 additions & 0 deletions tf_authoritative_scanner/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,11 @@ def _read(rel_path):
here = os.path.abspath(os.path.dirname(__file__))
with codecs.open(os.path.join(here, rel_path), "r") as fp:
return fp.read()


def remove_leading_trailing_newline(text):
if text.startswith("\n"):
text = text[1:]
if text.endswith("\n"):
text = text[:-1]
return text
66 changes: 30 additions & 36 deletions tf_authoritative_scanner/wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,55 +3,48 @@
import argparse

from tf_authoritative_scanner.scanner import TFAuthoritativeScanner
from tf_authoritative_scanner.util import _get_version
from tf_authoritative_scanner.util import _get_version, remove_leading_trailing_newline


def run_tfas_and_terraform(args):
scanner = TFAuthoritativeScanner(include_dotdirs=False, verbosity=0)
result = scanner.check_paths_for_authoritative_resources(".")
terraform_command = ["terraform"] + args
if result["authoritative_files_found"]:
count = result["authoritative_files_count"]
print(f"Authoritative files found ({count}). Run `tfas .` to view results.")
print(f"Not running `{' '.join(terraform_command)}`.")
sys.exit(1)

# If no authoritative files found, continue with `terraform`.

print(f"No authoritative files found. Continuing with `{' '.join(terraform_command)}`...")
print()
class Wrapper:
def __init__(self, args):
pass

# Replace the current process with `terraform` command
os.execvp("terraform", terraform_command)
def run_tfas_and_terraform(self, args):
scanner = TFAuthoritativeScanner(include_dotdirs=False, verbosity=0)
result = scanner.check_paths_for_authoritative_resources(".")
terraform_command = ["terraform"] + args
if result["authoritative_files_found"]:
count = result["authoritative_files_count"]
print(f"Authoritative files found ({count}). Run `tfas .` to view results.")
print(f"Not running `{' '.join(terraform_command)}`.")
sys.exit(1)

# If no authoritative files found, continue with `terraform`.

def remove_leading_trailing_newline(text):
if text.startswith("\n"):
text = text[1:]
if text.endswith("\n"):
text = text[:-1]
return text
print(f"No authoritative files found. Continuing with `{' '.join(terraform_command)}`...")
print()

# Replace the current process with `terraform` command
os.execvp("terraform", terraform_command)

def print_tfast_banner():
print(
remove_leading_trailing_newline(
r"""
def print_tfast_banner(self):
print(
remove_leading_trailing_newline(
r"""
__ ___ __
/\ \__ /'___\ /\ \__
\ \ ,_\/\ \__/ __ ____\ \ ,_\
\ \ \/\ \ ,__\/'__`\ /',__\\ \ \/
\ \ \_\ \ \_/\ \L\.\_/\__, `\\ \ \_
\ \__\\ \_\\ \__/.\_\/\____/ \ \__\
\/__/ \/_/ \/__/\/_/\/___/ \/__/
"""
"""
)
)
)


def is_terraform_directory():
return any(file.endswith(".tf") for file in os.listdir("."))
def is_terraform_directory(self):
return any(file.endswith(".tf") for file in os.listdir("."))


def main():
Expand All @@ -66,11 +59,12 @@ def main():
)
parser.add_argument("--no-ascii-art", "-A", action="store_true", help="Do not print ASCII art")
args = parser.parse_args()
w = Wrapper(args)

if not args.no_ascii_art:
print_tfast_banner()
if not is_terraform_directory():
w.print_tfast_banner()
if not w.is_terraform_directory():
print("No Terraform files found in the current directory. Please ensure you're in a directory with .tf files.")
# parser.print_help()
sys.exit(1)
run_tfas_and_terraform(args.terraform_args)
w.run_tfas_and_terraform(args.terraform_args)

0 comments on commit bf374e1

Please sign in to comment.