charts.d.dryrun-helper.sh 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #!/usr/bin/env bash
  2. # SPDX-License-Identifier: GPL-3.0-or-later
  3. # shellcheck disable=SC2181
  4. # will stop the script for any error
  5. set -e
  6. me="$0"
  7. name="$1"
  8. chart="$2"
  9. conf="$3"
  10. can_diff=1
  11. tmp1="$(mktemp)"
  12. tmp2="$(mktemp)"
  13. myset() {
  14. set | grep -v "^_=" | grep -v "^PIPESTATUS=" | grep -v "^BASH_LINENO="
  15. }
  16. # save 2 'set'
  17. myset >"$tmp1"
  18. myset >"$tmp2"
  19. # make sure they don't differ
  20. diff "$tmp1" "$tmp2" >/dev/null 2>&1
  21. if [ $? -ne 0 ]
  22. then
  23. # they differ, we cannot do the check
  24. echo >&2 "$me: cannot check with diff."
  25. can_diff=0
  26. fi
  27. # do it again, now including the script
  28. myset >"$tmp1"
  29. # include the plugin and its config
  30. if [ -f "$conf" ]
  31. then
  32. # shellcheck source=/dev/null
  33. . "$conf"
  34. if [ $? -ne 0 ]
  35. then
  36. echo >&2 "$me: cannot load config file $conf"
  37. rm "$tmp1" "$tmp2"
  38. exit 1
  39. fi
  40. fi
  41. # shellcheck source=/dev/null
  42. . "$chart"
  43. if [ $? -ne 0 ]
  44. then
  45. echo >&2 "$me: cannot load chart file $chart"
  46. rm "$tmp1" "$tmp2"
  47. exit 1
  48. fi
  49. # remove all variables starting with the plugin name
  50. myset | grep -v "^$name" >"$tmp2"
  51. if [ $can_diff -eq 1 ]
  52. then
  53. # check if they are different
  54. # make sure they don't differ
  55. diff "$tmp1" "$tmp2" >&2
  56. if [ $? -ne 0 ]
  57. then
  58. # they differ
  59. rm "$tmp1" "$tmp2"
  60. exit 1
  61. fi
  62. fi
  63. rm "$tmp1" "$tmp2"
  64. exit 0