git.sh 1.5 KB

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