Beagle

 view release on metacpan or  search on metacpan

lib/Beagle/Cmd/Command/spread.pm  view on Meta::CPAN

    isa           => 'Bool',
    is            => 'rw',
    documentation => 'not prompt for confirmation',
    traits        => ['Getopt'],
);

has 'template' => (
    isa           => 'Str',
    is            => 'rw',
    documentation => 'xslate template string',
    traits        => ['Getopt'],
);

has 'template-file' => (
    isa           => 'Str',
    is            => 'rw',
    documentation => 'xslate template file path',
    accessor      => 'template_file',
    traits        => ['Getopt'],
);

has 'to' => (
    isa           => 'Str',
    is            => 'rw',
    documentation => 'to whom?',
    traits        => ['Getopt'],
    default       => '',
);

has 'from' => (
    isa           => 'Str',
    is            => 'rw',
    documentation => 'from who?',
    traits        => ['Getopt'],
    default       => '',
);

has 'subject' => (
    isa           => 'Str',
    is            => 'rw',
    documentation => 'subject',
    traits        => ['Getopt'],
    default       => '',
);

no Any::Moose;
__PACKAGE__->meta->make_immutable;

sub execute {
    my ( $self, $opt, $args ) = @_;
    $args = $self->resolve_ids( $args );
    die 'beagle spread id1 id2 [...]' unless @$args;

    die "can't use both --template and --template-file"
      if defined $self->template && defined $self->template_file;

    my $cmd = $self->command;
    for my $i (@$args) {
        my @ret = resolve_entry( $i, handle => current_handle() || undef );
        unless (@ret) {
            @ret = resolve_entry($i) or die_entry_not_found($i);
        }
        die_entry_ambiguous( $i, @ret ) unless @ret == 1;
        my $id    = $ret[0]->{id};
        my $bh    = $ret[0]->{handle};
        my $entry = $ret[0]->{entry};

        my $msg;

        my $template;

        if ( defined $self->template_file ) {
            my $name = $self->template_file;
            $name .= '.tx' unless $name =~ /\.tx$/;

            my $file;
            if ( -f $name ) {
                $file = $name;
            }
            else {
                for my $root( spread_template_roots ) {
                    if ( -f catfile( $root, $name ) ) {
                        $file = catfile( $root, $name );
                        last;
                    }
                }
                die "template file $name doesn't exist" unless defined $file;
            }

            $template = read_file($file);
        }
        elsif ( defined $self->template ) {
            $template = $self->template;
        }

        my $to = $self->to;
        my $from = $self->from || current_user();
        my $subject = $self->subject || $entry->summary(80);

        if ( defined $template ) {
            require Text::Xslate;
            my $tx = Text::Xslate->new(
                function => {
                    shorten => sub {
                        my $url = shift;
                        return $url unless defined $url;
                        return `shorten $url`;
                    },
                }
            );

            $msg = $tx->render_string(
                $template,
                {
                    handle  => $bh,
                    entry   => $entry,
                    id      => $id,
                    url     => $bh->info->url . '/entry/' . $id,
                    to      => $to,
                    from    => $from,
                    subject => $subject,



( run in 0.476 second using v1.01-cache-2.11-cpan-0bb4e1dffa6 )