our
$VERSION
=
'0.05'
;
sub
MODINFO {
"Modify structure using external process"
}
sub
arg_opts {
my
$self
=
shift
;
return
(
$self
->SUPER::arg_opts(),
'command|cmd=s'
=> \
$self
->{OPTS}->{command},
'preserve=s@'
=> \
$self
->{OPTS}->{preserve},
'strict'
=> \
$self
->{OPTS}->{strict},
)
}
sub
check_rule {
my
(
$self
,
$rule
) =
@_
;
my
$out
=
$self
;
push
@{
$rule
->{path}},
''
unless
(@{
$rule
->{path}});
unless
(
defined
$rule
->{command}) {
log_error {
'Command to run should be defined'
};
$out
=
undef
;
}
return
$out
;
}
sub
process_path {
my
(
$self
,
$data
,
$path
,
$opts
) =
@_
;
my
$spath
=
eval
{ str2path(
$path
) };
die_fatal
"Failed to parse path ($@)"
, 4
if
($@);
my
@refs
=
eval
{ path(${
$data
},
$spath
,
strict
=>
$opts
->{strict}) };
die_fatal
"Failed to lookup path '$path'"
, 4
if
($@);
for
my
$r
(
@refs
) {
my
$in
= s_encode(${
$r
},
'JSON'
, {
pretty
=> 1 });
my
(
$out
,
$err
);
run3(
$opts
->{command}, \
$in
, \
$out
, \
$err
, {
return_if_system_error
=> 1});
die_fatal
"Failed to run '$opts->{command}' ($!)"
, 2
if
($? == -1);
unless
($? == 0) {
die_fatal
"'$opts->{command}' exited with "
. ($? >> 8) .
(
$err
?
" ("
.
join
(
" "
,
split
(
"\n"
,
$err
)) .
")"
:
""
), 16;
}
${
$r
} = s_decode(
$out
,
'JSON'
);
}
}
1;