package Gruntmaster::App::Command::Set;

use 5.014000;
use warnings;

our $VERSION = '5999.000_004';

use Gruntmaster::App '-command';
use Gruntmaster::Data;

use File::Slurp qw/read_file/;

sub opt_spec {
	['file!', 'Use the contents of a file as value'],
}

sub usage_desc { "%c [-cjpu] set id column value [column value ...]\n%c [-cjpu] set --file id column filename [column filename ...]" }

sub validate_args {
	my ($self, $opt, $args) = @_;
	my @args = @$args;
	$self->usage_error('No table selected') unless $self->app->table;
	$self->usage_error('Not enough arguments provided') if @args < 3;
	$self->usage_error('The number of arguments must be odd') unless @args % 2;
}

sub execute {
	my ($self, $opt, $args) = @_;
	my ($id, %values) = @$args;
	%values = map { $_ => scalar read_file $values{$_} } keys %values if $opt->{file};
	db->update($self->app->table, \%values, {id => $id});
}

1;
__END__
