From 9aaf78b9dd844a584ec9887fc0b3a0804a7b2cca Mon Sep 17 00:00:00 2001 From: Florian Lindner Date: Mon, 24 Sep 2018 21:52:04 +0200 Subject: [PATCH] Add --json option for --create command line. Closes #94. --- NEWS | 1 + borgmatic/borg/create.py | 4 ++-- borgmatic/commands/borgmatic.py | 5 +++-- borgmatic/tests/unit/borg/test_create.py | 20 ++++++++++++++++++++ 4 files changed, 26 insertions(+), 4 deletions(-) diff --git a/NEWS b/NEWS index 0248028a9..2b94caa4a 100644 --- a/NEWS +++ b/NEWS @@ -8,6 +8,7 @@ * #88: Fix declared pykwalify compatibility version range in setup.py to prevent use of ancient versions of pykwalify with large version numbers. * #89: Pass --show-rc option to Borg when at highest verbosity level. + * #94: Add --json option for --create option. 1.2.2 * #85: Fix compatibility issue between pykwalify and ruamel.yaml 0.15.52, which manifested in diff --git a/borgmatic/borg/create.py b/borgmatic/borg/create.py index e048bd411..fb2fff93a 100644 --- a/borgmatic/borg/create.py +++ b/borgmatic/borg/create.py @@ -103,8 +103,7 @@ def _make_exclude_flags(location_config, exclude_filename=None): def create_archive( - dry_run, repository, location_config, storage_config, local_path='borg', remote_path=None, -): + dry_run, repository, location_config, storage_config, local_path='borg', remote_path=None, json=False): ''' Given vebosity/dry-run flags, a local or remote repository path, a location config dict, and a storage config dict, create a Borg archive. @@ -154,6 +153,7 @@ def create_archive( + (('--stats',) if not dry_run and logger.isEnabledFor(logging.INFO) else ()) + (('--debug', '--show-rc') if logger.isEnabledFor(logging.DEBUG) else ()) + (('--dry-run',) if dry_run else ()) + + (('--json',) if json else ()) ) logger.debug(' '.join(full_command)) diff --git a/borgmatic/commands/borgmatic.py b/borgmatic/commands/borgmatic.py index 8d99b316b..f1af9220e 100644 --- a/borgmatic/commands/borgmatic.py +++ b/borgmatic/commands/borgmatic.py @@ -99,8 +99,8 @@ def parse_arguments(*arguments): args = parser.parse_args(arguments) - if args.json and not (args.list or args.info): - raise ValueError('The --json option can only be used with the --list or --info options') + if args.json and not (args.create or args.list or args.info): + raise ValueError('The --json option can only be used with the --create, --list or --info options') if args.json and args.list and args.info: raise ValueError( @@ -192,6 +192,7 @@ def _run_commands_on_repository( consistency, local_path=local_path, remote_path=remote_path, + json=args.json ) if args.list: logger.info('{}: Listing archives'.format(repository)) diff --git a/borgmatic/tests/unit/borg/test_create.py b/borgmatic/tests/unit/borg/test_create.py index 29df1240a..22c0e6917 100644 --- a/borgmatic/tests/unit/borg/test_create.py +++ b/borgmatic/tests/unit/borg/test_create.py @@ -609,6 +609,26 @@ def test_create_archive_with_lock_wait_calls_borg_with_lock_wait_parameters(): ) +def test_create_archive_with_json_calls_borg_with_json_parameter(): + flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar')).and_return(()) + flexmock(module).should_receive('_write_pattern_file').and_return(None) + flexmock(module).should_receive('_make_pattern_flags').and_return(()) + flexmock(module).should_receive('_make_exclude_flags').and_return(()) + insert_subprocess_mock(CREATE_COMMAND + ('--json',)) + + module.create_archive( + dry_run=False, + repository='repo', + location_config={ + 'source_directories': ['foo', 'bar'], + 'repositories': ['repo'], + 'exclude_patterns': None, + }, + storage_config={}, + json=True + ) + + def test_create_archive_with_source_directories_glob_expands(): flexmock(module).should_receive('_expand_directories').and_return(('foo', 'food')).and_return(()) flexmock(module).should_receive('_write_pattern_file').and_return(None)