Last active
August 29, 2015 14:15
-
-
Save dmadisetti/1825ae8fe566d12aeb21 to your computer and use it in GitHub Desktop.
Autocompletion script for managing github projects
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # Make sure you source this in rc | |
| # Encapsulate everything since I don't feel like making an extra script | |
| gh(){ | |
| local folder="/home/dylan/projects/github" \ | |
| archive="/home/dylan/projects/archive" \ | |
| regex="[a-zA-Z]+[a-zA-Z0-9_-]*" \ | |
| USERNAME="dmadisetti" \ | |
| # Deal with projects | |
| local create=$(echo $1 | grep -oP "(?<=\^)$regex") \ | |
| new=$(echo $1 | grep -oP "(?<=\+)$regex") \ | |
| old=$(echo $1 | grep -oP "(?<=\-)$regex"); | |
| # Deal with archive | |
| local restore=$(echo $1 | grep -oP "(?<=\~)$regex") \ | |
| destroy=$(echo $1 | grep -oP "(?<=\_)$regex"); | |
| function newgit(){ | |
| mkdir $folder/$new &&\ | |
| cd $folder/$new &&\ | |
| git init . &&\ | |
| git remote add origin [email protected]:$USERNAME/$new.git &&\ | |
| return 0; | |
| return 1; | |
| } | |
| function creategit(){ | |
| cd $folder &&\ | |
| curl -i -d "{\"name\":\"$create\"}" https://api.github.com/user/repos -u $USERNAME:`read -s -p "Password:" && echo $REPLY` && { | |
| test -d $create && { | |
| cd $create; | |
| test -d .git || git init . | |
| git remote -v | grep origin || git remote add origin [email protected]:$USERNAME/$create.git; | |
| cd ..; | |
| } || git clone [email protected]:$USERNAME/$create.git; | |
| } &&\ | |
| cd $create &&\ | |
| return 0; | |
| return 1; | |
| } | |
| function oldgit(){ | |
| mv $folder/$old $archive/$old &&\ | |
| echo "$old moved to Archive" &&\ | |
| return 0; | |
| return 1; | |
| } | |
| function restoregit(){ | |
| mv $archive/$restore $folder/$restore &&\ | |
| echo "Restoring $restore" &&\ | |
| return 0; | |
| return 1; | |
| } | |
| function destroygit(){ | |
| read -p "Are you sure you want to kill $destroy?" -n 1 -r | |
| test $REPLY = "y" &&\ | |
| rm -rf $archive/$destroy &&\ | |
| echo &&\ | |
| echo "Killed $destroy" &&\ | |
| return 0; | |
| return 1; | |
| } | |
| test "" = "$new" || { | |
| newgit || { | |
| echo "Error creating git project" | |
| return 1; | |
| } | |
| return 0; | |
| } | |
| test "" = "$create" || { | |
| creategit || { | |
| echo "Error making Github project" | |
| return 1; | |
| } | |
| return 0; | |
| } | |
| test "" = "$old" || { | |
| oldgit || { | |
| echo "Error moving $old" | |
| return 1; | |
| } | |
| return 0; | |
| } | |
| test "" = "$restore" || { | |
| restoregit || { | |
| echo "Failed to restore $restore" | |
| return 1; | |
| } | |
| return 0; | |
| } | |
| test "" = "$destroy" || { | |
| destroygit || { | |
| echo "Aborted. Did not delete $destroy." | |
| return 1; | |
| } | |
| return 0; | |
| } | |
| cd $folder/$1; | |
| } | |
| _GithubComplete(){ | |
| local cur prefix directory | |
| COMREPLY={} | |
| prefix=$(echo ${COMP_WORDS[COMP_CWORD]} | grep -oP "^[-~_]") | |
| if [[ $prefix = "_" || $prefix = "~" ]]; | |
| then directory=/home/dylan/projects/archive | |
| else directory=/home/dylan/projects/github | |
| fi; | |
| cur=$(echo ${COMP_WORDS[COMP_CWORD]} | grep -oP "(?<=[-~_])?[a-zA-Z]+[a-zA-Z0-9_-]*") | |
| COMPREPLY=($prefix$(ls $directory | grep ^$cur)) | |
| return 0 | |
| } | |
| complete -F _GithubComplete -o filenames gh |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment