App-Muter

 view release on metacpan or  search on metacpan

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

$App::Muter::Main::VERSION = '0.003000';
use App::Muter::Backend ();
use App::Muter::Chain   ();
use FindBin             ();
use Getopt::Long        ();
use IO::Handle          ();
use IO::File            ();

use File::stat;

sub script {
    my (@args) = @_;

    my $chain = '';
    my $help;
    my $verbose;
    my $reverse;
    Getopt::Long::GetOptionsFromArray(
        \@args,
        'chain|c=s'  => \$chain,
        'verbose|v'  => \$verbose,
        'reverse|r!' => \$reverse,
        'help'       => \$help
        ) or
        return usage(1);

    load_backends();

    return usage(0, $verbose) if $help;
    return usage(1) unless $chain;

    run_chain($chain, $reverse, load_handles(\@args), \*STDOUT);

    return 0;
}

sub _uniq {    ## no critic(RequireArgUnpacking)
    my %seen;
    return grep { !$seen{$_}++ } @_;
}

sub load_backends {
    App::Muter::Registry->instance->load_backends();
    return;
}

sub load_handles {
    my ($files) = @_;
    my @handles = map { IO::File->new($_, 'r') } @$files;
    @handles = (\*STDIN) unless @handles;
    return \@handles;
}

sub run_chain {
    my ($chain, $reverse, $handles, $stdout, $blocksize) = @_;

    $chain = App::Muter::Chain->new($chain, $reverse);
    $blocksize ||= 512;

    foreach my $io (@$handles) {
        $io->binmode(1);
        while ($io->read(my $buf, $blocksize)) {
            $stdout->print($chain->process($buf));
        }
    }
    $stdout->print($chain->final(''));
    return;
}

sub usage {
    my ($ret, $verbose) = @_;
    my $fh = $ret ? \*STDERR : \*STDOUT;
    $fh->print(<<'EOM');
muter [-r | --reverse] -c CHAIN | --chain CHAIN [FILES...]
muter [--verbose] --help

Modify the bytes in the concatentation of FILES (or standard input) by using the
specification in CHAIN.

CHAIN is a colon-separated list of encoding transform.  A transform can be
prefixed with - to reverse it (if possible).  A transform can be followed by one
or more comma-separated parenthesized arguments as well.  Instead of
parentheses, a single comma may be used.

For example, '-hex:hash(sha256):base64' (or '-hex:hash,sha256:base64') decodes a
hex-encoded string, hashes it with SHA-256, and converts the result to base64.

If --reverse is specified, reverse the order of transforms in order and in sense.

The following transforms are available:
EOM
    my $reg = App::Muter::Registry->instance;
    foreach my $name ($reg->backends) {
        $fh->print("  $name\n");
        my $meta = $reg->info($name);
        if ($meta->{args} && ref($meta->{args}) eq 'HASH') {
            my @keys = sort keys %{$meta->{args}};
            if ($verbose) {
                $fh->printf("    %-10s: %s\n", $_, $meta->{args}->{$_})
                    for @keys;
            }
            else {
                $fh->print("    ", join(', ', sort keys %{$meta->{args}}),
                    "\n");
            }
        }
    }
    return $ret;
}

package App::Muter::Interface;
$App::Muter::Interface::VERSION = '0.003000';
sub process {
    my ($chain, $data) = @_;

    $chain = App::Muter::Chain->new($chain);
    my $result = $chain->process($data);
    $result .= $chain->final('');

    return $result;
}



( run in 0.505 second using v1.01-cache-2.11-cpan-8f98c5d2c55 )