Skip to content

Commit

Permalink
Add publish of a log file
Browse files Browse the repository at this point in the history
  • Loading branch information
exodist committed Jun 12, 2024
1 parent db124e1 commit 3abae40
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
4 changes: 4 additions & 0 deletions dist.ini
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ Time::HiRes = 0
XML::Generator = 0
YAML::Tiny = 0

DateTime::TimeZone::Local::Unix = 0

DBIx::Class::Helper::ResultSet::RemoveColumns = 0
DBIx::Class::InflateColumn::DateTime = 0
DBIx::Class::InflateColumn::Serializer = 0
Expand Down Expand Up @@ -203,6 +205,8 @@ UUID = 0.35
Data::UUID = 1.227
UUID::Tiny = 1.04
Data::UUID::MT = 1.001
LWP = 0
LWP::UserAgent = 0

DBIx::Class::Storage::DBI::MariaDB = 0
DBIx::Class::Storage::DBI::mysql::Retryable = 0
Expand Down
84 changes: 84 additions & 0 deletions lib/App/Yath/Command/client/publish.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package App::Yath::Command::client::publish;
use strict;
use warnings;

our $VERSION = '2.000000';

use parent 'App::Yath::Command';
use Test2::Harness::Util::HashBase;

use Test2::Harness::Util::JSON qw/decode_json/;

use LWP;
use LWP::UserAgent;
use Getopt::Yath;

include_options(
'App::Yath::Options::Yath',
'App::Yath::Options::WebClient',
'App::Yath::Options::Publish' => [qw/mode/],
);

sub summary { "Publish a log file to a yath server" }

sub description {
return <<" EOT";
Publish a log file to a yath server. (API key is required)
EOT
}

sub run {
my $self = shift;

my $args = $self->args;
my $settings = $self->settings;

shift @$args if @$args && $args->[0] eq '--';

my $log = shift @$args or die "You must specify a log file";
die "'$log' is not a valid log file" unless -f $log;
die "'$log' does not look like a log file" unless $log =~ m/\.jsonl(\.(gz|bz2))?$/;

my $api_key = $settings->webclient->api_key or die "No API key was specified.\n";
my $url = $settings->webclient->url or die "No URL specified.\n";
my $mode = $settings->publish->mode or die "No MODE specified.\n";
my $project = $settings->yath->project or die "No project specified.\n";

$url =~ s{/+$}{}g;

my $ua = LWP::UserAgent->new;
my $res = $ua->post(
"$url/upload",
'Content-Type' => 'multipart/form-data',
'Content' => [
mode => $mode,
api_key => $api_key,
project => $project,
action => 'upload log',
json => 1,
log_file => [$log],
],
);

if ($res->is_success) {
my $json = $res->decoded_content;
my $data = decode_json($json);

print "$_\n" for @{$data->{messages} // []};

print "\nView run at: $url/view/$data->{run_uuid}\n\n";

return 0;
}
else {
print STDERR $res->status_line, "\n";
return 1;
}
}


1;

__END__
=head1 POD IS AUTO-GENERATED

0 comments on commit 3abae40

Please sign in to comment.