#!/usr/bin/perl

=head1 NAME

moglistkeys -- Lists keys out of a MogileFS domain

=head1 SYNOPSIS

    $ moglistkeys --trackers=host --domain=foo --key_prefix="/foo/bar/"

=head1 DESCRIPTION

If you store your MogileFS keys in a logical "structure", you may use this
tool to view lists of subsets of keys. Note that this is not going to be
equivalent to "cd" and "ls" tools, as listing "foo/" will list everything
underneath, so it's more akin to "ls -R"

=head1 OPTIONS

=over

=item --trackers=host1:7001,host2:7001

Use these MogileFS trackers to negotiate with.

=item --domain=<domain>

Set the MogileFS domain to use.

=item --key_prefix="/foo/bar/"

Search for keys starting with this prefix. Can be an arbitrary string.

=back

=head1 AUTHOR

Dormando E<lt>L<dormando@rydia.net>E<gt>

=head1 BUGS

None known.

=head1 LICENSE

Licensed for use and redistribution under the same terms as Perl itself.

=cut

# TODO: Add ways to limit # of keys displayed

use strict;
use warnings;

use lib './lib';
use MogileFS::Utils;

my $util = MogileFS::Utils->new;
my $usage = "--trackers=host --domain=foo --key_prefix='bar/'";
my $c = $util->getopts($usage, 'key_prefix=s');

my $mogc = $util->client;

$mogc->foreach_key(prefix => $c->{key_prefix}, sub {
    my $key = shift;
    print $key, "\n";
});

if ($mogc->errcode) {
    print STDERR "Error listing files: ", $mogc->errstr, "\n";
}
