backup-tool/app/__main__.py

66 lines
1.6 KiB
Python
Raw Normal View History

2021-04-25 19:26:12 +02:00
"""The main entrypoint of the program."""
2021-01-15 13:01:17 +01:00
import argparse
import sys
from parser import read_specs_file
2021-01-15 13:01:17 +01:00
def except_hook(ext_type, value, traceback):
2021-04-26 17:45:19 +02:00
"""Make errors not show the stracktrace to stdout.
2021-04-25 19:26:12 +02:00
Todo:
* Replace this with proper error handling
"""
2021-01-15 13:01:17 +01:00
sys.stderr.write("{}: {}\n".format(ext_type.__name__, value))
2021-01-15 14:14:25 +01:00
2021-01-15 21:08:56 +01:00
# sys.excepthook = except_hook
2021-01-15 13:01:17 +01:00
2021-04-25 19:26:12 +02:00
if __name__ == "__main__":
# Define parser
parser = argparse.ArgumentParser(
description="Backup directories and Docker volumes."
)
parser.add_argument(
"-f",
"--file",
action="append",
dest="file",
required=True,
help="File containing spec definitions.",
)
parser.add_argument(
"-j",
"--json",
action="store_const",
const=True,
default=False,
help="Print out the parsed specs as JSON and exit",
)
parser.add_argument(
"spec", nargs="*", help="The specs to process. Defaults to all."
)
# Parse arguments
args = parser.parse_args()
specs = sum((read_specs_file(path) for path in args.file), [])
# Filter specs if needed
if args.spec:
specs = list(filter(lambda s: s.name in args.spec, specs))
# Dump parsed data as json
if args.json:
import json
# TODO replace this with error handling system
print(json.dumps([spec.to_dict() for spec in specs], indent=4))
elif not specs:
2021-04-26 17:45:19 +02:00
# TODO replace this with error handling system
print("No specs, exiting.")
sys.exit(0)
2021-04-25 19:26:12 +02:00
for spec in specs:
spec.backup()