| 1 | package App::Smolder::Report; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | 3 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 3 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | 3 | ▶ | use 5.008; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | 3 | ▶ | use LWP::UserAgent; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | 3 | ▶ | use Getopt::Long; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | 3 | ▶ | use Carp::Clan qw(App::Smolder::Report); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | |||||||||||||||
| 10 | 3 | our $VERSION = '0.04'; | |||||||||||||
| 11 | |||||||||||||||
| 12 | |||||||||||||||
| 13 | ################### | ||||||||||||||
| 14 | # Smolder reporting | ||||||||||||||
| 15 | |||||||||||||||
| 16 | 12 | ▶ | sub report { | ||||||||||||
|
Subroutine
report: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 17 | 12 | my $self = shift; | |||||||||||||
| 18 | |||||||||||||||
| 19 | 12 | $self->{run_as_api} = 1; | |||||||||||||
| 20 | 12 | return $self->_do_report(@_); | |||||||||||||
| 21 | } | ||||||||||||||
| 22 | |||||||||||||||
| 23 | 13 | ▶ | sub _do_report { | ||||||||||||
|
Subroutine
_do_report: called CC=6 SCAR=17.9
| |||||||||||||||
| 24 | 13 | my $self = shift; | |||||||||||||
| 25 | |||||||||||||||
| 26 | 13 | ▶ | $self->_fatal("Required 'server' setting is empty or missing") | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 27 | unless $self->server; | ||||||||||||||
| 28 | 12 | ▶ | $self->_fatal("Required 'project_id' setting is empty or missing") | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 29 | unless $self->project_id; | ||||||||||||||
| 30 | 11 | ▶ | $self->_fatal("Required 'username' setting is empty or missing") | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 31 | unless $self->username; | ||||||||||||||
| 32 | 10 | ▶ | $self->_fatal("Required 'password' setting is empty or missing") | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 33 | unless $self->password; | ||||||||||||||
| 34 | 9 | ▶ | $self->_fatal("You must provide at least one report to upload") | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 35 | unless @_; | ||||||||||||||
| 36 | |||||||||||||||
| 37 | 8 | return $self->_upload_reports(@_); | |||||||||||||
| 38 | } | ||||||||||||||
| 39 | |||||||||||||||
| 40 | 8 | ▶ | sub _upload_reports { | ||||||||||||
|
Subroutine
_upload_reports: called CC=11 SCAR=24.0
| |||||||||||||||
| 41 | 8 | my ($self, @reports) = @_; | |||||||||||||
| 42 | 8 | my $server = $self->server; | |||||||||||||
| 43 | 8 | my $reports_url; | |||||||||||||
| 44 | |||||||||||||||
| 45 | 8 | ▶ | $server = "http://$server" | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 46 | unless $server =~ m/^http/; | ||||||||||||||
| 47 | |||||||||||||||
| 48 | 8 | my $ua = LWP::UserAgent->new; | |||||||||||||
| 49 | 8 | my $url | |||||||||||||
| 50 | = $server | ||||||||||||||
| 51 | . '/app/developer_projects/process_add_report/' | ||||||||||||||
| 52 | . $self->project_id; | ||||||||||||||
| 53 | |||||||||||||||
| 54 | REPORT_FILE: | ||||||||||||||
| 55 | 8 | foreach my $report_file (@reports) { | |||||||||||||
| 56 | 11 | ▶ | $self->_fatal("Could not read report file '$report_file'") | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 57 | unless -r $report_file; | ||||||||||||||
| 58 | |||||||||||||||
| 59 | 10 | ▶ | if ($self->dry_run) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 60 | 1 | $self->_log("Dry run: would POST to $url: $report_file"); | |||||||||||||
| 61 | 1 | next REPORT_FILE; | |||||||||||||
| 62 | } | ||||||||||||||
| 63 | |||||||||||||||
| 64 | 9 | my $response = $ua->post( | |||||||||||||
| 65 | $url, | ||||||||||||||
| 66 | 'Content-Type' => 'form-data', | ||||||||||||||
| 67 | 'Content' => [ | ||||||||||||||
| 68 | username => $self->username, | ||||||||||||||
| 69 | password => $self->password, | ||||||||||||||
| 70 | tags => '', | ||||||||||||||
| 71 | report_file => [$report_file], | ||||||||||||||
| 72 | ], | ||||||||||||||
| 73 | ); | ||||||||||||||
| 74 | |||||||||||||||
| 75 | 9 | ▶ | if ($response->code == 302) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 76 | 8 | ▶ | if (! $reports_url) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 77 | 6 | $reports_url = $response->header('Location'); | |||||||||||||
| 78 | 6 | ▶ | $reports_url = "$server$reports_url" | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 79 | unless $reports_url =~ m/^http/; | ||||||||||||||
| 80 | } | ||||||||||||||
| 81 | |||||||||||||||
| 82 | 8 | $self->_log("Report '$report_file' sent successfully"); | |||||||||||||
| 83 | |||||||||||||||
| 84 | 8 | ▶ | if ($self->delete) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 85 | 2 | ▶ | if (!unlink($report_file)) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 86 | 0 | $self->_log("WARNING: could not delete file $report_file: $!"); | |||||||||||||
| 87 | } | ||||||||||||||
| 88 | } | ||||||||||||||
| 89 | } | ||||||||||||||
| 90 | else { | ||||||||||||||
| 91 | 1 | $self->_fatal( | |||||||||||||
| 92 | "Could not upload report '$report_file'", | ||||||||||||||
| 93 | "HTTP Code: ".$response->code, | ||||||||||||||
| 94 | $response->message, | ||||||||||||||
| 95 | ); | ||||||||||||||
| 96 | } | ||||||||||||||
| 97 | } | ||||||||||||||
| 98 | |||||||||||||||
| 99 | 6 | ▶ | $self->_log("See all reports at $reports_url") if $reports_url; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 100 | 6 | return $reports_url; | |||||||||||||
| 101 | } | ||||||||||||||
| 102 | |||||||||||||||
| 103 | |||||||||||||||
| 104 | ################################### | ||||||||||||||
| 105 | # Configuration loading and merging | ||||||||||||||
| 106 | |||||||||||||||
| 107 | 5 | ▶ | sub _load_configs { | ||||||||||||
|
Subroutine
_load_configs: called CC=4 SCAR=13.9
| |||||||||||||||
| 108 | 5 | my ($self) = @_; | |||||||||||||
| 109 | |||||||||||||||
| 110 | 5 | my $filename = '.smolder.conf'; | |||||||||||||
| 111 | 5 | my @files_to_check = ($filename); | |||||||||||||
| 112 | 5 | ▶ | unshift @files_to_check, "$ENV{HOME}/$filename" if $ENV{HOME}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 113 | push @files_to_check, $ENV{APP_SMOLDER_REPORT_CONF} | ||||||||||||||
| 114 | 5 | ▶ | if $ENV{APP_SMOLDER_REPORT_CONF}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 115 | |||||||||||||||
| 116 | 5 | foreach my $file (@files_to_check) { | |||||||||||||
| 117 | 14 | $self->_merge_cfg_file($file); | |||||||||||||
| 118 | } | ||||||||||||||
| 119 | |||||||||||||||
| 120 | 5 | return; | |||||||||||||
| 121 | } | ||||||||||||||
| 122 | |||||||||||||||
| 123 | 15 | ▶ | sub _merge_cfg_file { | ||||||||||||
|
Subroutine
_merge_cfg_file: called CC=3 SCAR=11.0
| |||||||||||||||
| 124 | 15 | my ($self, $file) = @_; | |||||||||||||
| 125 | |||||||||||||||
| 126 | 15 | my $cfg = $self->_read_cfg_file($file); | |||||||||||||
| 127 | 15 | ▶ | return unless $cfg; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 128 | |||||||||||||||
| 129 | 15 | $self->_merge_cfg_hash($cfg); | |||||||||||||
| 130 | |||||||||||||||
| 131 | 15 | ▶ | if (%$cfg) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 132 | 1 | my @bad_keys = sort keys %$cfg; | |||||||||||||
| 133 | 1 | $self->_fatal("Invalid configuration keys in $file:", @bad_keys); | |||||||||||||
| 134 | } | ||||||||||||||
| 135 | |||||||||||||||
| 136 | 14 | return; | |||||||||||||
| 137 | } | ||||||||||||||
| 138 | |||||||||||||||
| 139 | 17 | ▶ | sub _read_cfg_file { | ||||||||||||
|
Subroutine
_read_cfg_file: called CC=6 SCAR=17.9
| |||||||||||||||
| 140 | 17 | my ($self, $file) = @_; | |||||||||||||
| 141 | 17 | my %cfg; | |||||||||||||
| 142 | 17 | local $_; | |||||||||||||
| 143 | |||||||||||||||
| 144 | 17 | ▶ | open(my $fh, '<', $file) || return; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 145 | 17 | ▶ | while (<$fh>) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 146 | 57 | s/^\s+|\s+$//g; | |||||||||||||
| 147 | 57 | ▶ | next if /^(#.*)?$/; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 148 | |||||||||||||||
| 149 | 44 | ▶ | if (/^(\S+)\s*=\s*(["'])(.*)\2$/) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 150 | 32 | $cfg{$1} = $3; | |||||||||||||
| 151 | } | ||||||||||||||
| 152 | elsif (/^(\S+)\s*=\s*(.+)$/) { | ||||||||||||||
| 153 | 11 | $cfg{$1} = $2; | |||||||||||||
| 154 | } | ||||||||||||||
| 155 | else { | ||||||||||||||
| 156 | 1 | $self->_fatal("Could not parse line $. of $file: $_"); | |||||||||||||
| 157 | } | ||||||||||||||
| 158 | } | ||||||||||||||
| 159 | 16 | close($fh); | |||||||||||||
| 160 | |||||||||||||||
| 161 | 16 | return \%cfg; | |||||||||||||
| 162 | } | ||||||||||||||
| 163 | |||||||||||||||
| 164 | 24 | ▶ | sub _merge_cfg_hash { | ||||||||||||
|
Subroutine
_merge_cfg_hash: called CC=3 SCAR=11.0
| |||||||||||||||
| 165 | 24 | my ($self, $cfg) = @_; | |||||||||||||
| 166 | |||||||||||||||
| 167 | 24 | my @valid_settings = qw{ | |||||||||||||
| 168 | server project_id | ||||||||||||||
| 169 | username password | ||||||||||||||
| 170 | delete | ||||||||||||||
| 171 | }; | ||||||||||||||
| 172 | 24 | foreach my $cfg_key (@valid_settings) { | |||||||||||||
| 173 | 120 | ▶ | next unless exists $cfg->{$cfg_key}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 174 | 49 | $self->{$cfg_key} = delete $cfg->{$cfg_key}; | |||||||||||||
| 175 | } | ||||||||||||||
| 176 | |||||||||||||||
| 177 | 24 | return; | |||||||||||||
| 178 | } | ||||||||||||||
| 179 | |||||||||||||||
| 180 | |||||||||||||||
| 181 | ################################## | ||||||||||||||
| 182 | # Deal with command line arguments | ||||||||||||||
| 183 | |||||||||||||||
| 184 | 2 | ▶ | sub process_args { | ||||||||||||
|
Subroutine
process_args: called CC=9 SCAR=22.1
Pod
undocumented
| |||||||||||||||
| 185 | 2 | my ($self) = @_; | |||||||||||||
| 186 | |||||||||||||||
| 187 | 2 | my ($username, $password, $server, $project_id, $dry_run, $delete, $quiet); | |||||||||||||
| 188 | 2 | my $ok = GetOptions( | |||||||||||||
| 189 | "username=s" => \$username, | ||||||||||||||
| 190 | "password=s" => \$password, | ||||||||||||||
| 191 | "server=s" => \$server, | ||||||||||||||
| 192 | "project-id=i" => \$project_id, | ||||||||||||||
| 193 | "dry-run|n" => \$dry_run, | ||||||||||||||
| 194 | "quiet" => \$quiet, | ||||||||||||||
| 195 | "delete" => \$delete, | ||||||||||||||
| 196 | ); | ||||||||||||||
| 197 | 2 | ▶ | exit(2) unless $ok; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 198 | |||||||||||||||
| 199 | 2 | $self->_load_configs; | |||||||||||||
| 200 | |||||||||||||||
| 201 | 2 | ▶ | $self->{username} = $username if defined $username; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 202 | 2 | ▶ | $self->{password} = $password if defined $password; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 203 | 2 | ▶ | $self->{server} = $server if defined $server; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 204 | 2 | ▶ | $self->{project_id} = $project_id if defined $project_id; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 205 | 2 | ▶ | $self->{dry_run} = $dry_run if defined $dry_run; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 206 | 2 | ▶ | $self->{quiet} = $quiet if defined $quiet; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 207 | 2 | ▶ | $self->{delete} = $delete if defined $delete; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 208 | |||||||||||||||
| 209 | 2 | return; | |||||||||||||
| 210 | } | ||||||||||||||
| 211 | |||||||||||||||
| 212 | 1 | ▶ | sub run { | ||||||||||||
|
Subroutine
run: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 213 | 1 | my $self = shift; | |||||||||||||
| 214 | |||||||||||||||
| 215 | 1 | $self->{run_as_api} = 0; | |||||||||||||
| 216 | 1 | return $self->_do_report(@_); | |||||||||||||
| 217 | } | ||||||||||||||
| 218 | |||||||||||||||
| 219 | |||||||||||||||
| 220 | ####### | ||||||||||||||
| 221 | # Utils | ||||||||||||||
| 222 | |||||||||||||||
| 223 | 9 | ▶ | sub _fatal { | ||||||||||||
|
Subroutine
_fatal: called CC=3 SCAR=12.0
| |||||||||||||||
| 224 | 9 | my ($self, $mesg, @more) = @_; | |||||||||||||
| 225 | |||||||||||||||
| 226 | 9 | $mesg = "FATAL: $mesg\n"; | |||||||||||||
| 227 | 9 | foreach my $line (@more) { | |||||||||||||
| 228 | 3 | $mesg .= " $line\n"; | |||||||||||||
| 229 | } | ||||||||||||||
| 230 | |||||||||||||||
| 231 | 9 | ▶ | croak($mesg) if $self->run_as_api; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 232 | |||||||||||||||
| 233 | 0 | print $mesg; | |||||||||||||
| 234 | 0 | exit(1); | |||||||||||||
| 235 | } | ||||||||||||||
| 236 | |||||||||||||||
| 237 | 14 | ▶ | sub _log { | ||||||||||||
|
Subroutine
_log: called CC=3 SCAR=11.0
| |||||||||||||||
| 238 | 14 | my ($self, $mesg) = @_; | |||||||||||||
| 239 | 14 | ▶ | return if $self->run_as_api; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 240 | 2 | ▶ | return if $self->quiet; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 241 | |||||||||||||||
| 242 | 2 | print "$mesg\n"; | |||||||||||||
| 243 | |||||||||||||||
| 244 | 2 | return; | |||||||||||||
| 245 | } | ||||||||||||||
| 246 | |||||||||||||||
| 247 | |||||||||||||||
| 248 | ########################### | ||||||||||||||
| 249 | # Constructor and accessors | ||||||||||||||
| 250 | # boring stuff | ||||||||||||||
| 251 | |||||||||||||||
| 252 | 7 | ▶ | sub new { | ||||||||||||
|
Subroutine
new: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||
| 253 | 7 | my $class = shift; | |||||||||||||
| 254 | 7 | my $self = bless {}, $class; | |||||||||||||
| 255 | |||||||||||||||
| 256 | 7 | my %args; | |||||||||||||
| 257 | 7 | ▶ | if (ref($_[0])) { %args = %{$_[0]} } | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 258 | 5 | else { %args = @_ } | |||||||||||||
| 259 | |||||||||||||||
| 260 | 7 | ▶ | $self->_load_configs if delete $args{load_config}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 261 | |||||||||||||||
| 262 | 7 | ▶ | while (my ($k, $v) = each %args) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 263 | 2 | $self->{$k} = $v; | |||||||||||||
| 264 | } | ||||||||||||||
| 265 | |||||||||||||||
| 266 | 7 | return $self; | |||||||||||||
| 267 | } | ||||||||||||||
| 268 | |||||||||||||||
| 269 | 13 | ▶ | sub dry_run { return $_[0]{dry_run} } | ||||||||||||
|
Subroutine
dry_run: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 270 | 4 | ▶ | sub quiet { return $_[0]{quiet} } | ||||||||||||
|
Subroutine
quiet: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 271 | 28 | ▶ | sub username { return $_[0]{username} } | ||||||||||||
|
Subroutine
username: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 272 | 27 | ▶ | sub password { return $_[0]{password} } | ||||||||||||
|
Subroutine
password: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 273 | 10 | ▶ | sub delete { return $_[0]{delete} } | ||||||||||||
|
Subroutine
delete: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 274 | 28 | ▶ | sub project_id { return $_[0]{project_id} } | ||||||||||||
|
Subroutine
project_id: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 275 | 29 | ▶ | sub server { return $_[0]{server} } | ||||||||||||
|
Subroutine
server: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 276 | 24 | ▶ | sub run_as_api { return $_[0]{run_as_api} } | ||||||||||||
|
Subroutine
run_as_api: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 277 | |||||||||||||||
| 278 | __END__ | ||||||||||||||