diff --git a/borgmatic/borg/init.py b/borgmatic/borg/init.py index 85f7aa1..861b95a 100644 --- a/borgmatic/borg/init.py +++ b/borgmatic/borg/init.py @@ -1,6 +1,8 @@ +import argparse import logging import subprocess +from borgmatic.borg import info from borgmatic.execute import DO_NOT_CAPTURE, execute_command logger = logging.getLogger(__name__) @@ -23,17 +25,14 @@ def initialize_repository( whether the repository should be append-only, and the storage quota to use, initialize the repository. If the repository already exists, then log and skip initialization. ''' - info_command = ( - (local_path, 'info') - + (('--info',) if logger.getEffectiveLevel() == logging.INFO else ()) - + (('--debug',) if logger.isEnabledFor(logging.DEBUG) else ()) - + (('--remote-path', remote_path) if remote_path else ()) - + (repository,) - ) - logger.debug(' '.join(info_command)) - try: - execute_command(info_command, output_log_level=None) + info.display_archives_info( + repository, + storage_config, + argparse.Namespace(json=True, archive=None), + local_path, + remote_path, + ) logger.info('Repository already exists. Skipping initialization.') return except subprocess.CalledProcessError as error: diff --git a/tests/unit/borg/test_init.py b/tests/unit/borg/test_init.py index 8a6e639..556b40d 100644 --- a/tests/unit/borg/test_init.py +++ b/tests/unit/borg/test_init.py @@ -13,11 +13,11 @@ INIT_COMMAND = ('borg', 'init', '--encryption', 'repokey') def insert_info_command_found_mock(): - flexmock(module).should_receive('execute_command') + flexmock(module.info).should_receive('display_archives_info') def insert_info_command_not_found_mock(): - flexmock(module).should_receive('execute_command').and_raise( + flexmock(module.info).should_receive('display_archives_info').and_raise( subprocess.CalledProcessError(module.INFO_REPOSITORY_NOT_FOUND_EXIT_CODE, []) ) @@ -48,13 +48,13 @@ def test_initialize_repository_raises_for_borg_init_error(): def test_initialize_repository_skips_initialization_when_repository_already_exists(): - flexmock(module).should_receive('execute_command').once() + insert_info_command_found_mock() module.initialize_repository(repository='repo', storage_config={}, encryption_mode='repokey') def test_initialize_repository_raises_for_unknown_info_command_error(): - flexmock(module).should_receive('execute_command').and_raise( + flexmock(module.info).should_receive('display_archives_info').and_raise( subprocess.CalledProcessError(INFO_SOME_UNKNOWN_EXIT_CODE, []) )