-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgithub-bootstrap-v2.sh
More file actions
executable file
·122 lines (102 loc) · 3.72 KB
/
Copy pathgithub-bootstrap-v2.sh
File metadata and controls
executable file
·122 lines (102 loc) · 3.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/usr/bin/env bash
# This script sets up a GitHub SSH environment.
# It should be run in the Pop!_OS (or Ubuntu/Debian) GNOME terminal emulator.
# It installs git, generates an ed25519 SSH key pair, adds the key to the ssh-agent,
# prints the locations of the generated files, and copies the public key to the clipboard.
# Ensure script is not run as root
if [[ $EUID -eq 0 ]]; then
echo "Please run this script as a regular user with sudo privileges, not as root."
exit 1
fi
# Handle script args
if [[ ! $(($#)) = 2 ]] || [[ $1 =~ ^((-[hH])|(--[hH][eEaA][lL][pP]))$ ]] ; then
echo "Usage: $0 \"email@address.com\" \"your-computers-name\""
exit 1
fi
email=$1;
computerName=$2
# Detect if the system is Debian-based
if [[ -f /etc/debian_version ]]; then
sudo apt-get update
# Install git if not already installed
if ! command -v git &> /dev/null; then
sudo apt-get install git -y
fi
# Install xclip if not already installed
if ! command -v xclip &> /dev/null; then
sudo apt-get install xclip -y
fi
# Install keychain if not already installed
if ! command -v keychain &> /dev/null; then
sudo apt-get install keychain -y
fi
# Append keychain initialization command to ~/.bashrc if not already present
if ! grep -q "keychain --eval" ~/.bashrc; then
echo "" >> ~/.bashrc
echo "# Start Keychain and add your SSH key" >> ~/.bashrc
echo "eval \$(keychain --eval --agents ssh id_ed25519)" >> ~/.bashrc
echo "" >> ~/.bashrc
fi
else
echo "This script only supports Debian-based systems."
exit 1
fi
# Check for existing SSH keys and confirm before overwriting
if [ -f "${HOME}/.ssh/id_ed25519" ]; then
echo "An SSH key already exists. Overwrite? (y/N)"
read -r overwrite
if [[ $overwrite =~ ^[Yy]$ ]]; then
ssh-keygen -t ed25519 -C "$email" -f "${HOME}/.ssh/id_ed25519"
else
echo "Skipping key generation."
fi
else
ssh-keygen -t ed25519 -C "$email" -f "${HOME}/.ssh/id_ed25519"
fi
# Start the ssh-agent in the background
eval "$(ssh-agent -s)"
# Add SSH private key to the ssh-agent
ssh-add "${HOME}/.ssh/id_ed25519"
# Print the location of the key pair
echo "SSH key pair located at:"
echo "Private key: ${HOME}/.ssh/id_ed25519"
echo "Public key: ${HOME}/.ssh/id_ed25519.pub"
# Print the public key to the terminal
echo "Your public SSH key is:"
cat "${HOME}/.ssh/id_ed25519.pub"
# Copy the public key to the clipboard
if command -v xclip &> /dev/null; then
cat "${HOME}/.ssh/id_ed25519.pub" | tr -d '\n' | xclip -selection clipboard
echo
echo "The public key has been copied to the clipboard."
echo
else
echo "xclip not found. Please manually copy the public key:"
echo " ----> ${HOME}/.ssh/id_ed25519.pub"
echo
fi
# Attempt to open the GitHub SSH keys settings page in the default web browser
if command -v xdg-open &> /dev/null; then
echo "Attempting to open the GitHub SSH keys settings page in your default browser..."
xdg-open "https://github.com/settings/keys" &>/dev/null
if [[ $? -ne 0 ]]; then
echo "Failed to open the URL with xdg-open."
fi
else
echo "xdg-open not found. Please open https://github.com/settings/keys manually in your browser."
echo "Attempting to open with explorer (check for WSL not added)"
# TODO: only do this if WSL detected.
explorer.exe https://github.com/settings/keys
fi
# Set up Git configuration
git config --global user.email "$email"
git config --global user.name "$computerName"
git config --global push.default simple
# Display current Git configuration for verification
echo
echo ">>> Git Configuration:"
echo
git config --list
echo
# End of script
echo "SSH key setup and Git configuration have been completed."