git.sh 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #!/usr/bin/env bash
  2. # Don't break if command fails
  3. cd /runtipi || echo ""
  4. # Ensure PWD ends with /runtipi
  5. if [[ $(basename "$(pwd)") != "runtipi" ]] || [[ ! -f "${BASH_SOURCE[0]}" ]]; then
  6. echo "Please make sure this script is executed from runtipi/"
  7. exit 1
  8. fi
  9. ROOT_FOLDER="${PWD}"
  10. show_help() {
  11. cat <<EOF
  12. app 0.0.1
  13. CLI for managing Tipi apps
  14. Usage: git <command> <repo> [<arguments>]
  15. Commands:
  16. clone Clones a repo in the repo folder
  17. update Updates the repo folder
  18. get_hash Gets the local hash of the repo
  19. EOF
  20. }
  21. # Get a static hash based on the repo url
  22. function get_hash() {
  23. url="${1}"
  24. echo -n "${url}" | sha256sum | awk '{print $1}'
  25. }
  26. if [ -z ${1+x} ]; then
  27. command=""
  28. else
  29. command="$1"
  30. fi
  31. # Clone a repo
  32. if [[ "$command" = "clone" ]]; then
  33. repo="$2"
  34. repo_hash=$(get_hash "${repo}")
  35. echo "Cloning ${repo} to ${ROOT_FOLDER}/repos/${repo_hash}"
  36. repo_dir="${ROOT_FOLDER}/repos/${repo_hash}"
  37. if [ -d "${repo_dir}" ]; then
  38. echo "Repo already exists"
  39. exit 0
  40. fi
  41. echo "Cloning ${repo} to ${repo_dir}"
  42. git clone "${repo}" "${repo_dir}"
  43. echo "Done"
  44. exit
  45. fi
  46. # Update a repo
  47. if [[ "$command" = "update" ]]; then
  48. repo="$2"
  49. repo_hash=$(get_hash "${repo}")
  50. repo_dir="${ROOT_FOLDER}/repos/${repo_hash}"
  51. if [ ! -d "${repo_dir}" ]; then
  52. echo "Repo does not exist"
  53. exit 0
  54. fi
  55. echo "Updating ${repo} in ${repo_hash}"
  56. cd "${repo_dir}" || exit
  57. git pull origin master
  58. echo "Done"
  59. exit
  60. fi
  61. if [[ "$command" = "get_hash" ]]; then
  62. repo="$2"
  63. get_hash "${repo}"
  64. exit
  65. fi