App-Toolforge-MixNMatch

 view release on metacpan or  search on metacpan

MixNMatch.pm  view on Meta::CPAN

sub _command_download {
	my ($catalog_id, $output_file) = @_;

	if (! defined $catalog_id || $catalog_id !~ m/^\d+$/ms) {
		return (1, 'Missing or bad catalog ID.');
	}
	if (! defined $output_file) {
		$output_file = $catalog_id.'.json';
	}

	my $json = _download_catalog_detail($catalog_id);
	barf($output_file, $json);

	print "Catalog with '$catalog_id' ID was saved to '$output_file'.\n";

	return (0, undef);
}

sub _command_print {
	my ($json_file_or_catalog_id, $print_options) = @_;

	my $json;
	if (-r $json_file_or_catalog_id) {
		$json = slurp($json_file_or_catalog_id);
	} elsif ($json_file_or_catalog_id =~ m/^\d+$/ms) {
		$json = _download_catalog_detail($json_file_or_catalog_id);
	} else {
		return (1, "Doesn't exist JSON file or catalog ID for print.");
	}

	my $opts_hr = _process_print_options($print_options);

	my $struct_hr = decode_json($json);
	my $obj = Toolforge::MixNMatch::Struct::Catalog::struct2obj($struct_hr->{'data'});
	my $ret = Toolforge::MixNMatch::Print::Catalog::print($obj, $opts_hr);
	print encode_utf8($ret)."\n";

	return (0, undef);
}

sub _download_catalog_detail {
	my $catalog_id = shift;

	my $uri = sprintf $URI_CATALOG_DETAIL, $catalog_id;
	my $json = get($uri);
	if (! defined $json) {
		err "Cannot download '$uri'.";
	}

	return $json;
}

sub _process_print_options {
	my $print_options = shift;

	if (! defined $print_options) {
		return;
	}

	my $opts_hr = {};
	foreach my $print_option (split m/,/, $print_options) {
		$opts_hr->{$print_option} = 1;
	}

	return $opts_hr;
}

# Run script.
sub run {
	my $self = shift;

	# Process arguments.
	$self->{'_opts'} = {
		'h' => 0,
	};
	if (! getopts('h', $self->{'_opts'}) || $self->{'_opts'}->{'h'}
		|| @ARGV < 1) {

		print STDERR "Usage: $0 [-h] [--version] [command] [command_args ..]\n";
		print STDERR "\t-h\t\tPrint help.\n";
		print STDERR "\t--version\tPrint version.\n";
		print STDERR "\tcommand\t\tCommand (diff, download, print).\n\n";
		print STDERR "\tcommand 'diff' arguments:\n";
		print STDERR "\t\tjson_file1 - JSON file #1\n";
		print STDERR "\t\tjson_file2 - JSON file #2\n";
		print STDERR "\t\t[print_options] - Print options (type, count, year_months, users)\n";
		print STDERR "\tcommand 'download' arguments:\n";
		print STDERR "\t\tcatalog_id - Catalog ID\n";
		print STDERR "\t\t[output_file] - Output file (default is catalog_id.json)\n";
		print STDERR "\tcommand 'print' arguments:\n";
		print STDERR "\t\tjson_file or catalog_id - Catalog ID or JSON file\n";
		print STDERR "\t\t[print_options] - Print options (type, count, year_months, users)\n";
		return 1;
	}
	my $command = shift @ARGV;
	my @command_args = @ARGV;

	my ($return, $error);
	if ($command eq 'diff') {
		($return, $error) = _command_diff(@command_args);
	} elsif ($command eq 'download') {
		($return, $error) = _command_download(@command_args);
	} elsif ($command eq 'print') {
		($return, $error) = _command_print(@command_args);
	} else {
		print STDERR "Command '$command' doesn't supported.\n";
		return 1;
	}
	if ($return == 1) {
		print STDERR $error."\n";
		return 1;
	}

	return 0;
}

1;

__END__

=pod



( run in 1.284 second using v1.01-cache-2.11-cpan-af0e5977854 )