package App::ArchLinux::PackagerTools::Config; use strictures; use autodie; use Function::Parameters; use Log::Any qw($log); use Hash::Merge; use Path::Tiny; use TOML qw(from_toml); =head1 NAME App::ArchLinux::PackagerTools::Config - ShortDesc =head1 SYNOPSIS use App::ArchLinux::PackagerTools::Config; # synopsis... =head1 DESCRIPTION # longer description... =head1 METHODS =head2 new my $config = App::ArchLinux::PackagerTools::Config->new(); Returns a new instance. =cut method new($class: $context, $deps = {}) { return $class->new_no_defaults($context, $deps); } method new_no_defaults($class: $context, $deps = {}) { return $context->{$class} if defined $context->{$class}; my $self = {}; bless $self, $class; $self->{deps} = $deps; $self->{config} = $self->_load_config(); $context->{$class} = $self; return $self; } =head2 Public Methods =cut =head3 get_config my $conf = $config->get_config(); Return config content as a hash. =cut method get_config() { return $self->{config}; } =head2 Private Methods =cut =head3 _load_config my $conf = $config->_load_config(); Load configuration from config file. You should use get_config() instead since that returns a cached in-memory copy. =cut method _load_config() { my $config = $self->_get_default_config(); my $file = path(($ENV{XDG_CONFIG_HOME} // $ENV{HOME}."/.config")."/perlpkg/config.toml"); if ($file->exists) { $log->debugf("Reading config file: %s", $file); my $file_config = from_toml($file->slurp); $config = merge($file_config, $config); } else { $log->debug("Config file not found. Returning default config"); } return $config; } =head3 _get_default_config my $default_conf = $config->_get_default_config(); Return a hash with the default config values. =cut method _get_default_config() { return { cache => { root_dir => ($ENV{XDG_CACHE_HOME} // $ENV{HOME}."/.cache")."/perlpkg/v1/", }, archweb => { cache_timeout => '60m', base_url => 'https://www.archlinux.org/', }, cpan => { cache_timeout => '60m', mirror_url => 'https://cpan.metacpan.org/', }, }; } 1; __END__