blib/lib/Tapper/CLI/User.pm

filter: Statementstmt 19.2% 26 / 135 Branchbran 4.1% 1 / 24 Conditioncond 0.0% 0 / 20 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 37.5% 6 / 16 Podpod 100% 7 / 7 totaltotal 18.6% 40 / 215 CC 23 SCAR 31.0
CC
23
Cov
15%
contactadd
42.0
userdel
42.0
get_contacts
30.0
CRAP 22.2
« blib/lib/Tapper/CLI/Queue.pm ↑ Summary blib/lib/Tapper/CLI/Schema.pm »
1 package Tapper::CLI::User;
2 72 our $AUTHORITY = 'cpan:TAPPER';
3 72 $Tapper::CLI::User::VERSION = '5.0.8';
4 72 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 72 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 72 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 72 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 72 ▶use UNIVERSAL;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11  
12 sub get_contacts
13 0 ▶{
Subroutine get_contacts: not called CC=5 SCAR=34.0
Pod documented
14 0 my ($contacts) = @_;
15 0 my @contacts;
16  
17 0 require YAML::XS;
18  
19 0 ▶ foreach my $contact (@{$contacts || []}) {
Condition0%0/2
$contacts || []
l!l
00
MC/DC0%0/1
$contacts || []
$contacts
Truth table0%0/2
A
A= $contacts
Aresult
11
00
20 0 ▶ if ($contact !~ m/\n/ and -e $contact) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $contact =~ /\n/ and -e $contact) { } 0 0 0
Condition0%0/3
not $contact =~ /\n/ and -e $contact
!ll&&!rl&&r
000
MC/DC0%0/2
not $contact =~ /\n/ and -e $contact
not $contact =~ /\n/ -e $contact
Truth table0%0/3
not(A) and B
A= not $contact =~ /\n/B= -e $contact
ABresult
0X0
100
111
21 0 ▶ try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
22 0 my @newcontacts = YAML::XS::LoadFile($contact);
23 0 push @contacts, @newcontacts;
24 0 ▶ } catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
25 0 say STDERR "Can not load file '$contact'. Will ignore it. Error message was: $_";
26 }
27 0 } else {
28 0 ▶ try{
Subroutine __ANON__: not called CC=1 SCAR=6.9
29 0 my @newcontacts = YAML::XS::Load($contact);
30 0 push @contacts, @newcontacts;
31 0 ▶ } catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
32 0 say STDERR "I can not load '$contact' as YAML and there is no file with that name. Will ignore it. Error message was: $_";
33 }
34 0 }
35 }
36 0 return @contacts;
37 }
38  
39  
40  
41  
42 sub usernew
43 0 ▶{
Subroutine usernew: not called CC=4 SCAR=30.0
Pod documented
44 0 my ($c) = @_;
45 0 $c->getopt( 'contact|c=s@', 'login|l=s', 'name|n=s', 'quiet|q', 'default|d','help|?' );
46  
47 0 ▶ if ($c->options->{help} or not %{$c->options}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->options->{"help"} or not %{$c->options;}) 0 0 0
Condition0%0/3
$c->options->{"help"} or not %{$c->options;}
l!l&&r!l&&!r
000
MC/DC0%0/2
$c->options->{"help"} or not %{$c->options;}
$c->options->{"help"} not %{$c->options;}
Truth table0%0/3
A or not(B)
A= $c->options->{"help"}B= not %{$c->options;}
ABresult
1X1
011
000
48 0 say STDERR "Usage: $0 user-add [--default|d] [ --login=login ] [ --name=name ] [ --contact='type:type\\naddress:address' | --contact=filename ]*";
49 0 say STDERR "\n Optional arguments:";
50 0 say STDERR " --login login name for the user (default is $ENV{USER})";
51 0 say STDERR " --name real name of the user (try to get from system if empty)";
52 0 say STDERR " --contact contact information in YAML or name of a file containing this information (can be given multiple times)";
53 0 say STDERR " --quiet Stay silent when adding user succeeded";
54 0 say STDERR " --default Use default values for all parameters";
55 0 say STDERR " --help print this help message and exit";
56 0 exit -1;
57 }
58  
59 0 my @contacts = get_contacts($c->options->{contact});
60  
61 0 my $data;
62 0 $data = { login => $c->options->{login}, name => $c->options->{name} , contacts => \@contacts};
63  
64 0 require YAML::XS;
65 0 require Tapper::Cmd::User;
66  
67 0 my $cmd = Tapper::Cmd::User->new();
68 0 my $id = $cmd->add($data);
69 0 ▶ if (not $c->options->{quiet}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($c->options->{"quiet"}) 0 0 0
70 0 my @users = $cmd->list({id => $id});
71 0 print YAML::XS::Dump($users[0]);
72 }
73 0 return;
74 }
75  
76  
77 sub contactadd
78 0 ▶{
Subroutine contactadd: not called CC=6 SCAR=37.4
Pod documented
79 0 my ($c) = @_;
80 0 $c->getopt( 'contact|c=s@', 'login|l=s', 'quiet|q', 'help|?' );
81  
82 0 ▶ if ($c->options->{help} or not $c->options->{contact} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->options->{"help"} or not $c->options->{"contact"}) 0 0 0
Condition0%0/3
$c->options->{"help"} or not $c->options->{"contact"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$c->options->{"help"} or not $c->options->{"contact"}
$c->options->{"help"} not $c->options->{"contact"}
Truth table0%0/3
A or not(B)
A= $c->options->{"help"}B= not $c->options->{"contact"}
ABresult
1X1
011
000
83 0 say STDERR "Usage: $0 contact-add [ --login=login ] [ --contact=YAML | --contact=filename ]* [ --quiet ]";
84 0 say STDERR "\n Optional arguments:";
85 0 say STDERR " --login login name for the user (default is $ENV{USER})";
86 0 say STDERR " --contact contact information in YAML or name of a file containing this information (can be given multiple times)";
87 0 say STDERR " --quiet Stay silent when adding user succeeded";
88 0 say STDERR " --help print this help message and exit";
89 0 exit -1;
90 }
91  
92 0 ▶ my $login = $c->options->{login} || $ENV{USER};
Condition0%0/3
$c->options->{'login'} || $ENV{'USER'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$c->options->{'login'} || $ENV{'USER'}
$c->options->{'login'} $ENV{'USER'}
Truth table0%0/3
A || B
A= $c->options->{'login'}B= $ENV{'USER'}
ABresult
1X1
011
000
93  
94 0 require Tapper::Cmd::User;
95 0 my $cmd = Tapper::Cmd::User->new();
96  
97 0 my @contacts = get_contacts($c->options->{contact});
98 0 foreach my $contact (@contacts) {
99 0 my $id = $cmd->contact_add($login, $contact);
100 }
101  
102 0 require YAML::XS;
103 0 ▶ if (not $c->options->{quiet}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($c->options->{"quiet"}) 0 0 0
104 0 my @users = $cmd->list({login => $login});
105 0 print YAML::XS::Dump($users[0]);
106 }
107  
108 0 return;
109 }
110  
111  
112 sub userlist
113 0 ▶{
Subroutine userlist: not called CC=2 SCAR=17.9
Pod documented
114 0 my ($c) = @_;
115  
116 0 require YAML::XS;
117 0 require Tapper::Cmd::User;
118  
119 0 my $cmd = Tapper::Cmd::User->new();
120 0 my @users = $cmd->list();
121 0 foreach my $user (@users) {
122 0 print YAML::XS::Dump($user);
123 }
124 0 return;
125 }
126  
127  
128 sub userupdate
129 0 ▶{
Subroutine userupdate: not called CC=4 SCAR=30.0
Pod documented
130 0 my ($c) = @_;
131 0 $c->getopt( 'file|f=s', 'id|i=i','quiet|q', 'help|?' );
132  
133 0 ▶ if (not %{$c->options} or $c->options->{help} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not %{$c->options;} or $c->options->{"help"}) 0 0 0
Condition0%0/3
not %{$c->options;} or $c->options->{"help"}
l!l&&r!l&&!r
000
MC/DC0%0/2
not %{$c->options;} or $c->options->{"help"}
not %{$c->options;} $c->options->{"help"}
Truth table0%0/3
not(A) or B
A= not %{$c->options;}B= $c->options->{"help"}
ABresult
1X1
011
000
134 0 say STDERR "Usage: $0 user-update --file=filename --id=id [ --quiet ]";
135 0 say STDERR "\n\ Required Arguments:";
136 0 say STDERR " --file name of file containing the user subscriptions in YAML";
137 0 say STDERR " --id id of the user subscriptions";
138 0 say STDERR "\n Optional arguments:";
139 0 say STDERR " --quiet stay silent when updating succeeded";
140 0 say STDERR " --help print this help message and exit";
141 0 exit -1;
142 }
143  
144 0 require Tapper::Cmd::User;
145 0 my $cmd = Tapper::Cmd::User->new();
146  
147 0 require YAML::XS;
148 0 my $subscription = YAML::XS::LoadFile($c->options->{file});
149 0 my $id = $cmd->update($c->options->{id}, $subscription);
150  
151 0 ▶ return "The user subscription was updated: $id" unless $c->options->{quiet};
Branch: 1 branch
BranchTrueFalseTotal
unless $c->options->{"quiet"} 0 0 0
152 0 return;
153 }
154  
155  
156 sub userdel
157 0 ▶{
Subroutine userdel: not called CC=6 SCAR=37.4
Pod documented
158 0 my ($c) = @_;
159 0 $c->getopt( 'id|i=i', 'login|l=s', 'quiet|q', 'help|?' );
160  
161 0 ▶ if (not %{$c->options} or $c->options->{help} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not %{$c->options;} or $c->options->{"help"}) 0 0 0
Condition0%0/3
not %{$c->options;} or $c->options->{"help"}
l!l&&r!l&&!r
000
MC/DC0%0/2
not %{$c->options;} or $c->options->{"help"}
not %{$c->options;} $c->options->{"help"}
Truth table0%0/3
not(A) or B
A= not %{$c->options;}B= $c->options->{"help"}
ABresult
1X1
011
000
162 0 say STDERR "Usage: $0 user-del --user=login | --id=i [ --quiet ]";
163 0 say STDERR "\n\ Required Arguments (one of):";
164 0 say STDERR " --login login name of the user to delete";
165 0 say STDERR " --id database id of the user to delete. Note: This is not the UNIX id!";
166 0 say STDERR "\n Optional arguments:";
167 0 say STDERR " --quiet Stay silent when deleting succeeded";
168 0 say STDERR " --help print this help message and exit";
169 0 exit -1;
170 }
171  
172 0 require Tapper::Cmd::User;
173 0 my $cmd = Tapper::Cmd::User->new();
174  
175 0 ▶ if ($c->options->{login}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->options->{'login'}) { } 0 0 0
176 0 ▶ if ( $c->options->{id}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->options->{"id"}) 0 0 0
177 0 return "Please choose either login or id. Both are present and I don't know which one to use";
178 }
179 0 $cmd->del($c->options->{login});
180 } else {
181 0 $cmd->del($c->options->{id});
182 }
183  
184 0 ▶ return "The user was deleted." unless $c->options->{quiet};
Branch: 1 branch
BranchTrueFalseTotal
unless $c->options->{"quiet"} 0 0 0
185 0 return;
186 }
187  
188  
189  
190  
191 sub setup
192 71 ▶{
Subroutine setup: called CC=2 SCAR=7.0
Pod documented
193 71 my ($c) = @_;
194 71 $c->register('user-add', \&usernew, 'Register a new user');
195 71 $c->register('user-new', \&usernew, 'Alias for user-add');
196 71 $c->register('user-list', \&userlist, 'Show all users');
197 71 $c->register('user-update', \&userupdate, 'Update an existing user');
198 71 $c->register('user-del', \&userdel, 'Delete an existing user');
199 71 $c->register('contact-add', \&contactadd, 'Add contact information to an existing user');
200 71 ▶ if ($c->can('group_commands')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->can("group_commands")) 0 71 71
201 0 $c->group_commands('User commands', 'user-add', 'user-new', 'user-list', 'user-update', 'user-del', 'contact-add');
202 }
203 71 return;
204 }
205  
206 72 1; # End of Tapper::CLI
207  
208 __END__
« blib/lib/Tapper/CLI/Queue.pm ↑ Summary blib/lib/Tapper/CLI/Schema.pm »