Mojolicious-Command-nopaste

 view release on metacpan or  search on metacpan

lib/Mojolicious/Command/nopaste/Service.pm  view on Meta::CPAN

package Mojolicious::Command::nopaste::Service;
use Mojo::Base 'Mojolicious::Command';

use Mojo::UserAgent;
use Mojo::Util qw/decode monkey_patch/;
use Getopt::Long qw(GetOptionsFromArray :config no_ignore_case); # no_auto_abbrev

our $USAGE = <<END;
USAGE:

  $0 command SERVICE [OPTIONS] [FILES]

OPTIONS:
  Note that not all options are relevant for all services.

  --channel, -c       The channel for the service's pastebot or to post via Mojo::IRC
                          e.g. perl, #perl, irc://irc.perl.org:6667/perl
  --copy, -x          Copy the resulting URL to the clipboard (requires Clipboard.pm)
  --description, -d   Description or title of the nopaste
  --name, -n          Your name or nick, used for the pastebin and/or IRC
  --language, -l      Language for syntax highlighting, defaults to 'perl'
  --open, -o          Open a browser to the url (requires Browser::Open)
  --paste, -p         Read contents from clipboard (requires Clipboard.pm)
  --private, -P       Mark the paste as private (note: silently ignored if not relevant for service)
  --token, -t         A file containing an access token, or else the token string itself
  --update, -u        Update a paste of a given id

END

has usage => sub {
  my $self = shift;
  my $usage = $USAGE;
  if (my $add = $self->service_usage) {
    $usage .= "\n$add";
  }
  return $usage;
};

has [qw/channel name desc service_usage token update/];
has [qw/copy open private irc_handled/] => 0;
has clip => sub {
  die "Clipboard module not available. Do you need to install it?\n"
    unless eval 'use Clipboard; 1';
  monkey_patch 'Clipboard::Xclip',
    copy  => \&_xclip_copy,
    paste => \&_xclip_paste;
  return 'Clipboard';
};
has files    => sub { [] };
has language => 'perl';
has text     => sub { shift->slurp };
has ua       => sub { Mojo::UserAgent->new->max_redirects(10) };

sub run {
  my ($self, @args) = @_;
  GetOptionsFromArray( \@args,
    'channel|c=s'     => sub { $self->channel($_[1])           },
    'copy|x'          => sub { $self->copy($_[1])              },
    'description|d=s' => sub { $self->desc($_[1])              },
    'name|n=s'        => sub { $self->name($_[1])              },
    'language|l=s'    => sub { $self->language($_[1])          },
    'open|o'          => sub { $self->open($_[1])              },
    'paste|p'         => sub { $self->text($self->clip->paste) },
    'private|P'       => sub { $self->private($_[1])           },
    'token|t=s'       => sub { $self->add_token($_[1])         },
    'update|u=s'      => sub { $self->update($_[1])            },
  );
  $self->files(\@args);
  my $url = $self->paste or return;
  say $url;
  $self->clip->copy($url) if $self->copy;
  if ($self->open) {
    die "Browser::Open module not available. Do you need to install it?\n"
      unless eval { require Browser::Open; 1 };
    Browser::Open::open_browser($url);
  }
  if ($self->channel and not $self->irc_handled) {
    $self->post_to_irc($url);
  }
}

sub add_token {
  my ($self, $token) = @_;



( run in 1.351 second using v1.01-cache-2.11-cpan-302cb4679cc )