GitHub Repo creation via CLI

I am so excited after creating my first repository on github and successfully adding some test files to it from my local machine!
But due to my limited internet connection, I stumbled upon some diffulties.

It took me a lot of time and effort, and there were actually a few times when I thought that I wouldn’t be able to do it. It took me hours, and that is not how much it should have taken to just create a new repository.

So i had a huge challenge – How do I make more repositories and add my files to it without the need of a browser… mmmhhh after some vigorous Googling I came upon a simple command using ‘curl’.

Challenge was accepted and at 03:10am I was done creating a badass bash script that would not only create a github repository, it would initialise it and push my files to the repository with only a press of a button.

#!/bin/bash
# -*- coding: utf-8 -*-
#
#  gitrep-create
#  
#  The purpose of this script is to generate Github repository via cli instead of going to github.com
#  Instructions for use: 
#  1. Install github [debian: sudo apt-get install -y git]
#  2. Fill in/Enter details
#     git config --global user.name "Your Name"
#     git config --global user.email "youremail@domain.com"
#  3. Generate token on github.com > Settings > Applications > Generate token
#
#
#  Copyright 2015 Mpho Mphego <mpho112@gmail.com>
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#  
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#  
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#  MA 02110-1301, USA.
#  

function progress(){
echo -n "Please wait..."
while true
do
  echo -n "."
  sleep 1
done

}

# Note:
# To run from ~/.bash_profile, uncomment function below. and copy code to ~/.bash_profile
# else create script and place it in ~/bin.

#gitrepo-create(){

  #trap "killall background" EXIT
  repo_name=$1
  dir_name=`basename $(pwd)`
  username=`git config github.user` 
  token=`git config github.token` 
  WGET="/usr/bin/wget"


 if [ "$repo_name" = "" ]; then
    echo "Repo name (hit enter to use '$dir_name')?"
    read repo_name
  fi
 
  if [ "$repo_name" = "" ]; then
    repo_name=$dir_name
  fi

  if [ "$username" = "" ]; then
    echo "Could not find username, run 'git config --global github.user <username>'"
    invalid_credentials=1
  fi

  echo Git Username: $username

  if [ "$token" = "" ]; then
    echo "Could not find token, run 'git config --global github.token <token>'"
    invalid_credentials=1
  fi

  echo "Creating Github repository '$repo_name'"
  progress &
  myself=$!

  $WGET -q --tries=10 --timeout=5 http://www.google.com -O /tmp/index.google &> /dev/null
  if [ ! -s /tmp/index.google ];then
    
    echo "Check your Internet Connection."
    kill $myself &> /dev/null

  else
    curl -u "$username:$token" https://api.github.com/user/repos -d '{"name":"'$repo_name'"}' > /dev/null 2>&1
    kill $myself &> /dev/null
    echo -n "...done."
    echo " "
    echo "Enter 1 to Create new repository."
    echo "Enter 2 to Push on existing repository."
    read inputs
    if [ "$inputs" == "1" ]; then
      echo "Creating a new repository."
      echo "#" $repo_name >> README.md
      git init
      echo " "
      git add README.md
      echo " "
      git commit -m "first commit"
      echo " "
      git remote add origin git@github.com:$username/$repo_name.git
      echo " "
      git push -u origin master
      echo ".......Done........"
    fi
  
    if [ "$inputs" == "2" ]; then
      echo "Pushing an existing repository"
      git remote add origin git@github.com::$username/$repo_name.git
      echo " "
      git push -u origin master
      echo ".......Done........"
    fi
    
  fi 
#}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s