App-TrashUtils

 view release on metacpan or  search on metacpan

lib/App/TrashUtils.pm  view on Meta::CPAN

            schema => 'true*',
            cmdline_aliases => {W=>{}},
        },
    },
    features => {
        dry_run => 1,
    },
    examples => [
        {
            summary => 'Restore two files named "f1" and "f2" from trash',
            argv => ['f1', 'f2'],
            test => 0,
            'x.doc.show_result' => 0,
        },
        {
            summary => 'Restore all .pl and .pm files from trash',
            argv => ['*.pl', '*.pm'],
            test => 0,
            'x.doc.show_result' => 0,
        },
    ],
};
sub trash_restore {
    require File::Trash::FreeDesktop;
    require String::Wildcard::Bash;

    my %args = @_;

    my $trash = File::Trash::FreeDesktop->new;
    for my $file (@{ $args{files} }) {
        my $opts = {};
        if (!$args{no_wildcard} && String::Wildcard::Bash::contains_wildcard($file)) {
            if ($file =~ m!/!) {
                $opts->{path_wildcard} = $file;
            } else {
                $opts->{filename_wildcard} = $file;
            }
        } else {
            if ($file =~ m!/!) {
                $opts->{path} = $file;
            } else {
                $opts->{filename} = $file;
            }
        }

        if ($args{-dry_run}) {
            log_info "Listing files in trash: %s", $opts;
            my @ct = $trash->list_contents($opts);
            for my $e (@ct) {
                log_info "[DRY_RUN] Restoring path: %s ...", $e->{path};
            }
        } else {
            log_info "Restoring: %s ...", $opts;
            $trash->recover($opts);
        }
    }
    [200, "OK"];
}

1;
# ABSTRACT: Utilities related to desktop trash

__END__

=pod

=encoding UTF-8

=head1 NAME

App::TrashUtils - Utilities related to desktop trash

=head1 VERSION

This document describes version 0.004 of App::TrashUtils (from Perl distribution App-TrashUtils), released on 2023-08-07.

=head1 DESCRIPTION

This distributions provides the following command-line utilities:

=over

=item * L<trash-list>

=item * L<trash-list-trashes>

=item * L<trash-put>

=item * L<trash-restore>

=item * L<trash-rm>

=back

Prior to C<App::TrashUtils>, there is already C<trash-cli> [1] which is written
in Python. App::TrashUtils aims to scratch some itches and offers some
enhancements:

=over

=item * trash-restore accepts multiple arguments

=item * trash-list accepts files/wildcard patterns

=item * dry-run mode

=item * tab completion

=item * written in Perl

Lastly, App::TrashUtils is written in Perl and is easier to hack for Perl
programmers.

=back

=head1 FUNCTIONS


=head2 trash_list

Usage:

 trash_list(%args) -> [$status_code, $reason, $payload, \%result_meta]

List contents of trash directories.

Examples:

=over

=item * List all files in trash cans:

lib/App/TrashUtils.pm  view on Meta::CPAN

 trash_rm(files => ["f1", "f2"]);

=item * Permanently remove all .pl and .pm files in trash:

 trash_rm(files => ["*.pl", "*.pm"]);

=back

This function is not exported.

This function supports dry-run operation.


Arguments ('*' denotes required arguments):

=over 4

=item * B<files>* => I<array[str]>

Wildcard pattern will be interpreted (unless when --no-wildcard option is specified).

=item * B<no_wildcard> => I<true>

(No description)


=back

Special arguments:

=over 4

=item * B<-dry_run> => I<bool>

Pass -dry_run=E<gt>1 to enable simulation mode.

=back

Returns an enveloped result (an array).

First element ($status_code) is an integer containing HTTP-like status code
(200 means OK, 4xx caller error, 5xx function error). Second element
($reason) is a string containing error message, or something like "OK" if status is
200. Third element ($payload) is the actual result, but usually not present when enveloped result is an error response ($status_code is not 2xx). Fourth
element (%result_meta) is called result metadata and is optional, a hash
that contains extra information, much like how HTTP response headers provide additional metadata.

Return value:  (any)

=head1 HOMEPAGE

Please visit the project's homepage at L<https://metacpan.org/release/App-TrashUtils>.

=head1 SOURCE

Source repository is at L<https://github.com/perlancar/perl-App-TrashUtils>.

=head1 SEE ALSO

[1] L<https://github.com/andreafrancia/trash-cli>, Python-based CLIs delated to
desktop trash.

L<File::Trash::FreeDesktop>

Alternative CLI's: L<trash-u> (from L<App::trash::u>) which supports undo.

=head1 AUTHOR

perlancar <perlancar@cpan.org>

=head1 CONTRIBUTING


To contribute, you can send patches by email/via RT, or send pull requests on
GitHub.

Most of the time, you don't need to build the distribution yourself. You can
simply modify the code, then test via:

 % prove -l

If you want to build the distribution (e.g. to try to install it locally on your
system), you can install L<Dist::Zilla>,
L<Dist::Zilla::PluginBundle::Author::PERLANCAR>,
L<Pod::Weaver::PluginBundle::Author::PERLANCAR>, and sometimes one or two other
Dist::Zilla- and/or Pod::Weaver plugins. Any additional steps required beyond
that are considered a bug and can be reported to me.

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2023 by perlancar <perlancar@cpan.org>.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=head1 BUGS

Please report any bugs or feature requests on the bugtracker website L<https://rt.cpan.org/Public/Dist/Display.html?Name=App-TrashUtils>

When submitting a bug or request, please include a test-file or a
patch to an existing test-file that illustrates the bug or desired
feature.

=cut



( run in 1.885 second using v1.01-cache-2.11-cpan-df04353d9ac )