summaryrefslogtreecommitdiffstats
path: root/lib/App/ArchLinux/PackagerTools/Config.pm
blob: 37826e350a4095dcbb72966cbb48c0b6af9a7fc5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package App::ArchLinux::PackagerTools::Config;
use strictures;

use autodie;
use Function::Parameters;
use Log::Any qw($log);
use Hash::Merge qw(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, $err) = from_toml($file->slurp);
		die $log->errorf("Failed to parse config file: %s", $err) unless $file_config;
		$log->debugf("config = %s", $config);
		$log->debugf("file_config = %s", $file_config);
		$config = merge($file_config, $config);
	} else {
		$log->debug("Config file not found. Returning default config");
	}
	$log->debugf("Loaded configuration is: %s", $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/",
		},
		allowed_maintainers => [
		],
		archweb => {
			cache_timeout => '60min',
			base_url => 'https://www.archlinux.org/',
		},
		cpan => {
			cache_timeout => '60min',
			mirror_url => 'https://cpan.metacpan.org/',
		},
		pacman => {
			pkgname_to_cpan_dist_map => {
				# pkgname => cpan_name
				'perl-critic' => 'Perl::Critic',
				'perl-libintl-perl' => 'libintl-perl',
			},
		},
	};
}

1;

__END__