diff options
author | Florian Pritz <bluewind@xinu.at> | 2018-08-12 20:45:15 +0200 |
---|---|---|
committer | Florian Pritz <bluewind@xinu.at> | 2018-08-12 20:46:29 +0200 |
commit | 4bf9db2df37f151d388612924fcb9a7e60300329 (patch) | |
tree | cfab10ffd2ef1720f3db99993a10e6dbdd409943 | |
parent | f529b18a48b33911ac2e09ccf7d340aa0b8e9d55 (diff) | |
download | App-BorgRestore-4bf9db2df37f151d388612924fcb9a7e60300329.tar.gz App-BorgRestore-4bf9db2df37f151d388612924fcb9a7e60300329.tar.xz |
Add --json option
Signed-off-by: Florian Pritz <bluewind@xinu.at>
-rw-r--r-- | Changes | 1 | ||||
-rwxr-xr-x | script/borg-restore.pl | 31 |
2 files changed, 29 insertions, 3 deletions
@@ -1,6 +1,7 @@ Revision history for Perl extension App-BorgRestore {{$NEXT}} + - Add --json option - Add --detail option - Log cache update status message per archive to INFO - Add --quiet option diff --git a/script/borg-restore.pl b/script/borg-restore.pl index 91aae91..be3d7cd 100755 --- a/script/borg-restore.pl +++ b/script/borg-restore.pl @@ -16,6 +16,8 @@ borg-restore.pl [options] <path> --quiet show only warnings and errors --detail Output additional detail for some operations (currently only --list) + --json Output JSON instead of human readable text + (currently only --list) --update-cache, -u update cache files --list [pattern] List paths contained in the backups, optionally matching an SQLite LIKE pattern @@ -83,6 +85,11 @@ Reduce output by showing only show warnings and above (errors). Output additional detail information with some operations. Refer to the specific options for more information. Currently only works with B<--list> +=item B<--json> + +Output JSON instead of human readable text with some operations. Refer to the +specific options for more information. Currently only works with B<--list> + =item B<--update-cache>, B<-u> Update the lookup database. You should run this after creating or removing a backup. @@ -97,6 +104,8 @@ If B<--detail> is used, also outputs which archives contain a version of the file. If the same version is part of multiple archives, only one archive is shown. +If B<--json> is used, the output is JSON. Can also be combined with B<--detail>. + =item B<--destination=>I<path>, B<-d >I<path> Restore the backup to 'path' instead of its original location. The destination @@ -162,6 +171,7 @@ use Cwd qw(abs_path); use File::Basename; use Function::Parameters; use Getopt::Long; +use JSON; use Log::Any qw($log); use Log::Any::Adapter; use Log::Log4perl; @@ -241,7 +251,7 @@ sub main { $ENV{PATH} = App::BorgRestore::Helper::untaint($ENV{PATH}, qr(.*)); Getopt::Long::Configure ("bundling"); - GetOptions(\%opts, "help|h", "debug", "update-cache|u", "destination|d=s", "time|t=s", "adhoc", "version", "list", "quiet", "detail") or pod2usage(2); + GetOptions(\%opts, "help|h", "debug", "update-cache|u", "destination|d=s", "time|t=s", "adhoc", "version", "list", "quiet", "detail", "json") or pod2usage(2); pod2usage(0) if $opts{help}; if ($opts{version}) { @@ -274,15 +284,30 @@ sub main { my @patterns = @ARGV; push @patterns, '', if @patterns == 0; + my $json_data = {}; + for my $pattern (@patterns) { $pattern = App::BorgRestore::Helper::untaint($pattern, qr/.*/); my $paths = $app->search_path($pattern); for my $path (@$paths) { - printf "%s\n", $path; - print_archive_list($app->find_archives($path), 0) if $opts{detail}; + my $archives; $archives = $app->find_archives($path) if $opts{detail}; + if ($opts{json}) { + $json_data->{$path} = { + path => $path, + archives => $archives, + } unless defined $json_data->{$path}; + } else { + printf "%s\n", $path; + print_archive_list($archives, 0) if defined $archives; + } } } + + if ($opts{json}) { + print encode_json($json_data); + } + return 0; } |