App-padconsole

 view release on metacpan or  search on metacpan

padconsole  view on Meta::CPAN

if (defined $c->{browser}) {
    my ($wtr, $rdr, $err);
    my $pid = open3($wtr, $rdr, $err, 'which', $c->{browser});
    waitpid( $pid, 0 );
    my $xbro = $? >> 8;
    if ($xbro) {
        printf 'ERROR: specified browser %s not found!'."\n", $c->{browser};
        exit 7;
    }
    $ENV{BROWSER} = $c->{browser};
}

if (!defined $url || !defined $key) {
    print STDERR 'ERROR: No configuration found and not enough arguments!', "\n";
    HELP_MESSAGE();
    exit 3;
}

# Etherpad binding args
my %args = (
    url => $url,
    apikey => $key,
);
if (defined $user && defined $passwd) {
    $args{user} = $user;
    $args{password} = $passwd;
}

#################
# Initiate instance binding
#################
my $ec = Etherpad->new(\%args);
if (!$ec->check_token()) {
    print STDERR 'ERROR: Unable to bind with the etherpad instance.', "\n";
    exit 4;
}

#################
# Create console
#################
print <<EOF;
Welcome on padconsole.
Type 'help' to get some help, 'exit' to exit. Easy, isn't it?
EOF

my $term = Term::ReadLine->new('padconsole');
if (my $attr = $term->Attribs) {
    $attr->{completion_function} = \&_complete_word;
}

if (-f $history_file) {
    open my $hist, '<', $history_file or die "Unable to open $history_file: $!";
    while (defined(my $line = <$hist>)) {
        chomp $line;
        _addtohistory($line);
        $term->addhistory($line);
    }
    close $hist;
}

my $prompt = $alias.' $ ';

# For autocomplete
my @words = qw(exit         help         count
               list         search       delete
               text         text_to_file html
               html_to_file revcount     authors
               infos        writeconf    use
               alist        current      open);

my @commands;
while (defined($_ = $term->readline($prompt))) {
    chomp;
    @commands   = split(' ', $_);
    my $command = shift @commands || '';
    _addtohistory($_) unless ($command eq 'exit');

         if ($command eq 'exit') {
        _exit();
    } elsif ($command eq 'help') {
        _help();
    } elsif ($command eq 'count') {
        _count();
    } elsif ($command eq 'list') {
        _list();
    } elsif ($command eq 'search') {
        _search();
    } elsif ($command eq 'delete') {
        _delete()
    } elsif ($command eq 'text') {
        _text()
    } elsif ($command eq 'text_to_file') {
        _text_to_file()
    } elsif ($command eq 'html') {
        _html()
    } elsif ($command eq 'html_to_file') {
        _html_to_file()
    } elsif ($command eq 'revcount') {
        _revcount()
    } elsif ($command eq 'authors') {
        _authors()
    } elsif ($command eq 'infos') {
        _infos()
    } elsif ($command eq 'writeconf') {
        _writeconf()
    } elsif ($command eq 'use') {
        _use()
    } elsif ($command eq 'alist') {
        _alist()
    } elsif ($command eq 'current') {
        _current()
    } elsif ($command eq 'open') {
        _open()
    } elsif ($command eq 'create') {
        _create()
    } else {
        print 'WARNING: Unknown command', "\n";
        _help();
    }
}
print "\n";
_exit();

sub _help {
    print <<EOF
Available commands:
  - help                            : print this message
  - exit                            : exit program
  - count                           : print the number of existing pads
  - list                            : list all the existing pads (20 items per page)
  - search <PATTERN>                : print the list of the pads which name matches the pattern (Perl regex) (20 items per page)
  - delete <pad1> <pad2>            : delete the pads pad1 pad2 (have to be separated by space)

padconsole  view on Meta::CPAN

    }
}

sub _open {
    my $pad = shift @commands;
    my $url = $instances{$alias}->{url};
    if (defined $pad) {
        if (defined $ec->get_revisions_count($pad)) {
            my $separator = (substr($url, -1, 1) eq '/' ) ? 'p/' : '/p/';
            $url .= $separator.$pad;
        } else {
            printf 'ERROR: The pad %s doesn\'t exist on %s. Not opening the browser.'."\n", $pad, $alias;
            return;
        }
    }
    my ($wtr, $rdr, $err);
    unless (my $pid = open3($wtr, $rdr, $err, open_browser_cmd(), $url)) {
        undef $term;
        return;
    }

}

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 {



( run in 1.779 second using v1.01-cache-2.11-cpan-6aa56a78535 )