shell dot on steroids https://pagure.io/shellfu

common.sh 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #!/bin/bash
  2. #shellcheck disable=SC2034,SC1117
  3. #
  4. # Variables to hold exit status semantic
  5. #
  6. TF_ES_OK=0
  7. TF_ES_FAIL=1
  8. TF_ES_BAILOUT=2
  9. TF_ES_ERROR=3
  10. TF_ES_PANIC=4
  11. #
  12. # Color definition variables
  13. #
  14. TF_COLOR_BLACK="\033[0;30m"
  15. TF_COLOR_RED="\033[0;31m"
  16. TF_COLOR_GREEN="\033[0;32m"
  17. TF_COLOR_YELLOW="\033[0;33m"
  18. TF_COLOR_BLUE="\033[0;34m"
  19. TF_COLOR_MAGENTA="\033[0;35m"
  20. TF_COLOR_CYAN="\033[0;36m"
  21. TF_COLOR_WHITE="\033[0;37m"
  22. TF_COLOR_LBLACK="\033[1;30m"
  23. TF_COLOR_LRED="\033[1;31m"
  24. TF_COLOR_LGREEN="\033[1;32m"
  25. TF_COLOR_LYELLOW="\033[1;33m"
  26. TF_COLOR_LBLUE="\033[1;34m"
  27. TF_COLOR_LMAGENTA="\033[1;35m"
  28. TF_COLOR_LCYAN="\033[1;36m"
  29. TF_COLOR_LWHITE="\033[1;37m"
  30. TF_COLOR_NONE="\033[1;0m"
  31. _tf__is_word() {
  32. #
  33. # Check if $1 contains only alphanumeric chars or _
  34. #
  35. local tainted=$1 # "dirty" version
  36. local clean # clean version
  37. clean=$(tr -c -d '_[:alnum:]' <<< "$tainted")
  38. test "$tainted" = "$clean"
  39. }
  40. tf_exit_ok() {
  41. #
  42. # Exit with OK status
  43. #
  44. exit $TF_ES_OK
  45. }
  46. tf_exit_fail() {
  47. #
  48. # Warn $1 and exit with status FAIL
  49. #
  50. tf_warn "$@"
  51. exit $TF_ES_FAIL
  52. }
  53. tf_exit_bailout() {
  54. #
  55. # Warn $1 and exit with status FAIL
  56. #
  57. tf_warn "$@"
  58. exit $TF_ES_BAILOUT
  59. }
  60. tf_exit_error() {
  61. #
  62. # Warn $1 and exit with status FAIL
  63. #
  64. tf_warn "$@"
  65. exit $TF_ES_ERROR
  66. }
  67. tf_exit_panic() {
  68. #
  69. # Warn $1 and exit with status FAIL
  70. #
  71. tf_warn "$@"
  72. exit $TF_ES_PANIC
  73. }
  74. tf_debug() {
  75. #
  76. # Emit debug message
  77. #
  78. $TF_DEBUG || return 0
  79. local msg
  80. for msg in "$@";
  81. do
  82. $TF_COLOR && echo -ne "$TF_COLOR_CYAN" >&2
  83. echo "||| $msg" >&2;
  84. $TF_COLOR && echo -ne "$TF_COLOR_NONE" >&2
  85. done
  86. }
  87. tf_debugv() {
  88. #
  89. # Emit debug message showing value of each named variable
  90. #
  91. local varname
  92. local declare_str
  93. for varname in "$@";
  94. do
  95. if ! _tf__is_word "$varname";
  96. then
  97. tf_warn "tf_debugv: unsafe value skipped: $varname";
  98. continue
  99. fi
  100. if declare_str=$(declare -p "$varname" 2>/dev/null);
  101. then
  102. tf_debug "${declare_str#declare ?? }"
  103. else
  104. tf_debug "$varname #Unset"
  105. fi
  106. done
  107. }
  108. tf_think() {
  109. #
  110. # Emit status/progress message
  111. #
  112. $TF_VERBOSE || return 0
  113. local msg
  114. for msg in "$@";
  115. do
  116. $TF_COLOR && echo -ne "$TF_COLOR_LBLACK" >&2
  117. echo "$msg" >&2;
  118. $TF_COLOR && echo -ne "$TF_COLOR_NONE" >&2
  119. done
  120. }
  121. tf_warn() {
  122. #
  123. # Emit warning
  124. #
  125. local msg
  126. for msg in "$@";
  127. do
  128. $TF_COLOR && echo -ne "$TF_COLOR_LRED" >&2
  129. echo "$msg" >&2;
  130. $TF_COLOR && echo -ne "$TF_COLOR_NONE" >&2
  131. done
  132. }