From 5e26317ea435eaa5dd8d8fa7ccef5fd0e883b998 Mon Sep 17 00:00:00 2001 From: Marius Gavrilescu Date: Sun, 31 Jan 2016 13:56:11 +0000 Subject: [PATCH] Add an edit subcommand to gm --- MANIFEST | 1 + lib/Gruntmaster/App/Command/Edit.pm | 77 +++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 lib/Gruntmaster/App/Command/Edit.pm diff --git a/MANIFEST b/MANIFEST index 793b4d7..7c5cc2b 100644 --- a/MANIFEST +++ b/MANIFEST @@ -5,6 +5,7 @@ gruntmaster-opener lib/Gruntmaster/App.pm lib/Gruntmaster/App/Command.pm lib/Gruntmaster/App/Command/Add.pm +lib/Gruntmaster/App/Command/Edit.pm lib/Gruntmaster/App/Command/Get.pm lib/Gruntmaster/App/Command/List.pm lib/Gruntmaster/App/Command/Rerun.pm diff --git a/lib/Gruntmaster/App/Command/Edit.pm b/lib/Gruntmaster/App/Command/Edit.pm new file mode 100644 index 0000000..24785ab --- /dev/null +++ b/lib/Gruntmaster/App/Command/Edit.pm @@ -0,0 +1,77 @@ +package Gruntmaster::App::Command::Edit; + +use 5.014000; +use warnings; + +our $VERSION = '5999.000_015'; + +use File::Temp qw/tempfile/; +use File::Slurp qw/read_file write_file/; +use Gruntmaster::App '-command'; +use Gruntmaster::Data; + +sub usage_desc { '%c [-cjpu] edit id column' } + +sub validate_args { + my ($self, $opt, $args) = @_; + my @args = @$args; + $self->usage_error('No table selected') unless $self->app->table; + $self->usage_error('Wrong number of arguments') if @args != 2; +} + +sub execute { + my ($self, $opt, $args) = @_; + my ($obj, $col) = @$args; + my ($fh, $file) = tempfile 'gruntmaster-problem-editXXXX', TMPDIR => 1, UNLINK => 1; + write_file $fh, db->select($self->app->table, $col, {id => $obj})->flat; + close $fh; + my $editor = $ENV{EDITOR} // 'editor'; + system $editor, $file; + db->update($self->app->table, {$col => scalar read_file $file}, {id => $obj}); +} + +1; +__END__ + +=encoding utf-8 + +=head1 NAME + +Gruntmaster::App::Command::Edit - edit a property of an object + +=head1 SYNOPSIS + + gm -u edit MGV name + gm -p edit aplusb level + gm -c edit test_ct description + gm -j edit 100 result_text + +=head1 DESCRIPTION + +The get command takes two arguments: an object id and a property name, +and opens an editor with the value of that property. Upon exiting the +editor, the property is set to the contents of the file. + +=head1 ENVIRONMENT + +The editor is taken from the EDITOR environment variable. If this +variable is unset, the program F is executed instead. + +=head1 SEE ALSO + +L + +=head1 AUTHOR + +Marius Gavrilescu, Emarius@ieval.roE + +=head1 COPYRIGHT AND LICENSE + +Copyright (C) 2014-2015 by Marius Gavrilescu + +This library is free software; you can redistribute it and/or modify +it under the same terms as Perl itself, either Perl version 5.20.1 or, +at your option, any later version of Perl 5 you may have available. + + +=cut -- 2.39.2