JAT - Just A Testing library https://pagure.io/shellfu-bash-jat

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. #!/bin/bash
  2. # MKit - simple install helper
  3. # See LICENSE file for copyright and license details.
  4. __ini_cat() {
  5. #
  6. # A no-op for text stream
  7. #
  8. local line # each line
  9. while read -r line; do
  10. printf -- "%s\n" "$line"
  11. done
  12. }
  13. __ini_expand() {
  14. #
  15. # Expand reference value (prefix only)
  16. #
  17. local line # each input line
  18. local suffix # tail of the line
  19. local ref # reference
  20. local value # value if reference
  21. while read -r line; do # [foo:bar]/path
  22. suffix="${line#\[*\]}" # /path
  23. ref="${line%$suffix}" # [foo:bar]
  24. ref="${ref%\]}" # [foo:bar
  25. ref="${ref#\[}" # foo:bar
  26. value="$(ini 1value "$ref")" # foo_bar_value
  27. printf -- "%s\n" "$value$suffix" # foo_bar_value/path
  28. done
  29. }
  30. __ini_grepcmt() {
  31. #
  32. # Remove comments from INI file on stdin
  33. #
  34. grep -v '^[[:space:]]*#'
  35. }
  36. __ini_grepkey() {
  37. #
  38. # Read key from a section
  39. #
  40. local wnt=$1 # wanted key
  41. grep '.' \
  42. | sed -e 's/ *= */=/; s/ +$//; s/^//;' \
  43. | grep -e "^$wnt=" \
  44. | cut -d= -f2- \
  45. | __ini_maybe_expand
  46. }
  47. __ini_greppath() {
  48. #
  49. # Read key from the right section
  50. #
  51. # E.g. `files:share:my/lib.sh` should read
  52. #
  53. # [files:share]
  54. # my/lib.sh = proj/my/lib.sh
  55. #
  56. local wnt=$1 # wanted path
  57. local wntkey=${wnt##*:} # ^^ key part
  58. local wntsec=${wnt%:$wntkey} # ^^ section part
  59. local override # ENV override (only ENV section)
  60. if test "$wntsec" = 'ENV'; then
  61. override=${!wntkey}
  62. test -n "$override" \
  63. && echo "$override" \
  64. && return
  65. fi
  66. __ini_grepsec "$wntsec" | __ini_grepkey "$wntkey"
  67. }
  68. __ini_grepsec() {
  69. #
  70. # Read one INI section
  71. #
  72. local wnt=$1 # wanted section name
  73. local ok=false # are we in the section?
  74. local line # each input line
  75. grep '.' \
  76. | while read -r line; do
  77. case "$line" in
  78. \[$wnt\]) ok=true; continue ;;
  79. \[*\]) ok=false; continue ;;
  80. esac
  81. $ok || continue
  82. printf -- "%s\n" "$line"
  83. done \
  84. | sed -e 's/ *= */=/; s/ +$//; s/^//;'
  85. }
  86. __ini_lskeys() {
  87. #
  88. # List keys from a section
  89. #
  90. local sct=$1 # section of interest
  91. __ini_grepsec "$sct" | cut -d= -f1 | awk '!x[$0]++'
  92. }
  93. __ini_lssect() {
  94. #
  95. # List all section names
  96. #
  97. local arg=$1 # unused argument
  98. grep -x '\[.*\]' | sed 's/^.//; s/.$//'
  99. }
  100. __ini_maybe_expand() {
  101. #
  102. # Decide whether or not to expand
  103. #
  104. if test "$MKIT_INI_EXPAND" -gt 0; then
  105. MKIT_INI_EXPAND=$(( --MKIT_INI_EXPAND )) __ini_expand
  106. else
  107. __ini_cat
  108. fi
  109. }
  110. __ini_body() {
  111. #
  112. # Produce mkit.ini body including INCLUDE
  113. #
  114. # Note: recursive includes are not supported.
  115. #
  116. local inc # file to include
  117. local incre='\[INCLUDE:.*\]' # include directive regex
  118. local iline # include directive line
  119. if iline=$(grep -m1 -x "$incre" "$MKIT_INI"); then
  120. inc=${iline#*:}; inc=${inc%]}
  121. grep -vx "$incre" "$inc"
  122. grep -vx "$incre" "$MKIT_INI"
  123. else
  124. cat "$MKIT_INI"
  125. fi | __ini_grepcmt
  126. }
  127. ini() {
  128. #
  129. # do ini operation
  130. #
  131. local op=$1 # operator
  132. local arg=$2 # argument
  133. local fn # internal function implementing $op
  134. local limit=__ini_cat # limiting internal function
  135. case $op in
  136. lskeys) fn=__ini_lskeys ;;
  137. lssect) fn=__ini_lssect ;;
  138. sec) fn=__ini_grepsec ;;
  139. values) fn=__ini_greppath ;;
  140. 1value) fn=__ini_greppath; limit="tail -1" ;;
  141. *) die "incorrect use of \`ini()\`"
  142. esac
  143. __ini_body | $fn "$arg" | $limit
  144. }
  145. update_version() {
  146. #
  147. # Change project:version in mkit.ini at path $2 to value $1
  148. #
  149. local version=$1 # new version
  150. local inifile=$2 # mkit.ini path
  151. local tmp # mkit.ini cache
  152. tmp=$(mktemp -t mkit.update_version.XXXXXXXX)
  153. <"$inifile" perl -e '
  154. my $hit = 0;
  155. my $done = 0;
  156. foreach (<STDIN>) {
  157. if ($done) { print; next; }
  158. elsif (m/\[project\]/) { $hit++; print; next; }
  159. elsif (m/\[/) { $hit = 0; print; next; }
  160. elsif ($hit) { s/\bversion\b( *)=( *).*/version$1=$2$ARGV[0]/ and $done++; print; }
  161. else { print; next; }
  162. }
  163. ' "$version" >"$tmp" || die "failed to update version in mkit.ini"
  164. mv "$tmp" "$inifile"
  165. }