From 0c56663e09dd80a2c5225446a46b5bd908eb829d Mon Sep 17 00:00:00 2001 From: Florian Pritz Date: Sun, 9 Sep 2018 11:14:52 +0200 Subject: Settings: Add constructors Signed-off-by: Florian Pritz --- lib/App/BorgRestore.pm | 2 ++ lib/App/BorgRestore/Settings.pm | 37 +++++++++++++++++++++++++------------ 2 files changed, 27 insertions(+), 12 deletions(-) diff --git a/lib/App/BorgRestore.pm b/lib/App/BorgRestore.pm index d90afe2..327bb42 100644 --- a/lib/App/BorgRestore.pm +++ b/lib/App/BorgRestore.pm @@ -90,6 +90,8 @@ L =cut 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; diff --git a/lib/App/BorgRestore/Settings.pm b/lib/App/BorgRestore/Settings.pm index 31249c5..2aea1ae 100644 --- a/lib/App/BorgRestore/Settings.pm +++ b/lib/App/BorgRestore/Settings.pm @@ -118,6 +118,10 @@ See LICENSE for the full license text. =cut +method new($class: $deps = {}) { + return $class->new_no_defaults($deps); +} + our $borg_repo = "backup:borg-".hostname; our $cache_path_base; our @backup_prefixes = ( @@ -126,8 +130,27 @@ our @backup_prefixes = ( our $sqlite_cache_size = 102400; our $prepare_data_in_memory = 1; -if (defined $ENV{XDG_CACHE_HOME} or defined $ENV{HOME}) { - $cache_path_base = sprintf("%s/borg-restore.pl", $ENV{XDG_CACHE_HOME} // $ENV{HOME} ."/.cache"); +method new_no_defaults($class: $deps = {}) { + my $self = {}; + bless $self, $class; + $self->{deps} = $deps; + + + if (defined $ENV{XDG_CACHE_HOME} or defined $ENV{HOME}) { + $cache_path_base = sprintf("%s/borg-restore.pl", $ENV{XDG_CACHE_HOME} // $ENV{HOME} ."/.cache"); + } + + load_config_files(); + + if (not defined $cache_path_base) { + die "Error: \$cache_path_base is not defined. This is most likely because the\n" + ."environment variables \$HOME and \$XDG_CACHE_HOME are not set. Consider setting\n" + ."the path in the config file or ensure that the variables are set."; + } + + $cache_path_base = App::BorgRestore::Helper::untaint($cache_path_base, qr/.*/); + + return $self; } fun load_config_files() { @@ -150,16 +173,6 @@ fun load_config_files() { } } -load_config_files(); - -if (not defined $cache_path_base) { - die "Error: \$cache_path_base is not defined. This is most likely because the\n" - ."environment variables \$HOME and \$XDG_CACHE_HOME are not set. Consider setting\n" - ."the path in the config file or ensure that the variables are set."; -} - -$cache_path_base = App::BorgRestore::Helper::untaint($cache_path_base, qr/.*/); - fun get_cache_base_dir_path($path) { return "$cache_path_base/$path"; } -- cgit v1.2.3-24-g4f1b