Skip to content

Commit

Permalink
add some error handling to parsing of accounts.yml
Browse files Browse the repository at this point in the history
  • Loading branch information
saltydk committed Jun 10, 2024
1 parent f720082 commit 4dc9a11
Showing 1 changed file with 34 additions and 2 deletions.
36 changes: 34 additions & 2 deletions sb.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,45 @@

saltbox_accounts_path = '/srv/git/saltbox/accounts.yml'


def validate_structure(data):
required_keys = {
"apprise": [],
"cloudflare": ["api", "email"],
"dockerhub": ["token", "user"],
"plex": ["pass", "user"],
"user": ["domain", "email", "name", "pass", "ssh_key"]
}

for key, subkeys in required_keys.items():
if key not in data:
return False, f"Config file '{saltbox_accounts_path}' is missing required section: '{key}'"
for subkey in subkeys:
if subkey not in data[key]:
return False, f"Config file '{saltbox_accounts_path}' is missing required key '{subkey}' in section '{key}'"

return True, "Valid structure"


try:
with open(saltbox_accounts_path, 'r') as file:
data = yaml.safe_load(file)
except FileNotFoundError:
print(f"Error: The file '{saltbox_accounts_path}' was not found.")
print(f"Error: Config file '{saltbox_accounts_path}' was not found.")
sys.exit(1)
except yaml.YAMLError as e:
print(f"Error parsing the YAML file: {e}")
print(f"Error parsing config file '{saltbox_accounts_path}': {e}")
sys.exit(1)

# Check if the file is empty
if data is None:
print(f"Error: Config file '{saltbox_accounts_path}' is empty.")
sys.exit(1)

# Validate the structure of the parsed YAML
is_valid, message = validate_structure(data)
if not is_valid:
print(f"Error: {message}")
sys.exit(1)

################################
Expand Down Expand Up @@ -53,6 +84,7 @@

__version__ = "0.0.0"


################################
# Functions
################################
Expand Down

0 comments on commit 4dc9a11

Please sign in to comment.