From 6b9c0d7719afdf85820e703e58b1358c95b6586e Mon Sep 17 00:00:00 2001 From: Florian Pritz Date: Tue, 21 Nov 2017 15:56:04 +0100 Subject: Enable --adhoc automatically when cache is empty Signed-off-by: Florian Pritz --- lib/App/BorgRestore.pm | 13 +++++++++++++ script/borg-restore.pl | 5 +++++ t/07_cache_contains_data.t | 22 ++++++++++++++++++++++ 3 files changed, 40 insertions(+) create mode 100644 t/07_cache_contains_data.t diff --git a/lib/App/BorgRestore.pm b/lib/App/BorgRestore.pm index edc009c..d96b957 100644 --- a/lib/App/BorgRestore.pm +++ b/lib/App/BorgRestore.pm @@ -235,6 +235,19 @@ method get_all_archives() { return \@ret; } +=head3 cache_contains_data + + if ($app->cache_contains_data()) { ... } + +Returns 1 if the cache contains any archive data, 0 otherwise. + +=cut + +method cache_contains_data() { + my $existing_archives = $self->{db}->get_archive_names(); + return @{$existing_archives}+0 > 0 ? 1 : 0; +} + =head3 select_archive_timespec my $archive = $app->select_archive_timespec($archives, $timespec); diff --git a/script/borg-restore.pl b/script/borg-restore.pl index d6e97e5..f58ba33 100755 --- a/script/borg-restore.pl +++ b/script/borg-restore.pl @@ -235,6 +235,11 @@ sub main { return 0; } + if (!$app->cache_contains_data()) { + $opts{"adhoc"} = 1; + $log->warning("Cache is empty. --adhoc has been enabled for you automatically"); + } + my @paths = @ARGV; my $path; diff --git a/t/07_cache_contains_data.t b/t/07_cache_contains_data.t new file mode 100644 index 0000000..2187ab0 --- /dev/null +++ b/t/07_cache_contains_data.t @@ -0,0 +1,22 @@ +use strict; +use warnings; + +use Test::More; +use Test::MockObject; +use Test::Differences; + +use App::BorgRestore; + +my $db = Test::MockObject->new(); +my $app = App::BorgRestore->new_no_defaults({db => $db}); + +$db->mock('get_archive_names', sub {return [qw(a b c)];}); +is($app->cache_contains_data(), 1); + +$db->mock('get_archive_names', sub {return [];}); +is($app->cache_contains_data(), 0); + +$db->mock('get_archive_names', sub {return [qw(a)];}); +is($app->cache_contains_data(), 1); + +done_testing; -- cgit v1.2.3-24-g4f1b