| 1 | #!/usr/bin/perl | ||
| 2 | |||
| 3 | package Mail::Summary::Tools::CLI::Config; | ||
| 4 | use Moose; | ||
| 5 | |||
| 6 | use File::Save::Home (); | ||
| 7 | use Path::Class; | ||
| 8 | use YAML::Syck; | ||
| 9 | |||
| 10 | has homedir => ( | ||
| 11 | isa => "Path::Class::Dir", | ||
| 12 | is => "rw", | ||
| 13 | lazy => 1, | ||
| 14 | default => sub { Path::Class::dir($_[0]->find_homedir) }, | ||
| 15 | ); | ||
| 16 | |||
| 17 | has config_file => ( | ||
| 18 | isa => "Path::Class::File", | ||
| 19 | is => "rw", | ||
| 20 | lazy => 1, | ||
| 21 | default => sub { $_[0]->homedir->file("config") }, | ||
| 22 | ); | ||
| 23 | |||
| 24 | has config => ( | ||
| 25 | isa => "HashRef", | ||
| 26 | is => "rw", | ||
| 27 | lazy => 1, | ||
| 28 | default => sub { $_[0]->_load_config }, | ||
| 29 | ); | ||
| 30 | |||
| 31 | sub find_homedir { | ||
| 32 | my $self = shift; | ||
| 33 | |||
| 34 | return File::Save::Home::make_subhome_directory( | ||
| 35 | File::Save::Home::get_subhome_directory_status(".mailsum"), | ||
| 36 | ); | ||
| 37 | } | ||
| 38 | |||
| 39 | sub defaults { | ||
| 40 | my $self = shift; | ||
| 41 | $self->config->{defaults} || {}; | ||
| 42 | } | ||
| 43 | |||
| 44 | sub option_value { | ||
| 45 | my ( $self, $command, $option ) = @_; | ||
| 46 | my $defaults = $self->defaults; | ||
| 47 | |||
| 48 | for ( $command, "all" ) { | ||
| 49 | if ( exists $defaults->{$_}{$option} ) { | ||
| 50 | return $defaults->{$_}{$option}; | ||
| 51 | } | ||
| 52 | } | ||
| 53 | |||
| 54 | return; | ||
| 55 | } | ||
| 56 | |||
| 57 | sub _load_config { | ||
| 58 | my $self = shift; | ||
| 59 | |||
| 60 | if ( -e $self->config_file ) { | ||
| 61 | YAML::Syck::LoadFile( $self->config_file->stringify ); | ||
| 62 | } else { | ||
| 63 | return {}; | ||
| 64 | } | ||
| 65 | } | ||
| 66 | |||
| 67 | 1; |