| 1 | package Tapper::CLI::Resource; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | 72 | our $AUTHORITY = 'cpan:TAPPER'; | |||||||||||||||||||||||||||||||||||||||||
| 3 | 72 | $Tapper::CLI::Resource::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 Text::Table; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||
| 10 | sub resource_new | ||||||||||||||||||||||||||||||||||||||||||
| 11 | 0 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
resource_new: not called CC=6 SCAR=37.4
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | 0 | my ($c) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 13 | 0 | $c->getopt( 'name=s', 'comment=s', 'active', 'help|?'); | |||||||||||||||||||||||||||||||||||||||||
| 14 | 0 | ▶ | if ( $c->options->{help} or not $c->options->{name}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$c->options->{"help"} or not $c->options->{"name"}
MC/DC0%0/2
$c->options->{"help"} or not $c->options->{"name"}
$c->options->{"help"}
not $c->options->{"name"}
Truth table0%0/3
A or not(B)
A= $c->options->{"help"}B= not $c->options->{"name"}
| |||||||||||||||||||||||||||||||||||||||||||
| 15 | 0 | ▶ | say STDERR "Resource name missing!" unless $c->options->{name}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 16 | 0 | say STDERR "$0 resource-new --name=s [ --comment=s ] [ --active ] [ --help ]"; | |||||||||||||||||||||||||||||||||||||||||
| 17 | 0 | say STDERR " --name Name of the new resource"; | |||||||||||||||||||||||||||||||||||||||||
| 18 | 0 | say STDERR " --comment Comment for the new resource"; | |||||||||||||||||||||||||||||||||||||||||
| 19 | 0 | say STDERR " --active Make host active; without it resource will be initially deactivated"; | |||||||||||||||||||||||||||||||||||||||||
| 20 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 21 | } | ||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||
| 23 | 0 | require Tapper::Model; | |||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||
| 25 | my $resource = { | ||||||||||||||||||||||||||||||||||||||||||
| 26 | name => $c->options->{name}, | ||||||||||||||||||||||||||||||||||||||||||
| 27 | comment => $c->options->{comment}, | ||||||||||||||||||||||||||||||||||||||||||
| 28 | 0 | ▶ | active => $c->options->{active} ? 1 : 0, | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 29 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||
| 31 | 0 | my $newresource = Tapper::Model::model('TestrunDB')->resultset('Resource')->create($resource); | |||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||
| 33 | 0 | ▶ | if($newresource) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 34 | 0 | say "Resource created."; | |||||||||||||||||||||||||||||||||||||||||
| 35 | 0 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 36 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 37 | 0 | say STDERR "Error creating resource."; | |||||||||||||||||||||||||||||||||||||||||
| 38 | 0 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||
| 42 | sub resource_update | ||||||||||||||||||||||||||||||||||||||||||
| 43 | 0 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
resource_update: not called CC=12 SCAR=50.5
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 44 | 0 | my ($c) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 45 | 0 | $c->getopt( 'id|i=i', 'selectbyname=s', 'name|n=s', 'comment|c=s', 'active|a=i', 'help|?' ); | |||||||||||||||||||||||||||||||||||||||||
| 46 | 0 | ▶ | if ( $c->options->{help} or not defined $c->options->{id} and not defined $c->options->{selectbyname} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
$c->options->{"help"} or not defined $c->options->{"id"} || defined $c->options->{"selectbyname"}
defined $c->options->{'id'} || defined $c->options->{'selectbyname'}
MC/DC0%0/3
$c->options->{"help"} or not defined $c->options->{"id"} || defined $c->options->{"selectbyname"}
$c->options->{"help"}
defined $c->options->{'id'}
defined $c->options->{'selectbyname'}
Truth table0%0/4
A or not(B || C)
A= $c->options->{"help"}B= defined $c->options->{'id'}C= defined $c->options->{'selectbyname'}
| |||||||||||||||||||||||||||||||||||||||||||
| 47 | 0 | ▶ | say STDERR "Resource name or id missing!" unless $c->options->{id} or $c->options->{selectbyname}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$c->options->{"id"} or $c->options->{"selectbyname"}
MC/DC0%0/2
$c->options->{"id"} or $c->options->{"selectbyname"}
$c->options->{"id"}
$c->options->{"selectbyname"}
Truth table0%0/3
A or B
A= $c->options->{"id"}B= $c->options->{"selectbyname"}
| |||||||||||||||||||||||||||||||||||||||||||
| 48 | 0 | say STDERR "$0 resource-update [ --id | --selectbyname ] [ --name=s ] [ --comment=s ] [ --active=i ] [ --help ]"; | |||||||||||||||||||||||||||||||||||||||||
| 49 | 0 | say STDERR " --id Update resource with this id. Either this or selectbyname is required."; | |||||||||||||||||||||||||||||||||||||||||
| 50 | 0 | say STDERR " --selectbyname Update resource with this name. Either this or id is required."; | |||||||||||||||||||||||||||||||||||||||||
| 51 | 0 | say STDERR " --name New name for the resource"; | |||||||||||||||||||||||||||||||||||||||||
| 52 | 0 | say STDERR " --comment New comment for the resource"; | |||||||||||||||||||||||||||||||||||||||||
| 53 | 0 | say STDERR " --active Enable or disable resource"; | |||||||||||||||||||||||||||||||||||||||||
| 54 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||
| 57 | 0 | require Tapper::Model; | |||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||
| 59 | 0 | my $resource; | |||||||||||||||||||||||||||||||||||||||||
| 60 | 0 | ▶ | if ( $c->options->{id} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 61 | 0 | $resource = Tapper::Model::model('TestrunDB')->resultset('Resource')->find($c->options->{id}); | |||||||||||||||||||||||||||||||||||||||||
| 62 | } elsif ( $c->options->{selectbyname} ) { | ||||||||||||||||||||||||||||||||||||||||||
| 63 | $resource = Tapper::Model::model('TestrunDB')->resultset('Resource')->search({ | ||||||||||||||||||||||||||||||||||||||||||
| 64 | name => $c->options->{selectbyname}, | ||||||||||||||||||||||||||||||||||||||||||
| 65 | 0 | })->first; | |||||||||||||||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||
| 68 | 0 | ▶ | unless (defined($resource)) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 69 | { | ||||||||||||||||||||||||||||||||||||||||||
| 70 | 0 | say STDERR "Resource not found."; | |||||||||||||||||||||||||||||||||||||||||
| 71 | 0 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||
| 74 | 0 | ▶ | $resource->name($c->options->{name}) if defined $c->options->{name}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 75 | 0 | ▶ | $resource->comment($c->options->{comment}) if defined $c->options->{comment}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 76 | 0 | ▶ | $resource->active($c->options->{active}) if defined $c->options->{active}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||
| 78 | 0 | $resource->update; | |||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||
| 80 | 0 | say "Resource updated."; | |||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||
| 82 | 0 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||
| 85 | 0 | ▶ | sub resource_list { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
resource_list: not called CC=13 SCAR=52.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 86 | 0 | my ($c) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 87 | 0 | $c->getopt( 'name=s@', 'id=i@', 'active', 'free', 'json', 'verbose|v', 'help|?' ); | |||||||||||||||||||||||||||||||||||||||||
| 88 | 0 | ▶ | if ( $c->options->{help} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 89 | 0 | say STDERR "$0 resource-list [ --name=s ... ] [ --id=i ... ] [ --free ] [ --active ] [ --verbose ] [ --help ]"; | |||||||||||||||||||||||||||||||||||||||||
| 90 | 0 | say STDERR " --id Only show resources with this id. Can be specified multiple times"; | |||||||||||||||||||||||||||||||||||||||||
| 91 | 0 | say STDERR " --name Only show resources with this name. Can be specified multiple times"; | |||||||||||||||||||||||||||||||||||||||||
| 92 | 0 | say STDERR " --active Only show resources that are active"; | |||||||||||||||||||||||||||||||||||||||||
| 93 | 0 | say STDERR " --free Only show resources that are free"; | |||||||||||||||||||||||||||||||||||||||||
| 94 | 0 | say STDERR " --verbose Show extended information"; | |||||||||||||||||||||||||||||||||||||||||
| 95 | 0 | say STDERR " --json Give results as json"; | |||||||||||||||||||||||||||||||||||||||||
| 96 | 0 | say STDERR " --help Show this help"; | |||||||||||||||||||||||||||||||||||||||||
| 97 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||
| 100 | 0 | require Tapper::Model; | |||||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||||||
| 102 | 0 | my $resources = Tapper::Model::model('TestrunDB')->resultset('Resource'); | |||||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||||||
| 104 | 0 | ▶ | if ($c->options->{name}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 105 | 0 | $resources = $resources->search({ name => $c->options->{name} }); | |||||||||||||||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||
| 108 | 0 | ▶ | if ($c->options->{id}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 109 | 0 | $resources = $resources->search({ id => $c->options->{id} }); | |||||||||||||||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||
| 112 | 0 | ▶ | if ($c->options->{active}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 113 | 0 | $resources = $resources->search({ active => 1 }); | |||||||||||||||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||
| 116 | 0 | ▶ | if ($c->options->{free}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 117 | 0 | $resources = $resources->search({ used_by_scheduling_id => undef }); | |||||||||||||||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||||||||||
| 120 | 0 | my $table; | |||||||||||||||||||||||||||||||||||||||||
| 121 | 0 | my @entries = $resources->all; | |||||||||||||||||||||||||||||||||||||||||
| 122 | 0 | ▶ | if ($c->options->{json}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 123 | 0 | require JSON; | |||||||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||||||
| 125 | 0 | my @json_resources; | |||||||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||||||||
| 127 | push @json_resources, { | ||||||||||||||||||||||||||||||||||||||||||
| 128 | id => $_->id, | ||||||||||||||||||||||||||||||||||||||||||
| 129 | name => $_->name, | ||||||||||||||||||||||||||||||||||||||||||
| 130 | comment => $_->comment, | ||||||||||||||||||||||||||||||||||||||||||
| 131 | active => $_->active ? \1 : \0, | ||||||||||||||||||||||||||||||||||||||||||
| 132 | in_use => (defined $_->used_by_scheduling_id) ? \1 : \0, | ||||||||||||||||||||||||||||||||||||||||||
| 133 | 0 | ▶ | } foreach (@entries); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 134 | 0 | print JSON::encode_json(\@json_resources); | |||||||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||||||||
| 136 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 137 | } elsif ($c->options->{verbose}) { | ||||||||||||||||||||||||||||||||||||||||||
| 138 | 0 | $table = Text::Table->new( "ID", "Name", "Comment", "Active", "Status" ); | |||||||||||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||||||||
| 141 | 0 | ▶ | my @table_data = map { [$_->id, $_->name, $_->comment, $_->active ? "Y" : "N", | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 142 | (defined $_->used_by_scheduling_id) ? "In Use" : "Free"] } @entries; | ||||||||||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||||||||||
| 144 | 0 | $table->load(@table_data); | |||||||||||||||||||||||||||||||||||||||||
| 145 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 146 | 0 | $table = Text::Table->new( "ID", "Name" ); | |||||||||||||||||||||||||||||||||||||||||
| 147 | 0 | my @table_data = map { [$_->id, $_->name] } @entries; | |||||||||||||||||||||||||||||||||||||||||
| 148 | 0 | $table->load(@table_data); | |||||||||||||||||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||||||||||||
| 151 | 0 | print $table; | |||||||||||||||||||||||||||||||||||||||||
| 152 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||||||||||
| 155 | sub setup | ||||||||||||||||||||||||||||||||||||||||||
| 156 | 71 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
setup: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 157 | 71 | my ($c) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||||||||||||
| 159 | 71 | $c->register('resource-list', \&resource_list, 'List resources'); | |||||||||||||||||||||||||||||||||||||||||
| 160 | 71 | $c->register('resource-new', \&resource_new, 'Create new resource'); | |||||||||||||||||||||||||||||||||||||||||
| 161 | 71 | $c->register('resource-update', \&resource_update, 'Update resource'); | |||||||||||||||||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||||||||||||
| 164 | 72 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||||||||||||
| 166 | __END__ | ||||||||||||||||||||||||||||||||||||||||||