functions 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #!/usr/bin/env bash
  2. # shellcheck disable=SC1090,SC2034
  3. checkBashVersion () {
  4. if ! declare -n _assoc; then
  5. echo "You must use bash >= 4.3 (supports namerefs) to use build-wrapper"
  6. echo "You can still call most functions directly using arguments instead of arrays."
  7. _printHelpAndExit 1
  8. fi
  9. }
  10. getOS () {
  11. if [[ -e /etc/os-release ]]; then
  12. source /etc/os-release
  13. else
  14. echo "No /etc/os-release found!"
  15. echo "Your OS is unsupported"
  16. _printHelpAndExit 1
  17. fi
  18. }
  19. installPackage () {
  20. # We will add packages to this array if their command is not available
  21. local -a _pkg_array
  22. # parse commands
  23. for _pkg in "$@"; do
  24. _pkg=$(packageOverrides "$_pkg")
  25. # Insert the package name to test if already installed one element from the end
  26. # and silence output
  27. if ! "${_pkg_query_cmd[@]}" "$_pkg" > /dev/null 2>&1; then
  28. _pkg_array+=("$_pkg")
  29. else
  30. echo "$_pkg is already installed!"
  31. fi
  32. done
  33. if [[ ${#_pkg_array[@]} -ge 1 ]]; then
  34. [[ -n $_debug ]] && echo "${_install_cmd[@]}" "${_pkg_array[@]}"
  35. "${_install_cmd[@]}" "${_pkg_array[@]}"
  36. fi
  37. }
  38. packageOverrides () {
  39. if [[ "$ID" == "ubuntu" || "$ID" == "debian" ]]; then
  40. if [[ "$1" == "rpm-build" ]]; then
  41. echo "rpm"
  42. elif [[ "$1" == "createrepo_c" ]]; then
  43. echo "createrepo"
  44. else
  45. echo "$1"
  46. fi
  47. else
  48. echo "$1"
  49. fi
  50. }
  51. getBaseDir () {
  52. # Get base directory name of where this script resides
  53. # https://stackoverflow.com/questions/59895/how-to-get-the-source-directory-of-a-bash-script-from-within-the-script-itself#comment54598418_246128
  54. _basedir=$(dirname "$(readlink -f "$0")")
  55. }
  56. sourcePlugin () {
  57. getBaseDir
  58. _plugin="$_basedir/plugins/$1"
  59. if [[ -f "$_plugin" ]]; then
  60. source "$_plugin"
  61. else
  62. echo "Plugin $_plugin does not exist!"
  63. fi
  64. }
  65. sourcePlugins () {
  66. getBaseDir
  67. for _file in "$_basedir"/plugins/*/*; do
  68. [[ -f "$_file" ]] && source "$_file"
  69. done
  70. }
  71. pluginExists () {
  72. if [[ $# -lt 1 || ! $(type -t "$1") == "function" ]]; then
  73. echo "Plugin not found"
  74. _printHelpAndExit 1
  75. fi
  76. }
  77. fixPermissions () {
  78. # Allow container access to the workdir (SELinux)
  79. chcon -t container_file_t -R "$1"
  80. }
  81. debug () {
  82. [[ -n $_debug ]] && echo "debug: " "$@"
  83. }
  84. silent () {
  85. [[ -z $_silent ]] && echo "$@"
  86. }