Add the ability to provide a whitelist of repos.
[catagits/Gitalist.git] / lib / Gitalist / Model / CollectionOfRepos.pm
1 package Gitalist::Model::CollectionOfRepos;
2
3 use Moose;
4 use Gitalist::Git::CollectionOfRepositories::FromDirectoryRecursive;
5 use Gitalist::Git::CollectionOfRepositories::FromListOfDirectories;
6 use Gitalist::Git::CollectionOfRepositories::FromDirectory::WhiteList;
7 use MooseX::Types::Moose qw/Maybe ArrayRef/;
8 use MooseX::Types::Common::String qw/NonEmptySimpleStr/;
9 use Moose::Util::TypeConstraints;
10 use Moose::Autobox;
11 use namespace::autoclean;
12
13 extends 'Catalyst::Model';
14
15 with 'Catalyst::Component::InstancePerContext';
16
17 my $repo_dir_t = subtype NonEmptySimpleStr,
18     where { -d $_ },
19     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' };
20
21 my $arrayof_repos_dir_t = subtype ArrayRef[$repo_dir_t],
22     where { 1 },
23     message { 'Cannot find repository directories listed in config - these are invalid directories: ' . join(', ', $_->flatten) };
24
25 coerce $arrayof_repos_dir_t,
26     from NonEmptySimpleStr,
27     via { [ $_ ] };
28
29 has config_repo_dir => (
30     isa => NonEmptySimpleStr,
31     is => 'ro',
32     init_arg => 'repo_dir',
33     predicate => 'has_config_repo_dir',
34 );
35
36 has repo_dir => (
37     isa => $repo_dir_t,
38     is => 'ro',
39     lazy_build => 1
40 );
41
42 has repos => (
43     isa => $arrayof_repos_dir_t,
44     is => 'ro',
45     default => sub { [] },
46     traits => ['Array'],
47     handles => {
48         _repos_count => 'count',
49     },
50     coerce => 1,
51 );
52
53 has export_ok => (
54     is  => 'ro',
55     isa => 'Str',
56 );
57
58 has whitelist => (
59     is  => 'ro',
60     isa => 'Str',
61 );
62
63
64 sub _build_repo_dir {
65     my $self = shift;
66     $ENV{GITALIST_REPO_DIR} ?
67         $ENV{GITALIST_REPO_DIR}
68       : $self->has_config_repo_dir
69       ? $self->config_repo_dir
70         : '';
71 }
72
73 after BUILD => sub {
74     my $self = shift;
75     # Explode loudly at app startup time if there is no list of
76     # repositories or repos dir, rather than on first hit
77     $self->_repos_count || $self->repo_dir;
78 };
79
80 sub build_per_context_instance {
81     my ($self, $app) = @_;
82
83     my %args = (export_ok => $self->export_ok || '');
84     my $class;
85     if($self->whitelist && -f $self->whitelist) {
86         $class = 'Gitalist::Git::CollectionOfRepositories::FromDirectory::WhiteList';
87         $args{repo_dir}  = $self->repo_dir;
88         $args{whitelist} = $self->whitelist;
89     } elsif ($self->_repos_count) {
90         $class = 'Gitalist::Git::CollectionOfRepositories::FromListOfDirectories';
91         $args{repos} = $self->repos;
92     }
93     else {
94         $class = 'Gitalist::Git::CollectionOfRepositories::FromDirectoryRecursive';
95         $args{repo_dir} = $self->repo_dir;
96     }
97
98     return $class->new(%args);
99 }
100
101 __PACKAGE__->meta->make_immutable;
102
103 __END__
104
105 =head1 AUTHORS
106
107 See L<Gitalist> for authors.
108
109 =head1 LICENSE
110
111 See L<Gitalist> for the license.
112
113 =cut