From bf374e12d45fb3b9d39c43f16a6ed5cdcc93eb03 Mon Sep 17 00:00:00 2001 From: Andrew Erickson Date: Tue, 6 Aug 2024 18:44:40 -0700 Subject: [PATCH] object orient wrapper --- tf_authoritative_scanner/util.py | 8 ++++ tf_authoritative_scanner/wrapper.py | 66 +++++++++++++---------------- 2 files changed, 38 insertions(+), 36 deletions(-) diff --git a/tf_authoritative_scanner/util.py b/tf_authoritative_scanner/util.py index 9517295..3f6a1dc 100644 --- a/tf_authoritative_scanner/util.py +++ b/tf_authoritative_scanner/util.py @@ -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 diff --git a/tf_authoritative_scanner/wrapper.py b/tf_authoritative_scanner/wrapper.py index e72ce5f..4c914ad 100644 --- a/tf_authoritative_scanner/wrapper.py +++ b/tf_authoritative_scanner/wrapper.py @@ -3,40 +3,35 @@ 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""" __ ___ __ /\ \__ /'___\ /\ \__ \ \ ,_\/\ \__/ __ ____\ \ ,_\ @@ -44,14 +39,12 @@ def print_tfast_banner(): \ \ \_\ \ \_/\ \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(): @@ -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)