functions.sh 912 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #!/bin/sh
  2. # This file is included by download.sh & build.sh
  3. set -e
  4. color() {
  5. fg="$1"
  6. bg="${2}"
  7. ft="${3:-0}"
  8. printf "\33[%s;%s;%s" "$ft" "$fg" "$bg"
  9. }
  10. color_reset() {
  11. printf "\033[0m"
  12. }
  13. ok() {
  14. if [ -t 1 ]; then
  15. printf "%s[ OK ]%s\n" "$(color 37 42m 1)" "$(color_reset)"
  16. else
  17. printf "%s\n" "[ OK ]"
  18. fi
  19. }
  20. err() {
  21. if [ -t 1 ]; then
  22. printf "%s[ ERR ]%s\n" "$(color 37 41m 1)" "$(color_reset)"
  23. else
  24. printf "%s\n" "[ ERR ]"
  25. fi
  26. }
  27. run() {
  28. retval=0
  29. logfile="$(mktemp -t "run-XXXXXX")"
  30. if "$@" 2> "$logfile"; then
  31. ok
  32. else
  33. retval=$?
  34. err
  35. tail -n 100 "$logfile" || true
  36. fi
  37. rm -rf "$logfile"
  38. return $retval
  39. }
  40. progress() {
  41. printf "%-40s" "$(printf "%s ... " "$1")"
  42. }
  43. log() {
  44. printf "%s\n" "$1"
  45. }
  46. error() {
  47. log "ERROR: ${1}"
  48. }
  49. fail() {
  50. log "FATAL: ${1}"
  51. exit 1
  52. }
  53. debug() {
  54. log "Dropping into a shell for debugging ..."
  55. exec /bin/sh
  56. }