App-TrashUtils
view release on metacpan or search on metacpan
lib/App/TrashUtils.pm view on Meta::CPAN
[200, "OK", [map {$_->{path}} @contents]];
}
}
$SPEC{trash_list_trashes} = {
v => 1.1,
summary => 'List trash directories',
args => {
home_only => {
schema => 'bool*',
},
},
};
sub trash_list_trashes {
require File::Trash::FreeDesktop;
my %args = @_;
my @trashes = File::Trash::FreeDesktop->new(
home_only => $args{home_only},
)->list_trashes;
[200, "OK", \@trashes];
}
$SPEC{trash_put} = {
v => 1.1,
summary => 'Put files into trash',
args => {
files => {
'x.name.is_plural' => 1,
'x.name.singular' => 'file',
schema => ['array*', of=>'pathname*'],
req => 1,
pos => 0,
slurpy => 1,
},
},
features => {
dry_run => 1,
},
examples => [
{
summary => 'Trash two files',
argv => ['file1', 'file2.txt'],
test => 0,
'x.doc.show_result' => 0,
},
],
};
sub trash_put {
require File::Trash::FreeDesktop;
require Perinci::Object::EnvResultMulti;
my %args = @_;
my $trash = File::Trash::FreeDesktop->new;
my $res = Perinci::Object::EnvResultMulti->new;
for my $file (@{ $args{files} }) {
my @st = lstat $file;
if (!(-e _)) {
$res->add_result(404, "File not found: $file", {item_id=>$file});
next;
}
if ($args{-dry_run}) {
log_info "[DRY_RUN] Trashing %s ...", $file;
$res->add_result(200, "Trashed (DRY_RUN)", {item_id=>$file});
next;
}
log_info "Trashing %s ...", $file;
eval { $trash->trash($file) };
if ($@) {
$res->add_result(500, "Can't trash: $file: $@", {item_id=>$file});
next;
}
$res->add_result(200, "Trashed", {item_id=>$file});
}
$res->as_struct;
}
$SPEC{trash_rm} = {
v => 1.1,
summary => 'Permanently remove files in trash',
args => {
files => {
'x.name.is_plural' => 1,
'x.name.singular' => 'file',
summary => 'Wildcard pattern will be interpreted (unless when --no-wildcard option is specified)',
schema => ['array*', of=>'str*'],
req => 1,
pos => 0,
slurpy => 1,
element_completion => \&_complete_trashed_filenames,
},
no_wildcard => {
schema => 'true*',
cmdline_aliases => {W=>{}},
},
},
features => {
dry_run => 1,
},
examples => [
{
summary => 'Permanently remove files named "f1" and "f2" in trash',
argv => ['f1', 'f2'],
test => 0,
'x.doc.show_result' => 0,
},
{
summary => 'Permanently remove all .pl and .pm files in trash',
argv => ['*.pl', '*.pm'],
test => 0,
'x.doc.show_result' => 0,
},
],
};
sub trash_rm {
require File::Trash::FreeDesktop;
require String::Wildcard::Bash;
my %args = @_;
( run in 1.106 second using v1.01-cache-2.11-cpan-39bf76dae61 )