Permalink
Browse files

add edit role command

  • Loading branch information...
1 parent c46756e commit 6b5c7dffa1fb705ca3e046751fd2f2963600ace1 @xdg xdg committed May 11, 2012
Showing with 22 additions and 24 deletions.
  1. +16 −18 lib/Pantry/App/Command/edit.pm
  2. +6 −6 t/app/edit.t
@@ -23,34 +23,31 @@ sub command_type {
}
sub valid_types {
- return qw/node/
+ return qw/node role/
}
sub _edit_node {
my ($self, $opt, $name) = @_;
+ $self->_edit_obj($opt, 'node', $name);
+}
- my @editor = defined $ENV{EDITOR} ? split / /, $ENV{EDITOR} : ();
- if ( @editor ) {
- $editor[0] = can_run($editor[0]);
- }
+sub _edit_role {
+ my ($self, $opt, $name) = @_;
+ $self->_edit_obj($opt, 'role', $name);
+}
- if ( @editor ) {
- $self->_edit_file($name, @editor);
+sub _edit_obj {
+ my ($self, $opt, $type, $name) = @_;
+
+ my @editor = defined $ENV{EDITOR} ? split / /, $ENV{EDITOR} : ();
+ if ( @editor && (my $bin = can_run($editor[0])) ) {
+ $editor[0] = $bin;
}
else {
$self->usage_error( "EDITOR not set or not found" );
}
- return;
-}
-
-#--------------------------------------------------------------------------#
-# Internal
-#--------------------------------------------------------------------------#
-
-sub _edit_file {
- my ($self, $name, @editor) = @_;
- my $path = $self->pantry->node($name)->path;
+ my $path = $self->pantry->$type($name)->path;
if ( -e $path ) {
system( @editor, $path ) and die "System failed!: $!";
eval { decode_json(read_file($path,{ binmode => ":raw" })) };
@@ -60,7 +57,8 @@ sub _edit_file {
}
}
else {
- $self->usage_error("Node '$name' does not exist");
+ $type = ucfirst $type;
+ $self->usage_error("$type '$name' does not exist");
}
}
View
@@ -17,12 +17,12 @@ my @cases = (
name => 'foo.example.com',
new => sub { my ($p,$n) = @_; $p->node($n) },
},
-# {
-# label => "role",
-# type => "role",
-# name => 'web',
-# new => sub { my ($p,$n) = @_; $p->role($n) },
-# },
+ {
+ label => "role",
+ type => "role",
+ name => 'web',
+ new => sub { my ($p,$n) = @_; $p->role($n) },
+ },
);
for my $c ( @cases ) {

0 comments on commit 6b5c7df

Please sign in to comment.