Skip to content

Instantly share code, notes, and snippets.

@tibix
Last active December 19, 2023 15:48
Show Gist options
  • Save tibix/0f010dc3671f515a993c502ecba97967 to your computer and use it in GitHub Desktop.
Save tibix/0f010dc3671f515a993c502ecba97967 to your computer and use it in GitHub Desktop.
Remote GITHUB Repo Setup
#!/bin/zsh #!/bin/env python3
bold=$(tput bold) import os
normal=$(tput sgr0) import platform
# GitHub Info # get current OS
# Token current_os = platform.system()
GH_API_TOKEN='<your_github_api_token>'
# User Name
GH_USER='<your_github_user>' def get_current_username(this_os):
if this_os == "Linux" or this_os == "Darwin":
if [ $# -eq 0 ]; then return os.environ.get("USER")
echo "Usage: $0 <repo_name>" elif this_os == "Windows":
exit 1 return os.environ.get("USERNAME")
fi
return None
# Variable to store first argument to script as the repo name.
# Will be used as GitHub repo name too.
NEW_REPO_NAME=$1 print(os.environ.get("USER"))
# Get argument to set the repo visibility
if [[ -z "$2" ]] # check if REPOS dir is present in the default path
then def isRepos(this_os, username):
GH_REPO_VISIBILITY='false' path_to_check = ""
else if this_os == "Linux":
case $2 in path_to_check = f'/home/{username}/REPOS'
private) elif this_os == "Darwin":
GH_REPO_VISIBILITY='true' path_to_check = f'/Users/{username}/REPOS'
;; elif this_os == "Windows":
public) path_to_check = f'C:/Users/{usename}/REPOS'
GH_REPO_VISIBILITY='false'
;; isPath = os.path.exists(path_to_check)
*) print(isPath)
printf "Invalid visibility option! \nValid options are: ${bold}private${normal} or ${bold}public${normal}.\n"
exit 1 if not isPath:
;; os.makedirs(path_to_check)
esac print("REPOS dir created!")
fi else:
print("REPOS dir is alredy in the default path!")
# Store current working directory
CURRENT_DIR=$PWD
username = get_current_username(current_os)
# Check if 3rd argument (directory name) is provided isRepos(current_os, username)
if [ -z "$3" ]
then try:
# Create a directory with name NEW_REPO_NAME from git import Repo # used to clone remote repo to local (GitPython module)
mkdir $NEW_REPO_NAME from git import exc as exc
except:
# Change directory to the newly created directory os.system("pip install GitPython")
cd $NEW_REPO_NAME from git import Repo
from git import exc as exc
# Set CURRENT_DIR to the newly created directory
CURRENT_DIR=$PWD try:
from github import Github # used to interact with GitHub API (PyGithub module)
# Set PROJECT_DIR to basename of the current path (eg: if path is /home/user/project/ then PROJECT_DIR will be project) except:
PROJECT_DIR=$(basename "`pwd`") os.system("pip install PyGithub")
else from github import Github
# Project directory can be passed as third argument to script call, or will default to current working directory.
PROJECT_DIR=${3:-$CURRENT_DIR} # Add below your github token
fi GIT_TOKEN = '<personal_github_token_goes_here>'
# JUST FOR DEBUGGING # set BASE_DIR based on the current OS
# Check visibility settings if current_os == "Linux":
echo "$GH_REPO_VISIBILITY" BASE_DIR = f'/home/{username}/REPOS' # change this to match you preference
# DEBUGGING ENDED
if current_os == "Darwin":
# GitHub repos Create API call BASE_DIR = f'/Users/{username}/REPOS'
curl -H "Authorization: token $GH_API_TOKEN" https://api.github.com/user/repos -d '{"name": "'"${NEW_REPO_NAME}"'","private":'${GH_REPO_VISIBILITY}'}'
git init $PROJECT_DIR if current_os == "Windows":
BASE_DIR = f'C:/Users/{username}/REPOS'
# Initialize Git in project directory, and add the GH repo remote
git -C $PROJECT_DIR remote add origin git@github.com:$GH_USER/$NEW_REPO_NAME.git
@tibix
Copy link
Author

tibix commented Dec 19, 2023

Updated to accommodate for MacOS (Darwin based OS)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment