core_github.sh 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #!/bin/bash
  2. # LinuxGSM core_github.sh function
  3. # Author: Christian Birk
  4. # Website: https://linuxgsm.com
  5. # Description: core function file for updates via github
  6. functionselfname="$(basename "$(readlink -f "${BASH_SOURCE[0]}")")"
  7. github_api="https://api.github.com"
  8. fn_githublocalversionfile(){
  9. local githubreleaseuser="${1}"
  10. local githubreleaserepo="${2}"
  11. githublocalversionfile="${datadir}/github-${githubreleaseuser}-${githubreleaserepo}-version"
  12. }
  13. # $1 githubuser/group
  14. # $2 github repo name
  15. fn_github_get_latest_release_version(){
  16. local githubreleaseuser="${1}"
  17. local githubreleaserepo="${2}"
  18. local githublatestreleaseurl="${github_api}/repos/${githubreleaseuser}/${githubreleaserepo}/releases/latest"
  19. githubreleaseversion=$(curl -s --connect-timeout 10 "${githublatestreleaseurl}" | jq '.tag_name' )
  20. # error if no version is there
  21. if [ -z "${githubreleaseversion}" ]; then
  22. fn_print_fail_nl "Cannot get version from GitHub API for ${githubreleaseuser}/${githubreleaserepo}"
  23. fn_script_log_fatal "Cannot get version from GitHub API for ${githubreleaseuser}/${githubreleaserepo}"
  24. fi
  25. }
  26. # $1 githubuser/group
  27. # $2 github repo name
  28. fn_github_set_latest_release_version(){
  29. local githubreleaseuser="${1}"
  30. local githubreleaserepo="${2}"
  31. fn_githublocalversionfile "${githubreleaseuser}" "${githubreleaserepo}"
  32. local githublatestreleaseurl="${github_api}/repos/${githubreleaseuser}/${githubreleaserepo}/releases/latest"
  33. githubreleaseversion=$(curl -s "${githublatestreleaseurl}" | jq -r '.tag_name' )
  34. # error if no version is there
  35. if [ -z "${githubreleaseversion}" ]; then
  36. fn_print_fail_nl "Cannot get version from GitHub API for ${githubreleaseuser}/${githubreleaserepo}"
  37. fn_script_log_fatal "Cannot get version from GitHub API for ${githubreleaseuser}/${githubreleaserepo}"
  38. else
  39. echo "${githubreleaseversion}" > "${githublocalversionfile}"
  40. fi
  41. }
  42. # $1 githubuser/group
  43. # $2 github repo name
  44. fn_github_get_installed_version(){
  45. local githubreleaseuser="${1}"
  46. local githubreleaserepo="${2}"
  47. fn_githublocalversionfile "${githubreleaseuser}" "${githubreleaserepo}"
  48. githublocalversion=$(cat "${githublocalversionfile}")
  49. }
  50. # $1 githubuser/group
  51. # $2 github repo name
  52. # if a update needs to be downloaded - updateneeded is set to 1
  53. fn_github_compare_version(){
  54. local githubreleaseuser="${1}"
  55. local githubreleaserepo="${2}"
  56. exitcode=0
  57. updateneeded=0
  58. fn_githublocalversionfile "${githubreleaseuser}" "${githubreleaserepo}"
  59. local githublatestreleaseurl="${github_api}/repos/${githubreleaseuser}/${githubreleaserepo}/releases/latest"
  60. githublocalversion=$(cat "${githublocalversionfile}")
  61. githubreleaseversion=$(curl -s "${githublatestreleaseurl}" | jq '.tag_name' )
  62. # error if no version is there
  63. if [ -z "${githubreleaseversion}" ]; then
  64. fn_print_fail_nl "Can not get version from Github Api for ${githubreleaseuser}/${githubreleaserepo}"
  65. fn_script_log_fatal "Can not get version from Github Api for ${githubreleaseuser}/${githubreleaserepo}"
  66. else
  67. if [ "${githublocalversion}" == "${githubreleaseversion}" ]; then
  68. echo -en "\n"
  69. echo -e "No update from github.com/${githubreleaseuser}/${githubreleaserepo}/ available:"
  70. echo -e "* Local build: ${red}${githublocalversion}${default}"
  71. echo -e "* Remote build: ${green}${githubreleaseversion}${default}"
  72. echo -en "\n"
  73. else
  74. # check if version that is installed is higher than the remote version to not override it
  75. last_version=$(echo -e "${githublocalversion}\n${githubreleaseversion}" | sort -V | head -n1 )
  76. if [ "${githubreleaseversion}" == "${last_version}" ]; then
  77. echo -en "\n"
  78. echo -e "Update from github.com/${githubreleaseuser}/${githubreleaserepo}/ available:"
  79. echo -e "* Local build: ${red}${githublocalversion}${default}"
  80. echo -e "* Remote build: ${green}${githubreleaseversion}${default}"
  81. echo -en "\n"
  82. updateneeded=1
  83. else
  84. # local version is higher than the remote version output this to the user
  85. # strange case but could be possible, as a release could be removed from github
  86. echo -en "\n"
  87. echo -e "Local version is newer than the remote version"
  88. echo -e "* Local version: ${green}${githublocalversion}${default}"
  89. echo -e "* Remote version: ${green}${githubreleaseversion}${default}"
  90. echo -en "\n"
  91. exitcode=1
  92. fi
  93. fi
  94. fi
  95. }