MT#55289 Add del-value helper

Move perl code which actually modifies yml file to separate helper as
there is no sense to recreate it every call of 'ngcpcfg del'.

Change-Id: Iab9e023318cf9798bdb10d89b08dc2afe125c495
mr11.1
Mykola Malkov 3 years ago
parent a0dc08a9ba
commit 5c16ae0e7b

@ -6,6 +6,7 @@ BASH_SCRIPTS = \
sbin/ngcpcfg
PERL_SCRIPTS = \
lib/NGCP/Template.pm \
helper/del-value \
helper/set-value \
helper/sort-yml \
helper/sync-db \

@ -4,6 +4,7 @@ functions/logs usr/share/ngcp-ngcpcfg/functions/
functions/main usr/share/ngcp-ngcpcfg/functions/
helper/cat-yml usr/share/ngcp-ngcpcfg/helper/
helper/check-for-mysql usr/share/ngcp-ngcpcfg/helper/
helper/del-value usr/share/ngcp-ngcpcfg/helper/
helper/fileformat_version usr/share/ngcp-ngcpcfg/helper/
helper/instance-info usr/share/ngcp-ngcpcfg/helper/
helper/instances-info usr/share/ngcp-ngcpcfg/helper/

@ -0,0 +1,48 @@
#!/usr/bin/perl -wCSD
use strict;
use warnings;
use YAML::XS;
use Scalar::Util qw(looks_like_number);
my ($file, $option) = @ARGV;
unless (defined($file)) {
print {*STDERR} ("1st parameter 'file' is not defined\n");
exit (1);
}
unless (defined($option)) {
print {*STDERR} ("2nd parameter 'option' is not defined\n");
exit (1);
}
my $yaml = YAML::XS::LoadFile($file);
my $valref = \$yaml;
my @path = split(/\./, $option);
my $delete_element = pop(@path);
for my $component (@path) {
if (ref($valref) eq 'SCALAR' && defined(${$valref})) {
print {*STDERR} ("Key resolved to a SCALAR at '$component'; cannot continue.\n");
exit (1);
}
elsif (looks_like_number($component) && (! defined(${$valref}) || ref(${$valref}) eq 'ARRAY')) {
$valref = \${$valref}->[$component];
}
elsif ($component eq 'APPEND' && ref($$valref) eq 'ARRAY') {
$valref = \${$valref}->[$#{$$valref}+1];
}
elsif (! defined(${$valref}) || ref(${$valref}) eq 'HASH') {
$valref = \${$valref}->{$component};
}
else {
print {*STDERR} ("Key resolved to a " . ref(${$valref}) . " reference; refusing to overwrite.\n");
exit (1);
}
}
delete ${$valref}->{$delete_element};
YAML::XS::DumpFile($file, $yaml);

@ -60,39 +60,14 @@ option="$2"
[ -z "${option}" ] && ( log_error "missing option to set. Exiting." ; exit 1)
log_debug "Deleting option '${option}' from '${file}'"
perl_line="delete \$yaml->{${option//./\}->\{}};"
log_debug "perl line: ${perl_line}"
tmp=$(mktemp -t ngcpcfg-del.XXXXXXXXXX)
log_debug "Temporary perl file: ${tmp}"
cat > "${tmp}" << EOF
use strict;
use warnings;
use YAML::XS;
my \$file="${file}";
my \$yaml = YAML::XS::LoadFile("\$file");
${perl_line}
YAML::XS::DumpFile(\$file, \$yaml);
EOF
log_debug "perl -wCSD \"${tmp}\" || RC=$?"
perl -wCSD "${tmp}" || RC=$?
log_debug "${HELPER}/del-value '${file}' '${option}' || RC=\$?"
"${HELPER}/del-value" "${file}" "${option}" || RC=$?
if [ "${RC}" = "0" ] && "${b_show_diff:-false}"; then
log_debug "${SCRIPTS}/diff || true"
"${SCRIPTS}"/diff || true
fi
if [ -n "${DEBUG:-}" ] ; then
log_debug "Not removing temporary file ${tmp}"
else
rm -f "${tmp}"
fi
exit ${RC:-0}
## END OF FILE #################################################################

Loading…
Cancel
Save