Shellfu/Bash/JAT data-driven testing mini-framework https://pagure.io/shellfu-bash-xcase

mkit.sh 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #!/bin/bash
  2. # MKit - simple install helper
  3. # See LICENSE file for copyright and license details.
  4. die() {
  5. #
  6. # Exit with message and non-zero exit status
  7. #
  8. echo "fatal: $*" >&2
  9. exit 4
  10. }
  11. mkit_import() {
  12. #
  13. # Import mkit module $1.sh
  14. #
  15. # Check for module, source it and die with reasonable message if needed.
  16. #
  17. local modname=$1
  18. local modpath
  19. modpath="$MKIT_DIR/include/$modname.sh"
  20. test -f "$modpath" || die "no such module: $modpath"
  21. bash -n "$modpath" || die "bad syntax: $modpath"
  22. #shellcheck disable=SC1090
  23. . "$modpath" || die "failed to import: $modname"
  24. }
  25. mkit_import build
  26. mkit_import deploy
  27. mkit_import release
  28. mkit_import ini
  29. __valid_targets() {
  30. #
  31. # List valid routes
  32. #
  33. echo _mkit_data
  34. echo build
  35. echo clean
  36. echo debstuff
  37. echo dist
  38. echo install
  39. echo release
  40. echo release_x
  41. echo release_y
  42. echo release_z
  43. echo rpmstuff
  44. echo uninstall
  45. echo vbump
  46. echo vbump_x
  47. echo vbump_y
  48. echo vbump_z
  49. }
  50. debug() {
  51. #
  52. # Print debug message
  53. #
  54. $MKIT_DEBUG || return 0
  55. echo "MKIT_DEBUG: ${FUNCNAME[1]}()" "$@" >&2
  56. }
  57. debug_var() {
  58. #
  59. # Print debug message
  60. #
  61. $MKIT_DEBUG || return 0
  62. local __mkit_debug_var_name__ # variable name to debug
  63. local decl # declare string
  64. for __mkit_debug_var_name__ in "$@"; do
  65. {
  66. decl=$(declare -p "$__mkit_debug_var_name__")
  67. decl=${decl#declare ?? }
  68. echo "MKIT_DEBUG: ${FUNCNAME[1]}(): $decl"
  69. } >&2
  70. done
  71. }
  72. __compver() {
  73. #
  74. # True if version $1 matches our version
  75. #
  76. # If our x is 0, check first two fragments, otherwise check just
  77. # the x. Fragments must equal.
  78. #
  79. local their_ver=$1 # their version
  80. local our_x # our X
  81. local our_y # our Y
  82. local their_x # their X
  83. local their_y # their Y
  84. their_x=${their_ver%%.*}
  85. their_y=${their_ver##$their_x.}
  86. their_y=${their_y%%.*}
  87. our_x=${MKIT_VERSION%%.*}
  88. our_y=${MKIT_VERSION##$our_x.}
  89. our_y=${our_y%%.*}
  90. debug_var MKIT_VERSION our_x our_y their_ver their_x their_y
  91. test "$their_x" -eq "$our_x" || return 1
  92. test "$our_x" -eq 0 && {
  93. test "$their_y" = "$our_y"
  94. return $?
  95. }
  96. return 0
  97. }
  98. __chkiniversion() {
  99. #
  100. # Check if ini version is supported
  101. #
  102. # Look for "#mkit version=0.0.0" or similar in first or last
  103. # 3 lines of the file; then check if the version is supported.
  104. #
  105. local ver_line # line declaring version
  106. local their_ver # the declared version
  107. ver_line=$(
  108. {
  109. head -3 "$MKIT_INI"
  110. tail -3 "$MKIT_INI"
  111. } | grep -m 1 -E '^# *mkit +version *= *v?[0-9]+\.[0-9]+\.[0-9]+'
  112. )
  113. test -n "$ver_line" \
  114. || die "version mark ('#mkit version=x.y.z') not found in: $MKIT_INI"
  115. their_ver="$(tr -d '[:blank:]v' <<<"${ver_line##*=}")"
  116. __compver "$their_ver" \
  117. || die "bad mkit.ini version: $their_ver does not match $MKIT_VERSION"
  118. }
  119. mkit_init() {
  120. #
  121. # Do basic initialization
  122. #
  123. # Check for ini file and some variables
  124. #
  125. $MKIT_DRY && MKIT_DEBUG=true
  126. #shellcheck disable=SC2034
  127. MKIT_PROJ_PKGNAME=$(ini 1value "project:pkgname")
  128. test -f "$MKIT_INI" || die "cannot find mkit.ini: $MKIT_INI"
  129. __chkiniversion
  130. }
  131. route() {
  132. #
  133. # Call correct function based on $1
  134. #
  135. if __valid_targets | grep -qwx "^$1"; then
  136. "$1"
  137. else
  138. {
  139. echo "usage: $(basename "$0") TARGET"
  140. echo
  141. echo "valid targets:"
  142. __valid_targets | sed 's/^/ /'
  143. } >&2
  144. fi
  145. }
  146. warn() {
  147. #
  148. # Print warning message
  149. #
  150. echo "$@" >&2
  151. }