[Feature] Add label variable interpolation in hook context #861

Open
codimp wants to merge 5 commits from codimp/borgmatic:label-variable-interpolation-in-hook-context into main
3 changed files with 11 additions and 3 deletions

View File

@ -286,6 +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 = {
'repository_label': repository.get('label', ''),
codimp marked this conversation as resolved Outdated

You could simplify this all to just: repository_label = repository.get('label', '')

It could even be inline below without the temporary variable. (Do not feel strongly.)

You could simplify this all to just: `repository_label = repository.get('label', '')` It could even be inline below without the temporary variable. (Do not feel strongly.)

Good idea!

Good idea!
'repository': repository_path,
# Deprecated: For backwards compatibility with borgmatic < 1.6.0.
'repositories': ','.join([repo['path'] for repo in config['repositories']]),

View File

@ -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
* `repository_label`: current repository label as configured in the current
codimp marked this conversation as resolved Outdated

Sorry, another thing I think I mentioned on IRC.. What do you think of calling this repository_label instead of just label?

Sorry, another thing I think I mentioned on IRC.. What do you think of calling this `repository_label` instead of just `label`?

oups, right, my bad!

oups, right, my bad!
borgmatic configuration file
Note that you can also interpolate in [arbitrary environment
variables](https://torsion.org/borgmatic/docs/how-to/provide-your-passwords/).

View File

@ -494,10 +494,15 @@ 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',
'repository_label': '',
'repositories': '',
'log_file': 'foo',
},
local_borg_version=object,
create_arguments=object,
global_arguments=object,
@ -515,7 +520,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,)