Add purges after set/edit commands
[gruntmaster-data.git] / lib / Gruntmaster / App / Command / Edit.pm
CommitLineData
5e26317e
MG
1package Gruntmaster::App::Command::Edit;
2
3use 5.014000;
4use warnings;
5
6our $VERSION = '5999.000_015';
7
8use File::Temp qw/tempfile/;
9use File::Slurp qw/read_file write_file/;
10use Gruntmaster::App '-command';
11use Gruntmaster::Data;
12
8ab124f5
MG
13use Gruntmaster::App::Command::Set;
14BEGIN { *PAGES = *Gruntmaster::App::Command::Set::PAGES }
15
5e26317e
MG
16sub usage_desc { '%c [-cjpu] edit id column' }
17
18sub 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
25sub 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});
8ab124f5 34 purge PAGES->{$self->app->table}.$_ for '', $obj;
5e26317e
MG
35}
36
371;
38__END__
39
40=encoding utf-8
41
42=head1 NAME
43
44Gruntmaster::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
55The get command takes two arguments: an object id and a property name,
56and opens an editor with the value of that property. Upon exiting the
57editor, the property is set to the contents of the file.
58
59=head1 ENVIRONMENT
60
61The editor is taken from the EDITOR environment variable. If this
62variable is unset, the program F<editor> is executed instead.
63
64=head1 SEE ALSO
65
66L<gm>
67
68=head1 AUTHOR
69
70Marius Gavrilescu, E<lt>marius@ieval.roE<gt>
71
72=head1 COPYRIGHT AND LICENSE
73
74Copyright (C) 2014-2015 by Marius Gavrilescu
75
76This library is free software; you can redistribute it and/or modify
77it under the same terms as Perl itself, either Perl version 5.20.1 or,
78at your option, any later version of Perl 5 you may have available.
79
80
81=cut
This page took 0.014762 seconds and 4 git commands to generate.