|
| 1 | +package WGDev::Command::Trash; |
| 2 | +# ABSTRACT: Trash assets by URL/assetId |
| 3 | +use strict; |
| 4 | +use warnings; |
| 5 | +use WGDev::Command::Ls; |
| 6 | +use 5.008008; |
| 7 | + |
| 8 | +use parent qw(WGDev::Command::Base); |
| 9 | + |
| 10 | +use WGDev (); |
| 11 | + |
| 12 | +sub config_options { |
| 13 | + return qw( |
| 14 | + purge |
| 15 | + restore |
| 16 | + list |
| 17 | + ); |
| 18 | +} |
| 19 | + |
| 20 | +sub process { |
| 21 | + my $self = shift; |
| 22 | + my $wgd = $self->wgd; |
| 23 | + if ($self->option('list')) { |
| 24 | + return $self->list_trash; |
| 25 | + } |
| 26 | + else { |
| 27 | + return $self->trash; |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +sub trash { |
| 32 | + my $self = shift; |
| 33 | + my $wgd = $self->wgd; |
| 34 | + my $error; |
| 35 | + my $method = $self->option('purge') ? 'purge' |
| 36 | + : $self->option('restore') ? 'restore' |
| 37 | + : 'trash'; |
| 38 | + my @asset_specs = $self->arguments; |
| 39 | + ASSET: |
| 40 | + while ( my $asset_spec = shift @asset_specs ) { |
| 41 | + my $asset; |
| 42 | + if ( !eval { $asset = $wgd->asset->find($asset_spec) } ) { |
| 43 | + warn "wgd trash: $asset_spec: No such asset\n"; |
| 44 | + $error++; |
| 45 | + next ASSET; |
| 46 | + } |
| 47 | + my $success = $asset->$method; |
| 48 | + if ($method ne 'restore' && ! $success) { |
| 49 | + warn "wgd trash: unable to $method $asset_spec\n"; |
| 50 | + ++$error; |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + return (! $error); |
| 55 | +} |
| 56 | + |
| 57 | +sub list_trash { |
| 58 | + my $self = shift; |
| 59 | + my $wgd = $self->wgd; |
| 60 | + my $root = $wgd->asset->root; |
| 61 | + my $trashed_assets = $root->getAssetsInTrash(); |
| 62 | + my $ls = WGDev::Command::Ls->new($wgd); |
| 63 | + my $format = '%assetId% %url:-35% %title%'; |
| 64 | + ASSET: |
| 65 | + foreach my $asset ( @{ $trashed_assets } ) { |
| 66 | + next ASSET unless $asset; |
| 67 | + my $output = $ls->format_output( $format, $asset ); |
| 68 | + print $output . "\n"; |
| 69 | + } |
| 70 | + |
| 71 | + return 1; |
| 72 | +} |
| 73 | + |
| 74 | +1; |
| 75 | + |
| 76 | +=head1 SYNOPSIS |
| 77 | +
|
| 78 | + wgd trash [--purge] [--restore] <asset> [<asset> ...] |
| 79 | + wgd trash [--list] |
| 80 | +
|
| 81 | +=head1 DESCRIPTION |
| 82 | +
|
| 83 | +Methods for working with assets in the trash. |
| 84 | +
|
| 85 | +=head1 OPTIONS |
| 86 | +
|
| 87 | +=over 8 |
| 88 | +
|
| 89 | +=item C<--purge> |
| 90 | +
|
| 91 | +Purges the assets from the system instead of putting it into the trash. |
| 92 | +
|
| 93 | +=item C<--restore> |
| 94 | +
|
| 95 | +Restores the assets that have been trashed to the regular, published state. |
| 96 | +
|
| 97 | +=item C<--list> |
| 98 | +
|
| 99 | +Lists all assets in the trash. |
| 100 | +
|
| 101 | +=item C<< <asset> >> |
| 102 | +
|
| 103 | +Either an asset URL or an ID. As many can be specified as desired. |
| 104 | +Prepending with a slash will force it to be interpreted as a URL. |
| 105 | +
|
| 106 | +=back |
| 107 | +
|
| 108 | +=cut |
| 109 | + |
| 110 | +1; |
0 commit comments