Skip to content

Instantly share code, notes, and snippets.

@samthetechie
Created December 21, 2017 10:37
Show Gist options
  • Save samthetechie/b9591482cec7abe3118f837def030a64 to your computer and use it in GitHub Desktop.
Save samthetechie/b9591482cec7abe3118f837def030a64 to your computer and use it in GitHub Desktop.
my ~/.bashrc functions for creating and deleting github repos, tested on Debian GNU/Linux 9.3 (stretch)
#Tested with OS: Debian GNU/Linux 9.3 (stretch) x86_64
#bash --version
#GNU bash, version 4.4.12(1)-release (x86_64-pc-linux-gnu)
#curl --version
#curl 7.52.1 (x86_64-pc-linux-gnu) libcurl/7.52.1 OpenSSL/1.0.2l zlib/1.2.8 libidn2/0.16 libpsl/0.17.0 (+libidn2/0.16) libssh2/1.7.0 nghttp2/1.18.1 librtmp/2.3
# assumptions:
# 1) virtualenv and virtualenvwrapper are installed
# 2) github authentication using tokens
# 3) python2.7 default for new projects
# 4) paste onto end of ~/.bashrc and reload bash to be able to run mkgh, rmgh, mkpygh to save time when making new projects
# api docs, create: POST /user/repos
# as a oneliner / with no checks: user_name=`git config github.user` && token=`git config github.token` && echo "name of repo to create:" && read repo_name && curl -u "$user_name:$token" https://api.github.com/user/repos -d '{"name":"'$repo_name'"}' > /dev/null 2>&1 && git clone [email protected]:$user_name/$repo_name.git && cd $repo_name
mkgh(){
if [ $# -eq 0 ]
then
echo "usage: mkgh reponame"
return 1
fi
user_name=`git config github.user`
if [ "$user_name" = "" ]; then
echo "Could not find username, run 'git config --global github.user <username>'"
invalid_credentials=1
fi
# manage tokens at https://github.com/settings/tokens
token=`git config github.token`
if [ "$token" = "" ]; then
echo "Could not find token, run 'git config --global github.token <token>'"
invalid_credentials=1
fi
if [ "$invalid_credentials" == "1" ]; then
return 1
fi
curl -u "$user_name:$token" https://api.github.com/user/repos -d '{"name":"'$1'"}' > /dev/null 2>&1
git clone [email protected]:$user_name/$1.git > /dev/null 2>&1
cd $1
curl -o README.md -L http://git.io/Xy0Chg > /dev/null 2>&1
git add README.md > /dev/null 2>&1
git commit -m "automagcial first commit using my mkgh() fn in ~/.bashrc" -m "public gh repo + boilerplate README.md" -m "because life is too short to do it all manually each time..."
git push > /dev/null 2>&1
}
# api docs, delete: DELETE /repos/:owner/:repo
# as a oneliner / with no checks: user_name=`git config github.user` && token=`git config github.token` && echo "name of repo to delete:" && read reponame && curl -i -H "Authorization: token $token" -X "DELETE" "https://api.github.com/repos/$user_name/$repo_name"
rmgh(){
if [ $# -eq 0 ]
then
echo "usage: rmgh reponame"
return 1
fi
deactivate
rmvirtualenv $1
user_name=`git config github.user`
if [ "$user_name" = "" ]; then
echo "Could not find username, run 'git config --global github.user <username>'"
invalid_credentials=1
fi
# manage tokens at https://github.com/settings/tokens
token=`git config github.token`
if [ "$token" = "" ]; then
echo "Could not find token, run 'git config --global github.token <token>'"
invalid_credentials=1
fi
if [ "$invalid_credentials" == "1" ]; then
return 1
fi
curl -i -H "Authorization: token $token" -X "DELETE" "https://api.github.com/repos/$user_name/$1" > /dev/null 2>&1
# only delete the (remote) public github repo, don't delete the local version, let the user do that manually
}
mkpygh() {
if [ $# -eq 0 ]
then
echo "usage: mkpygh projectname"
return 1
fi
mkproject $1
username=`git config github.user`
token=`git config github.token`
curl -u "$username:$token" https://api.github.com/user/repos -d '{"name":"'$1'"}' > /dev/null 2>&1
git remote add origin [email protected]:$username/$1.git > /dev/null 2>&1
git push -u origin master > /dev/null 2>&1
cd ..
rm -rf $1
git clone [email protected]:$username/$1.git > /dev/null 2>&1
cd $1
curl -o README.md -L http://git.io/Xy0Chg > /dev/null 2>&1
git add README.md > /dev/null 2>&1
echo -e '#!/usr/bin/env python\n# -*- coding: utf-8 -*-\n"""\nModule Docstring\n"""\n\n__author__ = "Your Name"\n__version__ = "0.1.0"\n__license__ = "MIT"\n\n\ndef main():\n\t""" Main entry point of the app """\n\tprint "hello world"\n\n\nif __name__ == "__main__":\n\t""" This is executed when run from the command line """\n\tmain()'>main.py
chmod +x main.py > /dev/null 2>&1
git add main.py > /dev/null 2>&1
git commit -m "automagcial first commit using my mkpygh() fn in ~/.bashrc" -m "public gh repo + boilerplate README.md + main.py" -m "because life is too short to do it all manually each time..."
git push > /dev/null 2>&1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment