diff --git a/src/restic_compose_backup/cli.py b/src/restic_compose_backup/cli.py index 5119836..78892d6 100644 --- a/src/restic_compose_backup/cli.py +++ b/src/restic_compose_backup/cli.py @@ -1,9 +1,7 @@ import argparse import os import logging -from importlib import import_module -from pkgutil import iter_modules -from typing import Dict, List +from typing import List from restic_compose_backup import ( alerts, @@ -13,27 +11,16 @@ from restic_compose_backup import ( ) from restic_compose_backup.config import Config from restic_compose_backup.containers import RunningContainers -from restic_compose_backup import cron, utils +from restic_compose_backup import utils from restic_compose_backup import commands -from restic_compose_backup.commands.base import BaseCommand + logger = logging.getLogger(__name__) -def get_commands() -> Dict[str, BaseCommand]: - """Return the list of available command classes""" - _commands = {} - for module_info in iter_modules(commands.__path__): - module = import_module(f'restic_compose_backup.commands.{module_info.name}') - if hasattr(module, 'Command'): - _commands[module_info.name] = module.Command - return _commands - - def main(): - """CLI entrypoint""" - commands = get_commands() - args = parse_args(sorted(commands.keys())) - command = commands[args.action]() + """Main entry point for the application""" + args = parse_args(sorted(commands.COMMANDS.keys())) + command = commands.COMMANDS[args.action](args) command.run() return @@ -63,13 +50,6 @@ def main(): elif args.action == 'alert': alert(config, containers) - elif args.action == 'version': - import restic_compose_backup - print(restic_compose_backup.__version__) - - elif args.action == "crontab": - crontab(config) - # Random test stuff here elif args.action == "test": nodes = utils.get_swarm_nodes() @@ -296,20 +276,6 @@ def snapshots(config, containers): print(line) -def alert(config, containers): - """Test alerts""" - logger.info("Testing alerts") - alerts.send( - subject="{}: Test Alert".format(containers.project_name), - body="Test message", - ) - - -def crontab(config): - """Generate the crontab""" - print(cron.generate_crontab(config)) - - def parse_args(choices: List[str]): parser = argparse.ArgumentParser(prog='restic_compose_backup') parser.add_argument( diff --git a/src/restic_compose_backup/commands/__init__,py b/src/restic_compose_backup/commands/__init__,py deleted file mode 100644 index 7589815..0000000 --- a/src/restic_compose_backup/commands/__init__,py +++ /dev/null @@ -1 +0,0 @@ -from .base import BaseCommand diff --git a/src/restic_compose_backup/commands/__init__.py b/src/restic_compose_backup/commands/__init__.py new file mode 100644 index 0000000..b580bb0 --- /dev/null +++ b/src/restic_compose_backup/commands/__init__.py @@ -0,0 +1,19 @@ +import sys +from importlib import import_module +from pkgutil import iter_modules +from typing import Dict +from .base import BaseCommand + + +def get_commands() -> Dict[str, BaseCommand]: + """Return the list of available command classes""" + _commands = {} + current_module = sys.modules[__name__] + for module_info in iter_modules(current_module.__path__): + module = import_module(f'restic_compose_backup.commands.{module_info.name}') + if hasattr(module, 'Command'): + _commands[module_info.name] = module.Command + return _commands + + +COMMANDS = get_commands() diff --git a/src/restic_compose_backup/commands/alert.py b/src/restic_compose_backup/commands/alert.py index e65c3cf..fce9d5d 100644 --- a/src/restic_compose_backup/commands/alert.py +++ b/src/restic_compose_backup/commands/alert.py @@ -1,9 +1,15 @@ from .base import BaseCommand - +from restic_compose_backup import alerts class Command(BaseCommand): """Send an alert""" name = "alert" def run(self): - print("Alert!") + """Test alerts""" + self.logger.info("Testing alerts") + containers = self.get_containers() + alerts.send( + subject="{}: Test Alert".format(containers.project_name), + body="Test message", + ) diff --git a/src/restic_compose_backup/commands/base.py b/src/restic_compose_backup/commands/base.py index 9a6d9a8..22422cf 100644 --- a/src/restic_compose_backup/commands/base.py +++ b/src/restic_compose_backup/commands/base.py @@ -1,12 +1,30 @@ +import logging from restic_compose_backup.config import Config +from restic_compose_backup.containers import RunningContainers +from restic_compose_backup import log class BaseCommand: """Base class for all commands""" name = "base" - def __init__(self): + def __init__(self, cli_args, *args, **kwargs): + self.cli_args = cli_args + self.log_level = cli_args.log_level self.config = Config() - + self.logger = logging.getLogger(__name__) + log.setup(level=self.log_level or self.config.log_level) + + def get_containers(self): + """Get running containers""" + return RunningContainers() + def run(self): + """Run the command""" raise NotImplementedError + + def run_command(self, command: str): + """Run a command by name and return the result""" + from . import COMMANDS + command = COMMANDS[command] + command(self.cli_args).run() diff --git a/src/restic_compose_backup/commands/crontab.py b/src/restic_compose_backup/commands/crontab.py index 6bde082..53b565b 100644 --- a/src/restic_compose_backup/commands/crontab.py +++ b/src/restic_compose_backup/commands/crontab.py @@ -1,9 +1,10 @@ from .base import BaseCommand - +from restic_compose_backup import cron class Command(BaseCommand): """Manage crontab""" name = "crontab" def run(self): - print("Crontab!") + """Generate the crontab""" + print(cron.generate_crontab(self.config)) diff --git a/src/restic_compose_backup/commands/version.py b/src/restic_compose_backup/commands/version.py index 2dd8e4e..013d4ca 100644 --- a/src/restic_compose_backup/commands/version.py +++ b/src/restic_compose_backup/commands/version.py @@ -6,4 +6,5 @@ class Command(BaseCommand): name = "version" def run(self): - print("Version!") + import restic_compose_backup + print(restic_compose_backup.__version__)