-
-
Save jdrain/479c6003dabb82c5ed62feef329c08d2 to your computer and use it in GitHub Desktop.
Bash script for github management
This file contains 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 | |
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 git@github.com:$USERNAME/$new.git &&\ | |
return 0; | |
return 1; | |
} | |
function creategit(){ | |
cd $folder &&\ | |
curl -i -d "{\"name\":\"$create\",\"private\":true}" 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>/dev/null || git remote add origin git@github.com:$USERNAME/$create.git; | |
cd ..; | |
} || git clone git@github.com:$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 | |
activate(){ | |
dir=~/.pythonenvs/$1/; | |
if [ "x$1" != "x" ] && [ -d $dir ]; then | |
source $dir/bin/activate; | |
gh $1; | |
else | |
echo Env doesn\'t exist; | |
fi; | |
} | |
virt(){ | |
if [ "x$1" != "x" ]; then | |
virtualenv --no-site-packages ~/.pythonenvs/$1; | |
activate $1; | |
echo Env $1 made; | |
else | |
echo Provide name; | |
fi; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment