Add an edit subcommand to gm
[gruntmaster-data.git] / lib / Gruntmaster / App / Command / Edit.pm
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 sub usage_desc { '%c [-cjpu] edit id column' }
14
15 sub 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
22 sub 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
33 1;
34 __END__
35
36 =encoding utf-8
37
38 =head1 NAME
39
40 Gruntmaster::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
51 The get command takes two arguments: an object id and a property name,
52 and opens an editor with the value of that property. Upon exiting the
53 editor, the property is set to the contents of the file.
54
55 =head1 ENVIRONMENT
56
57 The editor is taken from the EDITOR environment variable. If this
58 variable is unset, the program F<editor> is executed instead.
59
60 =head1 SEE ALSO
61
62 L<gm>
63
64 =head1 AUTHOR
65
66 Marius Gavrilescu, E<lt>marius@ieval.roE<gt>
67
68 =head1 COPYRIGHT AND LICENSE
69
70 Copyright (C) 2014-2015 by Marius Gavrilescu
71
72 This library is free software; you can redistribute it and/or modify
73 it under the same terms as Perl itself, either Perl version 5.20.1 or,
74 at your option, any later version of Perl 5 you may have available.
75
76
77 =cut
This page took 0.023711 seconds and 4 git commands to generate.