err.sh 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. for p in projectname version versiondate; do
  43. eval "[ -n \"\$$p\" ] || fail \"$p unset\" || err \"$p unset\""
  44. p_="x_ printf \"%s\\n\" \"\$$p\" > $p"
  45. eval "x$p_ || $p_"
  46. done
  47. export LOCALVERSION="-${projectname}-${version%%-*}"
  48. }
  49. setvars()
  50. {
  51. _setvars=""
  52. [ $# -lt 2 ] && err "setvars: too few arguments"
  53. val="${1}" && shift 1
  54. for var in $@; do
  55. _setvars="${var}=\"${val}\"; ${_setvars}"
  56. done
  57. printf "%s\n" "${_setvars% }"
  58. }
  59. err()
  60. {
  61. printf "ERROR %s: %s\n" "${0}" "${1}" 1>&2
  62. exit 1
  63. }