mirror of https://github.com/sipwise/ngcpcfg.git
root@spce:~# ngcpcfg set /etc/ngcp-config/config.yml "aaa.bbb=123"
@@ -1,4 +1,6 @@
---
+aaa:
+ bbb: 123
root@spce:~# ngcpcfg set /etc/ngcp-config/config.yml "aaa.bbb={'ccc','123','ddd','567'}"
@@ -1,4 +1,8 @@
---
+aaa:
+ bbb:
+ ccc: '123'
+ ddd: '567'
root@spce:~# ngcpcfg set /etc/ngcp-config/config.yml "aaa.bbb=['ccc','123','ddd','567']"
@@ -1,4 +1,10 @@
---
+aaa:
+ bbb:
+ - ccc
+ - '123'
+ - ddd
+ - '567'
root@spce:~# ngcpcfg del /etc/ngcp-config/config.yml "aaa.bbb"
@@ -1,4 +1,5 @@
---
+aaa: {}
apps:
malicious_call: no
party_call_control:
root@spce:~# ngcpcfg del /etc/ngcp-config/config.yml "aaa"
root@spce:~#
Change-Id: Ife4810ee12c921a3e8ed5b323435802aa821fb60
changes/28/13428/12
parent
acfa825767
commit
4d207fc4f9
@ -0,0 +1,69 @@
|
||||
#!/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 #################################################################
|
||||
@ -0,0 +1,94 @@
|
||||
#!/bin/bash
|
||||
# Purpose: set config value
|
||||
################################################################################
|
||||
|
||||
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 set <file> <key>"
|
||||
echo "Example: ngcpcfg set /etc/ngcp-config/config.yml general.maintenance=yes"
|
||||
}
|
||||
|
||||
## }}}
|
||||
|
||||
[ "${#:-}" -ne 2 ] && (help >&2 ; exit 1)
|
||||
|
||||
file="$1"
|
||||
data="$2"
|
||||
RC=0
|
||||
|
||||
[ -f "${file}" ] || (log_error "missing ${file}. Exiting." ; exit 1)
|
||||
[ -z "${data}" ] && ( log_error "missing data to set. Exiting." ; exit 1)
|
||||
|
||||
log_debug "Received data: ${data}"
|
||||
option="${data/=*/}"
|
||||
value="${data/*=/}"
|
||||
|
||||
[ -z "${option}" ] && ( log_error "missing option to set. Exiting." ; exit 1)
|
||||
[ -z "${value}" ] && ( log_error "missing value to set. Exiting." ; exit 1)
|
||||
log_debug "Saving option '${option}' value '${value}' into '${file}'"
|
||||
|
||||
if [[ ${value} =~ ^\'.*\'$ ]] ; then
|
||||
log_debug "\$value is already quoted."
|
||||
else
|
||||
log_debug "\$value is NOT quoted, doing so if necessary."
|
||||
case ${value} in
|
||||
[0-9]*|\[*\]|\{*\})
|
||||
log_debug "Do not quoting \$value for integers, arrays and hashes."
|
||||
;;
|
||||
*)
|
||||
log_debug "Quoting \$value to prevent further Perl errors."
|
||||
value="\"${value}\""
|
||||
;;
|
||||
esac
|
||||
fi
|
||||
|
||||
perl_line="\$yaml->[0]->{${option//./\}->\{}}=${value};"
|
||||
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=$?
|
||||
|
||||
if [ "${RC}" = "0" ]; then
|
||||
log_debug "ngcpcfg diff | tail -n +5 || true"
|
||||
ngcpcfg diff | tail -n +5 || true
|
||||
fi
|
||||
|
||||
if [ -n "${DEBUG:-}" ] ; then
|
||||
log_debug "Not removing temporary file ${tmp}"
|
||||
else
|
||||
rm -f "${tmp}"
|
||||
fi
|
||||
|
||||
exit ${RC}
|
||||
|
||||
## END OF FILE #################################################################
|
||||
Loading…
Reference in new issue