diff --git a/sshgen/cli.py b/sshgen/cli.py index 7e81171..7b6ef98 100644 --- a/sshgen/cli.py +++ b/sshgen/cli.py @@ -22,10 +22,11 @@ def generate_hosts_file(hosts_file: Annotated[str, typer.Option('--hosts-file', Example usage: sshconf generate -o my_ssh_config """ - hosts_file = FileUtils.as_file(file_path=hosts_file) - output_file = FileUtils.as_file(file_path=output) + hosts_file = FileUtils.get_hosts_path(file_path=hosts_file) + output_file = FileUtils.get_output_path(file_path=output) - AppUtils(hosts_file, output_file).generate_ssh_config() + sshgen = AppUtils(hosts_file, output_file) + sshgen.generate_ssh_config() def _version_callback(value: bool) -> None: diff --git a/sshgen/logger.py b/sshgen/logger.py index 5f26eda..61c4fc3 100644 --- a/sshgen/logger.py +++ b/sshgen/logger.py @@ -5,12 +5,6 @@ def init_logger(level: LogLevel) -> None: - match level: - case LogLevel.INFO: - logging.basicConfig(level=logging.INFO, - format='[%(asctime)s] [%(levelname)s] - %(message)s', - datefmt='%Y-%m-%d %H:%M:%S') - case LogLevel.DEBUG: - logging.basicConfig(level=logging.DEBUG, - format='[%(asctime)s] [%(levelname)s] - %(message)s', - datefmt='%Y-%m-%d %H:%M:%S') + logging.basicConfig(level=level.value, + format='[%(asctime)s] [%(levelname)s] - %(message)s', + datefmt='%Y-%m-%d %H:%M:%S') diff --git a/sshgen/models/loglevel.py b/sshgen/models/loglevel.py index ad94016..f313e68 100644 --- a/sshgen/models/loglevel.py +++ b/sshgen/models/loglevel.py @@ -1,7 +1,8 @@ #!/usr/bin/env python -from enum import StrEnum, auto +import logging +from enum import Enum -class LogLevel(StrEnum): - INFO = auto() - DEBUG = auto() +class LogLevel(Enum): + INFO = logging.INFO + DEBUG = logging.DEBUG diff --git a/sshgen/parsers/ansible.py b/sshgen/parsers/ansible.py index 7e3f679..c7730af 100644 --- a/sshgen/parsers/ansible.py +++ b/sshgen/parsers/ansible.py @@ -13,7 +13,7 @@ class AnsibleHostsParser: def __init__(self, hosts_file_path: Path | None = None): self._yaml = YAML() self._default_hosts_file = 'examples/hosts.yml' - self._file_path: Path = hosts_file_path or FileUtils.as_file(self._default_hosts_file) + self._file_path: Path = hosts_file_path or FileUtils.get_hosts_path(self._default_hosts_file) def as_map(self) -> CommentedMap: log.debug('Loading ansible hosts file: %s', self._file_path) diff --git a/sshgen/run.py b/sshgen/run.py index b7f30e6..0c7e485 100644 --- a/sshgen/run.py +++ b/sshgen/run.py @@ -1,13 +1,29 @@ #!/usr/bin/env python +import logging + +from sshgen.logger import init_logger +from sshgen.models.loglevel import LogLevel from sshgen.utils.app import AppUtils from sshgen.utils.file import FileUtils +log = logging.getLogger(__name__) + def run() -> None: - hosts_file = FileUtils.as_file(file_path='../examples/hosts.yml') - output_file = FileUtils.as_file(file_path='../config') + init_logger(level=LogLevel.DEBUG) + hosts_file = './examples/hosts.yml' + config_file = './config' + + log.info('Starting sshgen with pre-defined settings') + log.debug('Debug mode is ON') + log.debug('Reading hosts from %s', hosts_file) + log.debug('Saving generated config to %s', config_file) + + hosts_file = FileUtils.get_hosts_path(file_path=hosts_file) + output_file = FileUtils.get_output_path(file_path=config_file) - AppUtils(hosts_file, output_file).generate_ssh_config() + sshgen = AppUtils(hosts_file, output_file) + sshgen.generate_ssh_config() if __name__ == '__main__': diff --git a/sshgen/utils/common.py b/sshgen/utils/common.py new file mode 100644 index 0000000..f7717fb --- /dev/null +++ b/sshgen/utils/common.py @@ -0,0 +1,14 @@ +#!/usr/bin/env python +import logging +import sys + +log = logging.getLogger(__name__) + + +class CommonUtils: + @staticmethod + def check_and_exit(condition: bool, message: str, exit_code: int = 1) -> None: + if not condition: + log.error(message) + log.debug('Exiting with code: %s', exit_code) + sys.exit(exit_code) diff --git a/sshgen/utils/file.py b/sshgen/utils/file.py index 271546c..4f92525 100644 --- a/sshgen/utils/file.py +++ b/sshgen/utils/file.py @@ -3,23 +3,33 @@ import sys from pathlib import Path +from sshgen.utils.common import CommonUtils + log = logging.getLogger(__name__) class FileUtils: @staticmethod - def as_file(file_path: str) -> Path: - resolved_path = FileUtils._get_base_path() / Path(file_path) + def get_hosts_path(file_path: str) -> Path: + resolved_path = FileUtils.resolve_path(file_path) - if FileUtils._is_yaml_file(resolved_path): - if resolved_path.exists() and FileUtils._is_empty(resolved_path): - log.error('Ansible hosts file %s is empty. Exiting...', resolved_path) - sys.exit(1) - elif not resolved_path.exists(): - log.error('Ansible hosts file does not exists at %s. Exiting...', resolved_path) - sys.exit(1) + CommonUtils.check_and_exit(resolved_path.exists(), + f'Ansible hosts file does not exists at {resolved_path}. Exiting...') + CommonUtils.check_and_exit(FileUtils.is_yaml_file(resolved_path), + 'Ansible hosts file is not a yaml file. Valid extensions are: yaml or yml.' + 'Exiting...') + CommonUtils.check_and_exit(not FileUtils.is_empty(resolved_path), + f'Ansible hosts file {resolved_path} is empty. Exiting...') - FileUtils._create_file(resolved_path) + return resolved_path + + @staticmethod + def get_output_path(file_path: str) -> Path: + resolved_path = FileUtils.resolve_path(file_path) + + if not resolved_path.exists(): + log.debug('Path %s is not exists, creating required directories', resolved_path) + FileUtils.create_file(resolved_path) return resolved_path @@ -28,19 +38,23 @@ def as_package_file(file_path: str) -> Path: return Path(__file__).resolve().parent.parent / Path(file_path) @staticmethod - def _get_base_path() -> Path: + def resolve_path(file_path: str) -> Path: + return FileUtils.get_base_path() / Path(file_path) + + @staticmethod + def get_base_path() -> Path: return Path(__file__).cwd().resolve() @staticmethod - def _is_yaml_file(file_path: Path) -> bool: + def is_yaml_file(file_path: Path) -> bool: return file_path.suffix in {'.yml', '.yaml'} @staticmethod - def _is_empty(file_path: Path) -> bool: + def is_empty(file_path: Path) -> bool: return file_path.is_file() and file_path.stat().st_size == 0 @staticmethod - def _create_file(file_path: Path) -> None: + def create_file(file_path: Path) -> None: if not file_path.is_file(): try: file_path.parent.mkdir(parents=True, exist_ok=True)