| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Tapper::CLI::API::Command::upload; | 
| 2 |  |  |  |  |  |  | our $AUTHORITY = 'cpan:TAPPER'; | 
| 3 |  |  |  |  |  |  | $Tapper::CLI::API::Command::upload::VERSION = '5.0.6'; | 
| 4 | 1 |  |  | 1 |  | 1300 | use 5.010; | 
|  | 1 |  |  |  |  | 4 |  | 
| 5 |  |  |  |  |  |  |  | 
| 6 | 1 |  |  | 1 |  | 7 | use strict; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 23 |  | 
| 7 | 1 |  |  | 1 |  | 7 | use warnings; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 28 |  | 
| 8 |  |  |  |  |  |  |  | 
| 9 | 1 |  |  | 1 |  | 6 | use parent 'App::Cmd::Command'; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 8 |  | 
| 10 |  |  |  |  |  |  |  | 
| 11 | 1 |  |  | 1 |  | 84 | use IO::Socket; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 9 |  | 
| 12 | 1 |  |  | 1 |  | 677 | use Tapper::Config; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 26 |  | 
| 13 | 1 |  |  | 1 |  | 6 | use File::Slurp 'slurp'; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 71 |  | 
| 14 | 1 |  |  | 1 |  | 8 | use Data::Dumper; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 60 |  | 
| 15 | 1 |  |  | 1 |  | 7 | use Moose; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 6 |  | 
| 16 |  |  |  |  |  |  |  | 
| 17 |  |  |  |  |  |  | sub abstract { | 
| 18 | 0 |  |  | 0 | 1 |  | 'Upload and attach a file to a report' | 
| 19 |  |  |  |  |  |  | } | 
| 20 |  |  |  |  |  |  |  | 
| 21 |  |  |  |  |  |  | sub opt_spec { | 
| 22 |  |  |  |  |  |  | return ( | 
| 23 | 0 |  |  | 0 | 1 |  | [ "verbose",         "some more informational output" ], | 
| 24 |  |  |  |  |  |  | [ "reportid=s",      "INT; the testrun id to change", ], | 
| 25 |  |  |  |  |  |  | [ "file=s",          "STRING; the file to upload, use '-' for STDIN", ], | 
| 26 |  |  |  |  |  |  | [ "filename=s",      "STRING; alternate file name, especially when reading from STDIN", ], | 
| 27 |  |  |  |  |  |  | [ "reportserver=s",  "STRING; use this host for upload", ], | 
| 28 |  |  |  |  |  |  | [ "reportport=s",    "STRING; use this port for upload", ], | 
| 29 |  |  |  |  |  |  | [ "contenttype=s",   "STRING; content-type, default 'plain', use 'application/octet-stream' for binaries", ], | 
| 30 |  |  |  |  |  |  | ); | 
| 31 |  |  |  |  |  |  | } | 
| 32 |  |  |  |  |  |  |  | 
| 33 |  |  |  |  |  |  | sub usage_desc | 
| 34 |  |  |  |  |  |  | { | 
| 35 | 0 |  |  | 0 | 1 |  | my $allowed_opts = join ' ', map { '--'.$_ } _allowed_opts(); | 
|  | 0 |  |  |  |  |  |  | 
| 36 | 0 |  |  |  |  |  | "tapper-api upload --reportid=s --file=s [ --contenttype=s ]"; | 
| 37 |  |  |  |  |  |  | } | 
| 38 |  |  |  |  |  |  |  | 
| 39 |  |  |  |  |  |  | sub _allowed_opts | 
| 40 |  |  |  |  |  |  | { | 
| 41 | 0 |  |  | 0 |  |  | my @allowed_opts = map { $_->[0] } opt_spec(); | 
|  | 0 |  |  |  |  |  |  | 
| 42 |  |  |  |  |  |  | } | 
| 43 |  |  |  |  |  |  |  | 
| 44 |  |  |  |  |  |  | sub validate_args | 
| 45 |  |  |  |  |  |  | { | 
| 46 | 0 |  |  | 0 | 1 |  | my ($self, $opt, $args) = @_; | 
| 47 |  |  |  |  |  |  |  | 
| 48 |  |  |  |  |  |  | # -- file constraints -- | 
| 49 | 0 |  |  |  |  |  | my $file    = $opt->{file}; | 
| 50 | 0 |  | 0 |  |  |  | my $file_ok = $file && ($file eq '-' || -r $file); | 
| 51 | 0 | 0 |  |  |  |  | say "Missing argument --file"                                  unless $file; | 
| 52 | 0 | 0 | 0 |  |  |  | say "Error: file '".($file//"")."' must be readable or STDIN." unless $file_ok; | 
| 53 |  |  |  |  |  |  |  | 
| 54 |  |  |  |  |  |  | # -- report constraints -- | 
| 55 | 0 |  |  |  |  |  | my $reportid  = $opt->{reportid}; | 
| 56 | 0 |  | 0 |  |  |  | my $report_ok = $reportid && $reportid =~ /^\d+$/; | 
| 57 | 0 | 0 |  |  |  |  | say "Missing argument --reportid"                              unless $reportid; | 
| 58 | 0 | 0 | 0 |  |  |  | say "Error: Strange target report (id '".($reportid//"")."')." unless $report_ok; | 
| 59 |  |  |  |  |  |  |  | 
| 60 | 0 | 0 | 0 |  |  |  | return 1 if $opt->{reportid} && $file_ok && $report_ok; | 
|  |  |  | 0 |  |  |  |  | 
| 61 | 0 |  |  |  |  |  | die $self->usage->text; | 
| 62 |  |  |  |  |  |  | } | 
| 63 |  |  |  |  |  |  |  | 
| 64 |  |  |  |  |  |  | sub execute | 
| 65 |  |  |  |  |  |  | { | 
| 66 | 0 |  |  | 0 | 1 |  | my ($self, $opt, $args) = @_; | 
| 67 |  |  |  |  |  |  |  | 
| 68 | 0 |  |  |  |  |  | $self->upload ($opt, $args); | 
| 69 |  |  |  |  |  |  | } | 
| 70 |  |  |  |  |  |  |  | 
| 71 |  |  |  |  |  |  | sub _read_file | 
| 72 |  |  |  |  |  |  | { | 
| 73 | 0 |  |  | 0 |  |  | my ($self, $opt, $args) = @_; | 
| 74 |  |  |  |  |  |  |  | 
| 75 | 0 |  |  |  |  |  | my $file = $opt->{file}; | 
| 76 | 0 |  |  |  |  |  | my $content; | 
| 77 |  |  |  |  |  |  |  | 
| 78 |  |  |  |  |  |  | # read from file or STDIN if filename == '-' | 
| 79 | 0 | 0 |  |  |  |  | if ($file eq '-') { | 
| 80 | 0 |  |  |  |  |  | $content = slurp (\*STDIN); | 
| 81 |  |  |  |  |  |  | } else { | 
| 82 | 0 |  |  |  |  |  | $content = slurp ($file); | 
| 83 |  |  |  |  |  |  | } | 
| 84 | 0 |  |  |  |  |  | return $content; | 
| 85 |  |  |  |  |  |  | } | 
| 86 |  |  |  |  |  |  |  | 
| 87 |  |  |  |  |  |  |  | 
| 88 |  |  |  |  |  |  | sub upload | 
| 89 |  |  |  |  |  |  | { | 
| 90 | 0 |  |  | 0 | 0 |  | my ($self, $opt, $args) = @_; | 
| 91 |  |  |  |  |  |  |  | 
| 92 | 0 |  | 0 |  |  |  | my $host = $opt->{reportserver} || Tapper::Config->subconfig->{report_server}; | 
| 93 | 0 |  | 0 |  |  |  | my $port = $opt->{reportport}   || Tapper::Config->subconfig->{report_api_port}; | 
| 94 |  |  |  |  |  |  |  | 
| 95 | 0 |  |  |  |  |  | my $reportid    = $opt->{reportid}; | 
| 96 | 0 |  |  |  |  |  | my $file        = $opt->{file}; | 
| 97 | 0 |  |  |  |  |  | my $filename    = $opt->{filename}; | 
| 98 | 0 |  | 0 |  |  |  | my $contenttype = $opt->{contenttype} || 'plain'; | 
| 99 | 0 |  |  |  |  |  | my $content     = $self->_read_file($opt, $args); | 
| 100 |  |  |  |  |  |  |  | 
| 101 | 0 |  | 0 |  |  |  | my $cmdline     = "#! upload $reportid ".($filename || $file)." $contenttype\n"; | 
| 102 |  |  |  |  |  |  |  | 
| 103 | 0 |  |  |  |  |  | my $REMOTEAPI   = IO::Socket::INET->new(PeerAddr => $host, PeerPort => $port); | 
| 104 | 0 | 0 |  |  |  |  | if ($REMOTEAPI) { | 
| 105 |  |  |  |  |  |  | #my $oldfh       = select $REMOTEAPI; | 
| 106 | 0 |  |  |  |  |  | print $REMOTEAPI $cmdline; | 
| 107 | 0 |  |  |  |  |  | print $REMOTEAPI $content; | 
| 108 |  |  |  |  |  |  | #select($oldfh); | 
| 109 | 0 |  |  |  |  |  | close ($REMOTEAPI); | 
| 110 |  |  |  |  |  |  | } | 
| 111 |  |  |  |  |  |  | else { | 
| 112 | 0 |  |  |  |  |  | say "Cannot open remote receiver $host:$port."; | 
| 113 |  |  |  |  |  |  | } | 
| 114 |  |  |  |  |  |  | } | 
| 115 |  |  |  |  |  |  |  | 
| 116 |  |  |  |  |  |  | # perl -Ilib bin/tapper-api upload --reportid=552 --file ~/xyz | 
| 117 |  |  |  |  |  |  | # perl -Ilib bin/tapper-api upload --reportid=552 --file=$HOME/xyz | 
| 118 |  |  |  |  |  |  | # dmesg | perl -Ilib bin/tapper-api upload --reportid=552 --file=- --filename="dmesg" | 
| 119 |  |  |  |  |  |  |  | 
| 120 |  |  |  |  |  |  | 1; | 
| 121 |  |  |  |  |  |  |  | 
| 122 |  |  |  |  |  |  | __END__ | 
| 123 |  |  |  |  |  |  |  | 
| 124 |  |  |  |  |  |  | =pod | 
| 125 |  |  |  |  |  |  |  | 
| 126 |  |  |  |  |  |  | =encoding UTF-8 | 
| 127 |  |  |  |  |  |  |  | 
| 128 |  |  |  |  |  |  | =head1 NAME | 
| 129 |  |  |  |  |  |  |  | 
| 130 |  |  |  |  |  |  | Tapper::CLI::API::Command::upload | 
| 131 |  |  |  |  |  |  |  | 
| 132 |  |  |  |  |  |  | =head1 AUTHOR | 
| 133 |  |  |  |  |  |  |  | 
| 134 |  |  |  |  |  |  | AMD OSRC Tapper Team <tapper@amd64.org> | 
| 135 |  |  |  |  |  |  |  | 
| 136 |  |  |  |  |  |  | =head1 COPYRIGHT AND LICENSE | 
| 137 |  |  |  |  |  |  |  | 
| 138 |  |  |  |  |  |  | This software is Copyright (c) 2020 by Advanced Micro Devices, Inc.. | 
| 139 |  |  |  |  |  |  |  | 
| 140 |  |  |  |  |  |  | This is free software, licensed under: | 
| 141 |  |  |  |  |  |  |  | 
| 142 |  |  |  |  |  |  | The (two-clause) FreeBSD License | 
| 143 |  |  |  |  |  |  |  | 
| 144 |  |  |  |  |  |  | =cut |