Add --json option for --create command line.

Closes #94.
This commit is contained in:
Florian Lindner 2018-09-24 21:52:04 +02:00
parent 5d8ac158ce
commit 9aaf78b9dd
4 changed files with 26 additions and 4 deletions

1
NEWS
View file

@ -8,6 +8,7 @@
* #88: Fix declared pykwalify compatibility version range in setup.py to prevent use of ancient * #88: Fix declared pykwalify compatibility version range in setup.py to prevent use of ancient
versions of pykwalify with large version numbers. versions of pykwalify with large version numbers.
* #89: Pass --show-rc option to Borg when at highest verbosity level. * #89: Pass --show-rc option to Borg when at highest verbosity level.
* #94: Add --json option for --create option.
1.2.2 1.2.2
* #85: Fix compatibility issue between pykwalify and ruamel.yaml 0.15.52, which manifested in * #85: Fix compatibility issue between pykwalify and ruamel.yaml 0.15.52, which manifested in

View file

@ -103,8 +103,7 @@ def _make_exclude_flags(location_config, exclude_filename=None):
def create_archive( 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 Given vebosity/dry-run flags, a local or remote repository path, a location config dict, and a
storage config dict, create a Borg archive. 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 ()) + (('--stats',) if not dry_run and logger.isEnabledFor(logging.INFO) else ())
+ (('--debug', '--show-rc') if logger.isEnabledFor(logging.DEBUG) else ()) + (('--debug', '--show-rc') if logger.isEnabledFor(logging.DEBUG) else ())
+ (('--dry-run',) if dry_run else ()) + (('--dry-run',) if dry_run else ())
+ (('--json',) if json else ())
) )
logger.debug(' '.join(full_command)) logger.debug(' '.join(full_command))

View file

@ -99,8 +99,8 @@ def parse_arguments(*arguments):
args = parser.parse_args(arguments) args = parser.parse_args(arguments)
if args.json and not (args.list or args.info): if args.json and not (args.create or args.list or args.info):
raise ValueError('The --json option can only be used with the --list or --info options') 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: if args.json and args.list and args.info:
raise ValueError( raise ValueError(
@ -192,6 +192,7 @@ def _run_commands_on_repository(
consistency, consistency,
local_path=local_path, local_path=local_path,
remote_path=remote_path, remote_path=remote_path,
json=args.json
) )
if args.list: if args.list:
logger.info('{}: Listing archives'.format(repository)) logger.info('{}: Listing archives'.format(repository))

View file

@ -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(): 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('_expand_directories').and_return(('foo', 'food')).and_return(())
flexmock(module).should_receive('_write_pattern_file').and_return(None) flexmock(module).should_receive('_write_pattern_file').and_return(None)