From 0e6b425ac50aeff73165c8c694b38c5b9b0bc40e Mon Sep 17 00:00:00 2001 From: Dan Helfman Date: Sat, 23 Apr 2022 14:03:15 -0700 Subject: [PATCH] Fix "borgmatic borg key ..." to pass parameters to Borg in correct order (#515). --- NEWS | 1 + borgmatic/borg/borg.py | 11 +++++++---- tests/unit/borg/test_borg.py | 10 ++++++++++ 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/NEWS b/NEWS index 0d1aec99..5105226d 100644 --- a/NEWS +++ b/NEWS @@ -6,6 +6,7 @@ once for each configured repository instead of once per configuration file. Additionally, the "repositories" interpolated variable has been changed to "repository", containing the path to the current repository for the hook. + * #515: Fix "borgmatic borg key ..." to pass parameters to Borg in correct order. * #516: Fix handling of TERM signal to exit borgmatic, not just forward the signal to Borg. * #517: Fix borgmatic exit code (so it's zero) when initial Borg calls fail but later retries succeed. diff --git a/borgmatic/borg/borg.py b/borgmatic/borg/borg.py index 59ffe213..ff3212f4 100644 --- a/borgmatic/borg/borg.py +++ b/borgmatic/borg/borg.py @@ -21,17 +21,20 @@ def run_arbitrary_borg( try: options = options[1:] if options[0] == '--' else options - borg_command = options[0] - command_options = tuple(options[1:]) + + # Borg's "key" command has a sub-command ("export", etc.) that must follow it. + command_options_start_index = 2 if options[0] == 'key' else 1 + borg_command = tuple(options[:command_options_start_index]) + command_options = tuple(options[command_options_start_index:]) except IndexError: - borg_command = None + borg_command = () command_options = () repository_archive = '::'.join((repository, archive)) if repository and archive else repository full_command = ( (local_path,) - + ((borg_command,) if borg_command else ()) + + borg_command + ((repository_archive,) if borg_command and repository_archive else ()) + command_options + (('--info',) if logger.getEffectiveLevel() == logging.INFO else ()) diff --git a/tests/unit/borg/test_borg.py b/tests/unit/borg/test_borg.py index ffd78357..0174a91c 100644 --- a/tests/unit/borg/test_borg.py +++ b/tests/unit/borg/test_borg.py @@ -121,3 +121,13 @@ def test_run_arbitrary_borg_without_borg_specific_parameters_does_not_raise(): module.run_arbitrary_borg( repository='repo', storage_config={}, options=[], ) + + +def test_run_arbitrary_borg_passes_key_sub_command_to_borg_before_repository(): + flexmock(module).should_receive('execute_command').with_args( + ('borg', 'key', 'export', 'repo'), output_log_level=logging.WARNING, borg_local_path='borg', + ) + + module.run_arbitrary_borg( + repository='repo', storage_config={}, options=['key', 'export'], + )