err.sh 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. # SPDX-License-Identifier: MIT
  2. # SPDX-FileCopyrightText: 2022, 2023 Leah Rowe <leah@libreboot.org>
  3. version=""; versiondate=""; projectname=""; _nogit=""
  4. x_() {
  5. [ $# -lt 1 ] || ${@} || err_exit err ${@}
  6. }
  7. xx_() {
  8. [ $# -lt 1 ] || ${@} || err_exit fail ${@}
  9. }
  10. err_exit()
  11. {
  12. _fail="${1}" && shift 1
  13. echo "Non-zero exit: $@"
  14. $_fail "Unhandled error"
  15. }
  16. check_git()
  17. {
  18. which git 1>/dev/null 2>/dev/null || \
  19. git_err "git not installed. please install git-scm."
  20. git config --global user.name 1>/dev/null 2>/dev/null || \
  21. git_err "git config --global user.name \"John Doe\""
  22. git config --global user.email 1>/dev/null 2>/dev/null || \
  23. git_err "git config --global user.email \"john.doe@example.com\""
  24. }
  25. git_err()
  26. {
  27. printf "You need to set git name/email, like so:\n%s\n\n" "${1}" 1>&2
  28. fail "Git name/email not configured" || \
  29. err "Git name/email not configured"
  30. }
  31. check_project()
  32. {
  33. read -r projectname < projectname || :
  34. [ ! -f version ] || read -r version < version || :
  35. version_="${version}"
  36. [ ! -e ".git" ] || version="$(git describe --tags HEAD 2>&1)" || \
  37. version="git-$(git rev-parse HEAD 2>&1)" || version="${version_}"
  38. [ ! -f versiondate ] || read -r versiondate < versiondate || :
  39. versiondate_="${versiondate}"
  40. [ ! -e ".git" ] || versiondate="$(git show --no-patch --no-notes \
  41. --pretty='%ct' HEAD)" || versiondate="${versiondate_}"
  42. [ -n "${versiondate}" ] || fail "Unknown version date" || \
  43. err "Unknown version date"
  44. [ -n "${version}" ] || fail "Unknown version" || \
  45. err "Unknown version"
  46. [ -n "${projectname}" ] || fail "Unknown project" || \
  47. err "Unknown project"
  48. xx_ printf "%s\n" "${version}" > version || \
  49. x_ printf "%s\n" "${version}" > version
  50. xx_ printf "%s\n" "${versiondate}" > versiondate || \
  51. x_ printf "%s\n" "${versiondate}" > versiondate
  52. }
  53. setvars()
  54. {
  55. _setvars=""
  56. [ $# -lt 2 ] && err "setvars: too few arguments"
  57. val="${1}"
  58. shift 1
  59. for var in $@; do
  60. _setvars="${var}=\"${val}\"; ${_setvars}"
  61. done
  62. printf "%s\n" "${_setvars% }"
  63. }
  64. err()
  65. {
  66. printf "ERROR %s: %s\n" "${0}" "${1}" 1>&2
  67. exit 1
  68. }