summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFlorian Pritz <bluewind@xinu.at>2018-09-09 13:20:10 +0200
committerFlorian Pritz <bluewind@xinu.at>2018-09-09 15:45:11 +0200
commit4618b97a5ee76a9b7bf469ff5d10d8ccacbce011 (patch)
treecf0caef3a500c5d1853a0ee306ae55d4f9a22255
parent0c56663e09dd80a2c5225446a46b5bd908eb829d (diff)
downloadApp-BorgRestore-4618b97a5ee76a9b7bf469ff5d10d8ccacbce011.tar.gz
App-BorgRestore-4618b97a5ee76a9b7bf469ff5d10d8ccacbce011.tar.xz
Get settings via hash instead of direct variable access
Signed-off-by: Florian Pritz <bluewind@xinu.at>
-rw-r--r--lib/App/BorgRestore.pm18
-rw-r--r--lib/App/BorgRestore/Settings.pm30
-rw-r--r--t/handle_added_archives.t2
3 files changed, 26 insertions, 24 deletions
diff --git a/lib/App/BorgRestore.pm b/lib/App/BorgRestore.pm
index 327bb42..04b563a 100644
--- a/lib/App/BorgRestore.pm
+++ b/lib/App/BorgRestore.pm
@@ -92,13 +92,12 @@ L<App::BorgRestore::DB>
method new($class: $deps = {}) {
$deps->{settings} //= App::BorgRestore::Settings->new();
- my $db_path = App::BorgRestore::Settings::get_db_path();
- my $cache_size = $App::BorgRestore::Settings::sqlite_cache_size;
+ my $config = $deps->{settings}->get_config();
- $deps->{borg} //= App::BorgRestore::Borg->new($App::BorgRestore::Settings::borg_repo);
- $deps->{db} //= App::BorgRestore::DB->new($db_path, $cache_size);
+ $deps->{borg} //= App::BorgRestore::Borg->new($config->{borg}->{repo});
+ $deps->{db} //= App::BorgRestore::DB->new($config->{cache}->{database_path}, $config->{cache}->{sqlite_memory_cache_size});
- return $class->new_no_defaults($deps);
+ return $class->new_no_defaults($deps, $config);
}
=head3 new_no_defaults
@@ -108,11 +107,12 @@ their default values. This is probably only useful for tests.
=cut
-method new_no_defaults($class: $deps) {
+method new_no_defaults($class: $deps, $config = {}) {
my $self = {};
bless $self, $class;
$self->{deps} = $deps;
+ $self->{config} = $config;
return $self;
}
@@ -155,7 +155,7 @@ Returns the mapped path (string).
method map_path_to_backup_path($abs_path) {
my $backup_path = $abs_path;
- for my $backup_prefix (@App::BorgRestore::Settings::backup_prefixes) {
+ for my $backup_prefix (@{$self->{config}->{borg}->{path_prefixes}}) {
if ($backup_path =~ m/$backup_prefix->{regex}/) {
$backup_path =~ s/$backup_prefix->{regex}/$backup_prefix->{replacement}/;
last;
@@ -441,7 +441,7 @@ method _handle_added_archives($borg_archives) {
for my $archive (@$add_archives) {
my $start = Time::HiRes::gettimeofday();
- my $lookuptable_class = $App::BorgRestore::Settings::prepare_data_in_memory == 1 ? "Memory" : "DB";
+ my $lookuptable_class = $self->{config}->{cache}->{prepare_data_in_memory} == 1 ? "Memory" : "DB";
$log->debugf("Using '%s' class for PathTimeTable", $lookuptable_class);
my $lookuptable = "App::BorgRestore::PathTimeTable::$lookuptable_class"->new({db => $self->{deps}->{db}});
@@ -485,7 +485,7 @@ Updates the database used by e.g. C<find_archives>.
=cut
method update_cache() {
- my $v2_basedir = App::BorgRestore::Settings::get_cache_base_dir_path("v2");
+ my $v2_basedir = $self->{deps}->{settings}->get_cache_base_dir_path("v2");
if (-e $v2_basedir) {
$log->info("Removing old v2 cache directory: $v2_basedir");
path($v2_basedir)->remove_tree;
diff --git a/lib/App/BorgRestore/Settings.pm b/lib/App/BorgRestore/Settings.pm
index 2aea1ae..81c2cac 100644
--- a/lib/App/BorgRestore/Settings.pm
+++ b/lib/App/BorgRestore/Settings.pm
@@ -153,6 +153,21 @@ method new_no_defaults($class: $deps = {}) {
return $self;
}
+method get_config() {
+ return {
+ borg => {
+ repo => $borg_repo,
+ path_prefixes => [@backup_prefixes],
+ },
+ cache => {
+ base_path => $cache_path_base,
+ database_path => "$cache_path_base/v3/archives.db",
+ prepare_data_in_memory => $prepare_data_in_memory,
+ sqlite_memory_cache_size => $sqlite_cache_size,
+ }
+ };
+}
+
fun load_config_files() {
my @configfiles;
@@ -173,23 +188,10 @@ fun load_config_files() {
}
}
-fun get_cache_base_dir_path($path) {
+method get_cache_base_dir_path($path) {
return "$cache_path_base/$path";
}
-fun get_cache_dir() {
- return "$cache_path_base/v3";
-}
-
-fun get_cache_path($item) {
- return get_cache_dir()."/$item";
-}
-
-fun get_db_path() {
- return get_cache_path('archives.db');
-}
-
-
1;
__END__
diff --git a/t/handle_added_archives.t b/t/handle_added_archives.t
index 01f8663..d29c52a 100644
--- a/t/handle_added_archives.t
+++ b/t/handle_added_archives.t
@@ -32,7 +32,7 @@ $borg->mock('list_archive', sub {
} );
# Call the actual function we want to test
-my $app = App::BorgRestore->new({borg => $borg, db => $db});
+my $app = App::BorgRestore->new_no_defaults({borg => $borg, db => $db}, {cache => {prepare_data_in_memory => 1}});
$app->_handle_added_archives(['archive-1']);
# Check if $db->add_path has been called properly