From 177c958572c447962e34a81c5e920b90aadc37ea Mon Sep 17 00:00:00 2001 From: Dan Helfman Date: Wed, 5 Jun 2024 14:47:37 -0700 Subject: [PATCH] Add configured repository "label" to the interpolated variables passed to command hooks (#874). --- NEWS | 2 ++ borgmatic/commands/borgmatic.py | 5 +-- ...reparation-and-cleanup-steps-to-backups.md | 3 ++ tests/unit/commands/test_borgmatic.py | 36 ++++++++++++++++++- 4 files changed, 43 insertions(+), 3 deletions(-) diff --git a/NEWS b/NEWS index 419308d..6b04454 100644 --- a/NEWS +++ b/NEWS @@ -2,6 +2,8 @@ * #860: Fix interaction between environment variable interpolation in constants and shell escaping. * #863: When color output is disabled (explicitly or implicitly), don't prefix each log line with the log level. + * #874: Add configured repository "label" to the interpolated variables passed to before/after + command hooks. 1.8.11 * #815: Add optional Healthchecks auto-provisioning via "create_slug" option. diff --git a/borgmatic/commands/borgmatic.py b/borgmatic/commands/borgmatic.py index 2c23ad0..bbaeb2c 100644 --- a/borgmatic/commands/borgmatic.py +++ b/borgmatic/commands/borgmatic.py @@ -286,10 +286,11 @@ def run_actions( global_arguments = arguments['global'] dry_run_label = ' (dry run; not making any changes)' if global_arguments.dry_run else '' hook_context = { - 'repository': repository_path, + 'label': repository.get('label', ''), + 'log_file': global_arguments.log_file if global_arguments.log_file else '', # Deprecated: For backwards compatibility with borgmatic < 1.6.0. 'repositories': ','.join([repo['path'] for repo in config['repositories']]), - 'log_file': global_arguments.log_file if global_arguments.log_file else '', + 'repository': repository_path, } skip_actions = set(get_skip_actions(config, arguments)) diff --git a/docs/how-to/add-preparation-and-cleanup-steps-to-backups.md b/docs/how-to/add-preparation-and-cleanup-steps-to-backups.md index 2c71a41..aec7319 100644 --- a/docs/how-to/add-preparation-and-cleanup-steps-to-backups.md +++ b/docs/how-to/add-preparation-and-cleanup-steps-to-backups.md @@ -79,6 +79,9 @@ variables you can use here: * `configuration_filename`: borgmatic configuration filename in which the hook was defined + * `label` New in version + 1.8.12: label of the current repository as configured in the current + borgmatic configuration file * `log_file` New in version 1.7.12: path of the borgmatic log file, only set when the `--log-file` flag is used diff --git a/tests/unit/commands/test_borgmatic.py b/tests/unit/commands/test_borgmatic.py index 911e2ad..f49f314 100644 --- a/tests/unit/commands/test_borgmatic.py +++ b/tests/unit/commands/test_borgmatic.py @@ -487,6 +487,40 @@ def test_run_actions_runs_rcreate(): ) +def test_run_actions_adds_label_file_to_hook_context(): + flexmock(module).should_receive('add_custom_log_levels') + flexmock(module).should_receive('get_skip_actions').and_return([]) + flexmock(module.command).should_receive('execute_hook') + expected = flexmock() + flexmock(borgmatic.actions.create).should_receive('run_create').with_args( + config_filename=object, + repository={'path': 'repo', 'label': 'my repo'}, + config={'repositories': []}, + config_paths=[], + hook_context={'label': 'my repo', 'log_file': '', 'repositories': '', 'repository': 'repo'}, + local_borg_version=object, + create_arguments=object, + global_arguments=object, + dry_run_label='', + local_path=object, + remote_path=object, + ).once().and_return(expected) + + result = tuple( + module.run_actions( + arguments={'global': flexmock(dry_run=False, log_file=None), 'create': flexmock()}, + config_filename=flexmock(), + config={'repositories': []}, + config_paths=[], + local_path=flexmock(), + remote_path=flexmock(), + local_borg_version=flexmock(), + repository={'path': 'repo', 'label': 'my repo'}, + ) + ) + assert result == (expected,) + + def test_run_actions_adds_log_file_to_hook_context(): flexmock(module).should_receive('add_custom_log_levels') flexmock(module).should_receive('get_skip_actions').and_return([]) @@ -497,7 +531,7 @@ def test_run_actions_adds_log_file_to_hook_context(): repository={'path': 'repo'}, config={'repositories': []}, config_paths=[], - hook_context={'repository': 'repo', 'repositories': '', 'log_file': 'foo'}, + hook_context={'label': '', 'log_file': 'foo', 'repositories': '', 'repository': 'repo'}, local_borg_version=object, create_arguments=object, global_arguments=object,