From 4ac5550856dfbe4cd64a33b2f0b90b54d8bc0586 Mon Sep 17 00:00:00 2001 From: Andrew Chilton Date: Sun, 20 Apr 2008 00:01:32 +1200 Subject: [PATCH] Added the 'edit' command --- bin/cil | 74 +++++++++++++++++++++++++++++++++++++++++++++--- lib/CIL/Base.pm | 5 ++++ lib/CIL/Issue.pm | 21 +++++++++----- 3 files changed, 89 insertions(+), 11 deletions(-) diff --git a/bin/cil b/bin/cil index bc6c9dd..64077b0 100755 --- 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 = <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 + edit See for further information. Report bugs to . diff --git a/lib/CIL/Base.pm b/lib/CIL/Base.pm index 121d372..b0c58cf 100644 --- a/lib/CIL/Base.pm +++ b/lib/CIL/Base.pm @@ -58,6 +58,11 @@ sub updated { $self->{Changed} = '1'; } +sub changed { + my ($self) = @_; + return $self->{Changed}; +} + ## ---------------------------------------------------------------------------- 1; ## ---------------------------------------------------------------------------- diff --git a/lib/CIL/Issue.pm b/lib/CIL/Issue.pm index 0fe4c8b..26fa996 100644 --- a/lib/CIL/Issue.pm +++ b/lib/CIL/Issue.pm @@ -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}; } } -- 2.39.5