diff --git a/NEWS b/NEWS index 0248028..2b94caa 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 e048bd4..fb2fff9 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 8d99b31..f1af922 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 29df124..22c0e69 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)