<commit_before>package Artemis::Cmd::API::Command::upload;

use 5.010;

use strict;
use warnings;

use parent 'App::Cmd::Command';

use IO::Socket;
use Artemis::Config;
use File::Slurp 'slurp';
use Data::Dumper;
use Moose;

sub abstract {
        'Upload and attach a file to a report'
}

sub opt_spec {
        return (
                [ "verbose",       "some more informational output" ],
                [ "reportid=s",    "INT; the testrun id to change", ],
                [ "file=s",        "STRING; the file to upload, use '-' for STDIN", ],
                [ "contenttype=s", "STRING; content-type, default 'plain', use 'application/octed-stream' for binaries", ],
               );
}

sub usage_desc
{
        my $allowed_opts = join ' ', map { '--'.$_ } _allowed_opts();
        "artemis-api upload --reportid=s --file=s [ --contenttype=s ]";
}

sub _allowed_opts
{
        my @allowed_opts = map { $_->[0] } opt_spec();
}

sub validate_args
{
        my ($self, $opt, $args) = @_;

        # -- file constraints --
        my $file    = $opt->{file};
        my $file_ok = $file && ($file eq '-' || -r $file);
        say "Missing argument --file"                        unless $file;
        say "Error: file '$file' must be readable or STDIN." unless $file_ok;

        # -- report constraints --
        my $reportid  = $opt->{reportid};
        my $report_ok = $reportid && $reportid =~ /^\d+$/;
        say "Missing argument --reportid"           unless $reportid;
        say "Error: Strange report id '$reportid'." unless $report_ok;

        return 1 if $opt->{reportid} && $file_ok && $report_ok;
        die $self->usage->text;
}

sub run
{
        my ($self, $opt, $args) = @_;
        $self->upload ($opt, $args);
}

sub read_file
{
        my ($self, $opt, $args) = @_;

        my $file = $opt->{file};
        my $content;

        # read from file or STDIN if filename == '-'
        if ($file eq '-') {
                $content = read_file (\*STDIN);
        } else {
                $content = read_file ($file);
        }
        return $content;
}


sub upload
{
        my ($self, $opt, $args) = @_;

        my $host = Artemis::Config->subconfig->{report_server};
        my $port = Artemis::Config->subconfig->{report_api_port};

        my $reportid    = $opt->{reportid};
        my $file        = $opt->{file};
        my $contenttype = $opt->{contenttype} || 'plain';
        my $content     = $self->_read_file($opt, $args);

        my $cmdline     = "#! upload $reportid $file $contenttype\n";

        my $REMOTEAPI   = IO::Socket::INET->new(PeerAddr => $host, PeerPort => $port);
        if ($REMOTEAPI) {
                #my $oldfh       = select $REMOTEAPI;
                print $REMOTEAPI $cmdline;
                print $REMOTEAPI $content;<commit_msg>- method renaming<commit_after>package Artemis::Cmd::API::Command::upload;

use 5.010;

use strict;
use warnings;

use parent 'App::Cmd::Command';

use IO::Socket;
use Artemis::Config;
use File::Slurp 'slurp';
use Data::Dumper;
use Moose;

sub abstract {
        'Upload and attach a file to a report'
}

sub opt_spec {
        return (
                [ "verbose",       "some more informational output" ],
                [ "reportid=s",    "INT; the testrun id to change", ],
                [ "file=s",        "STRING; the file to upload, use '-' for STDIN", ],
                [ "contenttype=s", "STRING; content-type, default 'plain', use 'application/octed-stream' for binaries", ],
               );
}

sub usage_desc
{
        my $allowed_opts = join ' ', map { '--'.$_ } _allowed_opts();
        "artemis-api upload --reportid=s --file=s [ --contenttype=s ]";
}

sub _allowed_opts
{
        my @allowed_opts = map { $_->[0] } opt_spec();
}

sub validate_args
{
        my ($self, $opt, $args) = @_;

        # -- file constraints --
        my $file    = $opt->{file};
        my $file_ok = $file && ($file eq '-' || -r $file);
        say "Missing argument --file"                        unless $file;
        say "Error: file '$file' must be readable or STDIN." unless $file_ok;

        # -- report constraints --
        my $reportid  = $opt->{reportid};
        my $report_ok = $reportid && $reportid =~ /^\d+$/;
        say "Missing argument --reportid"           unless $reportid;
        say "Error: Strange report id '$reportid'." unless $report_ok;

        return 1 if $opt->{reportid} && $file_ok && $report_ok;
        die $self->usage->text;
}

sub run
{
        my ($self, $opt, $args) = @_;
        $self->upload ($opt, $args);
}

sub _read_file
{
        my ($self, $opt, $args) = @_;

        my $file = $opt->{file};
        my $content;

        # read from file or STDIN if filename == '-'
        if ($file eq '-') {
                $content = slurp (\*STDIN);
        } else {
                $content = slurp ($file);
        }
        return $content;
}


sub upload
{
        my ($self, $opt, $args) = @_;

        my $host = Artemis::Config->subconfig->{report_server};
        my $port = Artemis::Config->subconfig->{report_api_port};

        my $reportid    = $opt->{reportid};
        my $file        = $opt->{file};
        my $contenttype = $opt->{contenttype} || 'plain';
        my $content     = $self->_read_file($opt, $args);

        my $cmdline     = "#! upload $reportid $file $contenttype\n";

        my $REMOTEAPI   = IO::Socket::INET->new(PeerAddr => $host, PeerPort => $port);
        if ($REMOTEAPI) {
                #my $oldfh       = select $REMOTEAPI;
                print $REMOTEAPI $cmdline;
                print $REMOTEAPI $content;
{'fraction_non_alphanumeric': 0.16270762636185032, 'fraction_numerical': 0.0021432398642614752, 'mean_word_length': 1.9567053854276664, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4905621', 'n_tokens_mistral': 1869, 'n_tokens_neox': 1770, 'n_words': 481}