#!perl

our $DATE = '2015-03-19'; # DATE
our $VERSION = '0.06'; # VERSION

use 5.010;
use strict;
use warnings;
no warnings 'once';

use App::IODUtils;
use Perinci::CmdLine::Any;

our %SPEC;
$SPEC{insert_iod_key} = {
    v => 1.1,
    summary => 'Insert a key to IOD configuration file',
    args_groups => [
        {args=>[qw/add ignore replace/], rel=>'one_of'},
        {args=>[qw/top linum/], rel=>'one_of'},
    ],
    args => {
        %App::IODUtils::common_args,
        %App::IODUtils::inplace_arg,
        section => {
            schema  => 'str*',
            req     => 1,
            pos     => 1,
        },
        key => {
            schema  => 'str*',
            req     => 1,
            pos     => 2,
        },
        value => {
            summary => '(Raw) value',
            schema  => 'str*',
            req     => 1,
            pos     => 3,
        },
        create_section => {
            summary => 'When section does not exist, create it first',
            schema => [bool => is=>1],
        },
        add => {
            summary => 'When key already exists, add',
            schema => [bool => is=>1],
        },
        ignore => {
            summary => 'When key already exists, do nothing',
            schema => [bool => is=>1],
        },
        replace => {
            summary => 'When key already exists, delete previous key(s) first',
            schema => [bool => is=>1],
        },
        top => {
            summary => 'Add to the beginning of section instead of end',
            schema => [bool => is=>1],
        },
        linum => {
            summary => 'Put key line at a specific line number '.
                '(line number starts from 1)',
            schema => ['int*' => min=>1],
        },
    },
};
sub insert_iod_key {
    my %args = @_;
    App::IODUtils::_check_inplace(\%args);

    my $parser = App::IODUtils::_get_parser(\%args);

    my $doc = $parser->read_string($args{iod});

    $doc->insert_key(
        {
            create_section => $args{create_section},
            add     => $args{add},
            ignore  => $args{ignore},
            replace => $args{replace},
            top     => $args{top},
            linum   => $args{linum},
        },
        $args{section}, $args{key}, $args{value},
    );

    App::IODUtils::_return_mod_result(\%args, $doc);
}

Perinci::CmdLine::Any->new(
    url => '/main/insert_iod_key',
)->run;

# ABSTRACT: Insert a key to IOD configuration file
# PODNAME: insert-iod-key

__END__

=pod

=encoding UTF-8

=head1 NAME

insert-iod-key - Insert a key to IOD configuration file

=head1 VERSION

This document describes version 0.06 of insert-iod-key (from Perl distribution App-IODUtils), released on 2015-03-19.

=head1 SYNOPSIS

Basic usage:

 % insert-iod-key /path/to/file.iod section key value

=head1 OPTIONS

C<*> marks required options.

=over

=item B<--add>

When key already exists, add.

=item B<--config-path>=I<s>

Set path to configuration file.

Can be specified multiple times.

=item B<--config-profile>=I<s>

Set configuration profile to use.

=item B<--create-section>

When section does not exist, create it first.

=item B<--enable-expr>, B<-e>

=item B<--format>=I<s>

Choose output format, e.g. json, text.

Default value:

 undef

=item B<--help>, B<-h>, B<-?>

Display this help message.

=item B<--ignore>

When key already exists, do nothing.

=item B<--ignore-unknown-directive>

=item B<--inplace>

Modify file in-place.

Note that this can only be done if you specify an actual file and not STDIN.
Otherwise, an error will be thrown.


=item B<--iod>=I<s>*

IOD file.

=item B<--json>

Set output format to json.

=item B<--key>=I<s>*

=item B<--linum>=I<i>

Put key line at a specific line number (line number starts from 1).

=item B<--naked-res>

When outputing as JSON, strip result envelope.

Default value:

 0

By default, when outputing as JSON, the full enveloped result is returned, e.g.:

    [200,"OK",[1,2,3],{"func.extra"=>4}]

The reason is so you can get the status (1st element), status message (2nd
element) as well as result metadata/extra result (4th element) instead of just
the result (3rd element). However, sometimes you want just the result, e.g. when
you want to pipe the result for more post-processing. In this case you can use
`--naked-res` so you just get:

    [1,2,3]


=item B<--no-config>

Do not use any configuration file.

=item B<--replace>

When key already exists, delete previous key(s) first.

=item B<--section>=I<s>*

=item B<--top>

Add to the beginning of section instead of end.

=item B<--value>=I<s>*

(Raw) value.

=item B<--version>, B<-v>

=back

=head1 ENVIRONMENT

INSERT_IOD_KEY_OPT

=head1 FILES

~/insert-iod-key.conf

/etc/insert-iod-key.conf

=head1 COMPLETION

This script has shell tab completion capability with support for several shells.

=head2 bash

To activate bash completion for this script, put:

 complete -C insert-iod-key insert-iod-key

in your bash startup (e.g. C<~/.bashrc>). Your next shell session will then recognize tab completion for the command. Or, you can also directly execute the line above in your shell to activate immediately.

It is recommended, however, that you install L<shcompgen> which allows you to activate completion scripts for several kinds of scripts on multiple shells. Some CPAN distributions (those that are built with L<Dist::Zilla::Plugin::GenShellCompletion>) will even automatically enable shell completion for their included scripts (using C<shcompgen>) at installation time, so you can immadiately have tab completion.

=head2 tcsh

To activate tcsh completion for this script, put:

 complete insert-iod-key 'p/*/`insert-iod-key`/'

in your tcsh startup (e.g. C<~/.tcshrc>). Your next shell session will then recognize tab completion for the command. Or, you can also directly execute the line above in your shell to activate immediately.

It is also recommended to install C<shcompgen> (see above).

=head2 other shells

For fish and zsh, install C<shcompgen> as described above.

=head1 HOMEPAGE

Please visit the project's homepage at L<https://metacpan.org/release/App-IODUtils>.

=head1 SOURCE

Source repository is at L<https://github.com/perlancar/perl-App-IODUtils>.

=head1 BUGS

Please report any bugs or feature requests on the bugtracker website L<https://rt.cpan.org/Public/Dist/Display.html?Name=App-IODUtils>

When submitting a bug or request, please include a test-file or a
patch to an existing test-file that illustrates the bug or desired
feature.

=head1 AUTHOR

perlancar <perlancar@cpan.org>

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2015 by perlancar@cpan.org.

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
