]> git.mjollnir.org Git - cil.git/commitdiff
Added the 'edit' command
authorAndrew Chilton <andychilton@gmail.com>
Sat, 19 Apr 2008 12:01:32 +0000 (00:01 +1200)
committerAndrew Chilton <andychilton@gmail.com>
Sat, 19 Apr 2008 12:01:32 +0000 (00:01 +1200)
bin/cil
lib/CIL/Base.pm
lib/CIL/Issue.pm

diff --git a/bin/cil b/bin/cil
index bc6c9dd36a961c941925c1153540f87c9a896de4..64077b051809749600119b829108b399de0d6abe 100755 (executable)
--- a/bin/cil
+++ b/bin/cil
@@ -7,7 +7,6 @@ use Data::Dumper;
 use CIL::Issue;
 use Term::CallEditor;
 use File::Touch;
-use YAML;
 use File::Glob ':glob';
 use File::Basename;
 
@@ -16,6 +15,7 @@ my $COMMANDS = {
     list    => 1,
     add     => 1,
     show    => 1,
+    edit    => 1,
 };
 
 my $new_issue_text = <<"EOF";
@@ -36,7 +36,7 @@ EOF
     my ($command) = shift;
     unless ( defined $command and exists $COMMANDS->{$command} ) {
         usage();
-        exit 1;
+        exit 2;
     }
 
     if ( $command eq 'init' ) {
@@ -57,6 +57,14 @@ EOF
     elsif ( $command eq 'add' ) {
         add();
     }
+    elsif ( $command eq 'edit' ) {
+        my ($issue_name) = @ARGV;
+        edit($issue_name);
+    }
+
+    else {
+        fatal("program error when trying command '$command'");
+    }
 
 }
 
@@ -122,8 +130,7 @@ sub show {
     # firstly, read the issue in
     my $issue = CIL::Issue->new_load_issue($issue_name);
     unless ( defined $issue ) {
-        print STDERR "Couldn't load issue '$issue'\n";
-        return;
+        fatal("Couldn't load issue '$issue'");
     }
     display_issue_full( $issue );
 }
@@ -151,6 +158,59 @@ sub add {
     display_issue_full( $issue );
 }
 
+sub edit {
+    my ($issue_name) = @_;
+
+    my $issue = CIL::Issue->new_load_issue($issue_name);
+    unless ( defined $issue ) {
+        fatal("Couldn't load issue '$issue'");
+    }
+
+    my $summary     = $issue->Summary;
+    my $status      = $issue->Status;
+    my $created_by  = $issue->CreatedBy;
+    my $labels      = $issue->Labels;
+    my $description = $issue->Description;
+
+    # create the ini file, then edit it
+    my $edit_issue_text = <<"EOF";
+[Issue]
+
+Summary     = $summary
+Status      = $status
+CreatedBy   = $created_by
+Labels      = $labels
+Description = <<END_OF_DESCRIPTION
+$description
+END_OF_DESCRIPTION
+EOF
+
+    # read in the new issue text
+    my $fh = solicit( $edit_issue_text );
+
+    my $issue_edited;
+    eval {
+        $issue_edited = CIL::Issue->new_parse_issue( $fh );
+    };
+    if ( $@ ) {
+        fatal("couldn't parse issue: $@");
+    }
+    unless ( defined $issue_edited ) {
+        fatal("couldn't parse issue (program error)");
+    }
+
+    # for the fields we're interested in, set them
+    foreach my $field ( qw(Summary Status CreatedBy Labels Description) ) {
+        $issue->$field( $issue_edited->$field() );
+    }
+    if ( $issue->changed ) {
+        $issue->save();
+    }
+    else {
+        msg('Issue not changed');
+    }
+}
+
 ## ----------------------------------------------------------------------------
 
 sub check_paths {
@@ -225,6 +285,11 @@ sub text {
     msg "";
 }
 
+sub err {
+    print STDERR ( defined $_[0] ? $_[0] : '' );
+    print STDERR "\n";
+}
+
 sub fatal {
     my ($msg) = @_;
     chomp $msg;
@@ -244,6 +309,7 @@ Commands:
    add
    list
    show    <issue>
+   edit    <issue>
 
 See <http://kapiti.geek.nz/software/cil.html> for further information.
 Report bugs to <andychilton -at- gmail -dot- com>.
index 121d372ac0a0d124dcca151c1beacb58de14709c..b0c58cf4edc4855d3cbe17d9462ce08d7aeba885 100644 (file)
@@ -58,6 +58,11 @@ sub updated {
     $self->{Changed} = '1';
 }
 
+sub changed {
+    my ($self) = @_;
+    return $self->{Changed};
+}
+
 ## ----------------------------------------------------------------------------
 1;
 ## ----------------------------------------------------------------------------
index 0fe4c8b51a30d9618ff7333cf5aa01b7f40159b9..26fa9965046c6c1bd13cf199004562eb8f87c20e 100644 (file)
@@ -11,7 +11,7 @@ use YAML qw(LoadFile DumpFile);
 use base qw(CIL::Base);
 __PACKAGE__->mk_accessors(qw(Name Summary Status Labels Comments));
 
-my @ATTRS = ( qw(Name Summary Description CreatedBy Status Labels Comments) );
+my @FIELDS = ( qw(Name Summary Description CreatedBy Status Labels Comments) );
 
 ## ----------------------------------------------------------------------------
 
@@ -19,6 +19,8 @@ sub new {
     my ($proto) = @_;
     my $class = ref $proto || $proto;
     my $self = {};
+    $self->{data}    = {};
+    $self->{Changed} = 0;
     bless $self, $class;
     $self->inserted;
     return $self;
@@ -46,9 +48,9 @@ sub new_load_issue {
     }
 
     # my $issue = CIL::Issue->new();
-    foreach my $attr ( qw(Summary Name Description CreatedBy Status Labels Inserted Updated) ) {
+    foreach my $field ( qw(Summary Name Description CreatedBy Status Labels Inserted Updated) ) {
         # modify the data directly, otherwise Updated will kick in
-        $issue->{data}{$attr} = $cfg->val( 'Issue', $attr );
+        $issue->{data}{$field} = $cfg->val( 'Issue', $field );
     }
     $issue->{data}{Comments}    = [];
 
@@ -69,9 +71,14 @@ sub new_parse_issue {
     }
 
     my $issue = CIL::Issue->new();
-    foreach my $attr ( qw(Summary Name Description CreatedBy Status Labels Inserted Updated) ) {
+    foreach my $field ( qw(Summary Name Description CreatedBy Status Labels Inserted Updated) ) {
         # modify the data directly, otherwise Updated will kick in
-        $issue->set_no_update($attr, $cfg->val( 'Issue', $attr ));
+        my $value = $cfg->val( 'Issue', $field );
+        next unless defined $value;
+
+        $value =~ s/^\s*//;
+        $value =~ s/\s*$//;
+        $issue->set_no_update($field, $value);
     }
     $issue->set_no_update('Comments', []);
     return $issue;
@@ -87,8 +94,8 @@ sub save {
 sub reset {
     my ($self) = @_;
 
-    foreach my $attr ( @ATTRS ) {
-        delete $self->{$attr};
+    foreach my $field ( @FIELDS ) {
+        delete $self->{$field};
     }
 }