error handling and tests therefore
[scpubgit/App-EzPz.git] / lib / Email / EzPz / ListCore.pm
CommitLineData
c5f4b6d4 1package Email::EzPz::ListCore;
2
3use Moo::Role;
4use IO::All;
0a0e9549 5use Capture::Tiny qw(capture_merged);
c5f4b6d4 6use IPC::System::Simple qw(run capture);
7
8has list_dir => (is => 'ro', required => 1);
9
10has ezmlm_bindir => (is => 'ro', required => 1);
11
12requires 'sublist_type';
13
14sub _command_path {
15 my ($self, $command) = @_;
b1194426 16 if (ref(my $bindir = $self->ezmlm_bindir) eq 'ARRAY') { # prefix list
17 return (@{$bindir}, "ezmlm-${command}");
18 } else {
19 return io->dir($bindir)->catfile("ezmlm-${command}");
20 }
c5f4b6d4 21}
22
23sub _command_args {
24 my ($self, $command, @args) = @_;
25 return (
26 $self->_command_path($command),
27 $self->list_dir,
28 $self->sublist_type,
29 @args
30 );
31}
32
33sub _call_command {
34 my ($self, @cmd) = @_;
0a0e9549 35 my $ok;
36 my $out = capture_merged {
37 $ok = eval { run $self->_command_args(@cmd); 1 };
38 };
39 die "Command failed: $out\n" unless $ok;
40 return;
c5f4b6d4 41}
42
43sub _capture_command {
44 my ($self, @cmd) = @_;
45 map { chomp; $_ } capture $self->_command_args(@cmd);
46}
47
48sub add_member {
49 my ($self, $member) = @_;
50 $self->_call_command(sub => $member);
51 return $member;
52}
53
54sub remove_member {
55 my ($self, $member) = @_;
56 $self->_call_command(unsub => $member);
57 return $member;
58}
59
60sub members {
61 my ($self) = @_;
62 $self->_capture_command('list');
63}
64
651;