| 1 | package CPAN::MetaPackager::Config; | ||
| 2 | |||
| 3 | use boolean; | ||
| 4 | use warnings qw(FATAL utf8); # Fatalize encoding glitches. | ||
| 5 | |||
| 6 | use Config::Tiny; | ||
| 7 | |||
| 8 | use File::Spec; | ||
| 9 | |||
| 10 | use Mojo::Log; | ||
| 11 | use Moo; | ||
| 12 | |||
| 13 | use Types::Standard qw/ArrayRef HashRef Object Str/; | ||
| 14 | |||
| 15 | has config => | ||
| 16 | ( | ||
| 17 | default => sub{return {} }, | ||
| 18 | is => 'rw', | ||
| 19 | isa => HashRef, | ||
| 20 | required => 1, | ||
| 21 | ); | ||
| 22 | |||
| 23 | has config_path => | ||
| 24 | ( | ||
| 25 | default => sub{return 'data/cpan.metapackager.conf'}, | ||
| 26 | is => 'rw', | ||
| 27 | isa => Str, | ||
| 28 | required => 0, | ||
| 29 | ); | ||
| 30 | |||
| 31 | |||
| 32 | has database_path => | ||
| 33 | ( | ||
| 34 | default => sub{return 'data/cpan.metapackager.sqlite'}, | ||
| 35 | is => 'rw', | ||
| 36 | isa => Str, | ||
| 37 | required => 0, | ||
| 38 | ); | ||
| 39 | |||
| 40 | has error => | ||
| 41 | ( | ||
| 42 | default => sub{return ''}, | ||
| 43 | is => 'rw', | ||
| 44 | isa => Str, | ||
| 45 | required => 0, | ||
| 46 | ); | ||
| 47 | |||
| 48 | has home_path => | ||
| 49 | ( | ||
| 50 | default => '', | ||
| 51 | is => 'rw', | ||
| 52 | isa => Str, | ||
| 53 | required => 0, | ||
| 54 | ); | ||
| 55 | |||
| 56 | # Available log levels are trace, debug, info, warn, error and fatal, in that order. | ||
| 57 | |||
| 58 | has log_level => | ||
| 59 | ( | ||
| 60 | default => sub{return 'info'}, | ||
| 61 | is => 'rw', | ||
| 62 | isa => Str, | ||
| 63 | required => 0, | ||
| 64 | ); | ||
| 65 | |||
| 66 | has logger => | ||
| 67 | ( | ||
| 68 | is => 'rw', | ||
| 69 | isa => Object, | ||
| 70 | required => 0, | ||
| 71 | ); | ||
| 72 | |||
| 73 | has separator => | ||
| 74 | ( | ||
| 75 | default => '-' x 50, | ||
| 76 | is => 'ro', | ||
| 77 | isa => Str, | ||
| 78 | required => 0, | ||
| 79 | ); | ||
| 80 | |||
| 81 | # Warning. Order is important because of foreign key constraints. | ||
| 82 | # The tables are created in this order, and dropped in reverse order. | ||
| 83 | # See also Database.build_pad(). | ||
| 84 | |||
| 85 | has table_names => | ||
| 86 | ( | ||
| 87 | default => sub{return [qw/log packages/]}, | ||
| 88 | is => 'rw', | ||
| 89 | isa => ArrayRef, | ||
| 90 | required => 0, | ||
| 91 | ); | ||
| 92 | |||
| 93 | our $VERSION = '1.05'; | ||
| 94 | |||
| 95 | # ----------------------------------------------- | ||
| 96 | |||
| 97 | sub init_config | ||
| 98 | { | ||
| 99 | my($self) = @_; | ||
| 100 | my($path) = File::Spec -> catfile($self -> home_path, $self -> config_path); | ||
| 101 | my($config) = $self -> config($self -> _init_config($path) ); | ||
| 102 | $$config{config_path} = $path; | ||
| 103 | $$config{log_path} = File::Spec -> catfile($self -> home_path, $$config{log_path}); | ||
| 104 | |||
| 105 | $self -> config($config); | ||
| 106 | $self -> logger(Mojo::Log -> new(level => $self -> log_level, path => $$config{log_path}) ); | ||
| 107 | |||
| 108 | } # End of init_config. | ||
| 109 | |||
| 110 | # ----------------------------------------------- | ||
| 111 | |||
| 112 | sub _init_config | ||
| 113 | { | ||
| 114 | my($self, $path) = @_; | ||
| 115 | |||
| 116 | # Section: [global]. | ||
| 117 | |||
| 118 | my($config) = Config::Tiny -> read($path); | ||
| 119 | |||
| 120 | die 'Error: ' . Config::Tiny -> errstr . "\n" if (Config::Tiny -> errstr); | ||
| 121 | |||
| 122 | # Sections: [localhost] and [webhost]. | ||
| 123 | |||
| 124 | my($section); | ||
| 125 | |||
| 126 | for my $i (1 .. 2) | ||
| 127 | { | ||
| 128 | $section = $i == 1 ? 'global' : $$config{$section}{host}; | ||
| 129 | |||
| 130 | $self -> error("Error: Config file '$path' does not contain the section [$section]") if (! $$config{$section}); | ||
| 131 | } | ||
| 132 | |||
| 133 | return $$config{$section}; | ||
| 134 | |||
| 135 | } # End of _init_config. | ||
| 136 | |||
| 137 | # -------------------------------------------------- | ||
| 138 | |||
| 139 | 1; | ||
| 140 | |||
| 141 | =head1 NAME | ||
| 142 | |||
| 143 | CPAN::MetaPackager::Config - Manage the cpan.metapackager.sqlite database | ||
| 144 | |||
| 145 | =head1 Instructions | ||
| 146 | |||
| 147 | See POD in MetaPackager.pm. | ||
| 148 | |||
| 149 | =head1 Author | ||
| 150 | |||
| 151 | Ron Savage I<E<lt>ron@savage.net.auE<gt>>. | ||
| 152 | |||
| 153 | My homepage: L<https://savage.net.au/>. | ||
| 154 | |||
| 155 | =head1 License | ||
| 156 | |||
| 157 | Perl 5. | ||
| 158 | |||
| 159 | =head1 Copyright | ||
| 160 | |||
| 161 | Australian copyright (c) 2025, Ron Savage. | ||
| 162 | All Programs of mine are 'OSI Certified Open Source Software'; | ||
| 163 | you can redistribute them and/or modify them under the terms of | ||
| 164 | The Artistic License V 2, a copy of which is available at: | ||
| 165 | http://www.opensource.org/licenses/index.html | ||
| 166 | |||
| 167 | =cut |