Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix initialize checks, execption for reading and colors on prints #46

Merged
merged 11 commits into from
May 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions terrabutler/click.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
terraform_command_runner
)
from terrabutler.settings import (
check_settings,
get_settings,
validate_settings
)
Expand All @@ -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()


Expand Down
11 changes: 5 additions & 6 deletions terrabutler/requirements.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
31 changes: 15 additions & 16 deletions terrabutler/settings.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
from terrabutler.utils import paths
from colorama import Fore
from os import path
from schema import Schema, SchemaError
import yaml

Expand Down Expand Up @@ -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
Expand All @@ -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):
Expand Down
2 changes: 2 additions & 0 deletions terrabutler/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from terrabutler.requirements import check_requirements
from os import getenv

check_requirements()
ROOT_PATH = getenv("TERRABUTLER_ROOT")

paths = {
Expand Down