github-repos.sh 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #!/usr/bin/env bash
  2. # -----------------------------------------------------------------------------
  3. # Info:
  4. # author: Miroslav Vidovic
  5. # file: github-repos.sh
  6. # created: 04.04.2017.-09:18:34
  7. # revision: ---
  8. # version: 1.0
  9. # -----------------------------------------------------------------------------
  10. # Requirements:
  11. # rofi, git
  12. # Description:
  13. # Display all repositories connected with a GitHub user account in rofi and
  14. # clone the selected repository.
  15. # Usage:
  16. # github-repos.sh
  17. # -----------------------------------------------------------------------------
  18. # Script:
  19. # GitHub username
  20. USER="powellc"
  21. # GitHub user account URL
  22. URL="https://github.com/$USER/"
  23. # Clone a repository into the current directory
  24. clone_repository(){
  25. local repository=$1
  26. if [ -z "$repository" ]; then
  27. echo "ERROR: You need to enter the name of the repository you wish to clone."
  28. else
  29. git clone ~/src/"$URL$repository"
  30. fi
  31. }
  32. # Get all the repositories for the user with curl and GitHub API and filter only
  33. # the repository name from the output with sed substitution
  34. all_my_repositories_short_name(){
  35. curl -s "https://api.github.com/users/$USER/repos?per_page=1000" | grep -o 'git@[^"]*' |\
  36. sed "s/git@github.com:$USER\///g"
  37. }
  38. # Rofi dmenu
  39. rofi_dmenu(){
  40. rofi -dmenu -matching fuzzy -no-custom -p "Select a repository > "\
  41. -location 0 -bg "#F8F8FF" -fg "#000000" -hlbg "#ECECEC" -hlfg "#0366d6"
  42. }
  43. main(){
  44. repository=$(all_my_repositories_short_name | rofi_dmenu )
  45. clone_repository "$repository"
  46. }
  47. main
  48. exit 0