| 1 | #!perl | ||
| 2 | |||
| 3 | use v5.20.0; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | # The parts of this that we use have been stable and unchanged since v5.20.0: | ||
| 7 | use feature qw(postderef); | ||
| 8 | no warnings 'experimental::postderef'; | ||
| 9 | |||
| 10 | package Cron::Sequencer::Output; | ||
| 11 | |||
| 12 | our $VERSION = '0.04'; | ||
| 13 | |||
| 14 | use Carp qw(confess croak); | ||
| 15 | |||
| 16 | # TODO - take formatter options. Mininally, timezone to use | ||
| 17 | sub new { | ||
| 18 | my ($class, %opts) = @_; | ||
| 19 | confess('new() called as an instance method') | ||
| 20 | if ref $class; | ||
| 21 | |||
| 22 | my %state = %opts{qw(count group hide-env json)}; | ||
| 23 | |||
| 24 | return bless \%state; | ||
| 25 | } | ||
| 26 | |||
| 27 | sub render { | ||
| 28 | my ($self, @groups) = @_; | ||
| 29 | |||
| 30 | return $self->{json} | ||
| 31 | ? $self->render_json(@groups) : $self->render_text(@groups); | ||
| 32 | } | ||
| 33 | |||
| 34 | sub render_text { | ||
| 35 | my ($self, @groups) = @_; | ||
| 36 | |||
| 37 | # We assume that you normally want things grouped, so we aren't particularly | ||
| 38 | # optimising the "flat" --no-group path: | ||
| 39 | @groups = map { [$_] } map { @$_ } @groups | ||
| 40 | unless $self->{group}; | ||
| 41 | |||
| 42 | my @output; | ||
| 43 | |||
| 44 | for my $group (@groups) { | ||
| 45 | # Should this be an error? | ||
| 46 | unless (@$group) { | ||
| 47 | push @output, ""; | ||
| 48 | next; | ||
| 49 | } | ||
| 50 | |||
| 51 | # 1+ entries that all fire at the same time | ||
| 52 | my @cluster; | ||
| 53 | |||
| 54 | for my $entry (@$group) { | ||
| 55 | # The first blank line we add here is replaced below with the time. | ||
| 56 | push @cluster, ""; | ||
| 57 | if ($self->{count} > 1) { | ||
| 58 | push @cluster, "$entry->{file}:$entry->{lineno}: $entry->{when}"; | ||
| 59 | } else { | ||
| 60 | push @cluster, "line $entry->{lineno}: $entry->{when}"; | ||
| 61 | } | ||
| 62 | |||
| 63 | unless ($self->{'hide-env'}) { | ||
| 64 | push @cluster, map "unset $_", $entry->{unset}->@* | ||
| 65 | if $entry->{unset}; | ||
| 66 | my $env = $entry->{env}; | ||
| 67 | push @cluster, map "$_=$env->{$_}", sort keys %$env | ||
| 68 | if $env; | ||
| 69 | } | ||
| 70 | |||
| 71 | push @cluster, $entry->{command}; | ||
| 72 | } | ||
| 73 | |||
| 74 | # This replaces the blank line at the start of the "cluster". | ||
| 75 | my $when = DateTime->from_epoch(epoch => $group->[0]{time}); | ||
| 76 | $cluster[0] = $when->stringify(); | ||
| 77 | |||
| 78 | push @output, @cluster, "", ""; | ||
| 79 | } | ||
| 80 | |||
| 81 | # Drop the (second) empty string added just above in the last iteration of | ||
| 82 | # the loop. If we don't do this, the second would cause an extra blank line | ||
| 83 | # at the end. The first empty string causes the last (real) line to get a | ||
| 84 | # newline (a newline that we want to have). | ||
| 85 | pop @output; | ||
| 86 | |||
| 87 | return join "\n", @output; | ||
| 88 | } | ||
| 89 | |||
| 90 | sub render_json { | ||
| 91 | my ($self, @groups) = @_; | ||
| 92 | require JSON::MaybeXS; | ||
| 93 | |||
| 94 | my %opts = $self->{json}->%*; | ||
| 95 | |||
| 96 | # CLI parser forbids 'seq' and 'split' simultaneously. | ||
| 97 | my $seq = delete $opts{seq}; | ||
| 98 | my $start = $seq ? "\x1E" : ""; | ||
| 99 | my $split = delete $opts{split} || $seq; | ||
| 100 | |||
| 101 | my $json = JSON::MaybeXS->new(%opts); | ||
| 102 | |||
| 103 | my $munged; | ||
| 104 | if ($self->{'hide-env'}) { | ||
| 105 | # We shouldn't mutate our input, so we need to make a copy. As we need | ||
| 106 | # to loop over the data anyway for --no-groups, combine the two loops | ||
| 107 | if ($self->{group}) { | ||
| 108 | for my $group (@groups) { | ||
| 109 | # We need to create new anon arrays to hold our cleaned entries: | ||
| 110 | my @cleaned; | ||
| 111 | for my $entry (@$group) { | ||
| 112 | my %copy = %$entry; | ||
| 113 | # Clean | ||
| 114 | delete @copy{qw(unset env)}; | ||
| 115 | # and flatten | ||
| 116 | push @cleaned, \%copy; | ||
| 117 | } | ||
| 118 | push @$munged, \@cleaned; | ||
| 119 | } | ||
| 120 | } else { | ||
| 121 | for my $entry (map { @$_ } @groups) { | ||
| 122 | my %copy = %$entry; | ||
| 123 | # Clean | ||
| 124 | delete @copy{qw(unset env)}; | ||
| 125 | # and flatten | ||
| 126 | push @$munged, \%copy; | ||
| 127 | } | ||
| 128 | } | ||
| 129 | } elsif ($self->{group}) { | ||
| 130 | # Nothing to do! | ||
| 131 | $munged = \@groups; | ||
| 132 | } else { | ||
| 133 | @$munged = map { @$_ } @groups; | ||
| 134 | } | ||
| 135 | |||
| 136 | return $json->encode($munged) . "\n" | ||
| 137 | unless $split; | ||
| 138 | |||
| 139 | return join '', map { $start . $json->encode($_) . "\n" } @$munged; | ||
| 140 | } | ||
| 141 | |||
| 142 | # TODO - improve this documentation, as a side effect of adding other output | ||
| 143 | # formats | ||
| 144 | |||
| 145 | =head1 NAME | ||
| 146 | |||
| 147 | Cron::Sequencer::Output | ||
| 148 | |||
| 149 | =head1 SYNOPSIS | ||
| 150 | |||
| 151 | my $formatter = Cron::Sequencer::Output->new('hide-env' => 1); | ||
| 152 | print $formatter->render($crontab->sequence($start, $end)); | ||
| 153 | |||
| 154 | =head1 DESCRIPTION | ||
| 155 | |||
| 156 | This class implements output formatting for L<Cron::Sequencer> | ||
| 157 | |||
| 158 | Currently it can only output a pretty-printed text format, and the only option | ||
| 159 | is whether to show or hide environment variable declartions. | ||
| 160 | |||
| 161 | =head1 LICENSE | ||
| 162 | |||
| 163 | This library is free software; you can redistribute it and/or modify it under | ||
| 164 | the same terms as Perl itself. If you would like to contribute documentation, | ||
| 165 | features, bug fixes, or anything else then please raise an issue / pull request: | ||
| 166 | |||
| 167 | https://github.com/Humanstate/cron-sequencer | ||
| 168 | |||
| 169 | =head1 AUTHOR | ||
| 170 | |||
| 171 | Nicholas Clark - C<nick@ccl4.org> | ||
| 172 | |||
| 173 | =cut | ||
| 174 | |||
| 175 | 54; |