edit-config.in 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #!/usr/bin/env sh
  2. [ -f /etc/profile ] && . /etc/profile
  3. file="${1}"
  4. if [ "$(command -v editor)" ]; then
  5. EDITOR="${EDITOR-editor}"
  6. else
  7. EDITOR="${EDITOR-vi}"
  8. fi
  9. [ -z "${NETDATA_USER_CONFIG_DIR}" ] && NETDATA_USER_CONFIG_DIR="@configdir_POST@"
  10. [ -z "${NETDATA_STOCK_CONFIG_DIR}" ] && NETDATA_STOCK_CONFIG_DIR="@libconfigdir_POST@"
  11. if [ -z "${file}" ]; then
  12. cat << EOF
  13. USAGE:
  14. ${0} FILENAME
  15. Copy and edit the stock config file named: FILENAME
  16. if FILENAME is already copied, it will be edited as-is.
  17. The EDITOR shell variable is used to define the editor to be used.
  18. Stock config files at: '${NETDATA_STOCK_CONFIG_DIR}'
  19. User config files at: '${NETDATA_USER_CONFIG_DIR}'
  20. Available files in '${NETDATA_STOCK_CONFIG_DIR}' to copy and edit:
  21. EOF
  22. cd "${NETDATA_STOCK_CONFIG_DIR}" || exit 1
  23. ls >&2 -R ./*.conf ./*/*.conf
  24. exit 1
  25. fi
  26. edit() {
  27. echo >&2 "Editing '${1}' ..."
  28. # check we can edit
  29. if [ ! -w "${1}" ]; then
  30. echo >&2 "Cannot write to ${1}! Aborting ..."
  31. exit 1
  32. fi
  33. "${EDITOR}" "${1}"
  34. exit $?
  35. }
  36. copy_and_edit() {
  37. # check we can copy
  38. if [ ! -w "${NETDATA_USER_CONFIG_DIR}" ]; then
  39. echo >&2 "Cannot write to ${NETDATA_USER_CONFIG_DIR}! Aborting ..."
  40. exit 1
  41. fi
  42. if [ ! -f "${NETDATA_USER_CONFIG_DIR}/${1}" ]; then
  43. echo >&2 "Copying '${NETDATA_STOCK_CONFIG_DIR}/${1}' to '${NETDATA_USER_CONFIG_DIR}/${1}' ... "
  44. cp -p "${NETDATA_STOCK_CONFIG_DIR}/${1}" "${NETDATA_USER_CONFIG_DIR}/${1}" || exit 1
  45. fi
  46. edit "${NETDATA_USER_CONFIG_DIR}/${1}"
  47. }
  48. # make sure it is not absolute filename
  49. c1="$(echo "${file}" | cut -b 1)"
  50. if [ "${c1}" = "/" ] || [ "${c1}" = "." ]; then
  51. echo >&2 "Please don't use filenames starting with '/' or '.'"
  52. exit 1
  53. fi
  54. # already exists
  55. [ -f "${NETDATA_USER_CONFIG_DIR}/${file}" ] && edit "${NETDATA_USER_CONFIG_DIR}/${file}"
  56. # stock config is valid, copy and edit
  57. [ -f "${NETDATA_STOCK_CONFIG_DIR}/${file}" ] && copy_and_edit "${file}"
  58. # no such config found
  59. echo >&2 "File '${file}' is not found in '${NETDATA_STOCK_CONFIG_DIR}'"
  60. exit 1