From b36e11d3cada8ac4359ab9607a562792c89bd015 Mon Sep 17 00:00:00 2001 From: Codimp Date: Mon, 29 Apr 2024 01:00:07 +0200 Subject: [PATCH 1/5] add label variable interpolation in hook context --- borgmatic/commands/borgmatic.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/borgmatic/commands/borgmatic.py b/borgmatic/commands/borgmatic.py index 2c23ad0e..1c02c306 100644 --- a/borgmatic/commands/borgmatic.py +++ b/borgmatic/commands/borgmatic.py @@ -283,9 +283,14 @@ def run_actions( ''' add_custom_log_levels() repository_path = os.path.expanduser(repository['path']) + if 'label' in repository: + repository_label = repository['label'] + else: + repository_label = '' global_arguments = arguments['global'] dry_run_label = ' (dry run; not making any changes)' if global_arguments.dry_run else '' hook_context = { + 'label': repository_label, 'repository': repository_path, # Deprecated: For backwards compatibility with borgmatic < 1.6.0. 'repositories': ','.join([repo['path'] for repo in config['repositories']]), -- 2.45.1 From 3bc2c12f47e7c77c1a0c616e0424a799111ce209 Mon Sep 17 00:00:00 2001 From: Codimp Date: Mon, 29 Apr 2024 01:00:49 +0200 Subject: [PATCH 2/5] add unit test --- tests/unit/commands/test_borgmatic.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/unit/commands/test_borgmatic.py b/tests/unit/commands/test_borgmatic.py index 911e2add..30db260f 100644 --- a/tests/unit/commands/test_borgmatic.py +++ b/tests/unit/commands/test_borgmatic.py @@ -494,10 +494,10 @@ def test_run_actions_adds_log_file_to_hook_context(): expected = flexmock() flexmock(borgmatic.actions.create).should_receive('run_create').with_args( config_filename=object, - repository={'path': 'repo'}, + repository={'path': 'repo', 'label': ''}, config={'repositories': []}, config_paths=[], - hook_context={'repository': 'repo', 'repositories': '', 'log_file': 'foo'}, + hook_context={'repository': 'repo', 'label': '', 'repositories': '', 'log_file': 'foo'}, local_borg_version=object, create_arguments=object, global_arguments=object, @@ -515,7 +515,7 @@ def test_run_actions_adds_log_file_to_hook_context(): local_path=flexmock(), remote_path=flexmock(), local_borg_version=flexmock(), - repository={'path': 'repo'}, + repository={'path': 'repo', 'label': ''}, ) ) assert result == (expected,) -- 2.45.1 From 9f2facad9d812237e76436501190069cf38a45df Mon Sep 17 00:00:00 2001 From: Codimp Date: Mon, 29 Apr 2024 01:01:05 +0200 Subject: [PATCH 3/5] add documentation --- docs/how-to/add-preparation-and-cleanup-steps-to-backups.md | 2 ++ 1 file changed, 2 insertions(+) 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 2c71a415..22c61996 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 @@ -84,6 +84,8 @@ variables you can use here: path of the borgmatic log file, only set when the `--log-file` flag is used * `repository`: path of the current repository as configured in the current borgmatic configuration file + * `label`: current repository label as configured in the current borgmatic + configuration file Note that you can also interpolate in [arbitrary environment variables](https://torsion.org/borgmatic/docs/how-to/provide-your-passwords/). -- 2.45.1 From b67f729c7c572fe1c03b4b781667ecb232d364e5 Mon Sep 17 00:00:00 2001 From: Codimp Date: Mon, 29 Apr 2024 10:37:58 +0200 Subject: [PATCH 4/5] replace label presence condition with get method --- borgmatic/commands/borgmatic.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/borgmatic/commands/borgmatic.py b/borgmatic/commands/borgmatic.py index 1c02c306..bbcab20a 100644 --- a/borgmatic/commands/borgmatic.py +++ b/borgmatic/commands/borgmatic.py @@ -283,14 +283,10 @@ def run_actions( ''' add_custom_log_levels() repository_path = os.path.expanduser(repository['path']) - if 'label' in repository: - repository_label = repository['label'] - else: - repository_label = '' global_arguments = arguments['global'] dry_run_label = ' (dry run; not making any changes)' if global_arguments.dry_run else '' hook_context = { - 'label': repository_label, + 'label': repository.get('label', ''), 'repository': repository_path, # Deprecated: For backwards compatibility with borgmatic < 1.6.0. 'repositories': ','.join([repo['path'] for repo in config['repositories']]), -- 2.45.1 From e70d1b054009edd65262175449078c95acfa9dc4 Mon Sep 17 00:00:00 2001 From: Codimp Date: Mon, 29 Apr 2024 21:16:36 +0200 Subject: [PATCH 5/5] replace label with repository_label --- borgmatic/commands/borgmatic.py | 2 +- .../how-to/add-preparation-and-cleanup-steps-to-backups.md | 4 ++-- tests/unit/commands/test_borgmatic.py | 7 ++++++- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/borgmatic/commands/borgmatic.py b/borgmatic/commands/borgmatic.py index bbcab20a..68f416b2 100644 --- a/borgmatic/commands/borgmatic.py +++ b/borgmatic/commands/borgmatic.py @@ -286,7 +286,7 @@ def run_actions( global_arguments = arguments['global'] dry_run_label = ' (dry run; not making any changes)' if global_arguments.dry_run else '' hook_context = { - 'label': repository.get('label', ''), + 'repository_label': repository.get('label', ''), 'repository': repository_path, # Deprecated: For backwards compatibility with borgmatic < 1.6.0. 'repositories': ','.join([repo['path'] for repo in config['repositories']]), 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 22c61996..1d05c0d9 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 @@ -84,8 +84,8 @@ variables you can use here: path of the borgmatic log file, only set when the `--log-file` flag is used * `repository`: path of the current repository as configured in the current borgmatic configuration file - * `label`: current repository label as configured in the current borgmatic - configuration file + * `repository_label`: current repository label as configured in the current + borgmatic configuration file Note that you can also interpolate in [arbitrary environment variables](https://torsion.org/borgmatic/docs/how-to/provide-your-passwords/). diff --git a/tests/unit/commands/test_borgmatic.py b/tests/unit/commands/test_borgmatic.py index 30db260f..b0b6a153 100644 --- a/tests/unit/commands/test_borgmatic.py +++ b/tests/unit/commands/test_borgmatic.py @@ -497,7 +497,12 @@ def test_run_actions_adds_log_file_to_hook_context(): repository={'path': 'repo', 'label': ''}, config={'repositories': []}, config_paths=[], - hook_context={'repository': 'repo', 'label': '', 'repositories': '', 'log_file': 'foo'}, + hook_context={ + 'repository': 'repo', + 'repository_label': '', + 'repositories': '', + 'log_file': 'foo', + }, local_borg_version=object, create_arguments=object, global_arguments=object, -- 2.45.1