From: Andrew Chilton Date: Sat, 30 Jul 2011 10:27:25 +0000 (+1200) Subject: Issue:12 - Fixed, can now run in any dir in the project X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=dcd016bdee1af7a6010bbf83e45576109e389f3c;p=cil.git Issue:12 - Fixed, can now run in any dir in the project --- diff --git a/bin/cil b/bin/cil index f20b443..c740b98 100755 --- a/bin/cil +++ b/bin/cil @@ -111,11 +111,21 @@ my %BOOLEAN_ARGS = ( # make a new $cil object my $cil = CIL->new(); - # for all commands (except init), we need to know we can see the proper paths - # (ie. issues/) - CIL::Utils->check_paths( $cil ) - unless $command_name eq 'init'; + # do only the init command locally, do it first and exit - much easier + if ( $command_name eq 'init' ) { + $command->run($cil, $args, @ARGV); + exit 0; + } + + # for all other commands, we need to know we can see the proper paths (ie. '.cil') + my $base_dir = CIL::Utils->get_basedir( $cil ); + unless ( $base_dir ) { + Getopt::Mixed::abortMsg("Couldn't find '.cil' file - is this a 'cil' project?"); + exit 2; + } + # set the BaseDir and read in the other configs + $cil->BaseDir( $base_dir ); $cil->read_config_user(); $cil->read_config_file(); @@ -125,11 +135,6 @@ my %BOOLEAN_ARGS = ( $command->run($cil, $args, @ARGV); } -## ---------------------------------------------------------------------------- -# hooks - -# none yet - ## ---------------------------------------------------------------------------- # helper functions for this command line tool diff --git a/lib/CIL.pm b/lib/CIL.pm index 548ada3..ee82ff2 100644 --- a/lib/CIL.pm +++ b/lib/CIL.pm @@ -38,7 +38,7 @@ use Module::Pluggable use base qw(Class::Accessor); __PACKAGE__->mk_accessors(qw( - IssueDir + BaseDir IssueDir StatusStrict StatusAllowed StatusOpen StatusClosed LabelStrict LabelAllowed DefaultNewStatus @@ -49,6 +49,7 @@ __PACKAGE__->mk_accessors(qw( )); my $defaults = { + BaseDir => q{.}, # the dir where the .cil file is IssueDir => 'issues', # the dir to save the issues in StatusStrict => 0, # whether to complain if a status is invalid LabelStrict => 0, # whether to complain if a label is invalid @@ -85,6 +86,7 @@ sub new { # if we have been passed it in, use it, else use the default $self->$key( $cfg->{$key} || $defaults->{$key} ); } + return $self; } @@ -240,7 +242,7 @@ sub read_config_user { sub read_config_file { my ( $self ) = @_; - my $filename = '.cil'; + my $filename = $self->BaseDir() . q{/.cil}; # since we might not have a '.cil' file yet (in the case where we're calling 'init', # then we should just return whatever the defaults are @@ -275,7 +277,7 @@ sub read_config_file { } # set each config item - $self->IssueDir( $cfg->{IssueDir} ); + $self->IssueDir( $self->BaseDir() . q{/} . $cfg->{IssueDir} ); $self->UseGit( $cfg->{UseGit} ); # Status info diff --git a/lib/CIL/Command/List.pm b/lib/CIL/Command/List.pm index cdb5b03..98fbad4 100644 --- a/lib/CIL/Command/List.pm +++ b/lib/CIL/Command/List.pm @@ -33,8 +33,6 @@ sub name { 'list' } sub run { my ($self, $cil, $args) = @_; - CIL::Utils->check_paths($cil); - # find all the issues my $issues = $cil->get_issues(); $issues = CIL::Utils->filter_issues( $cil, $issues, $args ); diff --git a/lib/CIL/Command/ListLabels.pm b/lib/CIL/Command/ListLabels.pm index 7ba69e8..a7a7c57 100644 --- a/lib/CIL/Command/ListLabels.pm +++ b/lib/CIL/Command/ListLabels.pm @@ -33,8 +33,6 @@ sub name { 'list-labels' } sub run { my ($self, $cil, $args) = @_; - CIL::Utils->check_paths($cil); - # find all the issues my $issues = $cil->get_issues(); $issues = CIL::Utils->filter_issues( $cil, $issues, $args ); diff --git a/lib/CIL/Command/Summary.pm b/lib/CIL/Command/Summary.pm index f5a3ab6..2a5e325 100644 --- a/lib/CIL/Command/Summary.pm +++ b/lib/CIL/Command/Summary.pm @@ -33,8 +33,6 @@ sub name { 'summary' } sub run { my ($self, $cil, $args) = @_; - CIL::Utils->check_paths($cil); - # find all the issues my $issues = $cil->get_issues(); $issues = CIL::Utils->filter_issues( $cil, $issues, $args ); diff --git a/lib/CIL/Utils.pm b/lib/CIL/Utils.pm index 563cf34..6ca3cf4 100644 --- a/lib/CIL/Utils.pm +++ b/lib/CIL/Utils.pm @@ -26,6 +26,8 @@ use warnings; use Carp; use File::Slurp; use File::Temp qw(tempfile); +use Cwd; +use File::Basename; use Email::Find; use POSIX qw(getpgrp tcgetpgrp); use Fcntl qw(:DEFAULT :flock); @@ -533,13 +535,20 @@ sub text { ## ---------------------------------------------------------------------------- # system -sub check_paths { +sub get_basedir { my ($class, $cil) = @_; - # make sure an issue directory is available - unless ( $cil->dir_exists($cil->IssueDir) ) { - $class->fatal("couldn't find '" . $cil->IssueDir . "' directory"); + # ok, we just need to check that we can find a '.cil' file somewhere + my $dir = cwd(); + while ( ! -f qq{$dir/.cil} ) { + $dir = dirname($dir); + if ( $dir eq q{/} ) { + $class->fatal("couldn't find '.cil' file"); + } } + + # ok, we've found our .cil file, return this directory + return $dir; } sub ans {