#!/usr/bin/env perl
package yto;
our $VERSION = '0.038';
# ABSTRACT: Change YAML to another format (like JSON)

use ETL::Yertl;
use Pod::Usage::Return qw( pod2usage );
use Getopt::Long qw( GetOptionsFromArray );
use ETL::Yertl::Format;
use ETL::Yertl::FormatStream;
use IO::Async::Loop;

$|++; # no buffering

sub main {
    my ( $class, @argv ) = @_;
    my %opt;
    GetOptionsFromArray( \@argv, \%opt,
        'help|h',
        'version',
        'delimiter|d=s',
    );
    return pod2usage(0) if $opt{help};
    if ( $opt{version} ) {
        print "yto version $yto::VERSION (Perl $^V)\n";
        return 0;
    }

    my ( $format, @files ) = @argv;

    if ( !$format ) {
        return pod2usage( "ERROR: Must give a format" );
    }

    my $out_format = eval { ETL::Yertl::Format->get( $format, %opt ) };
    if ( $@ ) {
        warn "ERROR: $@\n";
        return 1;
    }

    my $loop = IO::Async::Loop->new;

    my $out = ETL::Yertl::FormatStream->new_for_stdout(
        format => $out_format,
    );
    $loop->add( $out );

    push @files, "-" unless @files;
    for my $file ( @files ) {

        # We're doing a similar behavior to <>, but manually for easier testing.
        my %args = (
            on_doc => sub {
                my ( $self, $doc, $eof ) = @_;
                $out->write( $doc );
            },
            on_read_eof => sub { $loop->stop },
        );
        my $in;
        if ( $file eq '-' ) {
            $in = ETL::Yertl::FormatStream->new_for_stdin( %args );
        }
        else {
            open my $fh, '<', $file or do {
                warn "Could not open file '$file' for reading: $!\n";
                next;
            };
            $in = ETL::Yertl::FormatStream->new(
                read_handle => $fh,
                %args,
            );
        }
        $loop->add( $in );
        $loop->run;
    }

    return 0;
}

exit __PACKAGE__->main( @ARGV ) unless caller(0);

__END__

=pod

=head1 NAME

yto - Change YAML to another format (like JSON)

=head1 VERSION

version 0.038

=head1 SYNOPSIS

    yto <format> [<file>...]

    yto csv [-d <delimiter>] [<file>...]

    yto -h|--help|--version

=head1 DESCRIPTION

This program takes a stream of YAML documents (on STDIN or file arguments),
and prints them in the desired format.

=head1 ARGUMENTS

=head2 format

The format to output. Currently supported formats: JSON

=head2 <file>

A YAML file to read. The special file "-" refers to STDIN. If no files are
specified, read STDIN.

=head1 OPTIONS

=head2 -d | --delimiter

The delimiter to use for the C<csv> format. Defaults to C<,>.

=head2 -h | --help

Show this help document.

=head2 --version

Print the current yto and Perl versions.

=head1 ENVIRONMENT VARIABLES

=over 4

=item YERTL_FORMAT

Specify the default format Yertl uses between commands. Defaults to C<yaml>. Can be
set to C<json> for interoperability with other programs.

=back

=head1 AUTHOR

Doug Bell <preaction@cpan.org>

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2016 by Doug Bell.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=cut
