Skip to content

Instantly share code, notes, and snippets.

@tinhvqbk
Last active November 25, 2024 07:34
Show Gist options
  • Select an option

  • Save tinhvqbk/b2822db9cf92b9c4010844e5dcb396d9 to your computer and use it in GitHub Desktop.

Select an option

Save tinhvqbk/b2822db9cf92b9c4010844e5dcb396d9 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Script Name: Git Clone Boilerplate Script
# Author: https://github.com/tinhvqbk
# Description: This script sets up the boilerplate for a new project.
# Usage: ./install.sh
# Exit immediately if a command exits with a non-zero status
set -e
# Function to prompt the user for confirmation
confirm() {
local prompt_message=$1
local valid_response=0
while [ $valid_response -eq 0 ]; do
read -r -p "$prompt_message (y/n) " -n 1 < /dev/tty
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
valid_response=1
elif [[ $REPLY =~ ^[Nn]$ ]]; then
echo "Operation aborted."
exit 1
else
echo "Invalid input. Please enter 'y' or 'n'."
fi
done
}
# Get REPO_NAME from user input if not provided as an argument
# Usage: curl -o- https://domain.com/install.sh | bash -s -- owner/repo
if [ -z "$1" ]; then
echo "? What is the name of the repository you want to clone?"
read -r REPO_NAME < /dev/tty
else
REPO_NAME=$1
fi
# Check if the repository name is empty.
if [ -z "$REPO_NAME" ]; then
echo "Error: Repository name cannot be empty."
exit 1
fi
# Check if required commands are available
command -v git >/dev/null 2>&1 || { echo >&2 "Git is required but not installed. Aborting."; exit 1; }
echo "? What is the name of your project?"
read -r APP_NAME < /dev/tty
# Check if the app name is empty.
if [ -z "$APP_NAME" ]; then
echo "Error: Project name cannot be empty."
exit 1
fi
# Check existing directories for conflicts.
if [ -d "$APP_NAME" ]; then
echo "[Boilerplate Setup]: Directory $APP_NAME already exists. Please choose a different name."
echo "Please enter a different project name or press Enter to remove the existing directory and continue with the setup."
read -r APP_NAME < /dev/tty
if [ -z "$APP_NAME" ]; then
confirm "Do you want to remove the existing directory and continue with the setup?"
confirm "Warning: This will delete the existing directory and its contents."
rm -rf "$APP_NAME"
fi
if [ -d "$APP_NAME" ]; then
echo "[Boilerplate Setup]: Failed to remove existing directory. Aborting."
exit 1
else
echo "[Boilerplate Setup]: Existing directory removed successfully."
fi
fi
# Proceed with the rest of the script
echo "[Boilerplate Setup]: Proceeding with the setup..."
# Clone the boilerplate repository into the current directory.
echo "[Boilerplate Setup]: Cloning the boilerplate repository $REPO_NAME..."
git clone git@github.com:$REPO_NAME.git "$APP_NAME"
if [ $? -eq 0 ]; then
echo "[Boilerplate Setup]: Repository cloned successfully."
else
echo "[Boilerplate Setup]: Failed to clone the repository. Aborting."
exit 1
fi
# Remove origin git information then reinitialize git
cd $APP_NAME
rm -rf .git
git init
# Notify the user that the script has finished.
echo "[Boilerplate Setup]: Setup complete. Your app is ready to go!"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment