Skip to content

Commit

Permalink
use docopt
Browse files Browse the repository at this point in the history
  • Loading branch information
vti committed Mar 18, 2014
1 parent 96f5305 commit c2cf9b3
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 58 deletions.
63 changes: 20 additions & 43 deletions bin/rmachine
Expand Up @@ -3,55 +3,32 @@
use strict;
use warnings;

use Getopt::Long;
use Docopt;
use App::rmachine;
use App::rmachine::backup;

my @command_argv;
my @commands = qw(help backup);
my $command;
for (my $i = 0; $i < @ARGV; $i++) {
if (grep { $ARGV[$i] eq $_ } @commands) {
$command = $ARGV[$i];
@command_argv = @ARGV[$i + 1 .. $#ARGV];
splice @ARGV, $i + 1;
last;
}
}

if (!$command && !@ARGV) {
die <<"EOH";
Usage: rmachine [--version]
rmachine <command> [arguments]
Available commands:
@commands
For help run
rmachine help <command>
EOH
}

my $version = '';
GetOptions('version' => \$version)
or die("Error in command line arguments\n");
my $opts = docopt();
use Data::Dumper; warn Dumper($opts);

if ($version) {
if ($opts->{'--version'}) {
print 'rmachine: ' . $App::rmachine::VERSION, "\n";
exit 0;
}
elsif ($opts->{backup}) {
App::rmachine::backup->new(%$opts)->run;
}

if ($command eq 'help') {
my ($help_for_command) = @command_argv;
die "Usage: rmachine help <command>" unless $help_for_command;
__END__
my $command_class = 'App::rmachine::' . $help_for_command;
print $command_class->help;
}
else {
my $command_class = 'App::rmachine::' . $command;
$command_class->new(\@command_argv)->run;
}
=head1 SYNOPSIS
rmachine backup [--config=rmachine.conf]
[--log=rmachine.log]
[--force]
[--test]
[--quiet]
rmachine -h | --help
rmachine --version
-h --help Show this screen.
--version Show version.
1 change: 1 addition & 0 deletions cpanfile
Expand Up @@ -10,6 +10,7 @@ requires 'Config::Tiny';
requires 'Error::Tiny';
requires 'File::ReadBackwards';
requires 'Algorithm::Cron';
requires 'Docopt';

requires 'Test::Fatal';
requires 'Test::MonkeyMock';
Expand Down
21 changes: 6 additions & 15 deletions lib/App/rmachine/backup.pm
Expand Up @@ -13,21 +13,19 @@ use App::rmachine::command_runner;

sub new {
my $class = shift;
my ($argv) = @_;
my (%opts) = @_;

my $self = {};
bless $self, $class;

open my $lock, '<', __FILE__ or die "Can't lock myself\n";
flock $lock, LOCK_EX | LOCK_NB or do { warn "Already running\n"; exit 255 };

my %params = $self->_parse_argv($argv);

$self->{config_file} = $params{config_file} || $self->_locate_config_file;
$self->{log_file} = $params{log_file} || $self->_locate_log_file;
$self->{quiet} = $params{quiet};
$self->{test} = $params{test};
$self->{force} = $params{force};
$self->{config_file} = $opts{'--config'} || $self->_locate_config_file;
$self->{log_file} = $opts{'--log'} || $self->_locate_log_file;
$self->{quiet} = $opts{'--quiet'};
$self->{test} = $opts{'--test'};
$self->{force} = $opts{'--force'};

$self->{logger} = App::rmachine::logger->new(
log_file => $self->{log_file},
Expand All @@ -37,13 +35,6 @@ sub new {
return $self;
}

sub help {
my $self = shift;

return <<"EOH";
EOH
}

sub run {
my $self = shift;

Expand Down

0 comments on commit c2cf9b3

Please sign in to comment.