imapfilter convenience wrapper

plugin.sh 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. #!/bin/bash
  2. # MKit - simple install helper
  3. # See LICENSE file for copyright and license details.
  4. mkit_import ini
  5. mkit_import util
  6. plugin__lspaths() {
  7. #
  8. # List existing paths from $MKIT_PLUGINPATH
  9. #
  10. local path
  11. debug_var MKIT_PLUGINPATH
  12. tr ':' '\n' <<<"$MKIT_PLUGINPATH" \
  13. | while read -r path; do
  14. debug_var path
  15. test -d "$path" || continue
  16. echo "$path"
  17. done
  18. }
  19. plugin__ls() {
  20. plugin__lspaths \
  21. | while read -r path; do
  22. debug_var path
  23. find "$path" -mindepth 1 -maxdepth 1 -printf '%P\n'
  24. done
  25. }
  26. plugin__option_bool() {
  27. #
  28. # Safely load boolean option $1
  29. #
  30. local name=$1
  31. __plugin__option_safeload \
  32. "$name" \
  33. "util__isa_bool" \
  34. "not a boolean (true|false)"
  35. }
  36. plugin__option_enum() {
  37. #
  38. # Safely load enum option $1, allowing values $2..
  39. #
  40. __plugin__check_call || return 5
  41. local name=$1; shift
  42. local allowed_values=("$@")
  43. local value
  44. local desc_allowed
  45. value=$(plugin__option_single "$name")
  46. debug_var name value
  47. test -n "$value" || return 1
  48. util__isa_enum "$value" "${allowed_values[@]}" || {
  49. desc_allowed=$(util__join "|" "${allowed_values[@]}")
  50. warn "not one of supported values ($desc_allowed): $name = $value"
  51. return 3
  52. }
  53. echo "$value"
  54. }
  55. plugin__option_multi() {
  56. #
  57. # Print multi-line plugin option $1
  58. #
  59. __plugin__check_call || return 5
  60. local name=$1
  61. ini values "options:$_MKIT__PLUGIN__NAME:$name" \
  62. | util__gate_printable
  63. }
  64. plugin__option_single() {
  65. #
  66. # Print single-line plugin option $1
  67. #
  68. __plugin__check_call || return 5
  69. local name=$1
  70. ini 1value "options:$_MKIT__PLUGIN__NAME:$name" \
  71. | grep .
  72. }
  73. plugin__option_int() {
  74. #
  75. # Safely load boolean option $1
  76. #
  77. local name=$1
  78. __plugin__option_safeload \
  79. "$name" \
  80. "util__isa_int" \
  81. "not an integer"
  82. }
  83. plugin__option_name() {
  84. #
  85. # Safely load boolean option $1
  86. #
  87. local name=$1
  88. __plugin__option_safeload \
  89. "$name" \
  90. "util__isa_name" \
  91. "not a valid name (alphanumeric, starting with letter)"
  92. }
  93. plugin__option_posint() {
  94. #
  95. # Safely load boolean option $1
  96. #
  97. local name=$1
  98. __plugin__option_safeload \
  99. "$name" \
  100. "util__isa_posint" \
  101. "not a positive integer"
  102. }
  103. plugin__path() {
  104. #
  105. # Print path to plugin file $1
  106. #
  107. local name=$1
  108. local binpath
  109. plugin__lspaths \
  110. | while read -r path; do
  111. binpath="$path/$name"
  112. debug_var binpath
  113. test -x "$binpath" || continue
  114. echo "$binpath"
  115. done
  116. }
  117. plugin__handle() {
  118. #
  119. # Run method $2 of plugin $1
  120. #
  121. local _MKIT__PLUGIN__NAME=$1
  122. local method=$2
  123. local require=true
  124. local path
  125. local fn
  126. test "${method:0:1}" == "." && {
  127. require=false
  128. method=${method:1}
  129. }
  130. fn=${_MKIT__PLUGIN__NAME}__${method}
  131. path=$(plugin__path "$_MKIT__PLUGIN__NAME")
  132. test -n "$path" || die "plugin not found: $_MKIT__PLUGIN__NAME"
  133. bash -n "$path" || die "syntax errors in plugin file: $path"
  134. #shellcheck disable=SC1090
  135. . "$path" || die "error importing plugin: $_MKIT__PLUGIN__NAME at $path"
  136. type -t "$fn" | grep -qw function || {
  137. $require && die "missing handler: $fn() in plugin $_MKIT__PLUGIN__NAME at $path"
  138. debug "SILENTLY SKIPPING UNDEFINED HANDLER: $fn() in $_MKIT__PLUGIN__NAME at $path"
  139. return 0
  140. }
  141. debug "RUNNING HANDLER"
  142. debug_var fn
  143. "$fn"
  144. }
  145. plugin__isvalid() {
  146. #
  147. # True if plugin $1 is valid
  148. #
  149. local plugin=$1
  150. plugin__ls | grep -qwxe "$plugin"
  151. }
  152. __plugin__check_call() {
  153. #
  154. # Check that this call is valid
  155. #
  156. local func=${FUNCNAME[1]}
  157. local caller=${FUNCNAME[2]}
  158. test -n "$_MKIT__PLUGIN__NAME" && return 0
  159. warn "illegal call: $func() from $caller() _MKIT__PLUGIN__NAME=$_MKIT__PLUGIN__NAME" \
  160. " hint: are we inside plugin?"
  161. return 3
  162. }
  163. __plugin__option_safeload() {
  164. #
  165. # Safely load option $1 with validator $2 and error message $3
  166. #
  167. __plugin__check_call || return 5
  168. local name=$1
  169. local validator=$2
  170. local msg=$3
  171. local value
  172. value=$(plugin__option_single "$name")
  173. debug_var name value
  174. test -n "$value" || return 1
  175. "$validator" "$value" || {
  176. warn "$msg: $name=$value"
  177. return 3
  178. }
  179. echo "$value"
  180. }