Skip to content

Instantly share code, notes, and snippets.

@shinokada
Last active June 9, 2021 06:51
Show Gist options
  • Save shinokada/fcd943db17a80959eba1d8f8579ce03b to your computer and use it in GitHub Desktop.
Save shinokada/fcd943db17a80959eba1d8f8579ce03b to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
########################
# Author: Shinichi Okada
# Date: 2021-05-22
###########################
unset user dir repo license_url
script_name=$(basename "$0")
dir=""
version="0.2.4"
usage() {
cat <<EOF
Script name: $script_name
Description:
============
This script automate creating git repo.
This will create a dir, add a license, README file, run git init and push to Github.
If a language is added, it will try to add the .gitignore.
Choose SSH as the default git protocol.
List of .gitignore supported by Github
======================================
See https://github.com/github/gitignore
Dependencies:
=============
- gh, GitHub CLI https://cli.github.com/manual/
- yq, https://github.com/mikefarah/yq
- jq
Usage:
======
$script_name [ -l | --lang programming_language ] [ -d | --dir directory ] [ -h | --help | -v | --version]
Examples:
=========
$script_name -l python -d newrepo
$script_name -d newrepo
# in a directory
$script_name -d .
# Show help
$script_name -h
# Show version
$script_name -v
EOF
exit 2
}
while (($# > 0)); do # while arguments count>0
case "$1" in
-l | --lang)
prog_lang=$2
shift
;;
-d | --dir)
dir=$2
shift
;;
-h | --help)
usage
;;
-v | --version)
echo ${version}
exit
;;
*) # unknown flag/switch
POSITIONAL+=("$1")
shift
;;
esac
done
############# Main body ############
# check if you are logged in github
if [[ $(gh auth status) -eq 1 ]]; then
# not logged-in
echo ">>> You must logged in. Use 'gh auth login'"
exit 1
fi
# don't allow to create a git repo in the ~ (HOME)
# user may use . in the HOME
cwd=$(pwd)
if [[ (${dir} = "$HOME") || ($cwd = "$HOME" && ${dir} = ".") ]]; then
echo "This script doesn't allow to create a git repo in the home directory."
echo "Use another directory."
exit 1
fi
# if dir is empty exit
if [[ -z ${dir} ]]; then
echo "You must set -d dir_name."
exit
fi
# check if you have Github CLI
if [ ! "$(command -v gh)" ]; then
echo "Please install Github CLI from https://cli.github.com/manual/."
exit 2
fi
# check if you have yq
if [ ! "$(command -v yq)" ]; then
echo "Please install yq from https://github.com/mikefarah/yq."
echo "Or run brew install yq"
exit 2
fi
# Directory path. If dir is . then use pwd
if [[ ${dir} = "." ]]; then
dir=$(pwd)
else
echo ">>> Creating ${dir}."
dir="$(pwd)/$dir"
mkdir -p "$dir" || exit
# cd "$dir" || exit
fi
user=$(yq e '."github.com".user' "$HOME"/.config/gh/hosts.yml)
repo=$(basename "${dir}")
license_url="mit"
echo ">>> Your github username is ${user}."
echo ">>> Your new repo name is ${repo}."
# lisence
PS3='Your lisence: '
lisences=("MIT: I want it simple and permissive." "Apache License 2.0: I need to work in a community." "GNU GPLv3: I care about sharing improvements." "None" "Quit")
echo "Select a license: "
select license in "${lisences[@]}"; do
case ${license} in
"MIT: I want it simple and permissive.")
echo "MIT"
break
;;
"Apache License 2.0: I need to work in a community.")
echo "Apache"
license_url="apache-2.0"
break
;;
"GNU GPLv3: I care about sharing improvements.")
echo "GNU"
license_url="lgpl-3.0"
break
;;
"None")
echo "License None"
license_url=false
break
;;
"Quit")
echo "User requested exit."
exit
;;
*) echo "Invalid option $REPLY" ;;
esac
done
echo "$dir"
cd "$dir" || exit
if [[ ${license_url} != false ]]; then
touch "${dir}"/license.txt
curl -s "https://api.github.com/licenses/${license_url}" | jq -r '.body' >"${dir}"/license.txt
echo ">>> license.txt created."
fi
if [[ ${prog_lang} ]]; then
# github gitignore url
url=https://raw.githubusercontent.com/github/gitignore/master/"${prog_lang^}".gitignore
# get the status http code, 200, 404 etc.
http_status=$(curl --write-out '%{http_code}' --silent --output /dev/null "${url}")
if [[ $http_status -eq 200 ]]; then
# get argument e.g python, go etc, capitalize it
echo ">>> Creating .gitignore for ${1^}..."
# create .gitignore
curl "${url}" -o .gitignore
echo ">>> .gitignore created."
else
echo ">>> Not able to find ${1^} gitignore at https://github.com/github/gitignore."
echo ">>> Adding .gitignore with minimum contents."
touch "${dir}/.gitignore"
echo ".DS_Store" >"${dir}/.gitignore"
fi
else
echo ">>> Adding .gitignore with minimum contents."
touch "${dir}/.gitignore"
echo ".DS_Store" >"${dir}/.gitignore"
fi
# README
echo ">>> Creating README.md."
printf "# %s \n
## Overview\n\n
## Requirement\n\n
## Usage\n\n
## Features\n\n
## Reference\n\n
## Author\n\n
## Licence
Please see license.txt.\n" "${repo}" >README.md
# git commands
echo ">>> Running git init."
git init
echo ">>> Adding README.md and .gitignore."
git add .
echo ">>> Commiting with a message 'first commit'."
git commit -m "first commit"
echo ">>> Creating the main branch."
git branch -M main
echo ">>> Creating your ${repo} in remote."
gh repo create "${repo}"
git remote add origin [email protected]:"${user}"/"${repo}".git
echo ">>> Pushing local repo to the remote."
git push -u origin main
echo ">>> You have created a github repo at https://github.com/${user}/${repo}"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment