]> iEval git - gruntmaster-data.git/blobdiff - gruntmaster-problem
Simplify problem_list condition
[gruntmaster-data.git] / gruntmaster-problem
index afcb1be7135e065fdc342d694a0297bd328f0f11..7fe2f76a7a13a4e262a4d6170b47e90f02cf7e8f 100755 (executable)
@@ -3,15 +3,25 @@ use v5.14;
 
 use Gruntmaster::Data;
 
+use File::Temp qw/tempfile/;
+
 use IO::Prompter [ -style => 'bold', '-stdio', '-verbatim' ];
-use File::Slurp qw/read_file/;
+use File::Slurp qw/read_file write_file/;
 use JSON qw/encode_json/;
 use Term::ANSIColor qw/RED RESET/;
 use Getopt::Long qw/:config require_order/;
 
+use constant LEVEL_VALUES => {
+       beginner => 100,
+       easy => 250,
+       medium => 500,
+       hard => 1000,
+};
+
 ##################################################
 
-my $db = Gruntmaster::Data->connect('dbi:Pg:');
+my $dsn = $ENV{GRUNTMASTER_DSN} // 'dbi:Pg:';
+my $db = Gruntmaster::Data->connect($dsn);
 
 sub cmd_help{
        exec perldoc => $0
@@ -23,8 +33,10 @@ sub cmd_add{
        my $private = prompt('Private?', '-yn') eq 'y';
        my $contest = prompt 'Contest';
        my $author = prompt 'Problem author (full name)';
+       my $writer = prompt 'Problem statement writer (full name)';
        my $owner = prompt 'Problem owner (username)';
        my $level = prompt 'Problem level', -menu => "beginner\neasy\nmedium\nhard";
+       my $value = LEVEL_VALUES->{$level};
        my $statement = read_file prompt 'File with problem statement', -complete => 'filenames';
        my $generator = prompt 'Generator', -menu => "File\nRun\nUndef";
        my $runner = prompt 'Runner', -menu => "File\nVerifier\nInteractive";
@@ -57,8 +69,10 @@ sub cmd_add{
                id => $id,
                name => $name,
                level => $level,
+               value => $value,
                statement => $statement,
                author => $author,
+               writer => $writer,
                owner => $owner,
                generator => $generator,
                runner => $runner,
@@ -81,9 +95,9 @@ sub cmd_add{
 sub cmd_set{
        my $file;
        GetOptions ( 'file!' => \$file );
-       my ($id, %values) = @_;
+       my ($id, %values) = @ARGV;
        %values = map { $_ => scalar read_file $values{$_} } keys %values if $file;
-       $db->problem($id)->update(\%values)
+       $db->problem($id)->update(\%values);
 }
 
 sub cmd_get{
@@ -91,13 +105,24 @@ sub cmd_get{
        say $db->problem($id)->get_column($col)
 }
 
+sub cmd_edit{
+       my ($id, $col) = @_;
+       my ($fh, $file) = tempfile 'gruntmaster-problem-editXXXX', TMPDIR => 1, UNLINK => 1;
+       write_file $fh, $db->problem($id)->get_column($col);
+       close $fh;
+       my $editor = $ENV{EDITOR} // 'editor';
+       system $editor, $file;
+       $db->problem($id)->update({$col => scalar read_file $file}) or die "$!";
+}
+
 sub cmd_list{
        local $, = "\n";
        say map {$_->id} $db->problems->all
 }
 
 sub cmd_rm{
-       $db->problem(shift)->delete
+       my ($id) = @_;
+       $db->problem($id)->delete;
 }
 
 sub cmd_show{
@@ -142,6 +167,7 @@ gruntmaster-problem - shell interface to Gruntmaster 6000 problems
   gruntmaster-problem show problem_id
   gruntmaster-problem set [--file] problem_id key value
   gruntmaster-problem get problem_id key
+  gruntmaster-problem edit problem_id key
 
 =head1 DESCRIPTION
 
@@ -173,6 +199,10 @@ Sets the I<key> configuration option of problem I<id> to I<value>.
 
 Get the value of the I<key> configuration option of problem I<id>
 
+=item B<edit> I<id> I<key>
+
+Opens an editor with the value of the I<key> configuration option. After the editor exits, the option is updated to the value of the file.
+
 =item B<set> --file I<id> I<key> I<file>
 
 Sets the I<key> configuration option of problem I<id> to the contents of the file I<file>.
This page took 0.024685 seconds and 4 git commands to generate.