Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

109 рядки
2.6 KiB

  1. #!/usr/bin/env bash
  2. set -e
  3. status=0
  4. program="${0##*/}"
  5. PROGRAM="$(echo "$program" | tr a-z- A-Z_)"
  6. [ -n "$TMPDIR" ] || TMPDIR="/tmp"
  7. _STUB_PLAN="${PROGRAM}_STUB_PLAN"
  8. _STUB_RUN="${PROGRAM}_STUB_RUN"
  9. _STUB_INDEX="${PROGRAM}_STUB_INDEX"
  10. _STUB_RESULT="${PROGRAM}_STUB_RESULT"
  11. _STUB_END="${PROGRAM}_STUB_END"
  12. _STUB_DEBUG="${PROGRAM}_STUB_DEBUG"
  13. if [ -n "${!_STUB_DEBUG}" ]; then
  14. echo "$program" "$@" >&${!_STUB_DEBUG}
  15. fi
  16. [ -e "${!_STUB_PLAN}" ] || exit 1
  17. [ -n "${!_STUB_RUN}" ] || eval "${_STUB_RUN}"="${TMPDIR}/${program}-stub-run"
  18. # Initialize or load the stub run information.
  19. eval "${_STUB_INDEX}"=1
  20. eval "${_STUB_RESULT}"=0
  21. [ ! -e "${!_STUB_RUN}" ] || source "${!_STUB_RUN}"
  22. # Loop over each line in the plan.
  23. index=0
  24. while IFS= read -r line; do
  25. index=$(($index + 1))
  26. if [ -z "${!_STUB_END}" ] && [ $index -eq "${!_STUB_INDEX}" ]; then
  27. # We found the plan line we're interested in.
  28. # Start off by assuming success.
  29. result=0
  30. # Split the line into an array of arguments to
  31. # match and a command to run to produce output.
  32. command=" $line"
  33. if [ "$command" != "${command/ : }" ]; then
  34. patterns="${command%% : *}"
  35. command="${command#* : }"
  36. fi
  37. # Naively split patterns by whitespace for now.
  38. # In the future, use a sed script to split while
  39. # respecting quoting.
  40. set -f
  41. patterns=($patterns)
  42. set +f
  43. arguments=("$@")
  44. # Match the expected argument patterns to actual
  45. # arguments.
  46. for (( i=0; i<${#patterns[@]}; i++ )); do
  47. pattern="${patterns[$i]}"
  48. argument="${arguments[$i]}"
  49. case "$argument" in
  50. $pattern ) ;;
  51. * ) result=1 ;;
  52. esac
  53. done
  54. # If the arguments matched, evaluate the command
  55. # in a subshell. Otherwise, log the failure.
  56. if [ $result -eq 0 ] ; then
  57. set +e
  58. ( eval "$command" )
  59. status="$?"
  60. set -e
  61. else
  62. eval "${_STUB_RESULT}"=1
  63. fi
  64. fi
  65. done < "${!_STUB_PLAN}"
  66. if [ -n "${!_STUB_END}" ]; then
  67. # Clean up the run file.
  68. rm -f "${!_STUB_RUN}"
  69. # If the number of lines in the plan is larger than
  70. # the requested index, we failed.
  71. if [ $index -ge "${!_STUB_INDEX}" ]; then
  72. eval "${_STUB_RESULT}"=1
  73. fi
  74. # Return the result.
  75. exit "${!_STUB_RESULT}"
  76. else
  77. # If the requested index is larger than the number
  78. # of lines in the plan file, we failed.
  79. if [ "${!_STUB_INDEX}" -gt $index ]; then
  80. eval "${_STUB_RESULT}"=1
  81. fi
  82. # Write out the run information.
  83. { echo "${_STUB_INDEX}=$((${!_STUB_INDEX} + 1))"
  84. echo "${_STUB_RESULT}=${!_STUB_RESULT}"
  85. } > "${!_STUB_RUN}"
  86. exit "$status"
  87. fi