App-padconsole
view release on metacpan or search on metacpan
#!/usr/bin/perl
# vim:set sw=4 ts=4 ft=perl expandtab:
use warnings;
use strict;
use Getopt::Std;
use File::Basename;
use IPC::Open3;
use File::Spec::Functions;
use File::Path qw/make_path/;
use Etherpad;
use Mojo::File;
use Mojo::Util qw/encode/;
use Term::ReadLine;
use Config::YAML;
use URI::Escape;
use DateTime;
use Browser::Open qw/open_browser_cmd/;
binmode STDOUT, ":utf8";
BEGIN {
use Exporter ();
use vars qw($VERSION);
$VERSION = '0.13';
}
$Getopt::Std::STANDARD_HELP_VERSION = 1;
sub VERSION_MESSAGE {
my ($handle) = @_;
print $handle "padconsole v$VERSION\n";
}
sub HELP_MESSAGE {
print <<EOF;
(c) 2013 Luc Didry <luc\@didry.org>
This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.
Usage : padconsole [options]
Options :
--help prints this message and exit
--version prints version and exit
-e http://pad.example.com URL of the etherpad instance
-k secretPadApiKey API key for the etherpad instance
-u padUserLogin username for the etherpad instance, if needed
-p padUserPassword password for the etherpad instance
-c /path/to/config/file use a different config file than \$XDG_HOME_DIR/padconsolerc or ~/.config/padconsolerc
-b x-www-browser use this browser to open pads or etherpad instance home page
EOF
}
my %opts;
getopts('e:k:u:p:c:b:', \%opts);
my $config_file;
my $old_conf_dir = (defined $ENV{XDG_HOME_DIR}) ? $ENV{XDG_HOME_DIR} : catdir($ENV{HOME}, '.config');
my $conf_dir = catdir($old_conf_dir, 'padconsole');
make_path $conf_dir unless (-d $conf_dir);
#################
# Options parsing
#################
if (defined $opts{c}) {
if (-f $opts{c}) {
$config_file = $opts{c};
} else {
print STDERR "ERROR: Unable to find '$opts{c}' configuration file.\n";
exit 1;
}
} else {
my $old_config = catfile($old_conf_dir, 'padconsolerc');
$config_file = catfile($conf_dir, 'padconsole.yml');
if (!-f $config_file) {
if (-f $old_config) {
print <<EOF;
sub _current {
print 'Alias : ' , $alias, "\n";
my $instance = $instances{$alias};
print 'Url : ' , $instance->{url} , "\n";
print 'ApiKey : ' , $instance->{key} , "\n";
print 'User : ' , $instance->{user} , "\n" if (defined $instance->{user});
print 'Password : ' , $instance->{passwd} , "\n" if (defined $instance->{passwd});
}
sub _alist {
my @keys = keys %instances;
print join("\n", sort @keys), "\n";
}
sub _use {
$alias = shift @commands;
if (defined $alias) {
if (defined $instances{$alias}) {
my $instance = $instances{$alias};
($url, $key, $user, $passwd) = ($instance->{url}, $instance->{key}, $instance->{user}, $instance->{password});
%args = (
url => $url,
apikey => $key,
);
if (defined $user && defined $passwd) {
$args{user} = $user;
$args{password} = $passwd;
}
$ec = Etherpad->new(\%args);
if (!$ec->check_token()) {
print STDERR 'ERROR: Unable to bind with the etherpad instance.', "\n";
exit 5;
}
$prompt = $alias.' $ ';
} else {
print 'ERROR: Bad instance alias. Unable to get configuration for instance alias ', $alias, "\n";
}
} else {
print 'ERROR: no alias given!', "\n";
}
}
sub _writeconf {
$c->{instances} = \%instances;
$c->write;
}
sub _infos {
my $pad = shift @commands;
if (defined $pad) {
my $revs = $ec->get_revisions_count($pad);
if (defined $revs) {
my @authors = do { my %seen; grep { !$seen{$_}++ } $ec->list_names_of_authors_of_pad($pad) };
my $last_edited = $ec->get_last_edited($pad);
$last_edited =~ s/\d{3}$//;
my $dt = DateTime->from_epoch(epoch => $last_edited);
$last_edited = $dt->strftime('%F %T');
my $separator = (substr($url, -1, 1) eq '/' ) ? 'p/' : '/p/';
printf 'Pad %s'."\n", $pad;
printf ' Number of revisions : %s'."\n", $revs;
printf ' Authors list : %s'."\n", join(', ', sort @authors);
printf ' Last edition : %s'."\n", $last_edited;
printf ' URL : %s%s%s'."\n", $url, $separator, uri_escape($pad);
printf ' Read only URL : %s%s%s'."\n", $url, $separator, uri_escape($ec->get_read_only_id($pad));
}
} else {
print 'ERROR: no pad given!', "\n";
}
}
sub _authors {
my $pad = shift @commands;
if (defined $pad) {
if (defined $ec->get_revisions_count($pad)) {
my @authors = do { my %seen; grep { !$seen{$_}++ } $ec->list_names_of_authors_of_pad($pad) };
printf 'Pad %s: %d authors'."\n".' %s'."\n", $pad, scalar @authors, join "\n ", @authors;
}
} else {
print 'ERROR: no pad given!', "\n";
}
}
sub _revcount {
my $pad = shift @commands;
if (defined $pad) {
if (defined $ec->get_revisions_count($pad)) {
printf 'Pad %s: %d revisions'."\n", $pad, $ec->get_revisions_count($pad);
}
} else {
print 'ERROR: no pad given!', "\n";
}
}
sub _text {
my $pad = shift @commands;
my $rev = shift @commands;
if (defined $pad) {
if (defined $rev) {
print $ec->get_text($pad, $rev);
} else {
print $ec->get_text($pad);
}
} else {
print 'ERROR: no pad given!', "\n";
}
}
sub _text_to_file {
my $path = shift @commands;
my $pad = shift @commands;
my $rev = shift @commands;
( run in 1.479 second using v1.01-cache-2.11-cpan-39bf76dae61 )