Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

add list roles command

  • Loading branch information...
commit e9676ae0758a4d66cd6ae0018623e77e19b818d2 1 parent c0bf147
@dagolden authored
View
9 lib/Pantry/App/Command/list.pm
@@ -19,7 +19,7 @@ sub command_type {
}
sub valid_types {
- return qw/node nodes/
+ return qw/node nodes role roles/
}
sub _list_nodes {
@@ -29,6 +29,13 @@ sub _list_nodes {
*_list_node = *_list_nodes; # alias
+sub _list_roles {
+ my ($self, $opt) = @_;
+ say $_ for $self->pantry->all_roles;
+}
+
+*_list_role = *_list_roles; # alias
+
1;
=for Pod::Coverage options validate
View
33 lib/Pantry/Model/Pantry.pm
@@ -25,7 +25,7 @@ has path => (
default => sub { dir(".")->absolute }
);
-sub _env_path {
+sub _env_dir {
my ($self, $env) = @_;
$env //= '_default';
my $path = $self->path->subdir("environments", $env);
@@ -33,16 +33,21 @@ sub _env_path {
return $path;
}
-sub _role_path {
- my ($self, $role_name) = @_;
+sub _role_dir {
+ my ($self) = @_;
my $path = $self->path->subdir("roles");
$path->mkpath;
- return $path->file("${role_name}.json");
+ return $path;
+}
+
+sub _role_path {
+ my ($self, $role_name) = @_;
+ return $self->_role_dir->file("${role_name}.json");
}
sub _node_path {
my ($self, $node_name, $env) = @_;
- return $self->_env_path($env)->file("${node_name}.json");
+ return $self->_env_dir($env)->file("${node_name}.json");
}
=method all_nodes
@@ -57,7 +62,7 @@ a count of nodes.
sub all_nodes {
my ($self, $env) = @_;
my @nodes = sort map { s/\.json$//r } map { $_->basename }
- $self->_env_path($env)->children;
+ $self->_env_dir($env)->children;
return @nodes;
}
@@ -84,6 +89,22 @@ sub node {
}
}
+=method all_roles
+
+ my @roles = $pantry->all_roles;
+
+In list context, returns a list of roles. In scalar context, returns
+a count of roles.
+
+=cut
+
+sub all_roles {
+ my ($self, $env) = @_;
+ my @roles = sort map { s/\.json$//r } map { $_->basename }
+ $self->_role_dir->children;
+ return @roles;
+}
+
=method C<role>
my $node = $pantry->role("web");
View
12 t/app/list.t
@@ -15,12 +15,12 @@ my @cases = (
names => ['foo.example.com', 'bar.example.com'],
new => sub { my ($p,$n) = @_; $p->node($n) },
},
-# {
-# label => "roles",
-# type => "role",
-# names => ['web', 'db'],
-# new => sub { my ($p,$n) = @_; $p->role($n) },
-# },
+ {
+ label => "roles",
+ type => "role",
+ names => ['web', 'db'],
+ new => sub { my ($p,$n) = @_; $p->role($n) },
+ },
);
for my $c ( @cases ) {
Please sign in to comment.
Something went wrong with that request. Please try again.