| 1 | #!/usr/bin/env perl | ||
| 2 | |||
| 3 | =pod | ||
| 4 | |||
| 5 | =head1 NAME | ||
| 6 | |||
| 7 | wrt-feed - print an Atom or JSON feed from given wrt entries | ||
| 8 | |||
| 9 | =head1 USAGE | ||
| 10 | |||
| 11 | # Print most recent entries: | ||
| 12 | wrt feed | ||
| 13 | wrt feed --json | ||
| 14 | |||
| 15 | # Print entries for a specific month: | ||
| 16 | wrt feed 2019/11 | ||
| 17 | wrt feed --json 2019/11 | ||
| 18 | |||
| 19 | =head1 DESCRIPTION | ||
| 20 | |||
| 21 | Detailed documentation can be found in the L<App::WRT> man page or at | ||
| 22 | L<https://code.p1k3.com/gitea/brennen/wrt>. | ||
| 23 | |||
| 24 | =head1 LICENSE | ||
| 25 | |||
| 26 | wrt is free software; you can redistribute it and/or modify | ||
| 27 | it under the terms of the GNU General Public License as published by | ||
| 28 | the Free Software Foundation; either version 2 of the License, or | ||
| 29 | (at your option) any later version. | ||
| 30 | |||
| 31 | =head1 AUTHOR | ||
| 32 | |||
| 33 | Brennen Bearnes <code@p1k3.com> | ||
| 34 | |||
| 35 | =cut | ||
| 36 | |||
| 37 | 1 | ▶ | use 5.14.0; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 38 | |||
| 39 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 40 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 41 | 1 | ▶ | no warnings 'uninitialized'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 42 | 1 | ▶ | use utf8; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 43 | 1 | ▶ | use open qw(:std :utf8); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 44 | |||
| 45 | # use Data::Dumper; | ||
| 46 | 0 | ▶ | use App::WRT; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 47 | |||
| 48 | use Carp; | ||
| 49 | use Getopt::Long qw(GetOptionsFromArray); | ||
| 50 | use Pod::Usage; | ||
| 51 | |||
| 52 | # If invoked directly from the command-line, caller() will return undef. | ||
| 53 | # Execute main() with a callback to print output directly, and a copy of | ||
| 54 | # our real @ARGV: | ||
| 55 | if (not caller()) { | ||
| 56 | my $output = sub { say @_; }; | ||
| 57 | my $retval = main($output, @ARGV); | ||
| 58 | exit($retval); | ||
| 59 | } | ||
| 60 | |||
| 61 | sub main { | ||
| 62 | my ($output, @local_argv) = @_; | ||
| 63 | # Handle options, including help generated from the POD above. See: | ||
| 64 | # - http://perldoc.perl.org/Getopt/Long.html#User-defined-subroutines-to-handle-options | ||
| 65 | # - https://metacpan.org/pod/Pod::Usage | ||
| 66 | # - http://michael.thegrebs.com/2014/06/08/Pod-Usage/ | ||
| 67 | my $from_stdin = 0; | ||
| 68 | my $print_json = 0; | ||
| 69 | my $config_file = 'wrt.json'; | ||
| 70 | GetOptionsFromArray( | ||
| 71 | \@local_argv, | ||
| 72 | |||
| 73 | stdin => \$from_stdin, | ||
| 74 | json => \$print_json, | ||
| 75 | 'config=s' => \$config_file, | ||
| 76 | help => sub { pod2usage(0) }, | ||
| 77 | |||
| 78 | ) or pod2usage(2); | ||
| 79 | |||
| 80 | unless (-e $config_file) { | ||
| 81 | croak "No wrt config file found. Tried: $config_file"; | ||
| 82 | } | ||
| 83 | |||
| 84 | my $w = App::WRT::new_from_file($config_file); | ||
| 85 | |||
| 86 | # With --stdin, take names of entries to display from standard input, one line | ||
| 87 | # per name. Otherwise, take names from arguments if any are given, or fall | ||
| 88 | # back to most recent entries. | ||
| 89 | my @to_display = (); | ||
| 90 | if ($from_stdin) { | ||
| 91 | while (my $entry = <>) { | ||
| 92 | chomp($entry); | ||
| 93 | push @to_display, $entry; | ||
| 94 | } | ||
| 95 | } elsif (@local_argv) { | ||
| 96 | (@to_display) = @local_argv; | ||
| 97 | } | ||
| 98 | |||
| 99 | my (@expanded_to_display) = map { $w->expand_alias($_) } @to_display; | ||
| 100 | |||
| 101 | foreach my $entry (@expanded_to_display) { | ||
| 102 | unless ($w->{entries}->is_extant($entry)) { | ||
| 103 | # TODO: Better error reporting strategy, print this on stderr: | ||
| 104 | say("No such entry: $entry"); | ||
| 105 | return 1; | ||
| 106 | } | ||
| 107 | } | ||
| 108 | |||
| 109 | if (@expanded_to_display) { | ||
| 110 | if ($print_json) { | ||
| 111 | $output->( $w->feed_print_json(@expanded_to_display) ); | ||
| 112 | } else { | ||
| 113 | $output->( $w->feed_print(@expanded_to_display) ); | ||
| 114 | } | ||
| 115 | } else { | ||
| 116 | if ($print_json) { | ||
| 117 | $output->( $w->feed_print_json_recent() ); | ||
| 118 | } else { | ||
| 119 | $output->( $w->feed_print_recent() ); | ||
| 120 | } | ||
| 121 | } | ||
| 122 | |||
| 123 | return 0; | ||
| 124 | } | ||
| 125 | |||
| 126 | 1; | ||