From b37592e99d0c0ada2c13f498270c0d85e2fce44e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9rgio=20Barbosa?= Date: Mon, 14 Jul 2025 10:54:34 +0100 Subject: [PATCH] Sure! Pl --- README.md | 140 ++++++++++++++++++++++++++++++++++++++++++++ setup.sh | 171 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 311 insertions(+) create mode 100644 README.md create mode 100755 setup.sh diff --git a/README.md b/README.md new file mode 100644 index 0000000..ea12a72 --- /dev/null +++ b/README.md @@ -0,0 +1,140 @@ +# GetGit - Git Repository Setup Tool + +A simple bash script that helps users set up SSH keys and clone repositories from a private git server. + +## Features + +- **SSH Key Generation**: Automatically generates SSH keys if they don't exist +- **Dynamic User Support**: Works with any username on the git server +- **Repository Discovery**: Automatically discovers available repositories for the user +- **Interactive Selection**: Allows users to choose which repository to clone +- **Dotfiles Support**: Special handling for dotfiles repositories with automatic setup execution + +## Usage + +1. Make the script executable: + ```bash + chmod +x setup.sh + ``` + +2. Run the script: + ```bash + ./setup.sh + ``` + +3. Follow the prompts: + - Enter your git username + - Generate SSH key (if needed) + - Add the SSH key to your git account + - Select which repository to clone + +## How It Works + +### 1. Username Input +The script prompts for your git username, which is used to: +- Discover available repositories +- Construct repository URLs +- Set up proper directory structure + +### 2. SSH Key Management +- Checks for existing SSH key (`~/.ssh/id_ed25519`) +- Generates a new key if none exists +- Displays the public key for copying to your git account + +### 3. Repository Discovery +The script automatically checks for common repository patterns: +- `username/dotfiles` +- `username/scripts` +- `username/configs` +- `username/tools` +- `username/projects` +- `username/notes` +- `username/backup` +- `username/workspace` + +### 4. Repository Selection +Users can: +- Select from numbered list of discovered repositories +- Press Enter to use `username/dotfiles` (if available) +- Type `custom` to enter a custom repository path + +### 5. Cloning and Setup +- Clones the selected repository to the appropriate directory +- For dotfiles repositories: runs `setup.sh` automatically +- For other repositories: simply clones to `~/repository-name` + +## Configuration + +The script can be configured by modifying the settings at the top: + +```bash +### === SETTINGS === ### +GIT_SERVER="git.del-c.net" # Change to your git server +``` + +## Directory Structure + +- **Dotfiles**: `~/.dotfiles/` +- **Other repos**: `~/repository-name/` + +## Requirements + +- Bash shell +- Git installed +- SSH client +- Access to the configured git server + +## Example Flow + +``` +$ ./setup.sh +[?] Enter your git username: +Username: john + +[+] No SSH key found. Need to generate a new SSH key... +[+] If you are using the correct user, please enter your email for the SSH key. +Enter your email for the SSH key: john@example.com + +[*] Add the following public key to your GitHub account: +---------------------------------------- +ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIGx... john@example.com +---------------------------------------- +Press Enter after you've added the key... + +[+] Fetching available git projects for user: john... +[*] Available repositories: +1. john/dotfiles +2. john/scripts +3. john/tools + +[?] Which project would you like to download? +Enter the number of the repository from the list above, or: +- Press Enter to use john/dotfiles (if available) +- Type 'custom' to enter a custom repository path + +Enter your choice: 1 +[*] Selected repository: git@git.del-c.net:john/dotfiles.git +[*] Download directory: /Users/john/.dotfiles + +[+] Cloning selected repository... +[+] Launching dotfiles setup using Bash... +``` + +## Troubleshooting + +### SSH Key Issues +- Ensure the SSH key is added to your git account +- Check SSH connection: `ssh -T git@git.del-c.net` + +### Repository Not Found +- Verify the repository exists on the server +- Check username spelling +- Ensure you have access to the repository + +### Permission Denied +- Confirm SSH key is properly added to git account +- Verify you have read access to the repository + +## Contributing + +Feel free to submit issues and enhancement requests! \ No newline at end of file diff --git a/setup.sh b/setup.sh new file mode 100755 index 0000000..9406b2e --- /dev/null +++ b/setup.sh @@ -0,0 +1,171 @@ +#!/bin/bash + +set -e + +### === SETTINGS === ### +GIT_SERVER="git.del-c.net" +USERNAME="" +SELECTED_REPO="" +SELECTED_DIR="" + +### === GET USERNAME === ### +get_username() { + echo "[?] Enter your git username:" + read -p "Username: " USERNAME + if [ -z "$USERNAME" ]; then + echo "[!] Username cannot be empty. Please try again." + get_username + fi + echo "[*] Using username: $USERNAME" + echo +} + +### === CHECK FOR SSH KEY === ### +generate_ssh_key() { + if [ ! -f "$HOME/.ssh/id_ed25519.pub" ]; then + echo "[+] No SSH key found. Need to generate a new SSH key..." + echo "[+] Are you using the correct system user?" + echo "[+] If not, please switch to the correct user and run this script again." + echo "[+] If you are using the correct user, please enter your email for the SSH key." + read -p "Enter your email for the SSH key: " user_email + ssh-keygen -t ed25519 -C "$user_email" -f "$HOME/.ssh/id_ed25519" -N "" + else + echo "[~] SSH key already exists." + fi +} + +show_ssh_key() { + echo "[*] Add the following public key to your GitHub account:" + echo "----------------------------------------" + cat "$HOME/.ssh/id_ed25519.pub" + echo "----------------------------------------" + echo "[*] Go to your git site and add the key to your account." + echo + read -p "Press Enter after you've added the key..." +} + +### === LIST AVAILABLE GIT PROJECTS === ### +list_git_projects() { + echo "[+] Fetching available git projects for user: $USERNAME..." + + # Get list of repositories using git ls-remote + echo "[*] Available repositories:" + + local repo_count=1 + local found_repos=() + + # Check for common repository patterns + for repo in "dotfiles" "scripts" "configs" "tools" "projects" "notes" "backup" "workspace"; do + local full_repo="$USERNAME/$repo" + if git ls-remote "git@$GIT_SERVER:$full_repo.git" &>/dev/null; then + echo "$repo_count. $full_repo" + found_repos+=("$full_repo") + ((repo_count++)) + fi + done + + if [ ${#found_repos[@]} -eq 0 ]; then + echo "[!] No repositories found for user: $USERNAME" + echo "[*] You can still enter a custom repository path." + fi + + echo +} + +### === SELECT PROJECT TO DOWNLOAD === ### +select_project() { + list_git_projects + + echo "[?] Which project would you like to download?" + echo "Enter the number of the repository from the list above, or:" + echo "- Press Enter to use $USERNAME/dotfiles (if available)" + echo "- Type 'custom' to enter a custom repository path" + echo + + read -p "Enter your choice: " choice + + if [ -z "$choice" ]; then + # Default to dotfiles if it exists + if git ls-remote "git@$GIT_SERVER:$USERNAME/dotfiles.git" &>/dev/null; then + SELECTED_REPO="git@$GIT_SERVER:$USERNAME/dotfiles.git" + SELECTED_DIR="$HOME/.dotfiles" + else + echo "[!] $USERNAME/dotfiles not found. Please select a repository." + select_project + return + fi + elif [ "$choice" = "custom" ]; then + read -p "Enter repository path (e.g., $USERNAME/myproject): " custom_repo + SELECTED_REPO="git@$GIT_SERVER:$custom_repo.git" + SELECTED_DIR="$HOME/$(basename $custom_repo)" + elif [[ "$choice" =~ ^[0-9]+$ ]]; then + # User selected a number from the list + local repo_count=1 + local selected_repo="" + + for repo in "dotfiles" "scripts" "configs" "tools" "projects" "notes" "backup" "workspace"; do + local full_repo="$USERNAME/$repo" + if git ls-remote "git@$GIT_SERVER:$full_repo.git" &>/dev/null; then + if [ "$repo_count" -eq "$choice" ]; then + selected_repo="$full_repo" + break + fi + ((repo_count++)) + fi + done + + if [ -n "$selected_repo" ]; then + SELECTED_REPO="git@$GIT_SERVER:$selected_repo.git" + if [ "$(basename $selected_repo)" = "dotfiles" ]; then + SELECTED_DIR="$HOME/.dotfiles" + else + SELECTED_DIR="$HOME/$(basename $selected_repo)" + fi + else + echo "[!] Invalid choice. Please try again." + select_project + return + fi + else + echo "[!] Invalid choice. Please try again." + select_project + return + fi + + echo "[*] Selected repository: $SELECTED_REPO" + echo "[*] Download directory: $SELECTED_DIR" + echo +} + +### === CLONE SELECTED REPO === ### +clone_selected_repo() { + if [ ! -d "$SELECTED_DIR" ]; then + echo "[+] Cloning selected repository..." + git clone "$SELECTED_REPO" "$SELECTED_DIR" + else + echo "[~] Repository already cloned. Force pulling from remote (overwriting local changes)..." + cd "$SELECTED_DIR" + git fetch origin + git reset --hard origin/$(git symbolic-ref --short HEAD) + git clean -fd + fi + + # Only run setup if it's the dotfiles repository + if [[ "$SELECTED_REPO" == *"dotfiles.git" && -f "$SELECTED_DIR/setup.sh" ]]; then + echo "[+] Launching dotfiles setup using Bash..." + bash "$SELECTED_DIR/setup.sh" + else + echo "[*] Repository cloned successfully to: $SELECTED_DIR" + fi +} + +### === MAIN === ### +main() { + get_username + generate_ssh_key + show_ssh_key + select_project + clone_selected_repo +} + +main