Make starting gitalist pointed at somewhere settable from an environment variable...
[catagits/Gitalist.git] / lib / Gitalist / Model / GitRepos.pm
index 5d2f66b..f988d8c 100644 (file)
@@ -3,18 +3,45 @@ package Gitalist::Model::GitRepos;
 use Moose;
 use Gitalist::Git::Repo;
 use MooseX::Types::Common::String qw/NonEmptySimpleStr/;
+use Moose::Util::TypeConstraints;
 use namespace::autoclean;
 
 extends 'Catalyst::Model';
 
 with 'Catalyst::Component::InstancePerContext';
 
-has repo_dir => (
+my $repo_dir_t = subtype NonEmptySimpleStr,
+    where { -d $_ },
+    message { 'Cannot find repository dir: "' . $_ . '", please set up gitalist.conf, or set GITALIST_REPO_DIR environment or pass the --repo_dir parameter when starting the application' };
+
+has config_repo_dir => (
     isa => NonEmptySimpleStr,
     is => 'ro',
-    required => 1,
+    init_arg => 'repo_dir',
+    predicate => 'has_config_repo_dir',
 );
 
+has repo_dir => (
+    isa => $repo_dir_t,
+    is => 'ro',
+    lazy_build => 1
+);
+
+sub _build_repo_dir {
+    my $self = shift;
+    $ENV{GITALIST_REPO_DIR} ?
+        $ENV{GITALIST_REPO_DIR}
+      : $self->has_config_repo_dir
+      ? $self->config_repo_dir
+        : '';
+}
+
+after BUILD => sub {
+    my $self = shift;
+    $self->repo_dir; # Explode loudly at app startup time if there is no repos
+                     # dir, rather than on first hit
+};
+
 sub build_per_context_instance {
     my ($self, $app) = @_;