Working Saturnin-based meta-command

common.sh 2.6KB

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