| 1 | # You may distribute under the terms of either the GNU General Public License | ||
| 2 | # or the Artistic License (the same terms as Perl itself) | ||
| 3 | # | ||
| 4 | # (C) Paul Evans, 2026 -- leonerd@leonerd.org.uk | ||
| 5 | |||
| 6 | use v5.36; | ||
| 7 | |||
| 8 | package App::perl::distrolint::Config 0.09; | ||
| 9 | |||
| 10 | =head1 NAME | ||
| 11 | |||
| 12 | C<App::perl::distrolint::Config> - configuration management for C<App::perl::distrolint> | ||
| 13 | |||
| 14 | =head1 SYNOPSIS | ||
| 15 | |||
| 16 | =for highlighter | ||
| 17 | |||
| 18 | $ cat distrolint.ini | ||
| 19 | [check Pod] | ||
| 20 | enabled = false | ||
| 21 | |||
| 22 | =for highlighter language=perl | ||
| 23 | |||
| 24 | use App::perl::distrolint::Check; | ||
| 25 | |||
| 26 | if( App::perl::distrolint::Config->is_check_enabled( "Foo", 1 ) ) { | ||
| 27 | say "The 'Foo' check is enabled"; | ||
| 28 | } | ||
| 29 | |||
| 30 | =head1 DESCRIPTION | ||
| 31 | |||
| 32 | This module manages reading of config files that configure how the | ||
| 33 | C<App::perl::distrolint> checks behave. On first use, it reads | ||
| 34 | F<distrolint.ini> files found in various places and stores the configuration | ||
| 35 | found there. Package methods allow modules to query that stored configuration. | ||
| 36 | |||
| 37 | There are two locations that files may be found. First is a F<distrolint.ini> | ||
| 38 | file in the current working directory; presumed to be the root directory of a | ||
| 39 | distribution source code. The second is F<$HOME/.config/distrolint.ini> in the | ||
| 40 | user's home directory, for storing user-wide configuration. | ||
| 41 | |||
| 42 | Each file should be in F<ini> format, with sections named for each check type, | ||
| 43 | using a section marker like C<[check Name]>. Values that are literal C<true> | ||
| 44 | or C<false> strings are converted into suitable boolean values; everything | ||
| 45 | else is taken literally. | ||
| 46 | |||
| 47 | Certain configuration keys are pre-defined as standard; individual check | ||
| 48 | modules may document other keys that are specific to that check. | ||
| 49 | |||
| 50 | =head2 Standard Configuration | ||
| 51 | |||
| 52 | =over 4 | ||
| 53 | |||
| 54 | =item C<enabled> | ||
| 55 | |||
| 56 | A boolean that enables or disables that check module entirely. | ||
| 57 | |||
| 58 | =item C<skip> | ||
| 59 | |||
| 60 | A space-separated list of glob patterns. Files which match any of these | ||
| 61 | patterns will be skipped by checks that would otherwise iterate over all | ||
| 62 | source files of a certain type. | ||
| 63 | |||
| 64 | A glob metacharacter of C<*> will only match parts of a path within a | ||
| 65 | directory; use C<**> to match subdirectories also. | ||
| 66 | |||
| 67 | =back | ||
| 68 | |||
| 69 | =cut | ||
| 70 | |||
| 71 | use Config::Tiny; | ||
| 72 | |||
| 73 | my $config; | ||
| 74 | |||
| 75 | my sub _read_config () | ||
| 76 | { | ||
| 77 | return if $config; | ||
| 78 | |||
| 79 | $config //= {}; | ||
| 80 | |||
| 81 | foreach my $path ( "distrolint.ini", "$ENV{HOME}/.config/distrolint.ini" ) { | ||
| 82 | -r $path or next; | ||
| 83 | |||
| 84 | my $thisconfig = Config::Tiny->read( $path ); | ||
| 85 | foreach my $section ( sort keys $thisconfig->%* ) { | ||
| 86 | foreach my $key ( sort keys $thisconfig->{$section}->%* ) { | ||
| 87 | my $value = $thisconfig->{$section}{$key}; | ||
| 88 | |||
| 89 | # convert boolean literals | ||
| 90 | $value = ( $value eq "true" ) if $value =~ m/^(?:true|false)$/; | ||
| 91 | |||
| 92 | $config->{$section}{$key} //= $value; | ||
| 93 | } | ||
| 94 | } | ||
| 95 | } | ||
| 96 | } | ||
| 97 | |||
| 98 | =head1 METHODS | ||
| 99 | |||
| 100 | =cut | ||
| 101 | |||
| 102 | =head2 check_config | ||
| 103 | |||
| 104 | $value = App::perl::distrolint::Config->check_config( $name, $key, $default ); | ||
| 105 | |||
| 106 | Returns the value of a configuration key from the C<[check $name]> section of | ||
| 107 | the configuration file. If no entry exists, the value of C<$default> is | ||
| 108 | returned instead. | ||
| 109 | |||
| 110 | C<$name> may be given as a literal string, or as an object reference to the | ||
| 111 | check object itself (often C<$self> will suffice). | ||
| 112 | |||
| 113 | =cut | ||
| 114 | |||
| 115 | sub check_config ( $, $name, $key, $default ) | ||
| 116 | { | ||
| 117 | ref $name and | ||
| 118 | $name = ( ref $name ) =~ s/^App::perl::distrolint::Check:://r; | ||
| 119 | |||
| 120 | $config or _read_config(); | ||
| 121 | |||
| 122 | exists $config->{"check $name"} or return $default; | ||
| 123 | return $config->{"check $name"}{$key} // $default; | ||
| 124 | } | ||
| 125 | |||
| 126 | =head2 is_check_enabled | ||
| 127 | |||
| 128 | $enabled = App::perl::distrolint::Config->is_check_enabled( $name, $default = true ); | ||
| 129 | |||
| 130 | Returns true if the check is enabled (or at least, has not been specifically | ||
| 131 | disabled), or false if a configuration file has specifically disabled this | ||
| 132 | check. | ||
| 133 | |||
| 134 | By default, all checks are enabled. | ||
| 135 | |||
| 136 | =cut | ||
| 137 | |||
| 138 | sub is_check_enabled ( $, $name, $default = 1 ) | ||
| 139 | { | ||
| 140 | return App::perl::distrolint::Config->check_config( $name, "enabled", $default ); | ||
| 141 | } | ||
| 142 | |||
| 143 | =head1 AUTHOR | ||
| 144 | |||
| 145 | Paul Evans <leonerd@leonerd.org.uk> | ||
| 146 | |||
| 147 | =cut | ||
| 148 | |||
| 149 | 0x55AA; |