App-padconsole

 view release on metacpan or  search on metacpan

padconsole  view on Meta::CPAN


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;
The default configuration file path has changed.
Do you want me to move your configuration file to the new place ($config_file)?
Please be aware the configuration will not be loaded from the old place ($old_config) anymore.
Type yes if you want me to move the configuration file.
EOF
            my $choice = <>;
            chomp $choice;
            if ($choice eq 'yes') {
                my $c = Config::YAML->new(
                    config => $old_config,
                    output => $config_file,
                );
                $c->write;
                unlink $old_config;
            } else {
                $config_file = undef;
            }
        } else {
            open my $cfile, '>', $config_file or die "Unable to create $config_file: $!\n";
            close $cfile;
        }
    }
    chmod 0600, $config_file if (defined $config_file);
}
my ($c, $alias, $url, $key, $user, $passwd, %instances);
my $history_file = catfile($conf_dir, 'history');
my @history;

if (defined $config_file) {
    $c = Config::YAML->new(
        config => $config_file,
        output => $config_file,
    );

    if (defined $c->{instances}) {
        %instances                   = %{$c->{instances}};
        my @keys                     = keys %instances;
        @keys                        = sort @keys;
        $alias                       = shift @keys;
        my $first                    = $instances{$alias};
        ($url, $key, $user, $passwd) = ($first->{url}, $first->{key}, $first->{user}, $first->{password});
    }
}

if (defined $opts{e} || defined $opts{k} || defined $opts{u} || defined $opts{p}) {
    if (!defined $opts{e} || $opts{e} eq '1' || !defined $opts{k} || $opts{k} eq '1') {
        print STDERR 'ERROR: Not enough arguments.', "\n";
        HELP_MESSAGE();
        exit 2;
    }

    ($alias, $url, $key, $user, $passwd) = ($opts{e}, $opts{e}, $opts{k}, $opts{u}, $opts{p});

    $alias =~ s#https?://|/$##g;
    $url   =~ s#/$##;

    $instances{$alias} = {
        url    => $url,
        key    => $key,
        user   => $user,
        passwd => $passwd
    };
}

# Browser test
$c->{browser} = $opts{b} if (defined $opts{b});
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;
}



( run in 0.585 second using v1.01-cache-2.11-cpan-cdf2f3d4e48 )