| 1 | package Gruntmaster::App::Command::Edit; |
| 2 | |
| 3 | use 5.014000; |
| 4 | use warnings; |
| 5 | |
| 6 | our $VERSION = '5999.000_015'; |
| 7 | |
| 8 | use File::Temp qw/tempfile/; |
| 9 | use File::Slurp qw/read_file write_file/; |
| 10 | use Gruntmaster::App '-command'; |
| 11 | use Gruntmaster::Data; |
| 12 | |
| 13 | use Gruntmaster::App::Command::Set; |
| 14 | BEGIN { *PAGES = *Gruntmaster::App::Command::Set::PAGES } |
| 15 | |
| 16 | sub usage_desc { '%c [-cjpu] edit id column' } |
| 17 | |
| 18 | sub validate_args { |
| 19 | my ($self, $opt, $args) = @_; |
| 20 | my @args = @$args; |
| 21 | $self->usage_error('No table selected') unless $self->app->table; |
| 22 | $self->usage_error('Wrong number of arguments') if @args != 2; |
| 23 | } |
| 24 | |
| 25 | sub execute { |
| 26 | my ($self, $opt, $args) = @_; |
| 27 | my ($obj, $col) = @$args; |
| 28 | my ($fh, $file) = tempfile 'gruntmaster-problem-editXXXX', TMPDIR => 1, UNLINK => 1; |
| 29 | write_file $fh, db->select($self->app->table, $col, {id => $obj})->flat; |
| 30 | close $fh; |
| 31 | my $editor = $ENV{EDITOR} // 'editor'; |
| 32 | system $editor, $file; |
| 33 | db->update($self->app->table, {$col => scalar read_file $file}, {id => $obj}); |
| 34 | purge PAGES->{$self->app->table}.$_ for '', $obj; |
| 35 | } |
| 36 | |
| 37 | 1; |
| 38 | __END__ |
| 39 | |
| 40 | =encoding utf-8 |
| 41 | |
| 42 | =head1 NAME |
| 43 | |
| 44 | Gruntmaster::App::Command::Edit - edit a property of an object |
| 45 | |
| 46 | =head1 SYNOPSIS |
| 47 | |
| 48 | gm -u edit MGV name |
| 49 | gm -p edit aplusb level |
| 50 | gm -c edit test_ct description |
| 51 | gm -j edit 100 result_text |
| 52 | |
| 53 | =head1 DESCRIPTION |
| 54 | |
| 55 | The get command takes two arguments: an object id and a property name, |
| 56 | and opens an editor with the value of that property. Upon exiting the |
| 57 | editor, the property is set to the contents of the file. |
| 58 | |
| 59 | =head1 ENVIRONMENT |
| 60 | |
| 61 | The editor is taken from the EDITOR environment variable. If this |
| 62 | variable is unset, the program F<editor> is executed instead. |
| 63 | |
| 64 | =head1 SEE ALSO |
| 65 | |
| 66 | L<gm> |
| 67 | |
| 68 | =head1 AUTHOR |
| 69 | |
| 70 | Marius Gavrilescu, E<lt>marius@ieval.roE<gt> |
| 71 | |
| 72 | =head1 COPYRIGHT AND LICENSE |
| 73 | |
| 74 | Copyright (C) 2014-2015 by Marius Gavrilescu |
| 75 | |
| 76 | This library is free software; you can redistribute it and/or modify |
| 77 | it under the same terms as Perl itself, either Perl version 5.20.1 or, |
| 78 | at your option, any later version of Perl 5 you may have available. |
| 79 | |
| 80 | |
| 81 | =cut |