diff --git a/terrabutler/click.py b/terrabutler/click.py index eb495ee..92e2e1d 100755 --- a/terrabutler/click.py +++ b/terrabutler/click.py @@ -23,7 +23,6 @@ terraform_command_runner ) from terrabutler.settings import ( - check_settings, get_settings, validate_settings ) @@ -38,7 +37,6 @@ @click.version_option(version=__version__, prog_name=__name__.capitalize(), message='%(prog)s v%(version)s') def main(): - check_settings() validate_settings() diff --git a/terrabutler/requirements.py b/terrabutler/requirements.py index bf261fb..99f449b 100644 --- a/terrabutler/requirements.py +++ b/terrabutler/requirements.py @@ -2,24 +2,23 @@ def check_requirements(): """ Check requirements before running the application. """ - from terrabutler.settings import validate_settings from os import getenv, path from colorama import Fore if getenv("TERRABUTLER_ENABLE") != "true": print(Fore.YELLOW + "Terrabutler is not currently enabled on this" " folder. Please set 'TERRABUTLER_ENABLE' in your environment" - " to true to enable it.") + " to true to enable it." + Fore.RESET) exit(1) root = getenv("TERRABUTLER_ROOT") if root is None or not path.exists(root): print(Fore.RED + "Terrabutler can't determine the root folder of" " your project or it doesn't exist.\nPlease set" " 'TERRABUTLER_ROOT' in your environment pointing" - " to the root folder of your project.") + " to the root folder of your project." + Fore.RESET) exit(1) - if not path.exists(root + "configs/settings.yml"): + if not path.exists(root + "/configs/settings.yml"): print(Fore.RED + "Terrabutler can't find you settings file\nPlease" - " create a 'settings.yml' file inside the 'configs' folder.") + " create a 'settings.yml' file inside the 'configs' folder." + + Fore.RESET) exit(1) - validate_settings() diff --git a/terrabutler/settings.py b/terrabutler/settings.py index 47d8054..1476ccc 100644 --- a/terrabutler/settings.py +++ b/terrabutler/settings.py @@ -1,6 +1,4 @@ from terrabutler.utils import paths -from colorama import Fore -from os import path from schema import Schema, SchemaError import yaml @@ -31,17 +29,6 @@ }) -def check_settings(): - """ - Check if the settings file exists - """ - if not path.exists(PATH): - print(Fore.YELLOW + "The settings file does not exist." - "\n\nPlease create a 'settings.yml' file inside the 'configs'" - " folder.") - exit(1) - - def get_settings(): """ Returns the settings object @@ -54,13 +41,25 @@ def validate_settings(): """ Validade settings file """ - with open(PATH) as f: - configuration = yaml.safe_load(f) + try: + with open(PATH, "r") as f: + configuration = yaml.safe_load(f) + except FileNotFoundError: + print(f"File {PATH} not found. Aborting") + exit(1) + except OSError: + print(f"OS error occurred trying to open {PATH}") + exit(1) + except Exception as err: + print(f"Unexpected error when reading {PATH}: {err}") + exit(1) try: SCHEMA.validate(configuration) except SchemaError as se: - raise se + print("Your settings file is not using the needed values:" + f" {se}") + exit(1) def write_settings(yaml_file): diff --git a/terrabutler/utils.py b/terrabutler/utils.py index 8bb3252..b159f10 100644 --- a/terrabutler/utils.py +++ b/terrabutler/utils.py @@ -1,5 +1,7 @@ +from terrabutler.requirements import check_requirements from os import getenv +check_requirements() ROOT_PATH = getenv("TERRABUTLER_ROOT") paths = {