add support for --list --json #74

Merged
witten merged 5 commits from :list-json into master 2018-07-28 21:21:39 +00:00
3 changed files with 109 additions and 61 deletions
Showing only changes of commit 35402c28e1 - Show all commits

View File

@ -131,8 +131,26 @@ def run_configuration(config_filename, args): # pragma: no cover
if args.create:
hook.execute_hook(hooks.get('before_backup'), config_filename, 'pre-backup')
_run_commands(args, consistency, local_path, location, remote_path, retention, storage)
if args.create:
hook.execute_hook(hooks.get('after_backup'), config_filename, 'post-backup')
except (OSError, CalledProcessError):
hook.execute_hook(hooks.get('on_error'), config_filename, 'on-error')
raise
def _run_commands(args, consistency, local_path, location, remote_path, retention, storage):
json_results = []
for unexpanded_repository in location['repositories']:
_run_commands_on_repository(args, consistency, json_results, local_path, location, remote_path, retention,
storage, unexpanded_repository)
if args.json:
sys.stdout.write(json.dumps(json_results))
def _run_commands_on_repository(args, consistency, json_results, local_path, location, remote_path, retention, storage,
unexpanded_repository): # pragma: no cover
repository = os.path.expanduser(unexpanded_repository)
dry_run_label = ' (dry run; not making any changes)' if args.dry_run else ''
if args.prune:
@ -190,14 +208,6 @@ def run_configuration(config_filename, args): # pragma: no cover
local_path=local_path,
remote_path=remote_path,
)
if args.json:
sys.stdout.write(json.dumps(json_results))
if args.create:
hook.execute_hook(hooks.get('after_backup'), config_filename, 'post-backup')
except (OSError, CalledProcessError):
hook.execute_hook(hooks.get('on_error'), config_filename, 'on-error')
raise
def main(): # pragma: no cover

View File

@ -0,0 +1,38 @@
from borgmatic.commands import borgmatic
from flexmock import flexmock
import json
import pytest
import sys
def test__run_commands_handles_multiple_json_outputs_in_array():
# THEN
(flexmock(borgmatic)
.should_receive("_run_commands_on_repository")
.times(3)
.replace_with(lambda args, consistency, json_results, local_path, location, remote_path, retention, storage,
unexpanded_repository: json_results.append({"whatever": unexpanded_repository}))
)
(flexmock(sys.stdout)
.should_call("write")
.with_args(json.dumps(json.loads('''
[
{"whatever": "fake_repo1"},
{"whatever": "fake_repo2"},
{"whatever": "fake_repo3"}
]
''')))
)
borgmatic._run_commands(args=flexmock(json=True),
consistency=None,
local_path=None,
location={"repositories": [
"fake_repo1",
"fake_repo2",
"fake_repo3"
]},
remote_path=None,
retention=None,
storage=None)