diff options
author | dakkar <dakkar@thenautilus.net> | 2016-12-28 17:46:34 +0000 |
---|---|---|
committer | dakkar <dakkar@thenautilus.net> | 2016-12-28 17:46:34 +0000 |
commit | 22480aff9a02cfba3e11694a1180ad18cf57ccf2 (patch) | |
tree | 3343a6f908824fa6da7ce4b3bb55407c3d879b8e | |
parent | make example executable (diff) | |
download | Sietima-22480aff9a02cfba3e11694a1180ad18cf57ccf2.tar.gz Sietima-22480aff9a02cfba3e11694a1180ad18cf57ccf2.tar.bz2 Sietima-22480aff9a02cfba3e11694a1180ad18cf57ccf2.zip |
simpler command-line script
-rwxr-xr-x | example/sietima | 59 | ||||
-rw-r--r-- | lib/Sietima/CmdLine.pm | 25 | ||||
-rw-r--r-- | lib/Sietima/Role/WithMailStore.pm | 3 | ||||
-rw-r--r-- | lib/Sietima/Types.pm | 13 |
4 files changed, 64 insertions, 36 deletions
diff --git a/example/sietima b/example/sietima index 5dbe2d1..4fafeb3 100755 --- a/example/sietima +++ b/example/sietima @@ -1,36 +1,31 @@ #!/usr/bin/env perl +use FindBin::libs; use Sietima::Policy; -use Sietima; -use Sietima::MailStore::FS; use Sietima::CmdLine; -my $s = Sietima->with_traits( - qw( - NoMail - ReplyTo - SubscriberOnly::Moderate - Headers - WithAdmin - Debounce - SubjectTag - WithMailStore - ), -)->new({ - admin => 'dakkar@thenautilus.net', - mail_store => Sietima::MailStore::FS->new({ - root => '/tmp/sietima-store', - }), - return_path => 'dakkar-sietima@thenautilus.net', - subject_tag => 'Test', - subscribers => [ - 'dakkar-a@thenautilus.net', - { address => 'dakkar-b@thenautilus.net', aliases => [ 'dakkar-bis@thenautilus.net' ] }, - { address => 'dakkar-c@thenautilus.net', prefs => { wants_mail => 0 } }, - ], -}); - -my $cli = Sietima::CmdLine->new({ - sietima => $s, -}); - -$cli->run; +Sietima::CmdLine->new({ + traits => [qw( + NoMail + ReplyTo + SubscriberOnly::Moderate + Headers + WithAdmin + Debounce + SubjectTag + WithMailStore + )], + args => { + admin => 'dakkar@thenautilus.net', + mail_store => { + class => 'Sietima::MailStore::FS', + root => '/tmp/sietima-store', + }, + return_path => 'dakkar-sietima@thenautilus.net', + subject_tag => 'Test', + subscribers => [ + 'dakkar-a@thenautilus.net', + { address => 'dakkar-b@thenautilus.net', aliases => [ 'dakkar-bis@thenautilus.net' ] }, + { address => 'dakkar-c@thenautilus.net', prefs => { wants_mail => 0 } }, + ], + }, +})->run; diff --git a/lib/Sietima/CmdLine.pm b/lib/Sietima/CmdLine.pm index 57d796f..3d78be2 100644 --- a/lib/Sietima/CmdLine.pm +++ b/lib/Sietima/CmdLine.pm @@ -1,18 +1,41 @@ package Sietima::CmdLine; use Moo; use Sietima::Policy; +use Sietima::Types qw(SietimaObj); +use Types::Standard qw(HashRef); +use Sietima; use App::Spec; use Sietima::Runner; has sietima => ( is => 'ro', required => 1, + isa => SietimaObj, ); +has extra_spec => ( + is => 'ro', + isa => HashRef, + default => sub { +{} }, +); + +sub BUILDARGS($class,@args) { + my $args = $class->next::method(@args); + $args->{sietima} //= do { + my $traits = delete $args->{traits} // []; + my $constructor_args = delete $args->{args} // {}; + Sietima->with_traits($traits->@*)->new($constructor_args); + }; + return $args; +} + sub run($self) { my $spec_data = $self->sietima->command_line_spec(); - my $app_spec = App::Spec->read($spec_data); + my $app_spec = App::Spec->read({ + $spec_data->%*, + $self->extra_spec->%*, + }); my $runner = Sietima::Runner->new({ spec => $app_spec, diff --git a/lib/Sietima/Role/WithMailStore.pm b/lib/Sietima/Role/WithMailStore.pm index 9d3a8bd..58fa731 100644 --- a/lib/Sietima/Role/WithMailStore.pm +++ b/lib/Sietima/Role/WithMailStore.pm @@ -1,13 +1,14 @@ package Sietima::Role::WithMailStore; use Moo::Role; use Sietima::Policy; -use Sietima::Types qw(MailStore); +use Sietima::Types qw(MailStore MailStoreFromHashRef); use namespace::clean; has mail_store => ( is => 'ro', isa => MailStore, required => 1, + coerce => MailStoreFromHashRef, ); 1; diff --git a/lib/Sietima/Types.pm b/lib/Sietima/Types.pm index 27a3e17..3addf46 100644 --- a/lib/Sietima/Types.pm +++ b/lib/Sietima/Types.pm @@ -5,15 +5,24 @@ use Types::Standard qw(Str HashRef); use namespace::clean; use Type::Library -base, - -declare => qw(Address AddressFromStr + -declare => qw(SietimaObj + Address AddressFromStr EmailMIME Message Subscriber SubscriberFromAddress SubscriberFromStr SubscriberFromHashRef - Transport MailStore); + Transport MailStore MailStoreFromHashRef); + +class_type SietimaObj, { class => 'Sietima' }; class_type EmailMIME, { class => 'Email::MIME' }; role_type Transport, { role => 'Email::Sender::Transport' }; role_type MailStore, { role => 'Sietima::MailStore' }; +declare_coercion MailStoreFromHashRef, + to_type MailStore, from HashRef, + q{ require Module::Runtime; + Module::Runtime::use_module(delete $_->{class})->new($_); + }; + class_type Address, { class => 'Email::Address' }; declare_coercion AddressFromStr, to_type Address, from Str, |