Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automated script or process for generating SSH keys and adding them to GitHub within the system-setup part of Linutil. #307

Closed
fam007e opened this issue Sep 8, 2024 · 1 comment

Comments

@fam007e
Copy link

fam007e commented Sep 8, 2024

Currently, there's no automated script or process for generating SSH keys and adding them to GitHub within the system-setup part of Linutil. This leaves users manually setting up SSH keys, which could be streamlined and standardized. For users who frequently need SSH keys for GitHub or other services, automating this process would save time and prevent potential setup errors.

I'd like to propose adding an automated script within the system-setup part of Linutil that generates SSH keys, prompts the user for details (like email and key type), optionally adds the key to the SSH agent, and copies the public key to the clipboard. After copying the key, the script can prompt the user to confirm if they've added it to GitHub and finally test the connection to GitHub.

Key features of the script:

  • Prompts for GitHub email and SSH key type (Ed25519 or RSA).
  • Allows a custom SSH key name or uses default.
  • Copies the generated public key to the clipboard using xclip.
  • Tests the SSH connection with GitHub.

This would simplify SSH key setup for users and fit well with the system-setup part of Linutil.

Alternatives include:

  1. Keeping the process manual, which may continue to cause issues for users who are not familiar with the SSH setup.
  2. Offering documentation for users to generate keys manually, but this would still leave them responsible for remembering and following multiple steps, which is prone to human error.

Here's a sample of how the script could look, which could be integrated into the system-setup section:

#!/bin/sh -e

# Import common utilities
. ./common-script.sh

# Function to prompt for GitHub configuration
setup_git_config() {
    # Prompt for GitHub email
    read -p "Enter your GitHub email address: " email

    # Prompt for SSH key type
    echo "Choose your SSH key type:"
    echo "1. Ed25519 (recommended)"
    echo "2. RSA (legacy)"
    read -p "Enter your choice (1 or 2): " key_type

    # Set key algorithm based on user choice
    case $key_type in
        1) key_algo="ed25519" ;;
        2) key_algo="rsa" ;;
        *)
            echo "Invalid choice. Exiting."
            exit 1
            ;;
    esac

    # Prompt for custom key name
    read -p "Enter a custom SSH key name (leave blank for default): " key_name

    # Set the SSH key path based on user input
    ssh_key_path="${HOME}/.ssh/${key_name:-id_$key_algo}"

    # Generate SSH key with specified type and email
    ssh-keygen -t "$key_algo" -C "$email" -f "$ssh_key_path"

    # Prompt for passphrase usage
    read -p "Do you want to use a passphrase? (y/n): " use_passphrase

    # If user opts for a passphrase, add key to SSH agent
    if [ "$use_passphrase" = "y" ]; then
        ssh-add -l &>/dev/null || eval "$(ssh-agent -s)"
        ssh-add "$ssh_key_path"
    else
        echo "Skipping passphrase setup."
    fi

    echo "SSH key generation and setup completed."
}

# Function to copy the SSH key to the clipboard and prompt user to add it to GitHub
copy_and_confirm_ssh_key() {
    # Check if xclip is installed
    checkCommandRequirements "xclip"

    # Copy the generated public key to the clipboard using xclip
    cat "${ssh_key_path}.pub" | xclip -selection clipboard
    echo "Your SSH public key has been copied to the clipboard."

    # Prompt user to confirm they've added the key to GitHub
    while true; do
        read -p "Have you pasted your SSH public key into your GitHub account? (y/n): " yn
        case $yn in
            [Yy]* ) echo "Proceeding..."; break ;;
            [Nn]* ) echo "Please paste your SSH public key into GitHub and try again."; exit ;;
            * ) echo "Please answer yes (y) or no (n)." ;;
        esac
    done

    # Test the SSH connection with GitHub
    ssh -T git@github.com
}

# Check environment and necessary tools
checkEnv

# Main execution
setup_git_config
copy_and_confirm_ssh_key

By automating this, the script will enhance usability and reduce setup errors.

@adamperkowski
Copy link
Contributor

Can you please stop this LLM-generated junk? You're bloating up the repo.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants