summaryrefslogtreecommitdiffstats
path: root/system/edit-config
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2018-11-07 12:22:44 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2018-11-07 12:22:44 +0000
commit1e6c93250172946eeb38e94a92a1fd12c9d3011e (patch)
tree8ca5e16dfc7ad6b3bf2738ca0a48408a950f8f7e /system/edit-config
parentUpdate watch file (diff)
downloadnetdata-1e6c93250172946eeb38e94a92a1fd12c9d3011e.tar.xz
netdata-1e6c93250172946eeb38e94a92a1fd12c9d3011e.zip
Merging upstream version 1.11.0+dfsg.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'system/edit-config')
-rw-r--r--system/edit-config96
1 files changed, 96 insertions, 0 deletions
diff --git a/system/edit-config b/system/edit-config
new file mode 100644
index 000000000..1696f9f34
--- /dev/null
+++ b/system/edit-config
@@ -0,0 +1,96 @@
+#!/usr/bin/env sh
+
+[ -f /etc/profile ] && . /etc/profile
+
+file="${1}"
+
+EDITOR="${EDITOR-vi}"
+[ -z "${NETDATA_USER_CONFIG_DIR}" ] && NETDATA_USER_CONFIG_DIR="/usr/local/etc/netdata"
+[ -z "${NETDATA_STOCK_CONFIG_DIR}" ] && NETDATA_STOCK_CONFIG_DIR="/usr/local/lib/netdata/conf.d"
+
+if [ -z "${file}" ]
+then
+ cat <<USAGE
+
+USAGE:
+ ${0} FILENAME
+
+ Copy and edit the stock config file named: FILENAME
+ if FILENAME is already copied, it will be edited as-is.
+
+ The EDITOR shell variable is used to define the editor to be used.
+
+ Stock config files at: '${NETDATA_STOCK_CONFIG_DIR}'
+ User config files at: '${NETDATA_USER_CONFIG_DIR}'
+
+ Available files in '${NETDATA_STOCK_CONFIG_DIR}' to copy and edit:
+
+USAGE
+
+ cd "${NETDATA_STOCK_CONFIG_DIR}" || exit 1
+ ls >&2 -R *.conf */*.conf
+ exit 1
+
+fi
+
+file_is_in_path() {
+ local file path real
+ file="${1}"
+ path="${2}"
+
+ real="$(readlink -f "${file}")"
+
+ # we don't have working readlink
+ [ -z "${real}" ] && return 0
+
+ if [ ! -z "${real}" ] && [ -z "$(echo "${real}" | grep -E "^${path}.*$")" ]
+ then
+ echo >&2 "File '${file}' is physically at '${real}', which is not in '${path}'. Aborting."
+ exit 1
+ fi
+
+ return 0
+}
+
+edit() {
+ echo >&2 "Editing '${1}' ..."
+
+ # check we can edit
+ file_is_in_path "${1}" "${NETDATA_USER_CONFIG_DIR}" || exit 1
+
+ "${EDITOR}" "${1}"
+ exit $?
+}
+
+copy_and_edit() {
+ # check we can copy
+ file_is_in_path "${NETDATA_STOCK_CONFIG_DIR}/${1}" "${NETDATA_STOCK_CONFIG_DIR}" || exit 1
+
+ if [ ! -f "${NETDATA_USER_CONFIG_DIR}/${1}" ]
+ then
+ echo >&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
+ fi
+
+ edit "${NETDATA_USER_CONFIG_DIR}/${1}"
+}
+
+# make sure it is not absolute filename
+c1="$(echo "${file}" | cut -b 1)"
+if [ "${c1}" = "/" ] || [ "${c1}" = "." ]
+then
+ echo >&2 "Please don't use filenames starting with '/' or '.'"
+ exit 1
+fi
+
+# already exists
+if [ -f "${NETDATA_USER_CONFIG_DIR}/${file}" ]
+then
+ edit "${NETDATA_USER_CONFIG_DIR}/${file}"
+fi
+
+[ -f "${NETDATA_USER_CONFIG_DIR}/${file}" ] && edit "${NETDATA_USER_CONFIG_DIR}/${file}"
+[ -f "${NETDATA_STOCK_CONFIG_DIR}/${file}" ] && copy_and_edit "${file}"
+
+echo >&2 "File '${file}' is not found in '${NETDATA_STOCK_CONFIG_DIR}'"
+exit 1