-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgswitch
executable file
·351 lines (291 loc) · 10.1 KB
/
gswitch
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
#!/usr/bin/env bash
# Author
author="Philip Mello <@Microsoft>"
# Version
version="1.2"
# License
license="MIT"
# Date
current_date=$(date +'%b %d, %Y')
# GitHub
github='https://github.com/PhilipMello/git-switch'
script="GitHub Account Switch"
description="Multiple Github Accounts into one CLI"
echo "
_____ _ _ _
/ ____| (_) | | | $version
__ _| (_____ ___| |_ ___| |__ $current_date
/ _. |\___ \ \ /\ / / | __/ __| |_ \ $author
| (_| |____) \ V V /| | || (__| | | | $license
\___ |_____/ \_/\_/ |_|\__\___|_| |_|
__/ |
|___/ $github
# --------------------------------------------------------------
# Script : $script
# Description: $description
# --------------------------------------------------------------
# How to use: Execute gswitch
# Exemples:
# Manual: gswitch -h OR gswitch --h OR gswitch --help
# Switch account using parameter: gswitch --account1
# --------------------------------------------------------------
"
WHITE=""
BLUE="\033[97;104m"
YELLOW="\033[97;103m"
CYAN="\033[97;106m"
MAGENTA="\033[97;45m"
GREEN="\033[97;102m"
RED="\033[97;41m"
ENDCOLOR="\e[0m"
function github_generate_sshkey() {
echo "Go to your GitHub Portal and add your SSH Key
'https://github.com/settings/ssh/new'
"
# Prompt user for the number of accounts to create
read -p "How many GitHub accounts do you want to create (0-9)? " num_accounts
# Validate the input for the number of accounts
if ! [[ "$num_accounts" =~ ^[0-9]+$ ]] || [ "$num_accounts" -lt 0 ] || [ "$num_accounts" -gt 9 ]; then
echo "Invalid number. Please enter a number between 0 and 9."
exit 1
fi
# Prompt user to create the specified number of accounts
for ((i = 0; i < num_accounts; i++)); do
read -p "Type your GitHub email account for account #$i: " github_email_account
# Validate email format (basic check)
if [[ ! "$github_email_account" =~ ^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$ ]]; then
echo "Invalid email format. Please try again."
continue
fi
# Generate the SSH key with the provided email and a custom filename
ssh-keygen -t ed25519 -C "$github_email_account" -f "$HOME/.ssh/id_ed25519_account_$i"
echo -e "
+-----------------------------------------------------------+
|${GREEN}SSH key generated for: $github_email_account${ENDCOLOR}
|${GREEN}Key saved as: $HOME/.ssh/id_ed25519_account_$i${ENDCOLOR}
+-----------------------------------------------------------+
"
done
}
function github_connection_test() {
# Github test connection
echo "Github connection testing..."
ssh -T git@github.com
}
function github_set_account() {
# Github set account
read -p "Enter your Github Account Name": github_set_account_name
read -p "Enter your Github Account Email": github_set_account_email
git config user.name "$github_set_account_name"
git config user.email "$github_set_account_email"
}
function github_account_switch() {
# GitHub Account Switch (2 Accounts)
echo -e "You're logged in as:\n"
ssh -T git@github.com
emails=()
files=()
while IFS=: read -r file email; do
emails+=("$email")
files+=("$(basename "$file")")
done < <(grep -H -Eo '[A-Za-z0-9._%+-]+@([A-Za-z0-9._%+-]+)?' ~/.ssh/id_* 2>/dev/null)
if [ ${#emails[@]} -eq 0 ]; then
echo "No accounts found."
exit 1
fi
echo "Select an email:"
for i in "${!emails[@]}"; do
echo "$((i+1)). ${emails[i]}"
done
read -p "Enter the number of your account: " choice
if ! [[ "$choice" =~ ^[0-9]+$ ]] || [ "$choice" -lt 1 ] || [ "$choice" -gt "${#emails[@]}" ]; then
echo "Invalid choice. Exiting."
exit 1
fi
selected_email=${emails[$((choice-1))]}
selected_file=${files[$((choice-1))]}
# Remove the "id_" prefix from the filename
stripped_file_prefix=${selected_file#id_}
# Remove the ".pub" suffix from the filename
stripped_file_suffix=${selected_file%.pub}
echo -e "GitHub Account $selected_email Selected!\n"
# Copy the selected private and public key files with stripped filename
cp ~/.ssh/$stripped_file_suffix ~/.ssh/id_ed25519
cp ~/.ssh/$selected_file ~/.ssh/id_ed25519.pub
# Test SSH connection
ssh -T git@github.com
}
function github_fix_permissions() {
# Git fix files permissions
echo "Fixing SSH file permissions..."
chown $USER:$USER -R ~/.ssh
chmod 600 ~/.ssh/*
echo -e "
+-----------------------------------------------------------+
|${GREEN}Files permissions has been fixed${ENDCOLOR}
+-----------------------------------------------------------+
"
}
function github_show_config() {
echo "Run gSwitch in git directory level"
grep -E "name|email" .git/config
}
function github_show_accounts() {
# Show account in ~/.ssh
cat ~/.ssh/id_* | grep -Eo '[A-Za-z0-9._%+-]+@([A-Za-z0-9._%+-]+)?' | while read -r email; do
echo "-----------------------------------------"
echo "| Email: $email |"
echo "-----------------------------------------"
done
}
function backup_ssh_folder() {
# Get the current username
current_user=$(whoami)
# Get the current date and time for the backup filename
timestamp=$(date +"%Y-%m-%d_%H-%M-%S")
backup_name="ssh_backup_"$current_user"_$timestamp.tar.gz"
# Define the mount point directory
mount_dir="/media/$current_user"
# Prompt user to choose a storage device
echo "Available devices for storage:"
# List all devices in the mount directory
devices=("$mount_dir"/*)
for i in "${!devices[@]}"; do
echo "$((i+1)). ${devices[i]}"
done
echo "$(( ${#devices[@]} + 1 )). Save to ~/Desktop/ssh_backup (default)"
read -p "Choose a storage device (1-${#devices[@]} or press Enter for default): " choice
# Determine the backup location
if [[ -z "$choice" || "$choice" -lt 1 || "$choice" -gt "$(( ${#devices[@]} + 1 ))" ]]; then
backup_location=~/Desktop/ssh_backup
else
backup_location="${devices[$((choice-1))]}"
fi
# Ensure the backup location exists
mkdir -p "$backup_location"
# Create the backup
tar -czf "$backup_location/$backup_name" -C ~ .ssh
echo "Backup saved to $backup_location/$backup_name"
}
function restore_ssh_folder() {
# Get the current username
current_user=$(whoami)
# Define the mount point directory
mount_dir="/media/$current_user"
# Prompt user to choose a storage device
echo "Available devices for storage:"
# List all devices in the mount directory
devices=("$mount_dir"/*)
for i in "${!devices[@]}"; do
echo "$((i+1)). ${devices[i]}"
done
echo "$(( ${#devices[@]} + 1 )). ~/Desktop/ssh_backup (default)"
read -p "Choose a storage device (1-${#devices[@]} or press Enter for default): " choice
# Determine the backup restore location
if [[ -z "$choice" || "$choice" -lt 1 || "$choice" -gt "$(( ${#devices[@]} + 1 ))" ]]; then
backup_location=~/Desktop/ssh_backup
else
backup_location="${devices[$((choice-1))]}"
fi
# List available backup files
echo "Searching for backup files in $backup_location..."
backup_files=("$backup_location"/*.tar.gz)
if [[ ${#backup_files[@]} -eq 0 || "${backup_files[0]}" == "$backup_location/*.tar.gz" ]]; then
echo "No backup files found in $backup_location."
return
fi
echo "Available backup files:"
for i in "${!backup_files[@]}"; do
echo "$((i+1)). ${backup_files[i]}"
done
read -p "Choose a file to restore (1-${#backup_files[@]}): " choice
if ! [[ "$choice" =~ ^[0-9]+$ ]] || [ "$choice" -lt 1 ] || [ "$choice" -gt "${#backup_files[@]}" ]; then
echo "Invalid choice. Exiting."
return
fi
selected_file="${backup_files[$((choice-1))]}"
# Confirm restore
echo "You selected: $selected_file"
read -p "This will overwrite your current ~/.ssh folder. Are you sure? (y/n): " confirm
if [[ "$confirm" != "y" ]]; then
echo "Restore cancelled."
return
fi
# Extract the backup
tar -xzf "$selected_file" -C ~
echo "Restore complete. ~/.ssh folder restored from $selected_file."
}
function ssh_options() {
read -p "Choose an option (1: Backup, 2: Restore): " option
[[ "$option" == "1" ]] && backup_ssh_folder \
|| ([[ "$option" == "2" ]] && restore_ssh_folder \
|| echo "Invalid option.")
}
# <-- Manual - BEGIN
manual() {
echo "Generate 2 SSH Keys and rename Github account to
id_ed25519_account1 and Github account 2 to id_ed25519_account2
in ~/.ssh
"
echo -e "Parameters:
--account1 Switch to GitHub Account 1
--account2 Switch to GitHub Account 2
"
exit 0
}
# Manual - END -->
if [[ $1 == "-h" || $1 == "--help" || $1 == "--h" ]]; then
manual
exit 1
elif [[ $1 == "--account1" ]]; then
cp ~/.ssh/id_ed25519_account1 ~/.ssh/id_ed25519
cp ~/.ssh/id_ed25519_account1.pub ~/.ssh/id_ed25519.pub
echo -e "Github Account #1 has been selected!\n"
ssh -T git@github.com
exit 0
elif [[ $1 == "--account2" ]]; then
cp ~/.ssh/id_ed25519_account2 ~/.ssh/id_ed25519
cp ~/.ssh/id_ed25519_account2.pub ~/.ssh/id_ed25519.pub
echo -e "Github Account #2 has been selected!\n"
ssh -T git@github.com
exit 0
fi
echo "Choose an option:"
echo "1. Switch GitHub Account"
echo "2. Generate SSH Key"
echo "3. Test Github SSH connection"
echo "4. Set Github account"
echo "5. Fix SSH file permissions"
echo "6. Show current GitHub config"
echo "7. Show Account in: ~/.ssh"
echo "8. Backup or Restore SSH folder: ~/.ssh"
read option
case $option in
1)
github_account_switch
;;
2)
github_generate_sshkey
;;
3)
github_connection_test
;;
4)
github_set_account
;;
5)
github_fix_permissions
;;
6)
github_show_config
;;
7)
github_show_accounts
;;
8)
ssh_options
;;
*)
echo "Invalid option"
;;
esac