| 1 | #!/usr/bin/env perl | ||
| 2 | |||
| 3 | # TODO: | ||
| 4 | # | ||
| 5 | # Ideally, this would be able to handle the following gracefully: | ||
| 6 | # | ||
| 7 | # wrt ls 2016 # all entries for 2016 | ||
| 8 | # wrt ls 2016/4 # all entries for April 2016 | ||
| 9 | # wrt ls 2016/4/1 # everything for April 1, 2016 | ||
| 10 | # | ||
| 11 | # ...but I think doing that right requires a much cleaner separation of how | ||
| 12 | # entries are _structured_ from how they're _displayed_, probably by moving | ||
| 13 | # more operations into WRT::EntryStore. | ||
| 14 | # | ||
| 15 | # It makes some sense that wrt-ls would just expose the interface of | ||
| 16 | # EntryStore in a relatively safe way, including the operations that find | ||
| 17 | # things by depth, locate the next/previous entry, etc. All of these could | ||
| 18 | # be useful in scripting and publishing pipelines. | ||
| 19 | |||
| 20 | =pod | ||
| 21 | |||
| 22 | =head1 NAME | ||
| 23 | |||
| 24 | wrt-ls - list | ||
| 25 | |||
| 26 | =head1 USAGE | ||
| 27 | |||
| 28 | wrt ls # all entries | ||
| 29 | wrt ls --days # entries for individual days | ||
| 30 | wrt ls --months # entries for individual months | ||
| 31 | wrt ls --years # entries for years | ||
| 32 | wrt ls --props # all properties | ||
| 33 | |||
| 34 | # Display help: | ||
| 35 | wrt ls --help | ||
| 36 | |||
| 37 | # Specify a different config file: | ||
| 38 | wrt ls --config ./wrt.json ... | ||
| 39 | |||
| 40 | =head1 DESCRIPTION | ||
| 41 | |||
| 42 | Lists entries in the current wrt archive. | ||
| 43 | |||
| 44 | This interface is experimental and subject to revision in upcoming releases. | ||
| 45 | |||
| 46 | Detailed documentation can be found in the L<App::WRT> man page or at | ||
| 47 | L<https://code.p1k3.com/gitea/brennen/wrt>. | ||
| 48 | |||
| 49 | =head1 LICENSE | ||
| 50 | |||
| 51 | wrt is free software; you can redistribute it and/or modify | ||
| 52 | it under the terms of the GNU General Public License as published by | ||
| 53 | the Free Software Foundation; either version 2 of the License, or | ||
| 54 | (at your option) any later version. | ||
| 55 | |||
| 56 | =head1 AUTHOR | ||
| 57 | |||
| 58 | Brennen Bearnes <code@p1k3.com> | ||
| 59 | |||
| 60 | =cut | ||
| 61 | |||
| 62 | 1 | ▶ | use 5.10.0; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 63 | |||
| 64 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 65 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 66 | |||
| 67 | 1 | ▶ | use Getopt::Long qw(GetOptionsFromArray); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 68 | 1 | ▶ | use Pod::Usage; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 69 | 0 | ▶ | use App::WRT; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 70 | use Carp; | ||
| 71 | |||
| 72 | # If invoked directly from the command-line, caller() will return undef. | ||
| 73 | # Execute main() with a callback to print output directly, and a copy of | ||
| 74 | # our real @ARGV: | ||
| 75 | if (not caller()) { | ||
| 76 | my $output = sub { say @_; }; | ||
| 77 | main($output, @ARGV); | ||
| 78 | exit(0); | ||
| 79 | } | ||
| 80 | |||
| 81 | # main() takes an output callback and an @ARGV to pass in to | ||
| 82 | # GetOptionsFromArray(). This allows relatively simple integration | ||
| 83 | # tests to be written. See also: t/bin-wrt-ls.t | ||
| 84 | sub main { | ||
| 85 | my ($output, @local_argv) = @_; | ||
| 86 | |||
| 87 | # Handle options, including help generated from the POD above. See: | ||
| 88 | # - http://perldoc.perl.org/Getopt/Long.html#User-defined-subroutines-to-handle-options | ||
| 89 | # - https://metacpan.org/pod/Pod::Usage | ||
| 90 | # - http://michael.thegrebs.com/2014/06/08/Pod-Usage/ | ||
| 91 | my $config_file = 'wrt.json'; | ||
| 92 | my $with_titles = 0; | ||
| 93 | |||
| 94 | my $list_days = 0; | ||
| 95 | my $list_months = 0; | ||
| 96 | my $list_years = 0; | ||
| 97 | my $list_props = 0; | ||
| 98 | my $list_all = 1; | ||
| 99 | GetOptionsFromArray( | ||
| 100 | \@local_argv, | ||
| 101 | 'config=s' => \$config_file, | ||
| 102 | help => sub { pod2usage(0) }, | ||
| 103 | days => \$list_days, | ||
| 104 | months => \$list_months, | ||
| 105 | years => \$list_years, | ||
| 106 | props => \$list_props, | ||
| 107 | 'with-titles' => \$with_titles, | ||
| 108 | ) or pod2usage(2); | ||
| 109 | |||
| 110 | # Allow only one of --days, --months, --years, --props. Default to listing | ||
| 111 | # all entries if none of these are specified. | ||
| 112 | my $option_count = 0; | ||
| 113 | foreach ($list_days, $list_months, $list_years, $list_props) { | ||
| 114 | $option_count += $_; | ||
| 115 | } | ||
| 116 | if ($option_count > 1) { | ||
| 117 | croak("Please specify at most one of --days, --months, --years, --props."); | ||
| 118 | } elsif ($option_count == 1) { | ||
| 119 | $list_all = 0; | ||
| 120 | } | ||
| 121 | |||
| 122 | unless (-e $config_file) { | ||
| 123 | croak("No wrt config file found. Tried: $config_file"); | ||
| 124 | } | ||
| 125 | |||
| 126 | my $w = App::WRT::new_from_file($config_file); | ||
| 127 | |||
| 128 | # Define the function that'll return the base list of entries to match | ||
| 129 | # against: | ||
| 130 | my $base_list; | ||
| 131 | if ($list_days) { | ||
| 132 | $base_list = sub { $w->{entries}->all_days(); }; | ||
| 133 | } elsif ($list_months) { | ||
| 134 | $base_list = sub { $w->{entries}->all_months(); }; | ||
| 135 | } elsif ($list_years) { | ||
| 136 | $base_list = sub { $w->{entries}->all_years(); }; | ||
| 137 | } elsif ($list_props) { | ||
| 138 | $base_list = sub { $w->{entries}->all_props(); }; | ||
| 139 | } elsif ($list_all) { | ||
| 140 | $base_list = sub { $w->{entries}->all(); }; | ||
| 141 | } | ||
| 142 | |||
| 143 | foreach my $entry ($base_list->()) { | ||
| 144 | # When invoked from command line, this will normally be a simple | ||
| 145 | # routine that does `say $entry`. Under testing, it may instead | ||
| 146 | # accumulate output for checking elsewhere. | ||
| 147 | |||
| 148 | if ($with_titles) { | ||
| 149 | $output->($entry . "\t" . $w->get_title($entry)); | ||
| 150 | } else { | ||
| 151 | $output->($entry); | ||
| 152 | } | ||
| 153 | } | ||
| 154 | } | ||
| 155 | |||
| 156 | 1; | ||