| 1 | |||
| 2 | =head1 NAME | ||
| 3 | |||
| 4 | File::Corresponding::Config::Find -- Locate config files (e.g. per user) | ||
| 5 | |||
| 6 | |||
| 7 | =head1 SYNOPSIS | ||
| 8 | |||
| 9 | use File::Corresponding::Config::Find; | ||
| 10 | my $my_config = ".myapp"; | ||
| 11 | |||
| 12 | #Find .myapp in any of the user's home directories | ||
| 13 | my $myapp_config_in_home | ||
| 14 | = File::Corresponding::Config::Find->new()->user_config($my_config) or die; | ||
| 15 | |||
| 16 | #Find .myapp in the current working directory, or in the user's home directory | ||
| 17 | use Path::Class qw/ dir /; | ||
| 18 | my $myapp_config_in_cwd_or_home | ||
| 19 | = File::Corresponding::Config::Find->new(preferred_dirs => [ dir(".") ])->user_config($my_config) | ||
| 20 | or die; | ||
| 21 | |||
| 22 | |||
| 23 | =head1 DESCRIPTION | ||
| 24 | |||
| 25 | Locate named config files in the usual places, e.g. the current dir, | ||
| 26 | the user's home directory (cross platform). | ||
| 27 | |||
| 28 | First the preferred_dirs are searched, then the user's document | ||
| 29 | directory, data directory, and home directory. | ||
| 30 | |||
| 31 | =head1 COMMENT | ||
| 32 | |||
| 33 | I searched for something like this, couldn't find anything. | ||
| 34 | |||
| 35 | So I wrote this module, and named it Config::Find. Which is a name | ||
| 36 | already taken by a CPAN module. | ||
| 37 | |||
| 38 | D'oh! | ||
| 39 | |||
| 40 | But now it's written, and it works, so it stays. | ||
| 41 | |||
| 42 | =cut | ||
| 43 | |||
| 44 | package File::Corresponding::Config::Find; | ||
| 45 | use Moose; | ||
| 46 | |||
| 47 | |||
| 48 | |||
| 49 | use Data::Dumper; | ||
| 50 | use Path::Class; | ||
| 51 | use Moose::Autobox; | ||
| 52 | |||
| 53 | use File::HomeDir; | ||
| 54 | |||
| 55 | |||
| 56 | |||
| 57 | =head1 ATTRIBUTES | ||
| 58 | |||
| 59 | =head2 preferred_dirs : ArrayRef[Path::Class] | ||
| 60 | |||
| 61 | =cut | ||
| 62 | has preferred_dirs => ( | ||
| 63 | is => "rw", | ||
| 64 | isa => "ArrayRef[Path::Class::Dir]", | ||
| 65 | default => sub { [] }, | ||
| 66 | auto_deref => 1, | ||
| 67 | ); | ||
| 68 | |||
| 69 | |||
| 70 | |||
| 71 | =head1 METHODS | ||
| 72 | |||
| 73 | =head2 user_config($config_file_name, $preferred_dirs = []) : Path::Class::File $found_file_name | undef | ||
| 74 | |||
| 75 | Find an existing readable file called $config_file_name | ||
| 76 | (e.g. ".myapp") in a) $preferred_dirs, or b) the usual user | ||
| 77 | directories ($HOME etc). | ||
| 78 | |||
| 79 | Return the complete file name to the config file, or undef if none was | ||
| 80 | found. | ||
| 81 | |||
| 82 | =cut | ||
| 83 | sub user_config { | ||
| 84 | my $self = shift; | ||
| 85 | my ($config_file_name) = @_; | ||
| 86 | |||
| 87 | my @potential_dirs = ( | ||
| 88 | $self->preferred_dirs, | ||
| 89 | File::HomeDir->my_documents, | ||
| 90 | File::HomeDir->my_data, | ||
| 91 | File::HomeDir->my_home, | ||
| 92 | ); | ||
| 93 | for my $dir (@potential_dirs) { | ||
| 94 | my $file = file($dir, $config_file_name); | ||
| 95 | -r $file and return $file; | ||
| 96 | } | ||
| 97 | |||
| 98 | return undef; | ||
| 99 | } | ||
| 100 | |||
| 101 | |||
| 102 | |||
| 103 | 1; | ||
| 104 | |||
| 105 | |||
| 106 | |||
| 107 | __END__ |