sources => '+SourcesWithFiles',
# 'config-files' => ["/etc/$APPNAME.json"],
options => [
{
getopt => 'config|c=s',
ion file',
environment => 1,
# default => "$ENV{HOME}/.$APPNAME.json",
},
],
children => [qw< foo bar >],
},
foo => {
nd-line interface that manages
key-value data in a JSON file:
=over
=item *
The file name is provided through command-line option
C<< --db | --json-db | -d >>, which can also be provided through
en
eadline close >;
use JSON::PP ();
use App::Easer::V2 qw< run >;
my $app = {
aliases => [qw< crud >],
help => 'A command for CRUD operations over a JSON file',
descripti
ions => [
{
getopt => 'db|json-db|d=s',
environment => 'CRUD_DB',
help => 'path to the JSON file for CRUD operations',
}
],
urce_JsonFileFromConfig ($self, $key, @ignore) {
$key = $key->[0] // 'config';
defined(my $filename = $self->config($key)) or return {};
require JSON::PP;
return JSON::PP::decode_json($sel
b source_JsonFileFromConfig
sub slurp ($self, $file, $mode = '<:encoding(UTF-8)') {
open my $fh, $mode, $file or die "open('$file'): $!\n";
local $/;
return <$fh>;
}
sub source_JsonFiles ($
nore) {
require JSON::PP;
return $self->merge_hashes(
map { JSON::PP::decode_json($self->slurp($_)) }
grep { -e $_ } $candidates->@*
);
} ## end sub source_JsonFiles
sub source_
is particularly useful when using
L<Sources as hash reference>.
=item * C<+JsonFileFromConfig>
Get keys/values from a JSON file, whose path is pointed by a key in the
collected options. By default t
JsonFileFromConfig => 'jcnf' ]
=item * C<+JsonFiles>
Get keys/values from a few JSON files (it's OK if they do not exist). To
set the files to try, pass this source as an array reference:
[ Json
on used by
C<JsonFileFromConfig>, as long as the C<+Default> source is placed
before C<JsonFileFromConfig>. In this case, it's usually necessary to
assign a lower priority value to C<JsonFileFromConfi
d through a hash reference (or something that
can I<be transformed> into a hash reference, like a JSON-encoded string
containing an object, or Perl code) with the description of the
different aspects
+JsonFileFromConfig> and C<+JsonFiles> to the ones above. The
former looks for a configuration named C<config> (or whatever is set as
C<config-option> in the overall configuration hash) to load a JSON
file
with additional configurations; the latter looks for a list of JSON
files to try in C<config-files> inside the configuration hash.
=over
Although the C<+Default> source is put I<first>, it act
## end else [ if ('SCALAR' eq ref $application)]
return eval {
require JSON::PP;
JSON::PP::decode_json($text);
} // eval { eval $text; } // die "cannot load application\n";
} ## en
ck_JsonFileFromConfig ($self, $spec, $args) {
my $key = $spec->{'config-option'} // 'config';
return {} if !exists($spec->{config}{$key});
require JSON::PP;
return JSON::PP::decode_json(sl
# end sub stock_JsonFileFromConfig
sub stock_JsonFiles ($self, $spec, @ignore) {
return merger($self, $spec)->(
map {
require JSON::PP;
JSON::PP::decode_json(slurp($_));