Beagle

 view release on metacpan or  search on metacpan

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

        );
        $temp->timezone( $bh->info->timezone ) if $bh->info->timezone;
        $temp->author( $self->author || current_user() );

        my $template = $temp->serialize(
            $self->verbose
            ? (
                path    => 1,
                created => 1,
                updated => 1,
                id      => 1,
              )
            : (
                path    => 0,
                created => 0,
                updated => 0,
                id      => 0,
            )
        );
        my $message = '';
        if ( $self->message ) {
            $message = $self->message;
            $message =~ s!^!# !mg;
            $message .= newline();
        }

        $template = encode_utf8( $message . $template );
        my $updated = edit_text( $template );

        if ( !$self->force && $template eq $updated ) {
            puts "aborted.";
            return;
        }

        $entry = $temp->new_from_string( decode_utf8($updated) );
    }

    $entry->timezone( $bh->info->timezone )
      if $bh->info->timezone
          && !$entry->timezone;
    $entry->author( $self->author || current_user() ) unless $entry->author;

    $entry->commit_message( $self->message )
      if $self->message && !$entry->commit_message;
    if ( $bh->create_entry( $entry, commit => 0 ) ) {
        $self->handle_attachments($entry);
    }
    else {
        die "failed to create the entry.";
    }

    $bh->backend->commit( message => $entry->commit_message || $self->message );
    puts "created " . $entry->id . ".";
}

sub handle_attachments {
    my $self   = shift;
    my $parent = shift;
    return unless $self->attachments;
    for my $file ( @{ $self->attachments } ) {
        if ( -f encode( locale_fs => $file ) ) {

            require File::Basename;
            my $basename = File::Basename::basename($file);
            my $att      = Beagle::Model::Attachment->new(
                name         => $basename,
                content_file => $file,
                parent_id    => $parent->id,
            );

            current_handle()->create_attachment( $att, commit => 0 );
        }
        else {
            die "$file is not a file or doesn't exist.";
        }
    }
}

sub handle_options {
    my $self = shift;
    my $opt  = shift;
    my $args = shift;

    my @new_args;
    while (@$args) {
        my $arg = shift @$args;
        if ( $arg && $arg =~ /^--([\-\w]+)$/ ) {
            my $field = $1;
            $field =~ s!-!_!g;
            if ( $self->class->can($field) ) {
                $opt->{$field} = shift @$args;
                next;
            }
        }
        push @new_args, $arg;
    }
    @$args = @new_args;

    $opt->{tags} = to_array( delete $opt->{tags} );
    if ( $self->body_file && !defined $opt->{body} ) {
        $opt->{body} = decode(
            $self->body_file_encoding || 'utf8',
            read_file( $self->body_file )
        ) or die $!;
    }
    $opt->{body} = join ' ', @$args if @$args && !defined $opt->{body};

}


1;

__END__

=head1 NAME

Beagle::Cmd::Command::create - create an entry

=head1 SYNOPSIS

    $ beagle create --type bark



( run in 0.832 second using v1.01-cache-2.11-cpan-ceb78f64989 )