From be1c7e50e1e8809ea56f2c9d472eccd8ffd73a97 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Fri, 19 Apr 2024 04:57:58 +0200 Subject: Adding upstream version 1.44.3. Signed-off-by: Daniel Baumann --- system/edit-config | 327 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 327 insertions(+) create mode 100755 system/edit-config (limited to 'system/edit-config') diff --git a/system/edit-config b/system/edit-config new file mode 100755 index 00000000..3944810d --- /dev/null +++ b/system/edit-config @@ -0,0 +1,327 @@ +#!/usr/bin/env sh + +# shellcheck disable=SC1091 +[ -f /etc/profile ] && . /etc/profile + +set -e + +script_dir="$(CDPATH="" cd -- "$(dirname -- "$0")" && pwd -P)" + +usage() { + check_directories + cat <&2 "ERROR: ${1}" +} + +abspath() { + if [ -d "${1}/" ]; then + echo "$(cd "${1}" && /usr/bin/env PWD= pwd -P)/" + elif [ -f "${1}" ]; then + echo "$(cd "$(dirname "${1}")" && /usr/bin/env PWD= pwd -P)/$(basename "${1}")" + elif echo "${1}" | grep -q '/'; then + if echo "${1}" | grep -q '^/'; then + mkdir -p "$(dirname "${1}")" + echo "$(cd "$(dirname "${1}")" && /usr/bin/env PWD= pwd -P)/$(basename "${1}")" + else + mkdir -p "${script_dir}/$(dirname "${1}")" + echo "${script_dir}/${1}" + fi + else + echo "${script_dir}/${1}" + fi +} + +is_prefix() { + echo "${2}" | grep -qE "^${1}" + return $? +} + +check_directories() { + if [ -f "${script_dir}/.container-hostname" ]; then + NETDATA_USER_CONFIG_DIR="${script_dir}" + NETDATA_STOCK_CONFIG_DIR="/usr/lib/netdata/conf.d" + return + fi + + if [ -e "${script_dir}/.environment" ]; then + OLDPATH="${PATH}" + # shellcheck disable=SC1091 + . "${script_dir}/.environment" + PATH="${OLDPATH}" + fi + + if [ -n "${NETDATA_PREFIX}" ] && [ -d "${NETDATA_PREFIX}/usr/lib/netdata/conf.d" ]; then + stock_dir="${NETDATA_PREFIX}/usr/lib/netdata/conf.d" + elif [ -n "${NETDATA_PREFIX}" ] && [ -d "${NETDATA_PREFIX}/lib/netdata/conf.d" ]; then + stock_dir="${NETDATA_PREFIX}/lib/netdata/conf.d" + elif [ -d "${script_dir}/../../usr/lib/netdata/conf.d" ]; then + stock_dir="${script_dir}/../../usr/lib/netdata/conf.d" + elif [ -d "${script_dir}/../../lib/netdata/conf.d" ]; then + stock_dir="${script_dir}/../../lib/netdata/conf.d" + elif [ -d "/usr/lib/netdata/conf.d" ]; then + stock_dir="/usr/lib/netdata/conf.d" + fi + + [ -z "${NETDATA_USER_CONFIG_DIR}" ] && NETDATA_USER_CONFIG_DIR="${script_dir}" + [ -z "${NETDATA_STOCK_CONFIG_DIR}" ] && NETDATA_STOCK_CONFIG_DIR="${stock_dir}" + + if [ -z "${NETDATA_STOCK_CONFIG_DIR}" ]; then + error "Unable to find stock config directory." + exit 1 + fi +} + +check_editor() { + if [ -z "${editor}" ]; then + if [ -n "${EDITOR}" ] && command -v "${EDITOR}" >/dev/null 2>&1; then + editor="${EDITOR}" + elif command -v editor >/dev/null 2>&1; then + editor="editor" + elif command -v vi >/dev/null 2>&1; then + editor="vi" + else + error "Unable to find a usable editor, tried \${EDITOR} (${EDITOR}), editor, and vi." + exit 1 + fi + elif ! command -v "${editor}" >/dev/null 2>&1; then + error "Unable to locate user specified editor ${editor}, is it in your PATH?" + exit 1 + fi +} + +running_in_container() { + [ -e /.dockerenv ] && return 0 + [ -e /.dockerinit ] && return 0 + [ -e /run/.containerenv ] && return 0 + [ -r /proc/1/environ ] && tr '\000' '\n' &2 "Copying '${NETDATA_STOCK_CONFIG_DIR}/${1}' to '${NETDATA_USER_CONFIG_DIR}/${1}' ... " + cp -p "${NETDATA_STOCK_CONFIG_DIR}/${1}" "${NETDATA_USER_CONFIG_DIR}/${1}" || exit 1 + else + echo >&2 "Creating empty '${NETDATA_USER_CONFIG_DIR}/${1}' ... " + touch "${NETDATA_USER_CONFIG_DIR}/${1}" || exit 1 + fi +} + +copy_container() { + if [ ! -w "${NETDATA_USER_CONFIG_DIR}" ]; then + error "Cannot write to ${NETDATA_USER_CONFIG_DIR}!" + exit 1 + fi + + if run_in_container "${container}" "[ -f \"${NETDATA_STOCK_CONFIG_DIR}/${1}\" ]"; then + echo >&2 "Copying '${NETDATA_STOCK_CONFIG_DIR}/${1}' to '${NETDATA_USER_CONFIG_DIR}/${1}' ... " + ${docker} cp -a "${container}:${NETDATA_STOCK_CONFIG_DIR}/${1}" "${NETDATA_USER_CONFIG_DIR}/${1}" || exit 1 + else + echo >&2 "Creating empty '${NETDATA_USER_CONFIG_DIR}/${1}' ... " + touch "${NETDATA_USER_CONFIG_DIR}/${1}" || exit 1 + fi +} + +copy() { + if [ -f "${NETDATA_USER_CONFIG_DIR}/${1}" ]; then + return 0 + elif [ -n "${container}" ]; then + copy_container "${1}" + else + copy_native "${1}" + fi +} + +edit() { + echo >&2 "Editing '${1}' ..." + + # check we can edit + if [ ! -w "${1}" ]; then + error "Cannot write to ${1}!" + exit 1 + fi + + "${editor}" "${1}" + exit $? +} + +main() { + parse_args "${@}" + check_directories + check_editor + handle_container + copy "${file}" + edit "${absfile}" +} + +main "${@}" -- cgit v1.2.3