| 1 | package App::Yath::Command::upload; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | |||
| 5 | our $VERSION = '0.000150'; | ||
| 6 | |||
| 7 | use IO::Uncompress::Bunzip2 qw($Bunzip2Error); | ||
| 8 | use IO::Uncompress::Gunzip qw($GunzipError); | ||
| 9 | |||
| 10 | use Test2::Harness::UI::Util qw/ config_from_settings /; | ||
| 11 | use Test2::Harness::Util::JSON qw/decode_json/; | ||
| 12 | |||
| 13 | use Test2::Harness::Renderer::UIDB; | ||
| 14 | |||
| 15 | use parent 'App::Yath::Command'; | ||
| 16 | use Test2::Harness::Util::HashBase; | ||
| 17 | |||
| 18 | use App::Yath::Options; | ||
| 19 | |||
| 20 | include_options( | ||
| 21 | 'App::Yath::Options::PreCommand', | ||
| 22 | ); | ||
| 23 | |||
| 24 | sub summary { "Use the YathUIDB plugin to upload a log file" } | ||
| 25 | |||
| 26 | sub group { 'log' } | ||
| 27 | |||
| 28 | sub cli_args { "[--] event_log.jsonl[.gz|.bz2]" } | ||
| 29 | |||
| 30 | sub description { | ||
| 31 | return <<" EOT"; | ||
| 32 | EOT | ||
| 33 | } | ||
| 34 | |||
| 35 | sub run { | ||
| 36 | my $self = shift; | ||
| 37 | |||
| 38 | my $args = $self->args; | ||
| 39 | my $settings = $self->settings; | ||
| 40 | |||
| 41 | shift @$args if @$args && $args->[0] eq '--'; | ||
| 42 | |||
| 43 | my $file = shift @$args or die "You must specify a log file"; | ||
| 44 | die "'$file' is not a valid log file" unless -f $file; | ||
| 45 | die "'$file' does not look like a log file" unless $file =~ m/\.jsonl(\.(gz|bz2))?$/; | ||
| 46 | |||
| 47 | die "The YathUIDB plugin is required" unless $settings->check_prefix('yathui-db'); | ||
| 48 | |||
| 49 | my $fh; | ||
| 50 | if ($file =~ m/\.bz2$/) { | ||
| 51 | $fh = IO::Uncompress::Bunzip2->new($file) or die "Could not open bz2 file: $Bunzip2Error"; | ||
| 52 | } | ||
| 53 | elsif ($file =~ m/\.gz$/) { | ||
| 54 | $fh = IO::Uncompress::Gunzip->new($file) or die "Could not open gz file: $GunzipError"; | ||
| 55 | } | ||
| 56 | else { | ||
| 57 | open($fh, '<', $file) or die "Could not open log file: $!"; | ||
| 58 | } | ||
| 59 | |||
| 60 | my $config = config_from_settings($settings); | ||
| 61 | |||
| 62 | my $ydb = $self->settings->prefix('yathui-db'); | ||
| 63 | my $yath = $settings->yathui; | ||
| 64 | my $user = $yath->user || $ENV{USER}; | ||
| 65 | |||
| 66 | my $renderer = Test2::Harness::Renderer::UIDB->new( | ||
| 67 | config => $config, | ||
| 68 | settings => $settings, | ||
| 69 | user => $user, | ||
| 70 | ); | ||
| 71 | |||
| 72 | my $is_term = -t STDOUT ? 1 : 0; | ||
| 73 | |||
| 74 | print "\n" if $is_term; | ||
| 75 | |||
| 76 | local $| = 1; | ||
| 77 | while (my $line = <$fh>) { | ||
| 78 | my $ln = $.; | ||
| 79 | |||
| 80 | print "\033[Fprocessing log line: $ln\n" | ||
| 81 | if $is_term; | ||
| 82 | |||
| 83 | next if $line =~ m/^null$/ims; | ||
| 84 | |||
| 85 | my $ok = eval { | ||
| 86 | my $event = decode_json($line); | ||
| 87 | $renderer->_render_event($event); | ||
| 88 | 1; | ||
| 89 | }; | ||
| 90 | my $err = $@; | ||
| 91 | next if $ok; | ||
| 92 | |||
| 93 | die "Error processing log on line $ln: $err"; | ||
| 94 | } | ||
| 95 | |||
| 96 | print "Upload Complete\n"; | ||
| 97 | |||
| 98 | $renderer->finish(); | ||
| 99 | |||
| 100 | return 0; | ||
| 101 | } | ||
| 102 | |||
| 103 | 1; |