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