You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ngcpcfg/scripts/del

70 lines
1.6 KiB

#!/bin/bash
# Purpose: delete config option from YAML file
################################################################################
set -e
set -u
# support testsuite
FUNCTIONS="${FUNCTIONS:-/usr/share/ngcp-ngcpcfg/functions/}"
HELPER="${HELPER:-/usr/share/ngcp-ngcpcfg/helper/}"
SCRIPTS="${SCRIPTS:-/usr/share/ngcp-ngcpcfg/scripts/}"
if ! [ -r "${FUNCTIONS}"/main ] ; then
printf "Error: %s/main could not be read. Exiting.\n" "${FUNCTIONS}">&2
exit 1
fi
. "${FUNCTIONS}"/main
## functions {{{
help() {
echo "Usage: ngcpcfg del <file> <option>"
echo "Example: ngcpcfg del /etc/ngcp-config/config.yml unnecessary.option"
}
## }}}
[ "${#:-}" -ne 2 ] && (help >&2 ; exit 1)
file="$1"
option="$2"
[ -f "${file}" ] || (log_error "missing ${file}. Exiting." ; exit 1)
[ -z "${option}" ] && ( log_error "missing option to set. Exiting." ; exit 1)
log_debug "Deleting option '${option}' from '${file}'"
perl_line="delete \$yaml->[0]->{${option//./\}->\{}};"
log_debug "perl line: ${perl_line}"
tmp=$(mktemp)
log_debug "Temporary perl file: ${tmp}"
cat > "${tmp}" << EOF
use strict;
use warnings;
use YAML::Tiny;
my \$file="${file}";
my \$yaml = YAML::Tiny->read("\$file") or die "YAML file '\$file' could not be loaded";
${perl_line}
\$yaml->write(\$file) or die "Could not write YAML to \$file";
EOF
log_debug "perl -wCSD \"${tmp}\" || RC=$?"
perl -wCSD "${tmp}" || RC=$?
log_debug "ngcpcfg diff | tail -n +5 || true"
ngcpcfg diff | tail -n +5 || true
if [ -n "${DEBUG:-}" ] ; then
log_debug "Not removing temporary file ${tmp}"
else
rm -f "${tmp}"
fi
exit ${RC:-0}
## END OF FILE #################################################################